1 21 22 package com.rift.coad.daemon.jython; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.rmi.RemoteException ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import org.apache.log4j.Logger; 34 import org.python.core.PyObject; 35 import org.python.util.PythonInterpreter; 36 import java.io.FileOutputStream ; 37 38 import com.rift.coad.lib.configuration.*; 39 40 45 public class JythonDaemonImpl implements JythonDaemon { 46 47 protected Logger log = 48 Logger.getLogger(JythonDaemonImpl.class.getName()); 49 50 public String scriptLocal = ""; 51 52 55 public JythonDaemonImpl() throws Exception { 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 ("Failed to set jython properties :" + ex); 68 } 69 } 70 71 82 public Object runScript(String name, String returnValue, Class javaclass) 83 throws RemoteException { 84 File scriptFile = new File (scriptLocal + File.separator + name); 85 try { 86 FileInputStream fis = new FileInputStream (scriptFile); 87 PythonInterpreter inter = new PythonInterpreter(); 88 inter.execfile(fis); 89 return inter.get(returnValue,javaclass); 90 } catch (Exception ex) { 91 log.error("Failed to retrieve and run script:" + ex, ex); 92 } 93 return null; 94 } 95 96 105 public void registerScript(byte[] file, String name) 106 throws RemoteException { 107 try { 108 File temp = File.createTempFile(name, null); 109 FileOutputStream fos = new FileOutputStream (temp); 110 fos.write(file); 111 fos.close(); 112 File supFile = new File (scriptLocal + File.separator + name); 113 temp.renameTo(supFile); 114 } catch (Exception ex) { 115 log.error("Failed to register script:" + ex, ex); 116 } 117 } 118 119 133 public Object runScript(String name, String returnValue, Class javaclass, 134 Map arguments) throws RemoteException { 135 File scriptFile = new File (scriptLocal + File.separator + name); 136 try { 137 FileInputStream fis = new FileInputStream (scriptFile); 138 PythonInterpreter inter = new PythonInterpreter(); 139 Iterator key = arguments.keySet().iterator(); 140 while (key.hasNext()) { 141 String temp = (String ) key.next(); 142 Object tempValue = arguments.get(temp); 143 inter.set(temp,tempValue); 144 } 145 inter.execfile(fis); 146 return inter.get(returnValue,javaclass); 147 } catch (Exception ex) { 148 log.error("Failed to retrieve and run script:" + ex, ex); 149 } 150 return null; 151 } 152 153 } 154 | Popular Tags |