001: /*
002: * Timer: The timer class
003: * Copyright (C) 2006-2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * JythonDaemonImpl.java
020: */
021:
022: package com.rift.coad.daemon.jython;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import java.io.Serializable;
029: import java.rmi.RemoteException;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Set;
033: import org.apache.log4j.Logger;
034: import org.python.core.PyObject;
035: import org.python.util.PythonInterpreter;
036: import java.io.FileOutputStream;
037:
038: import com.rift.coad.lib.configuration.*;
039:
040: /**
041: * This Daemon integrates Jython into coadunation.
042: *
043: * @author Glynn Chaldecott
044: */
045: public class JythonDaemonImpl implements JythonDaemon {
046:
047: protected Logger log = Logger.getLogger(JythonDaemonImpl.class
048: .getName());
049:
050: public String scriptLocal = "";
051:
052: /**
053: * Creates a new instance of JythonEmbedImpl and configures Jython for use.
054: */
055: public JythonDaemonImpl() throws Exception {
056: try {
057: com.rift.coad.lib.configuration.Configuration coadConfig = com.rift.coad.lib.configuration.ConfigurationFactory
058: .getInstance()
059: .getConfig(
060: com.rift.coad.daemon.jython.JythonDaemonImpl.class);
061: System.setProperty("python.home", coadConfig
062: .getString("python_home"));
063: scriptLocal = coadConfig.getString("script_location");
064: } catch (ConfigurationException ex) {
065: log.error("Failed to set jython properties :"
066: + ex.getMessage(), ex);
067: throw new Exception("Failed to set jython properties :"
068: + ex);
069: }
070: }
071:
072: /**
073: * This method is called when a user wants to run a stored script. It will
074: * then return the requested value.
075: *
076: * @param name This is the name of the script that a user wishes to run.
077: * @param returnValue This is the name of the value a user wishes to have
078: * returned.
079: * @param javaclass This is the type of object a user wants the returned
080: * value to returned as.
081: * @return Returns a value from the script.
082: */
083: public Object runScript(String name, String returnValue,
084: Class javaclass) throws RemoteException {
085: File scriptFile = new File(scriptLocal + File.separator + name);
086: try {
087: FileInputStream fis = new FileInputStream(scriptFile);
088: PythonInterpreter inter = new PythonInterpreter();
089: inter.execfile(fis);
090: return inter.get(returnValue, javaclass);
091: } catch (Exception ex) {
092: log.error("Failed to retrieve and run script:" + ex, ex);
093: }
094: return null;
095: }
096:
097: /**
098: * This script is called in order to register a new script within
099: * Coadunation.
100: *
101: * @param script This is a string containing the script that will be
102: * inserted in a python file.
103: * @param name This is what the script will be called as well as what the
104: * python file will be named.
105: */
106: public void registerScript(byte[] file, String name)
107: throws RemoteException {
108: try {
109: File temp = File.createTempFile(name, null);
110: FileOutputStream fos = new FileOutputStream(temp);
111: fos.write(file);
112: fos.close();
113: File supFile = new File(scriptLocal + File.separator + name);
114: temp.renameTo(supFile);
115: } catch (Exception ex) {
116: log.error("Failed to register script:" + ex, ex);
117: }
118: }
119:
120: /**
121: * This method is called when a user wants to run a stored script. It will
122: * then return the requested value. A user can also specify value for
123: * variables within the script.
124: *
125: * @param name This is the name of the script that a user wishes to run.
126: * @param returnValue This is the name of the value a user wishes to have
127: * returned.
128: * @param javaclass This is the type of object a user wants the returned
129: * value to returned as.
130: * @param arguments This is a Map object containing as the key the name of
131: * the variable and the value for that variable.
132: * @return Returns a value from the script.
133: */
134: public Object runScript(String name, String returnValue,
135: Class javaclass, Map arguments) throws RemoteException {
136: File scriptFile = new File(scriptLocal + File.separator + name);
137: try {
138: FileInputStream fis = new FileInputStream(scriptFile);
139: PythonInterpreter inter = new PythonInterpreter();
140: Iterator key = arguments.keySet().iterator();
141: while (key.hasNext()) {
142: String temp = (String) key.next();
143: Object tempValue = arguments.get(temp);
144: inter.set(temp, tempValue);
145: }
146: inter.execfile(fis);
147: return inter.get(returnValue, javaclass);
148: } catch (Exception ex) {
149: log.error("Failed to retrieve and run script:" + ex, ex);
150: }
151: return null;
152: }
153:
154: }
|