KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > jython > JythonTimerImpl


1 /*
2  * Timer: The timer class
3  * Copyright (C) 2006-2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.Serializable JavaDoc;
30 import java.rmi.RemoteException JavaDoc;
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 =
41             Logger.getLogger(JythonDaemonImpl.class.getName());
42     
43     public String JavaDoc scriptLocal = "";
44     
45     /** Creates a new instance of JythonTimerImpl */
46     public JythonTimerImpl() throws Exception JavaDoc {
47         try {
48             com.rift.coad.lib.configuration.Configuration coadConfig =
49                     com.rift.coad.lib.configuration.ConfigurationFactory.
50                     getInstance().getConfig(com.rift.coad.daemon.jython.
51
                    JythonDaemonImpl.class);
52             System.setProperty("python.home",
53                     coadConfig.getString("python_home"));
54             scriptLocal = coadConfig.getString("script_location");
55         } catch (ConfigurationException ex) {
56             log.error("Failed to set jython properties :" + ex.getMessage(),
57                             ex);
58             throw new Exception JavaDoc("Failed to set jython properties :" + ex);
59         }
60     }
61
62     /**
63      * This method implements the TimerEventHandler thus allowing a user to run
64      * scripts using the Coadunation Timer Daemon.
65      *
66      * @param serializable This is the serializable event supplied to the Timer
67      * Daemon and is used as the name of the script.
68      */

69     public void processEvent(Serializable JavaDoc serializable) throws RemoteException JavaDoc,
70             TimerException {
71         String JavaDoc name = (String JavaDoc) serializable;
72         File JavaDoc scriptFile = new File JavaDoc(scriptLocal + File.separator + name);
73         try {
74             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(scriptFile);
75             PythonInterpreter inter = new PythonInterpreter();
76             inter.execfile(fis);
77         } catch (Exception JavaDoc ex) {
78             log.error("Failed to retrieve and run script:" + ex, ex);
79         }
80     }
81     
82 }
83
Popular Tags