org.kualigan.tools.ant.tasks.RhinoShell.txt Maven / Gradle / Ivy
/*
* Copyright 2005-2007 The Kuali Foundation
*
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ecl2.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kualigan.tools.ant.tasks;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.kuali.kfs.sys.context.Log4jConfigurer;
import org.kuali.kfs.sys.context.SpringContext;
import org.mozilla.javascript.*;
/**
*
* @author Leo Przybylski ([email protected])
*/
public class RhinoShell extends Task {
private String javascript;
private Path classpath;
public RhinoShell() {
javascript = new String();
}
public Path createClasspath() {
if (this.classpath == null) {
setClasspath(new Path(getProject()).createPath());
}
return this.classpath;
}
public void setClasspathRef(Reference r) {
log("Setting classpath reference");
createClasspath().setRefid(r);
}
public void setClasspath(Path classpath) {
log("Setting new classpath");
this.classpath = classpath;
}
protected ClassLoader setupClassLoader() {
final String[] pathElems = classpath.list();
final Collection taskClassPath = new ArrayList();
ClassLoader loader = null;
try {
for (String path : pathElems) {
log("Adding " + path + " to classpath");
URL url = new File(path).toURL();
taskClassPath.add(url);
}
URLClassLoader urlLoader = AccessController.doPrivileged(new PrivilegedAction() {
public URLClassLoader run() {
return new URLClassLoader(taskClassPath.toArray(new URL[taskClassPath.size()]), Context.class.getClassLoader());
}
});
loader = urlLoader;
}
catch (Exception e) {
throw new BuildException("Couldn't create classloader from classpath ", e);
}
return loader;
}
protected Context enterContext(ClassLoader jsLoader) {
try {
final Class contextClass = Class.forName("org.mozilla.javascript.Context", false, jsLoader);
return (Context) contextClass.getMethod("enter").invoke(null);
}
catch (Exception e) {
throw new BuildException(e);
}
}
protected void exitContext(ClassLoader jsLoader) {
try {
final Class contextClass = Class.forName("org.mozilla.javascript.Context", true, jsLoader);
contextClass.getMethod("exit").invoke(null);
}
catch (Exception e) {
throw new BuildException(e);
}
}
public void execute() {
final ClassLoader loader = setupClassLoader();
final Context cx = enterContext(loader);
// cx.setApplicationClassLoader(loader);
// log("Custom classloader is " + loader);
try {
// Scriptable scope = cx.initStandardObjects();
final Scriptable scope = new ImporterTopLevel(cx);
// scope.put("loader", scope, loader);
log("executing " + javascript);
Log4jConfigurer.configureLogging(true);
DummyContext.setup();
cx.evaluateString(scope, javascript, "", 1, null);
}
catch(Exception e) {
throw new BuildException(e);
}
finally {
exitContext(loader);
}
}
public void addText(String script) {
javascript += script;
}
private static class DummyContext extends SpringContext {
public static final void setup() {
Log4jConfigurer.configureLogging(true);
SpringContext.initializeApplicationContext();
}
}
}