1 48 49 package org.jpublish.action.jython; 50 51 import java.util.ArrayList ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Properties ; 55 56 import com.anthonyeden.lib.config.Configuration; 57 import com.anthonyeden.lib.config.ConfigurationException; 58 import com.anthonyeden.lib.util.MessageUtilities; 59 import org.apache.commons.logging.Log; 60 import org.apache.commons.logging.LogFactory; 61 import org.jpublish.JPublishEngine; 62 import org.jpublish.JPublishRuntimeException; 63 import org.jpublish.RequestContext; 64 import org.jpublish.action.AbstractScriptHandler; 65 import org.python.core.Py; 66 import org.python.core.PySyntaxError; 67 import org.python.core.PySystemState; 68 import org.python.util.PythonInterpreter; 69 70 77 78 public class JythonScriptHandler extends AbstractScriptHandler { 79 80 private static Log log = LogFactory.getLog(JythonScriptHandler.class); 81 82 private List packages = new ArrayList (); 83 private PythonInterpreter interpreter = null; 84 85 private static final String [] DEFAULT_PACKAGES = { 86 "javax.servlet", 87 "javax.servlet.http" 88 }; 92 93 static { 94 PythonInterpreter.initialize(System.getProperties(), new Properties (), 95 new String [0]); 96 } 97 98 105 106 public void set(String name, Object value, Class c) { 107 interpreter.set(name, value); 108 } 109 110 118 119 public void execute(String name, String scriptString, 120 RequestContext context, Configuration configuration) { 121 try { 122 if (log.isDebugEnabled()) { 123 log.debug("Executing script: " + name); 124 } 125 interpreter.exec(scriptString); 127 } catch (PySyntaxError e) { 128 Object [] args = {name, e.getMessage()}; 129 String msg = MessageUtilities.getMessage(getClass(), 130 JPublishEngine.MESSAGE_PACKAGE, "scriptSyntaxError", args); 131 throw new JPublishRuntimeException(msg, e); 132 } catch (Exception e) { 133 Object [] args = {name, e.getMessage()}; 134 String msg = MessageUtilities.getMessage(getClass(), 135 JPublishEngine.MESSAGE_PACKAGE, "errorExecutingAction", args); 136 throw new JPublishRuntimeException(msg, e); 137 } 138 139 } 140 141 147 148 public void loadConfiguration(Configuration configuration) 149 throws ConfigurationException { 150 Iterator packageElements = 151 configuration.getChildren("package").iterator(); 152 while (packageElements.hasNext()) { 153 Configuration packageElement = 154 (Configuration) packageElements.next(); 155 String packageName = packageElement.getValue(); 156 packages.add(packageName); 157 } 158 } 159 160 163 164 public void init() { 165 167 171 172 interpreter = new PythonInterpreter(null, new PySystemState()); 173 PySystemState sys = Py.getSystemState(); 174 175 sys.setClassLoader(new JythonClassLoader(".", 176 getClass().getClassLoader())); 177 178 if (log.isDebugEnabled()) { 179 ClassLoader sysClassLoader = sys.getClassLoader(); 180 if (sysClassLoader == null) { 181 log.debug("ClassLoader is null"); 182 } else { 183 log.debug("ClassLoader: " + sysClassLoader.getClass()); 184 } 185 } 186 187 for (int i = 0; i < DEFAULT_PACKAGES.length; i++) { 188 sys.add_package(DEFAULT_PACKAGES[i]); 189 } 190 191 Iterator iter = packages.iterator(); 192 while (iter.hasNext()) { 193 String packageName = (String ) iter.next(); 194 sys.add_package(packageName); 195 } 196 197 } 198 199 } 200 | Popular Tags |