001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jWebTk.
006: *
007: * jWebTk 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: * jWebTk 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 jWebTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jwebtk.scripting;
020:
021: import java.util.Vector;
022: import java.util.StringTokenizer;
023: import java.io.File;
024: import java.net.URL;
025: import java.net.URLClassLoader;
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030: import org.apache.bsf.BSFManager;
031:
032: /**
033: * This class wraps the Apache Bean Scripting Framework (BSF).
034: *
035: * The BSF jar file(s) and the scripting jar file(s) for a given language are required when using this class.
036: */
037:
038: @SuppressWarnings("unchecked")
039: // working to complete a Java 1.5 version
040: public class ScriptingManager {
041: private Logger logger = Logger.getLogger(ScriptingManager.class
042: .getName());
043: private BSFManager manager;
044:
045: protected ScriptingManager() {
046: manager = new BSFManager();
047: }
048:
049: protected ScriptingManager(String language, String classpath,
050: String engineClass) throws ScriptingException {
051: if (logger.isLoggable(Level.FINE))
052: logger.fine("language=" + language + ", class_path="
053: + classpath + ", engine_class=" + engineClass);
054:
055: BSFManager.registerScriptingEngine(language, engineClass, null);
056:
057: manager = new BSFManager();
058:
059: if (classpath != null) {
060: Vector urls = new Vector();
061: StringTokenizer strtok = new StringTokenizer(classpath,
062: ";:");
063:
064: while (strtok.hasMoreTokens()) {
065: String path = strtok.nextToken();
066:
067: try {
068: if (path.indexOf(':') == -1)
069: urls.addElement(new File(path).toURI().toURL());
070: else
071: urls.addElement(new URL(path));
072: } catch (Exception e) {
073: throw new ScriptingException(e);
074: }
075: }
076:
077: URL url_arr[] = (URL[]) urls.toArray(new URL[urls.size()]);
078:
079: manager.setClassLoader(new URLClassLoader(url_arr));
080: }
081: }
082:
083: protected void declareObject(String name, Object obj)
084: throws ScriptingException {
085: try {
086: manager.declareBean(name, obj, obj.getClass());
087: } catch (Exception e) {
088: throw new ScriptingException(e);
089: }
090: }
091:
092: protected void undeclareObject(String name)
093: throws ScriptingException {
094: try {
095: manager.undeclareBean(name);
096: } catch (Exception e) {
097: throw new ScriptingException(e);
098: }
099: }
100:
101: protected Object execute(String language, String script)
102: throws ScriptingException {
103: try {
104: return manager.eval(language, null, 0, 0, script);
105: } catch (Exception e) {
106: throw new ScriptingException(e);
107: }
108: }
109:
110: /**
111: * Returns the scripting language name associated with extension.
112: *
113: * @param extension file extension for a given scripting language
114: *
115: * @return the scripting language name associated with extension.
116: */
117:
118: public static String getLanguage(String extension)
119: throws ScriptingException {
120: try {
121: return BSFManager.getLangFromFilename(extension);
122: } catch (Exception e) {
123: throw new ScriptingException(e);
124: }
125: }
126:
127: /**
128: * Executes a JavaScript script with the given implicit objects.
129: *
130: * @param script the script to execute
131: * @param objectsToDeclare implicit objects to declare
132: *
133: * @return the script result
134: */
135:
136: public static Object executeScript(String script,
137: Hashtable objectsToDeclare) throws ScriptingException {
138: try {
139: return executeScript("javascript", script, objectsToDeclare);
140: } catch (Exception e) {
141: throw new ScriptingException(e);
142: }
143: }
144:
145: /**
146: * Executes a JavaScript script with the given implicit objects.
147: *
148: * @param language the language of the script
149: * @param script the script to execute
150: * @param objectsToDeclare implicit objects to declare
151: *
152: * @return the script result
153: */
154:
155: public static Object executeScript(String language, String script,
156: Hashtable objectsToDeclare) throws ScriptingException {
157: try {
158: ScriptingManager scriptor = new ScriptingManager();
159:
160: Enumeration e = objectsToDeclare.keys();
161: String key;
162:
163: while (e.hasMoreElements()) {
164: key = (String) e.nextElement();
165: scriptor.declareObject(key, objectsToDeclare.get(key));
166: }
167:
168: return scriptor.execute(language, script);
169: } catch (Exception e) {
170: throw new ScriptingException(e);
171: }
172: }
173:
174: /**
175: * Executes a JavaScript script with the given implicit objects.
176: *
177: * @param language the language of the script
178: * @param classpath the classpath for the scripting language (jar/class) environment
179: * @param engineClass the class for the scripting language
180: * @param script the script to execute
181: * @param objectsToDeclare implicit objects to declare
182: *
183: * @return the script result
184: */
185:
186: public static Object executeScript(String language,
187: String classpath, String engineClass, String script,
188: Hashtable objectsToDeclare) throws ScriptingException {
189: try {
190: ScriptingManager scriptor = new ScriptingManager(language,
191: classpath, engineClass);
192:
193: Enumeration e = objectsToDeclare.keys();
194: String key;
195:
196: while (e.hasMoreElements()) {
197: key = (String) e.nextElement();
198: scriptor.declareObject(key, objectsToDeclare.get(key));
199: }
200:
201: return scriptor.execute(language, script);
202: } catch (Exception e) {
203: throw new ScriptingException(e);
204: }
205: }
206: }
|