1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.IOException ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.parameters.Parameters; 29 30 import org.apache.cocoon.Constants; 31 import org.apache.cocoon.ProcessingException; 32 import org.apache.cocoon.environment.SourceResolver; 33 34 import org.python.core.Py; 35 import org.python.core.PyCode; 36 import org.python.util.PythonInterpreter; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.helpers.AttributesImpl ; 39 40 48 public class PythonGenerator extends XSPGenerator 49 implements Configurable, Initializable { 50 51 54 private File file; 55 56 private PythonInterpreter python; 57 58 private PyCode code; 59 private Exception compileError; 60 61 62 public void configure(Configuration configuration) throws ConfigurationException { 63 this.file = new File (configuration.getChild("file").getValue()); 64 65 Configuration[] dependencies = configuration.getChildren("dependency"); 66 this.dependencies = new File [dependencies.length]; 67 for (int i = 0; i < dependencies.length; i ++) { 68 this.dependencies[i] = new File (dependencies[i].getValue()); 69 } 70 } 71 72 78 public boolean modifiedSince(long date) { 79 if (this.file.lastModified() < date) { 80 return true; 81 } 82 83 for (int i = 0; i < dependencies.length; i++) { 84 if (this.file.lastModified() < dependencies[i].lastModified()) { 85 return true; 86 } 87 } 88 89 return false; 90 } 91 92 public void initialize() throws Exception { 93 try { 94 Properties properties = new Properties (); 95 File workDir = (File )avalonContext.get(Constants.CONTEXT_WORK_DIR); 96 properties.setProperty("python.home", workDir.toString()); 97 properties.setProperty("python.packages.fakepath", 98 (String )avalonContext.get(Constants.CONTEXT_CLASSPATH)); 99 PythonInterpreter.initialize(System.getProperties(), properties, new String []{}); 100 101 python = new PythonInterpreter(); 102 python.set("page", this); 103 python.set("logger", getLogger()); 104 python.set("xspAttr", new AttributesImpl ()); 105 106 if (getLogger().isDebugEnabled()) { 107 getLogger().debug("Compiling script " + file); 108 } 109 110 this.code = Py.compile(new FileInputStream (this.file), 111 this.file.toString(), 112 "exec"); 113 } catch (Exception e) { 114 this.compileError = e; 115 } 116 } 117 118 119 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 120 throws ProcessingException, SAXException , IOException { 121 super.setup(resolver, objectModel, src, par); 122 123 if (this.compileError != null) { 124 throw new ProcessingException("Failed to compile script", compileError); 125 } 126 127 python.set("objectModel", this.objectModel); 128 python.set("request", this.request); 129 python.set("response", this.response); 130 python.set("context", this.context); 131 python.set("resolver", this.resolver); 132 python.set("parameters", this.parameters); 133 } 134 135 public void generate() throws IOException , ProcessingException { 136 try { 137 python.set("contentHandler", this.contentHandler); 138 139 if (getLogger().isDebugEnabled()) { 140 getLogger().debug("Executing script " + file); 141 } 142 python.exec(code); 143 } catch (Exception e) { 144 throw new ProcessingException("generate: Got Python exception", e); 145 } 146 } 147 148 public void recycle() { 149 python.set("contentHandler", null); 150 151 python.set("objectModel", null); 152 python.set("request", null); 153 python.set("response", null); 154 python.set("context", null); 155 python.set("resolver", null); 156 python.set("parameters", null); 157 158 super.recycle(); 159 } 160 161 public void dispose() { 162 python.set("page", null); 163 python.set("logger", null); 164 python.set("xspAttr", null); 165 166 this.python = null; 167 this.compileError = null; 168 this.code = null; 169 170 super.dispose(); 171 } 172 } 173 | Popular Tags |