in src/java/org/apache/turbine/services/assemblerbroker/util/python/PythonBaseFactory.java [71:156]
public T getAssembler(String subDirectory, String name)
throws Exception
{
String path = conf.getString(PYTHON_PATH);
if (StringUtils.isEmpty(path))
{
throw new Exception(
"Python path not found - check your Properties");
}
log.debug("Screen name for JPython: {}", name);
T assembler = null;
String confName = path + "/" + PYTHON_CONFIG_FILE;
// The filename of the Python script
StringBuilder fName = new StringBuilder();
fName.append(path);
fName.append("/");
fName.append(subDirectory);
fName.append("/");
fName.append(name.toLowerCase());
fName.append(".py");
File f = new File(fName.toString());
if (f.exists())
{
// We try to open the Py Interpreter
try (PythonInterpreter interp = new PythonInterpreter())
{
// Make sure the Py Interpreter use the right classloader
// This is necessary for servlet engines generally has
// their own classloader implementations and servlets aren't
// loaded in the system classloader. The python script will
// load java package
// org.apache.turbine.services.assemblerbroker.util.python;
// the new classes to it as well.
Py.getSystemState().setClassLoader(this.getClass().getClassLoader());
// We import the Python SYS module. Now we don't need to do this
// explicitly in the script. We always use the sys module to
// do stuff like loading java package
// org.apache.turbine.services.assemblerbroker.util.python;
interp.exec("import sys");
// Now we try to load the script file
interp.execfile(confName);
interp.execfile(fName.toString());
try
{
// We create an instance of the screen class from the
// python script
interp.exec("scr = " + name + "()");
}
catch (Throwable e)
{
throw new Exception(
"\nCannot create an instance of the python class.\n"
+ "You probably gave your class the wrong name.\n"
+ "Your class should have the same name as your "
+ "filename.\nFilenames should be all lowercase and "
+ "classnames should start with a capital.\n"
+ "Expected class name: " + name + "\n");
}
// Here we convert the python screen instance to a java instance.
@SuppressWarnings("unchecked") // Cast from Object necessary
T t = (T) interp.get("scr", Assembler.class);
assembler = t;
}
catch (Exception e)
{
// We log the error here because this code is not widely tested
// yet. After we tested the code on a range of platforms this
// won't be useful anymore.
log.error("PYTHON SCRIPT SCREEN LOADER ERROR:", e);
throw e;
}
}
return assembler;
}