View Javadoc

1   package org.apache.turbine.services.assemblerbroker.util.python;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // The filename of the Python script
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                 // We try to open the Py Interpreter
106                 PythonInterpreter interp = new PythonInterpreter();
107 
108                 // Make sure the Py Interpreter use the right classloader
109                 // This is necessary for servlet engines generally has
110                 // their own classloader implementations and servlets aren't
111                 // loaded in the system classloader.  The python script will
112                 // load java package
113                 // org.apache.turbine.services.assemblerbroker.util.python;
114                 // the new classes to it as well.
115                 Py.getSystemState().setClassLoader(
116                         this.getClass().getClassLoader());
117 
118                 // We import the Python SYS module. Now we don't need to do this
119                 // explicitely in the script.  We always use the sys module to
120                 // do stuff like loading java package
121                 // org.apache.turbine.services.assemblerbroker.util.python;
122                 interp.exec("import sys");
123 
124                 // Now we try to load the script file
125                 interp.execfile(confName);
126                 interp.execfile(fName.toString());
127 
128                 try
129                 {
130                     // We create an instance of the screen class from the
131                     // python script
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                 // Here we convert the python sceen instance to a java instance.
146                 assembler = (Assembler) interp.get("scr", Assembler.class);
147             }
148             catch (Exception e)
149             {
150                 // We log the error here because this code is not widely tested
151                 // yet. After we tested the code on a range of platforms this
152                 // won't be usefull anymore.
153                 log.error("PYTHON SCRIPT SCREEN LOADER ERROR:", e);
154                 throw e;
155             }
156         }
157         return assembler;
158     }
159 }