26,6 → 26,8 |
|
// $Id$ |
|
import net.outlyer.plugins.Sandbox; |
import java.io.File; |
import java.io.IOException; |
import java.lang.reflect.Method; |
import java.net.URI; |
38,6 → 40,8 |
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 |
106,9 → 110,7 |
return new ScriptEngineManager().getEngineByName("rhino"); |
} |
|
private ScriptEngine execute(final String prependText, |
final String appendText, |
final Map<String,Object> extraBindings) |
private ScriptEngine execute(boolean appendText, String text, final Map<String,Object> extraBindings) |
throws PluginExecutionException { |
final ScriptEngine rhino = getEngine(); |
|
151,14 → 153,10 |
// Create the $net.outlyer.runtime pseudo-namespace... |
rhino.eval(runtime.toString()); |
|
if (null != prependText) { |
rhino.eval(prependText); |
} |
|
rhino.eval(new PluginReader(pluginUri)); |
|
if (null != appendText) { |
rhino.eval(appendText); |
if (appendText) { |
rhino.eval(text); |
} |
|
// Execute any end hooks |
177,36 → 175,31 |
} |
|
public void execute() throws PluginExecutionException { |
execute(null, null, null); |
execute(false, 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 preCode = new StringBuilder(); |
final StringBuilder code = new StringBuilder(); |
|
preCode.append("var ").append(objectName).append(" = {\n"); |
code.append("var ").append(objectName).append(" = {\n"); |
for (final Method method : c.getMethods()) { |
if (method.getDeclaringClass() != c) { |
continue; |
} |
preCode.append(method.getName()).append(": null,\n"); |
code.append(method.getName()).append(": null,"); |
} |
preCode.append("};\n"); |
code.append("};\n"); |
|
// 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 ") |
code.append(varImpl).append(" = new ") |
.append(c.getCanonicalName()).append("(").append(objectName).append(");"); |
System.err.println(code.toString()); |
|
final ScriptEngine rhino = execute(preCode.toString(), postCode.toString(), null); |
final ScriptEngine rhino = execute(true, code.toString(), null); |
|
assert (c.isInstance(rhino.get(varImpl))); |
return (T) rhino.get(varImpl); |
291,7 → 284,7 |
} |
//System.err.println(script.toString()); |
|
final ScriptEngine rhino = execute(null, script.toString(), bn); |
final ScriptEngine rhino = execute(true, script.toString(), bn); |
assert (c.isInstance(rhino.get(varImpl))); |
return (T) rhino.get(varImpl); |
} |
311,8 → 304,7 |
*/ |
public String getPluginName() { |
if (null == properties.name) { |
final String[] elements = loadedFrom().toString().split("/"); |
return elements[elements.length-1]; |
return new File(loadedFrom().getPath()).getName(); // FIXME: <= Is this safe? |
} |
return properties.name; |
} |