Search in sources :

Example 1 with NameSpace

use of bsh.NameSpace in project qi4j-sdk by Qi4j.

the class BeanShellMixin method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Interpreter runtime;
    synchronized (BeanShellMixin.class) {
        runtime = runtimes.get(module);
    }
    if (runtime == null) {
        runtime = new Interpreter();
        BshClassManager.createClassManager(runtime);
        Class compositeType = me.getClass().getInterfaces()[0];
        NameSpace namespace = buildNamespace(compositeType, runtime);
        runtime.setNameSpace(namespace);
        synchronized (BeanShellMixin.class) {
            runtimes.put(module, runtime);
        }
        runtime.set("compositeBuilderFactory", compositeBuilderFactory);
        runtime.set("unitOfWorkFactory", uowFactory);
    }
    if (mixins == null) {
        mixins = extractMixins(runtime.getClassManager());
    }
    Object instance = mixins.get(method.getDeclaringClass());
    return method.invoke(instance, args);
}
Also used : Interpreter(bsh.Interpreter) NameSpace(bsh.NameSpace)

Example 2 with NameSpace

use of bsh.NameSpace in project qi4j-sdk by Qi4j.

the class BeanShellMixin method buildNamespace.

private static NameSpace buildNamespace(Class compositeType, Interpreter runtime) throws IOException {
    ClassLoader loader = compositeType.getClassLoader();
    BshClassManager classManager = BshClassManager.createClassManager(runtime);
    classManager.setClassLoader(loader);
    NameSpace namespace = new NameSpace(classManager, compositeType.getName());
    URL scriptUrl = getFunctionResource(compositeType);
    if (scriptUrl == null) {
        return null;
    }
    Reader source = getSource(compositeType, scriptUrl);
    try {
        runtime.eval(source, namespace, scriptUrl.toString());
    } catch (EvalError evalError) {
        //TODO: Auto-generated, need attention.
        evalError.printStackTrace();
    }
    return namespace;
}
Also used : NameSpace(bsh.NameSpace) EvalError(bsh.EvalError) BshClassManager(bsh.BshClassManager) URL(java.net.URL)

Aggregations

NameSpace (bsh.NameSpace)2 BshClassManager (bsh.BshClassManager)1 EvalError (bsh.EvalError)1 Interpreter (bsh.Interpreter)1 URL (java.net.URL)1