View Javadoc
1 package org.apache.turbine.services.assemblerbroker.util.python; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 // JDK Classes 58 import java.io.File; 59 60 // Turbine Classes 61 import org.apache.turbine.modules.Assembler; 62 63 import org.apache.turbine.services.TurbineServices; 64 65 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService; 66 67 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory; 68 69 import org.apache.turbine.services.resources.TurbineResources; 70 71 import org.apache.turbine.util.Log; 72 73 // JPython Classes 74 import org.python.core.Py; 75 76 import org.python.util.PythonInterpreter; 77 78 79 80 /*** 81 * A screen factory that attempts to load a python class in the 82 * JPython interpreter and execute it as a Turbine screen. 83 * The JPython script should inherit from Turbine Screen or one 84 * of its subclasses. 85 */ 86 public abstract class PythonBaseFactory implements AssemblerFactory 87 { 88 89 90 public Assembler getAssembler( String subDirectory, String name ) throws Exception 91 { 92 Assembler assembler = null; 93 94 // The filename of the Python script 95 String fName = null; 96 String confName = null; 97 98 99 Log.info ("Screen name for JPython "+name); 100 101 try 102 { 103 String path = TurbineResources.getString ( 104 TurbineServices.SERVICE_PREFIX + 105 AssemblerBrokerService.SERVICE_NAME + ".python.path")+"/"; 106 confName = path+"conf.py"; 107 fName = path+subDirectory+"/"+name.toLowerCase()+".py"; 108 } 109 catch (Exception e) 110 { 111 throw new Exception ("Python path not found - check your Properties"); 112 } 113 114 File f = new File (fName); 115 if (f.exists()) 116 { 117 try 118 { 119 // We try to open the Py Interpreter 120 PythonInterpreter interp = new PythonInterpreter(); 121 122 // Make sure the Py Interpreter use the right classloader 123 // This is necissarry for servlet engines generally has 124 // their own classloader implementations and servlets aren't 125 // loaded in the system classloader. The python script will 126 // load java package org.apache.turbine.services.assemblerbroker.util.python; 127 // the new classes to it as well. 128 Py.getSystemState().setClassLoader(this.getClass().getClassLoader()); 129 130 // We import the Python SYS module. Now we don't need to do this 131 // explicitely in the scrypt. We always use the sys module to 132 // do stuff like loading java package org.apache.turbine.services.assemblerbroker.util.python; 133 interp.exec("import sys"); 134 135 // Now we try to load the script file 136 interp.execfile (confName); 137 interp.execfile (fName); 138 139 try 140 { 141 // We create an instance of the screen class from the python script 142 interp.exec("scr = "+name+"()"); 143 } 144 catch (Throwable e) 145 { 146 throw new Exception ("\nCannot create an instance of the python class.\n"+ 147 "You probably gave your class the wrong name.\n"+ 148 "Your class should have the same name as your filename.\n"+ 149 "Filenames should be all lowercase and classnames should "+ 150 "start with a capital.\n"+ 151 "Expected class name: "+name+"\n"); 152 } 153 154 155 156 // Here we convert the python sceen instance to a java instance. 157 158 assembler = (Assembler)interp.get ("scr",Assembler.class); 159 160 } 161 catch (Exception e) 162 { 163 // We log the error here because this code is not widely tested yet. 164 // After we tested the code on a range of platforms this won't be 165 // usefull anymore. 166 Log.error ("PYTHON SCRIPT SCREEN LOADER ERROR:"); 167 Log.error (e.toString()); 168 // Let the error fall through like the normal way. 169 throw e; 170 } 171 172 } 173 174 return assembler; 175 } 176 177 }

This page was automatically generated by Maven