KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > PythonGenerator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.language.markup.xsp;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
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 JavaDoc;
38 import org.xml.sax.helpers.AttributesImpl JavaDoc;
39
40 /**
41  * Class representing interpreted XSP-generated
42  * <code>ServerPagesGenerator</code> programs
43  * written in Python language
44  *
45  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
46  * @version CVS $Id: PythonGenerator.java 30932 2004-07-29 17:35:38Z vgritsenko $
47  */

48 public class PythonGenerator extends XSPGenerator
49         implements Configurable, Initializable {
50
51     /**
52      * Python source file
53      */

54     private File JavaDoc file;
55
56     private PythonInterpreter python;
57
58     private PyCode code;
59     private Exception JavaDoc compileError;
60
61
62     public void configure(Configuration configuration) throws ConfigurationException {
63         this.file = new File JavaDoc(configuration.getChild("file").getValue());
64
65         Configuration[] dependencies = configuration.getChildren("dependency");
66         this.dependencies = new File JavaDoc[dependencies.length];
67         for (int i = 0; i < dependencies.length; i ++) {
68             this.dependencies[i] = new File JavaDoc(dependencies[i].getValue());
69         }
70     }
71
72     /**
73      * Determines whether this generator's source files have changed
74      *
75      * @return Whether any of the files this generator depends on has changed
76      * since it was created
77      */

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 JavaDoc {
93         try {
94             Properties JavaDoc properties = new Properties JavaDoc();
95             File JavaDoc workDir = (File JavaDoc)avalonContext.get(Constants.CONTEXT_WORK_DIR);
96             properties.setProperty("python.home", workDir.toString());
97             properties.setProperty("python.packages.fakepath",
98                     (String JavaDoc)avalonContext.get(Constants.CONTEXT_CLASSPATH));
99             PythonInterpreter.initialize(System.getProperties(), properties, new String JavaDoc[]{});
100
101             python = new PythonInterpreter();
102             python.set("page", this);
103             python.set("logger", getLogger());
104             python.set("xspAttr", new AttributesImpl JavaDoc());
105         
106             if (getLogger().isDebugEnabled()) {
107                 getLogger().debug("Compiling script " + file);
108             }
109             
110             this.code = Py.compile(new FileInputStream JavaDoc(this.file),
111                                    this.file.toString(),
112                                    "exec");
113         } catch (Exception JavaDoc e) {
114             this.compileError = e;
115         }
116     }
117
118
119     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
120             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
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 JavaDoc, 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 JavaDoc 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