1 package org.apache.turbine.services.assemblerbroker.util.python; 2 3 18 19 import java.io.File ; 20 21 import org.apache.commons.configuration.Configuration; 22 23 import org.apache.commons.lang.StringUtils; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.apache.turbine.modules.Assembler; 29 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker; 30 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory; 31 32 import org.python.core.Py; 33 import org.python.util.PythonInterpreter; 34 35 45 public abstract class PythonBaseFactory 46 implements AssemblerFactory 47 { 48 49 public static final String PYTHON_PATH = "python.path"; 50 51 52 public static final String PYTHON_CONFIG_FILE = "conf.py"; 53 54 55 private static Log log = LogFactory.getLog(PythonBaseFactory.class); 56 57 58 private Configuration conf = 59 TurbineAssemblerBroker.getService().getConfiguration(); 60 61 69 public Assembler getAssembler(String subDirectory, String name) 70 throws Exception 71 { 72 String path = conf.getString(PYTHON_PATH); 73 74 if (StringUtils.isEmpty(path)) 75 { 76 throw new Exception ( 77 "Python path not found - check your Properties"); 78 } 79 80 log.debug("Screen name for JPython: " + name); 81 82 Assembler assembler = null; 83 84 String confName = path + "/" + PYTHON_CONFIG_FILE; 85 86 StringBuffer fName = new StringBuffer (); 88 89 fName.append(path); 90 fName.append("/"); 91 fName.append(subDirectory); 92 fName.append("/"); 93 fName.append(name.toLowerCase()); 94 fName.append(".py"); 95 96 File f = new File (fName.toString()); 97 98 if (f.exists()) 99 { 100 try 101 { 102 PythonInterpreter interp = new PythonInterpreter(); 104 105 Py.getSystemState().setClassLoader( 113 this.getClass().getClassLoader()); 114 115 interp.exec("import sys"); 120 121 interp.execfile(confName); 123 interp.execfile(fName.toString()); 124 125 try 126 { 127 interp.exec("scr = " + name + "()"); 130 } 131 catch (Throwable e) 132 { 133 throw new Exception ( 134 "\nCannot create an instance of the python class.\n" 135 + "You probably gave your class the wrong name.\n" 136 + "Your class should have the same name as your " 137 + "filename.\nFilenames should be all lowercase and " 138 + "classnames should start with a capital.\n" 139 + "Expected class name: " + name + "\n"); 140 } 141 142 assembler = (Assembler) interp.get("scr", Assembler.class); 144 } 145 catch (Exception e) 146 { 147 log.error("PYTHON SCRIPT SCREEN LOADER ERROR:", e); 151 throw e; 152 } 153 } 154 return assembler; 155 } 156 } 157 | Popular Tags |