Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 58 → Rev 59

/pluggablejs/trunk/src/net/outlyer/plugins/SandboxImpl.java
26,8 → 26,6
 
// $Id$
 
import net.outlyer.plugins.Sandbox;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
40,8 → 38,6
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import net.outlyer.plugins.BasePluginObject;
import net.outlyer.plugins.PluginProperties;
 
/**
* Implementation of the Sandbox interface
110,7 → 106,9
return new ScriptEngineManager().getEngineByName("rhino");
}
private ScriptEngine execute(boolean appendText, String text, final Map<String,Object> extraBindings)
private ScriptEngine execute(final String prependText,
final String appendText,
final Map<String,Object> extraBindings)
throws PluginExecutionException {
final ScriptEngine rhino = getEngine();
153,10 → 151,14
// Create the $net.outlyer.runtime pseudo-namespace...
rhino.eval(runtime.toString());
 
if (null != prependText) {
rhino.eval(prependText);
}
rhino.eval(new PluginReader(pluginUri));
if (appendText) {
rhino.eval(text);
if (null != appendText) {
rhino.eval(appendText);
}
// Execute any end hooks
175,31 → 177,36
}
public void execute() throws PluginExecutionException {
execute(false, null, null);
execute(null, null, null);
}
 
public <T> T createDelayedImplementation(final Class<T> c,
final String objectName)
throws PluginExecutionException {
System.err.println("createDelayedImple");
final String varImpl = uniqueVarName();
 
final StringBuilder code = new StringBuilder();
final StringBuilder preCode = new StringBuilder();
 
code.append("var ").append(objectName).append(" = {\n");
preCode.append("var ").append(objectName).append(" = {\n");
for (final Method method : c.getMethods()) {
if (method.getDeclaringClass() != c) {
continue;
}
code.append(method.getName()).append(": null,");
preCode.append(method.getName()).append(": null,\n");
}
code.append("};\n");
preCode.append("};\n");
 
code.append(varImpl).append(" = new ")
// Could be safely instantiated before actually being assigned IF
// plugins were required to define their methods with
// objectName.methodName = function() {} ...
// but would file with
// objectName = { methodName: function() {}, ... }
// Creating the instance as postCode does the job for both
final StringBuilder postCode = new StringBuilder();
postCode.append(varImpl).append(" = new ")
.append(c.getCanonicalName()).append("(").append(objectName).append(");");
System.err.println(code.toString());
 
final ScriptEngine rhino = execute(true, code.toString(), null);
final ScriptEngine rhino = execute(preCode.toString(), postCode.toString(), null);
 
assert (c.isInstance(rhino.get(varImpl)));
return (T) rhino.get(varImpl);
284,7 → 291,7
}
//System.err.println(script.toString());
 
final ScriptEngine rhino = execute(true, script.toString(), bn);
final ScriptEngine rhino = execute(null, script.toString(), bn);
assert (c.isInstance(rhino.get(varImpl)));
return (T) rhino.get(varImpl);
}
304,7 → 311,8
*/
public String getPluginName() {
if (null == properties.name) {
return new File(loadedFrom().getPath()).getName(); // FIXME: <= Is this safe?
final String[] elements = loadedFrom().toString().split("/");
return elements[elements.length-1];
}
return properties.name;
}