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
23
24 import java.io.File;
25
26 import org.apache.commons.configuration2.Configuration;
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.apache.turbine.modules.Assembler;
31 import org.apache.turbine.modules.Loader;
32 import org.apache.turbine.services.TurbineServices;
33 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
34 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
35 import org.python.core.Py;
36 import org.python.util.PythonInterpreter;
37
38
39
40
41
42
43
44
45
46
47
48 public abstract class PythonBaseFactory<T extends Assembler>
49 implements AssemblerFactory<T>
50 {
51
52 public static final String PYTHON_PATH = "python.path";
53
54
55 public static final String PYTHON_CONFIG_FILE = "conf.py";
56
57
58 private static Logger log = LogManager.getLogger(PythonBaseFactory.class);
59
60
61 private final Configuration conf = TurbineServices.getInstance().getConfiguration(AssemblerBrokerService.SERVICE_NAME);
62
63
64
65
66
67
68
69
70
71 public T getAssembler(String subDirectory, String name)
72 throws Exception
73 {
74 String path = conf.getString(PYTHON_PATH);
75
76 if (StringUtils.isEmpty(path))
77 {
78 throw new Exception(
79 "Python path not found - check your Properties");
80 }
81
82 log.debug("Screen name for JPython: {}", name);
83
84 T assembler = null;
85
86 String confName = path + "/" + PYTHON_CONFIG_FILE;
87
88
89 StringBuilder fName = new StringBuilder();
90
91 fName.append(path);
92 fName.append("/");
93 fName.append(subDirectory);
94 fName.append("/");
95 fName.append(name.toLowerCase());
96 fName.append(".py");
97
98 File f = new File(fName.toString());
99
100 if (f.exists())
101 {
102
103 try (PythonInterpreter interp = new PythonInterpreter())
104 {
105
106
107
108
109
110
111
112 Py.getSystemState().setClassLoader(this.getClass().getClassLoader());
113
114
115
116
117
118 interp.exec("import sys");
119
120
121 interp.execfile(confName);
122 interp.execfile(fName.toString());
123
124 try
125 {
126
127
128 interp.exec("scr = " + name + "()");
129 }
130 catch (Throwable e)
131 {
132 throw new Exception(
133 "\nCannot create an instance of the python class.\n"
134 + "You probably gave your class the wrong name.\n"
135 + "Your class should have the same name as your "
136 + "filename.\nFilenames should be all lowercase and "
137 + "classnames should start with a capital.\n"
138 + "Expected class name: " + name + "\n");
139 }
140
141
142 @SuppressWarnings("unchecked")
143 T t = (T) interp.get("scr", Assembler.class);
144 assembler = t;
145 }
146 catch (Exception e)
147 {
148
149
150
151 log.error("PYTHON SCRIPT SCREEN LOADER ERROR:", e);
152 throw e;
153 }
154 }
155 return assembler;
156 }
157
158
159
160
161
162
163 @Override
164 public abstract Loader<T> getLoader();
165
166
167
168
169
170
171 @Override
172 public int getCacheSize()
173
174 {
175 return getLoader().getCacheSize();
176 }
177 }