/**
* Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
* All Rights Reserved.
*
* This file is part of jWebTk.
*
* jWebTk is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License (Version 2) as published by
* the Free Software Foundation.
*
* jWebTk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with jWebTk; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jwebtk.scripting;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.bsf.BSFManager;
/**
* This class wraps the Apache Bean Scripting Framework (BSF).
*
* The BSF jar file(s) and the scripting jar file(s) for a given language are required when using this class.
*/
@SuppressWarnings("unchecked") // working to complete a Java 1.5 version
public class ScriptingManager
{
private Logger logger = Logger.getLogger(ScriptingManager.class.getName());
private BSFManager manager;
protected ScriptingManager()
{
manager = new BSFManager();
}
protected ScriptingManager(String language, String classpath, String engineClass) throws ScriptingException
{
if (logger.isLoggable(Level.FINE))
logger.fine("language=" + language + ", class_path=" + classpath + ", engine_class=" + engineClass);
BSFManager.registerScriptingEngine(language,engineClass,null);
manager = new BSFManager();
if (classpath != null)
{
Vector urls = new Vector();
StringTokenizer strtok = new StringTokenizer(classpath,";:");
while (strtok.hasMoreTokens())
{
String path = strtok.nextToken();
try
{
if (path.indexOf(':') == -1)
urls.addElement(new File(path).toURI().toURL());
else
urls.addElement(new URL(path));
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
URL url_arr[] = (URL[])urls.toArray(new URL[urls.size()]);
manager.setClassLoader(new URLClassLoader(url_arr));
}
}
protected void declareObject(String name, Object obj) throws ScriptingException
{
try
{
manager.declareBean(name,obj,obj.getClass());
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
protected void undeclareObject(String name) throws ScriptingException
{
try
{
manager.undeclareBean(name);
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
protected Object execute(String language, String script) throws ScriptingException
{
try
{
return manager.eval(language,null,0,0,script);
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
/**
* Returns the scripting language name associated with extension.
*
* @param extension file extension for a given scripting language
*
* @return the scripting language name associated with extension.
*/
public static String getLanguage(String extension) throws ScriptingException
{
try
{
return BSFManager.getLangFromFilename(extension);
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
/**
* Executes a JavaScript script with the given implicit objects.
*
* @param script the script to execute
* @param objectsToDeclare implicit objects to declare
*
* @return the script result
*/
public static Object executeScript(String script, Hashtable objectsToDeclare) throws ScriptingException
{
try
{
return executeScript("javascript", script, objectsToDeclare);
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
/**
* Executes a JavaScript script with the given implicit objects.
*
* @param language the language of the script
* @param script the script to execute
* @param objectsToDeclare implicit objects to declare
*
* @return the script result
*/
public static Object executeScript(String language, String script, Hashtable objectsToDeclare) throws ScriptingException
{
try
{
ScriptingManager scriptor = new ScriptingManager();
Enumeration e = objectsToDeclare.keys();
String key;
while (e.hasMoreElements())
{
key = (String)e.nextElement();
scriptor.declareObject(key,objectsToDeclare.get(key));
}
return scriptor.execute(language,script);
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
/**
* Executes a JavaScript script with the given implicit objects.
*
* @param language the language of the script
* @param classpath the classpath for the scripting language (jar/class) environment
* @param engineClass the class for the scripting language
* @param script the script to execute
* @param objectsToDeclare implicit objects to declare
*
* @return the script result
*/
public static Object executeScript(String language, String classpath, String engineClass, String script, Hashtable objectsToDeclare) throws ScriptingException
{
try
{
ScriptingManager scriptor = new ScriptingManager(language, classpath, engineClass);
Enumeration e = objectsToDeclare.keys();
String key;
while (e.hasMoreElements())
{
key = (String)e.nextElement();
scriptor.declareObject(key,objectsToDeclare.get(key));
}
return scriptor.execute(language,script);
}
catch (Exception e)
{
throw new ScriptingException(e);
}
}
}
|