KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > action > jython > JythonScriptHandler


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.action.jython;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import com.anthonyeden.lib.config.Configuration;
57 import com.anthonyeden.lib.config.ConfigurationException;
58 import com.anthonyeden.lib.util.MessageUtilities;
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61 import org.jpublish.JPublishEngine;
62 import org.jpublish.JPublishRuntimeException;
63 import org.jpublish.RequestContext;
64 import org.jpublish.action.AbstractScriptHandler;
65 import org.python.core.Py;
66 import org.python.core.PySyntaxError;
67 import org.python.core.PySystemState;
68 import org.python.util.PythonInterpreter;
69
70 /**
71  * Implementation of a ScriptHandler which uses the Jython scripting engine. While Jython could be implemented using the
72  * BSFScriptHandler, there are a number of Jython-specific calls which must be made to the Jython interpreter in order
73  * for Jython to work properly in an environment where custom class loaders are used.
74  *
75  * @author Anthony Eden
76  */

77
78 public class JythonScriptHandler extends AbstractScriptHandler {
79
80     private static Log log = LogFactory.getLog(JythonScriptHandler.class);
81
82     private List JavaDoc packages = new ArrayList JavaDoc();
83     private PythonInterpreter interpreter = null;
84
85     private static final String JavaDoc[] DEFAULT_PACKAGES = {
86         "javax.servlet",
87         "javax.servlet.http"
88         //"com.anthonyeden.lib",
89
//"com.anthonyeden.lib.config",
90
//"com.anthonyeden.lib.util"
91
};
92
93     static {
94         PythonInterpreter.initialize(System.getProperties(), new Properties JavaDoc(),
95                 new String JavaDoc[0]);
96     }
97
98     /**
99      * Set the variable.
100      *
101      * @param name The variable name
102      * @param value The variable value
103      * @param c The Class
104      */

105
106     public void set(String JavaDoc name, Object JavaDoc value, Class JavaDoc c) {
107         interpreter.set(name, value);
108     }
109
110     /**
111      * Execute the specified script.
112      *
113      * @param name The script name
114      * @param scriptString The script text
115      * @param context The request context
116      * @param configuration The Configuration object
117      */

118
119     public void execute(String JavaDoc name, String JavaDoc scriptString,
120             RequestContext context, Configuration configuration) {
121         try {
122             if (log.isDebugEnabled()) {
123                 log.debug("Executing script: " + name);
124             }
125             // execute the script
126
interpreter.exec(scriptString);
127         } catch (PySyntaxError e) {
128             Object JavaDoc[] args = {name, e.getMessage()};
129             String JavaDoc msg = MessageUtilities.getMessage(getClass(),
130                     JPublishEngine.MESSAGE_PACKAGE, "scriptSyntaxError", args);
131             throw new JPublishRuntimeException(msg, e);
132         } catch (Exception JavaDoc e) {
133             Object JavaDoc[] args = {name, e.getMessage()};
134             String JavaDoc msg = MessageUtilities.getMessage(getClass(),
135                     JPublishEngine.MESSAGE_PACKAGE, "errorExecutingAction", args);
136             throw new JPublishRuntimeException(msg, e);
137         }
138
139     }
140
141     /**
142      * Load the script handler's configuration.
143      *
144      * @param configuration The Configuration object
145      * @throws ConfigurationException
146      */

147
148     public void loadConfiguration(Configuration configuration)
149             throws ConfigurationException {
150         Iterator JavaDoc packageElements =
151                 configuration.getChildren("package").iterator();
152         while (packageElements.hasNext()) {
153             Configuration packageElement =
154                     (Configuration) packageElements.next();
155             String JavaDoc packageName = packageElement.getValue();
156             packages.add(packageName);
157         }
158     }
159
160     /**
161      * Initialize the script handler.
162      */

163
164     public void init() {
165         // configure Python Interpreter
166

167         /*
168         FileSystemManager fileSystemManager =
169             siteContext.getContextFileSystemManager();
170         */

171                      
172         interpreter = new PythonInterpreter(null, new PySystemState());
173         PySystemState sys = Py.getSystemState();
174
175         sys.setClassLoader(new JythonClassLoader(".",
176                 getClass().getClassLoader()));
177
178         if (log.isDebugEnabled()) {
179             ClassLoader JavaDoc sysClassLoader = sys.getClassLoader();
180             if (sysClassLoader == null) {
181                 log.debug("ClassLoader is null");
182             } else {
183                 log.debug("ClassLoader: " + sysClassLoader.getClass());
184             }
185         }
186
187         for (int i = 0; i < DEFAULT_PACKAGES.length; i++) {
188             sys.add_package(DEFAULT_PACKAGES[i]);
189         }
190
191         Iterator JavaDoc iter = packages.iterator();
192         while (iter.hasNext()) {
193             String JavaDoc packageName = (String JavaDoc) iter.next();
194             sys.add_package(packageName);
195         }
196
197     }
198
199 }
200
Popular Tags