KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JythonDaemonImpl.java
20  */

21
22 package com.rift.coad.daemon.jython;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.apache.log4j.Logger;
34 import org.python.core.PyObject;
35 import org.python.util.PythonInterpreter;
36 import java.io.FileOutputStream JavaDoc;
37
38 import com.rift.coad.lib.configuration.*;
39
40 /**
41  * This Daemon integrates Jython into coadunation.
42  *
43  * @author Glynn Chaldecott
44  */

45 public class JythonDaemonImpl implements JythonDaemon {
46     
47     protected Logger log =
48             Logger.getLogger(JythonDaemonImpl.class.getName());
49     
50     public String JavaDoc scriptLocal = "";
51     
52     /**
53      * Creates a new instance of JythonEmbedImpl and configures Jython for use.
54      */

55     public JythonDaemonImpl() throws Exception JavaDoc {
56         try {
57             com.rift.coad.lib.configuration.Configuration coadConfig =
58                     com.rift.coad.lib.configuration.ConfigurationFactory.
59                     getInstance().getConfig(com.rift.coad.daemon.jython.
60
                    JythonDaemonImpl.class);
61             System.setProperty("python.home",
62                     coadConfig.getString("python_home"));
63             scriptLocal = coadConfig.getString("script_location");
64         } catch (ConfigurationException ex) {
65             log.error("Failed to set jython properties :" + ex.getMessage(),
66                             ex);
67             throw new Exception JavaDoc("Failed to set jython properties :" + ex);
68         }
69     }
70     
71     /**
72      * This method is called when a user wants to run a stored script. It will
73      * then return the requested value.
74      *
75      * @param name This is the name of the script that a user wishes to run.
76      * @param returnValue This is the name of the value a user wishes to have
77      * returned.
78      * @param javaclass This is the type of object a user wants the returned
79      * value to returned as.
80      * @return Returns a value from the script.
81      */

82     public Object JavaDoc runScript(String JavaDoc name, String JavaDoc returnValue, Class JavaDoc javaclass)
83             throws RemoteException JavaDoc {
84         File JavaDoc scriptFile = new File JavaDoc(scriptLocal + File.separator + name);
85         try {
86             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(scriptFile);
87             PythonInterpreter inter = new PythonInterpreter();
88             inter.execfile(fis);
89             return inter.get(returnValue,javaclass);
90         } catch (Exception JavaDoc ex) {
91             log.error("Failed to retrieve and run script:" + ex, ex);
92         }
93         return null;
94     }
95
96     /**
97      * This script is called in order to register a new script within
98      * Coadunation.
99      *
100      * @param script This is a string containing the script that will be
101      * inserted in a python file.
102      * @param name This is what the script will be called as well as what the
103      * python file will be named.
104      */

105     public void registerScript(byte[] file, String JavaDoc name)
106             throws RemoteException JavaDoc {
107         try {
108             File JavaDoc temp = File.createTempFile(name, null);
109             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(temp);
110             fos.write(file);
111             fos.close();
112             File JavaDoc supFile = new File JavaDoc(scriptLocal + File.separator + name);
113             temp.renameTo(supFile);
114         } catch (Exception JavaDoc ex) {
115             log.error("Failed to register script:" + ex, ex);
116         }
117     }
118
119     /**
120      * This method is called when a user wants to run a stored script. It will
121      * then return the requested value. A user can also specify value for
122      * variables within the script.
123      *
124      * @param name This is the name of the script that a user wishes to run.
125      * @param returnValue This is the name of the value a user wishes to have
126      * returned.
127      * @param javaclass This is the type of object a user wants the returned
128      * value to returned as.
129      * @param arguments This is a Map object containing as the key the name of
130      * the variable and the value for that variable.
131      * @return Returns a value from the script.
132      */

133     public Object JavaDoc runScript(String JavaDoc name, String JavaDoc returnValue, Class JavaDoc javaclass,
134             Map JavaDoc arguments) throws RemoteException JavaDoc {
135         File JavaDoc scriptFile = new File JavaDoc(scriptLocal + File.separator + name);
136         try {
137             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(scriptFile);
138             PythonInterpreter inter = new PythonInterpreter();
139             Iterator JavaDoc key = arguments.keySet().iterator();
140             while (key.hasNext()) {
141                 String JavaDoc temp = (String JavaDoc) key.next();
142                 Object JavaDoc tempValue = arguments.get(temp);
143                 inter.set(temp,tempValue);
144             }
145             inter.execfile(fis);
146             return inter.get(returnValue,javaclass);
147         } catch (Exception JavaDoc ex) {
148             log.error("Failed to retrieve and run script:" + ex, ex);
149         }
150         return null;
151     }
152
153 }
154
Popular Tags