01: /*
02: * Timer: The timer class
03: * Copyright (C) 2006-2007 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18: *
19: * JythonTimerImpl.java
20: */
21:
22: package com.rift.coad.daemon.jython;
23:
24: import com.rift.coad.daemon.timer.TimerEventHandler;
25: import com.rift.coad.daemon.timer.TimerException;
26: import com.rift.coad.lib.configuration.ConfigurationException;
27: import java.io.File;
28: import java.io.FileInputStream;
29: import java.io.Serializable;
30: import java.rmi.RemoteException;
31: import org.apache.log4j.Logger;
32: import org.python.util.PythonInterpreter;
33:
34: /**
35: *
36: * @author Glynn Chaldecott
37: */
38: public class JythonTimerImpl implements TimerEventHandler {
39:
40: protected Logger log = Logger.getLogger(JythonDaemonImpl.class
41: .getName());
42:
43: public String scriptLocal = "";
44:
45: /** Creates a new instance of JythonTimerImpl */
46: public JythonTimerImpl() throws Exception {
47: try {
48: com.rift.coad.lib.configuration.Configuration coadConfig = com.rift.coad.lib.configuration.ConfigurationFactory
49: .getInstance()
50: .getConfig(
51: com.rift.coad.daemon.jython.JythonDaemonImpl.class);
52: System.setProperty("python.home", coadConfig
53: .getString("python_home"));
54: scriptLocal = coadConfig.getString("script_location");
55: } catch (ConfigurationException ex) {
56: log.error("Failed to set jython properties :"
57: + ex.getMessage(), ex);
58: throw new Exception("Failed to set jython properties :"
59: + ex);
60: }
61: }
62:
63: /**
64: * This method implements the TimerEventHandler thus allowing a user to run
65: * scripts using the Coadunation Timer Daemon.
66: *
67: * @param serializable This is the serializable event supplied to the Timer
68: * Daemon and is used as the name of the script.
69: */
70: public void processEvent(Serializable serializable)
71: throws RemoteException, TimerException {
72: String name = (String) serializable;
73: File scriptFile = new File(scriptLocal + File.separator + name);
74: try {
75: FileInputStream fis = new FileInputStream(scriptFile);
76: PythonInterpreter inter = new PythonInterpreter();
77: inter.execfile(fis);
78: } catch (Exception ex) {
79: log.error("Failed to retrieve and run script:" + ex, ex);
80: }
81: }
82:
83: }
|