001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jWebApp.
006: *
007: * jWebApp is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jWebApp is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jWebApp; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jwebapp.request;
020:
021: import java.net.MalformedURLException;
022: import jwebtk.scripting.ScriptingException;
023: import jwebtk.scripting.ScriptingManager;
024: import java.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.InputStreamReader;
027: import java.net.URL;
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030: import java.util.logging.Logger;
031: import jwebapp.controller.RequestHandler;
032: import jwebapp.controller.ServerInterface;
033:
034: /**
035: * <p>Provides unlimited scriping abilities in place of writing request handlers.
036: * This class allows you to use any Bean Scripting Framework (BSF) compatable
037: * scripting language such as:
038: * <pre>
039: * javascript, jacl, netrexx, java, javaclass, bml,
040: * vbscript, jscript, perlscript, perl, jpython,
041: * jython, lotusscript, xslt, pnuts, beanbasic,
042: * beanshell, ruby, judoscript
043: * </pre>
044: * <p>or any other scripting language that works under BSF. Simply copy the JAR files
045: * for the scripting language into the appropriate lib directory. And define:
046: * <pre>
047: * <request id="yourRequestId" handlerClass="jwebapp.requestHandlers.Scripting">
048: * <parameter name="scriptFileName" value="/WEB-INF/YourScript.bsh" />
049: * </request>
050: * </pre>
051: * <p>The script has access to an implicit object named "serverInterface" of the ServerInterface
052: * class. The script also has implicit object access to any additional parameters defined
053: * for the request. The script should return the same forwarding values as any
054: * RequestHandler method.
055: *
056: * <p>The script file is located with:
057: * <pre>
058: * getServletContext().getResource(scriptFileName);
059: * </pre>
060: * <p>So the script file can be located anywhere including the WEB-INF directory.
061: *
062: * <p>The extension of the defined 'scriptFileName' defines the scripting language being used
063: * and that scripting language will automatically be loaded to execute the script.
064: *
065: * <p>However, if you need to you can define:
066: * <pre>
067: * <parameter name="scriptingLanguage" value="" />
068: * </pre>
069: * <p>To override the script language associated with the script extension.
070: * <p>You can also optionally define the BSF engine and its classpath, use:
071: * <pre>
072: * <parameter name="scriptingEngineClass" value="" />
073: * <parameter name="scriptingClasspath" value="" />
074: * </pre>
075: *
076: * <p>You can also define:
077: * <pre>
078: * <parameter name="noCache" value="true" />
079: * </pre>
080: * <p>To turn script caching off. This is very usefull during the development of the scripts.
081: */
082:
083: @SuppressWarnings("unchecked")
084: // working to complete a Java 1.5 version
085: public class Scripting extends RequestHandler {
086: private static Logger logger = Logger.getLogger(Scripting.class
087: .getName());
088: private static Hashtable scriptHash = new Hashtable();
089:
090: public String processRequest(ServerInterface serverInterface)
091: throws MalformedURLException, IOException,
092: ScriptingException {
093: String filename = null, language = null, classpath = null, engineClass = null, script = null;
094: Enumeration en = serverInterface.getRequestData()
095: .getParameterHash().keys();
096: Hashtable objectsHash = new Hashtable();
097: boolean noCache = false;
098:
099: objectsHash.put("serverInterface", serverInterface);
100:
101: while (en.hasMoreElements()) {
102: String name = (String) en.nextElement();
103:
104: if (name.equalsIgnoreCase("scriptFileName"))
105: filename = serverInterface.getRequestData()
106: .getParameter(name);
107: else if (name.equalsIgnoreCase("scriptingLanguage"))
108: language = serverInterface.getRequestData()
109: .getParameter(name);
110: else if (name.equalsIgnoreCase("scriptingClasspath"))
111: classpath = serverInterface.getRequestData()
112: .getParameter(name);
113: else if (name.equalsIgnoreCase("scriptingEngineClass"))
114: engineClass = serverInterface.getRequestData()
115: .getParameter(name);
116: else if (name.equalsIgnoreCase("noCache"))
117: noCache = true;
118: else
119: objectsHash.put(name, serverInterface.getRequestData()
120: .getParameter(name));
121: }
122:
123: if (filename == null) {
124: serverInterface.setErrorMessage(
125: ServerInterface.VALIDATION_ERROR,
126: "scriptFileName must be defined");
127:
128: logger.severe("scriptFileName must be defined");
129:
130: return VALIDATION_ERROR;
131: }
132:
133: URL scriptUrl = serverInterface.getServletContext()
134: .getResource(filename);
135:
136: if (scriptUrl == null) {
137: serverInterface.setErrorMessage(
138: ServerInterface.VALIDATION_ERROR,
139: "scriptFileName '" + filename
140: + "' - resource not found");
141:
142: logger.severe("scriptFileName '" + filename
143: + "' - resource not found");
144:
145: return VALIDATION_ERROR;
146: }
147:
148: if (language == null)
149: try {
150: language = ScriptingManager.getLanguage(filename);
151: } catch (Exception e) {
152: }
153:
154: if (noCache)
155: script = loadFile(scriptUrl).toString();
156: else if ((script = (String) scriptHash.get(filename)) == null)
157: scriptHash.put(filename, script = loadFile(scriptUrl)
158: .toString());
159:
160: String returnValue = null;
161:
162: if (language != null && classpath != null
163: && engineClass != null)
164: returnValue = (String) ScriptingManager.executeScript(
165: language, classpath, engineClass, script,
166: objectsHash);
167: else if (language != null)
168: returnValue = (String) ScriptingManager.executeScript(
169: language, script, objectsHash);
170: else
171: returnValue = (String) ScriptingManager.executeScript(
172: script, objectsHash);
173:
174: if (returnValue != null)
175: return returnValue;
176:
177: return NO_FORWARDING;
178: }
179:
180: static StringBuffer loadFile(URL filename) throws IOException {
181: StringBuffer stringBuffer = new StringBuffer();
182:
183: BufferedReader rd = new BufferedReader(new InputStreamReader(
184: filename.openStream()));
185: String str;
186:
187: while ((str = rd.readLine()) != null)
188: stringBuffer.append(str + "\n");
189:
190: rd.close();
191:
192: return stringBuffer;
193: }
194: }
|