Search in sources :

Example 56 with Scriptable

use of org.mozilla.javascript.Scriptable in project cxf by apache.

the class ProviderFactory method publishImpl.

private void publishImpl(File f, String epAddr, boolean isBase) throws Exception {
    if (!f.exists()) {
        throw new Exception(f.getPath() + NO_SUCH_FILE);
    }
    boolean isE4X = f.getName().endsWith(".jsx");
    StringBuilder sb = new StringBuilder();
    try (BufferedReader bufrd = new BufferedReader(new FileReader(f))) {
        String line = null;
        for (; ; ) {
            line = bufrd.readLine();
            if (line == null) {
                break;
            }
            sb.append(line).append("\n");
        }
    }
    String scriptStr = sb.toString();
    Context cx = ContextFactory.getGlobal().enterContext();
    boolean providerFound = false;
    try {
        Scriptable scriptScope = cx.initStandardObjects(null, true);
        Object[] ids = compileScript(cx, scriptStr, scriptScope, f);
        if (ids.length > 0) {
            Service.Mode mode = Service.Mode.PAYLOAD;
            for (Object idObj : ids) {
                if (!(idObj instanceof String)) {
                    continue;
                }
                String id = (String) idObj;
                if (!id.startsWith("WebServiceProvider")) {
                    continue;
                }
                Object obj = scriptScope.get(id, scriptScope);
                if (!(obj instanceof Scriptable)) {
                    continue;
                }
                Scriptable wspVar = (Scriptable) obj;
                providerFound = true;
                obj = wspVar.get("ServiceMode", wspVar);
                if (obj != Scriptable.NOT_FOUND) {
                    if (obj instanceof String) {
                        String value = (String) obj;
                        if ("PAYLOAD".equalsIgnoreCase(value)) {
                            mode = Service.Mode.PAYLOAD;
                        } else if ("MESSAGE".equalsIgnoreCase(value)) {
                            mode = Service.Mode.MESSAGE;
                        } else {
                            throw new Exception(f.getPath() + ILLEGAL_SVCMD_MODE + value);
                        }
                    } else {
                        throw new Exception(f.getPath() + ILLEGAL_SVCMD_TYPE);
                    }
                }
                AbstractDOMProvider provider = createProvider(mode, scriptScope, wspVar, epAddr, isBase, isE4X);
                try {
                    provider.publish();
                    providers.add(provider);
                } catch (AbstractDOMProvider.JSDOMProviderException ex) {
                    StringBuilder msg = new StringBuilder(f.getPath());
                    msg.append(": ").append(ex.getMessage());
                    throw new Exception(msg.toString());
                }
            }
        }
    } finally {
        Context.exit();
    }
    if (!providerFound) {
        throw new Exception(f.getPath() + NO_PROVIDER);
    }
}
Also used : Context(org.mozilla.javascript.Context) Service(javax.xml.ws.Service) Scriptable(org.mozilla.javascript.Scriptable) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 57 with Scriptable

use of org.mozilla.javascript.Scriptable in project cxf by apache.

the class RPCClientTest method testBean2ToJS.

public static Object testBean2ToJS(JavascriptTestUtilities testUtilities, Context context, TestBean2 beanTwoItem) {
    if (beanTwoItem == null) {
        return null;
    }
    Scriptable rv = context.newObject(testUtilities.getRhinoScope(), "org_apache_cxf_javascript_testns3_testBean2");
    testUtilities.rhinoCallMethod(rv, "setStringItem", beanTwoItem.stringItem);
    return rv;
}
Also used : Scriptable(org.mozilla.javascript.Scriptable)

Example 58 with Scriptable

use of org.mozilla.javascript.Scriptable in project cxf by apache.

the class RPCClientTest method testBean1ToJS.

public static Scriptable testBean1ToJS(JavascriptTestUtilities testUtilities, Context context, TestBean1 b1) {
    if (b1 == null) {
        // black is always in fashion. (Really, we can be called with a null).
        return null;
    }
    Scriptable rv = context.newObject(testUtilities.getRhinoScope(), "org_apache_cxf_javascript_testns_testBean1");
    testUtilities.rhinoCallMethod(rv, "setStringItem", testUtilities.javaToJS(b1.stringItem));
    testUtilities.rhinoCallMethod(rv, "setIntItem", testUtilities.javaToJS(b1.intItem));
    testUtilities.rhinoCallMethod(rv, "setLongItem", testUtilities.javaToJS(b1.longItem));
    testUtilities.rhinoCallMethod(rv, "setBase64Item", testUtilities.javaToJS(b1.base64Item));
    testUtilities.rhinoCallMethod(rv, "setOptionalIntItem", testUtilities.javaToJS(b1.optionalIntItem));
    testUtilities.rhinoCallMethod(rv, "setOptionalIntArrayItem", testUtilities.javaToJS(b1.optionalIntArrayItem));
    testUtilities.rhinoCallMethod(rv, "setDoubleItem", testUtilities.javaToJS(b1.doubleItem));
    testUtilities.rhinoCallMethod(rv, "setBeanTwoItem", testBean2ToJS(testUtilities, context, b1.beanTwoItem));
    testUtilities.rhinoCallMethod(rv, "setBeanTwoNotRequiredItem", testBean2ToJS(testUtilities, context, b1.beanTwoNotRequiredItem));
    return rv;
}
Also used : Scriptable(org.mozilla.javascript.Scriptable)

