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
20
21
22 import java.io.File;
23
24 import org.apache.commons.configuration.Configuration;
25
26 import org.apache.commons.lang.StringUtils;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.apache.turbine.modules.Assembler;
32 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
33 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
34
35 import org.python.core.Py;
36 import org.python.util.PythonInterpreter;
37
38 /***
39 * A factory that attempts to load a python class in the
40 * JPython interpreter and execute it as a Turbine screen.
41 * The JPython script should inherit from Turbine Screen or one
42 * of its subclasses.
43 *
44 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
45 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46 * @version $Id: PythonBaseFactory.java 534527 2007-05-02 16:10:59Z tv $
47 */
48 public abstract class PythonBaseFactory
49 implements AssemblerFactory
50 {
51 /*** Key for the python path */
52 public static final String PYTHON_PATH = "python.path";
53
54 /*** Global config file. This is executed before every screen */
55 public static final String PYTHON_CONFIG_FILE = "conf.py";
56
57 /*** Logging */
58 private static Log log = LogFactory.getLog(PythonBaseFactory.class);
59
60 /*** Our configuration */
61 private Configuration conf =
62 TurbineAssemblerBroker.getService().getConfiguration();
63
64 /***
65 * Get an Assembler.
66 *
67 * @param subDirectory subdirectory within python.path
68 * @param name name of the requested Assembler
69 * @return an Assembler
70 * @throws Exception generic exception
71 */
72 public Assembler getAssembler(String subDirectory, String name)
73 throws Exception
74 {
75 String path = conf.getString(PYTHON_PATH);
76
77 if (StringUtils.isEmpty(path))
78 {
79 throw new Exception(
80 "Python path not found - check your Properties");
81 }
82
83 log.debug("Screen name for JPython: " + name);
84
85 Assembler assembler = null;
86
87 String confName = path + "/" + PYTHON_CONFIG_FILE;
88
89
90 StringBuffer fName = new StringBuffer();
91
92 fName.append(path);
93 fName.append("/");
94 fName.append(subDirectory);
95 fName.append("/");
96 fName.append(name.toLowerCase());
97 fName.append(".py");
98
99 File f = new File(fName.toString());
100
101 if (f.exists())
102 {
103 try
104 {
105
106 PythonInterpreter interp = new PythonInterpreter();
107
108
109
110
111
112
113
114
115 Py.getSystemState().setClassLoader(
116 this.getClass().getClassLoader());
117
118
119
120
121
122 interp.exec("import sys");
123
124
125 interp.execfile(confName);
126 interp.execfile(fName.toString());
127
128 try
129 {
130
131
132 interp.exec("scr = " + name + "()");
133 }
134 catch (Throwable e)
135 {
136 throw new Exception(
137 "\nCannot create an instance of the python class.\n"
138 + "You probably gave your class the wrong name.\n"
139 + "Your class should have the same name as your "
140 + "filename.\nFilenames should be all lowercase and "
141 + "classnames should start with a capital.\n"
142 + "Expected class name: " + name + "\n");
143 }
144
145
146 assembler = (Assembler) interp.get("scr", Assembler.class);
147 }
148 catch (Exception e)
149 {
150
151
152
153 log.error("PYTHON SCRIPT SCREEN LOADER ERROR:", e);
154 throw e;
155 }
156 }
157 return assembler;
158 }
159 }