1 package org.apache.turbine.services.assemblerbroker.util.python;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 /***
36 * A factory that attempts to load a python class in the
37 * JPython interpreter and execute it as a Turbine screen.
38 * The JPython script should inherit from Turbine Screen or one
39 * of its subclasses.
40 *
41 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
42 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43 * @version $Id: PythonBaseFactory.java 264148 2005-08-29 14:21:04Z henning $
44 */
45 public abstract class PythonBaseFactory
46 implements AssemblerFactory
47 {
48 /*** Key for the python path */
49 public static final String PYTHON_PATH = "python.path";
50
51 /*** Global config file. This is executed before every screen */
52 public static final String PYTHON_CONFIG_FILE = "conf.py";
53
54 /*** Logging */
55 private static Log log = LogFactory.getLog(PythonBaseFactory.class);
56
57 /*** Our configuration */
58 private Configuration conf =
59 TurbineAssemblerBroker.getService().getConfiguration();
60
61 /***
62 * Get an Assembler.
63 *
64 * @param subDirectory subdirectory within python.path
65 * @param name name of the requested Assembler
66 * @return an Assembler
67 * @throws Exception generic exception
68 */
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
87 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
103 PythonInterpreter interp = new PythonInterpreter();
104
105
106
107
108
109
110
111
112 Py.getSystemState().setClassLoader(
113 this.getClass().getClassLoader());
114
115
116
117
118
119 interp.exec("import sys");
120
121
122 interp.execfile(confName);
123 interp.execfile(fName.toString());
124
125 try
126 {
127
128
129 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
143 assembler = (Assembler) interp.get("scr", Assembler.class);
144 }
145 catch (Exception e)
146 {
147
148
149
150 log.error("PYTHON SCRIPT SCREEN LOADER ERROR:", e);
151 throw e;
152 }
153 }
154 return assembler;
155 }
156 }