Example 59 with Scriptable

use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.

the class Require method getExportedModuleInterface.

private Scriptable getExportedModuleInterface(Context cx, String id, URI uri, boolean isMain) {
    // Check if the requested module is already completely loaded
    Scriptable exports = exportedModuleInterfaces.get(id);
    if (exports != null) {
        if (isMain) {
            throw new IllegalStateException("Attempt to set main module after it was loaded");
        }
        return exports;
    }
    // Check if it is currently being loaded on the current thread 
    // (supporting circular dependencies).
    Map<String, Scriptable> threadLoadingModules = loadingModuleInterfaces.get();
    if (threadLoadingModules != null) {
        exports = threadLoadingModules.get(id);
        if (exports != null) {
            return exports;
        }
    }
    // with locks.
    synchronized (loadLock) {
        // Recheck if it is already loaded - other thread might've 
        // completed loading it just as we entered the synchronized block.
        exports = exportedModuleInterfaces.get(id);
        if (exports != null) {
            return exports;
        }
        // Nope, still not loaded; we're loading it then.
        final ModuleScript moduleScript = getModule(cx, id, uri);
        if (sandboxed && !moduleScript.isSandboxed()) {
            throw ScriptRuntime.throwError(cx, nativeScope, "Module \"" + id + "\" is not contained in sandbox.");
        }
        exports = cx.newObject(nativeScope);
        // Are we the outermost locked invocation on this thread?
        final boolean outermostLocked = threadLoadingModules == null;
        if (outermostLocked) {
            threadLoadingModules = new HashMap<String, Scriptable>();
            loadingModuleInterfaces.set(threadLoadingModules);
        }
        // Must make the module exports available immediately on the 
        // current thread, to satisfy the CommonJS Modules/1.1 requirement 
        // that "If there is a dependency cycle, the foreign module may not
        // have finished executing at the time it is required by one of its
        // transitive dependencies; in this case, the object returned by 
        // "require" must contain at least the exports that the foreign 
        // module has prepared before the call to require that led to the 
        // current module's execution."
        threadLoadingModules.put(id, exports);
        try {
            // Support non-standard Node.js feature to allow modules to
            // replace the exports object by setting module.exports.
            Scriptable newExports = executeModuleScript(cx, id, exports, moduleScript, isMain);
            if (exports != newExports) {
                threadLoadingModules.put(id, newExports);
                exports = newExports;
            }
        } catch (RuntimeException e) {
            // Throw loaded module away if there was an exception
            threadLoadingModules.remove(id);
            throw e;
        } finally {
            if (outermostLocked) {
                // Make loaded modules visible to other threads only after 
                // the topmost triggering load has completed. This strategy
                // (compared to the one where we'd make each module 
                // globally available as soon as it loads) prevents other
                // threads from observing a partially loaded circular
                // dependency of a module that completed loading.
                exportedModuleInterfaces.putAll(threadLoadingModules);
                loadingModuleInterfaces.set(null);
            }
        }
    }
    return exports;
}
Also used : Scriptable(org.mozilla.javascript.Scriptable)

Example 60 with Scriptable

use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.

the class Require method executeModuleScript.

private Scriptable executeModuleScript(Context cx, String id, Scriptable exports, ModuleScript moduleScript, boolean isMain) {
    final ScriptableObject moduleObject = (ScriptableObject) cx.newObject(nativeScope);
    URI uri = moduleScript.getUri();
    URI base = moduleScript.getBase();
    defineReadOnlyProperty(moduleObject, "id", id);
    if (!sandboxed) {
        defineReadOnlyProperty(moduleObject, "uri", uri.toString());
    }
    final Scriptable executionScope = new ModuleScope(nativeScope, uri, base);
    // Set this so it can access the global JS environment objects. 
    // This means we're currently using the "MGN" approach (ModuleScript 
    // with Global Natives) as specified here: 
    // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension>
    executionScope.put("exports", executionScope, exports);
    executionScope.put("module", executionScope, moduleObject);
    moduleObject.put("exports", moduleObject, exports);
    install(executionScope);
    if (isMain) {
        defineReadOnlyProperty(this, "main", moduleObject);
    }
    executeOptionalScript(preExec, cx, executionScope);
    moduleScript.getScript().exec(cx, executionScope);
    executeOptionalScript(postExec, cx, executionScope);
    return ScriptRuntime.toObject(nativeScope, ScriptableObject.getProperty(moduleObject, "exports"));
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) URI(java.net.URI)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)106 Context (org.mozilla.javascript.Context)49 ScriptableObject (org.mozilla.javascript.ScriptableObject)30 ContextAction (org.mozilla.javascript.ContextAction)12 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 NativeObject (org.mozilla.javascript.NativeObject)7 Function (org.mozilla.javascript.Function)6 Map (java.util.Map)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 RhinoException (org.mozilla.javascript.RhinoException)4 Wrapper (org.mozilla.javascript.Wrapper)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 ScriptContext (javax.script.ScriptContext)3 Callable (org.mozilla.javascript.Callable)3 ContextFactory (org.mozilla.javascript.ContextFactory)3 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)3