Search in sources :

Example 11 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RhinoTopLevel method setInterval.

public static String setInterval(Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "setTimeout";
    int argsCount = args.length;
    if (argsCount < 2) {
        HostObjectUtil.invalidNumberOfArgs(EngineConstants.GLOBAL_OBJECT_NAME, functionName, argsCount, false);
    }
    Function function = null;
    long interval;
    if (args[0] instanceof Function) {
        function = (Function) args[0];
    } else if (args[0] instanceof String) {
        function = getFunction(cx, thisObj, (String) args[0], functionName);
    } else {
        HostObjectUtil.invalidArgsError(EngineConstants.GLOBAL_OBJECT_NAME, EngineConstants.GLOBAL_OBJECT_NAME, "1", "string|function", args[0], false);
    }
    if (!(args[1] instanceof Number)) {
        HostObjectUtil.invalidArgsError(EngineConstants.GLOBAL_OBJECT_NAME, EngineConstants.GLOBAL_OBJECT_NAME, "2", "number", args[1], false);
    }
    if (function == null) {
        String error = "Callback cannot be null in " + functionName;
        log.error(error);
        throw new ScriptException(error);
    }
    final JaggeryContext context = getJaggeryContext();
    final Object[] params = Arrays.copyOfRange(args, 2, args.length);
    final Function callback = function;
    final ContextFactory factory = cx.getFactory();
    interval = ((Number) args[1]).longValue();
    String uuid = UUID.randomUUID().toString();
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    final int tenantId = carbonContext.getTenantId();
    final String tenantDomain = carbonContext.getTenantDomain();
    final String applicationName = carbonContext.getApplicationName();
    final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    ScheduledFuture future = timerExecutor.scheduleAtFixedRate(new Runnable() {

        private boolean firstTime = true;

        @Override
        public void run() {
            //set the context classloader
            Thread currentThread = Thread.currentThread();
            ClassLoader originalClassLoader = currentThread.getContextClassLoader();
            Thread.currentThread().setContextClassLoader(contextClassLoader);
            // child inherits context properties form the parent thread.
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            carbonContext.setTenantId(tenantId);
            carbonContext.setTenantDomain(tenantDomain);
            carbonContext.setApplicationName(applicationName);
            try {
                Context cx = RhinoEngine.enterContext(factory);
                RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, context);
                callback.call(cx, thisObj, thisObj, params);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                PrivilegedCarbonContext.endTenantFlow();
                RhinoEngine.exitContext();
                currentThread.setContextClassLoader(originalClassLoader);
            }
        }
    }, interval, interval, TimeUnit.MILLISECONDS);
    Map<String, ScheduledFuture> tasks = intervals.get(context.getTenantDomain());
    if (tasks == null) {
        tasks = new HashMap<String, ScheduledFuture>();
        intervals.put(context.getTenantDomain(), tasks);
    }
    tasks.put(uuid, future);
    return uuid;
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 12 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RhinoURISecurityDomain method getCodeSource.

public CodeSource getCodeSource() throws ScriptException {
    if (codeSource != null) {
        return codeSource;
    }
    try {
        URL url = new URI(scriptURI).toURL();
        codeSource = new CodeSource(url, (Certificate[]) null);
        return codeSource;
    } catch (MalformedURLException e) {
        throw new ScriptException(e);
    } catch (URISyntaxException e) {
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URI(java.net.URI) URL(java.net.URL)

Example 13 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class WebAppSessionListener method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();
    List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS);
    if (jsListeners == null) {
        return;
    }
    JaggeryContext shared = WebAppManager.sharedJaggeryContext(ctx);
    Context cx = shared.getEngine().enterContext();
    JaggeryContext context = CommonManager.getJaggeryContext();
    if (CommonManager.getJaggeryContext() == null) {
        context = WebAppManager.clonedJaggeryContext(ctx);
        CommonManager.setJaggeryContext(context);
    }
    RhinoEngine engine = context.getEngine();
    ScriptableObject clonedScope = context.getScope();
    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(cx.newObject(clonedScope, "Session", new Object[] { httpSessionEvent.getSession() }));
    session.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(clonedScope, session);
    for (Object jsListener : jsListeners) {
        CommonManager.getCallstack(context).push((String) jsListener);
        try {
            ScriptReader sr = new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {

                @Override
                protected void build() throws IOException {
                    try {
                        sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                    } catch (ScriptException e) {
                        throw new IOException(e);
                    }
                }
            };
            engine.exec(sr, clonedScope, null);
        } catch (ScriptException e) {
            log.error(e.getMessage(), e);
        } finally {
            CommonManager.getCallstack(context).pop();
        }
    }
    Context.exit();
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) Context(org.mozilla.javascript.Context) ServletContext(javax.servlet.ServletContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) IOException(java.io.IOException) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) List(java.util.List) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 14 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class ResourceHostObject method jsGet_content.

public Object jsGet_content() throws ScriptException {
    try {
        Object result = this.resource.getContent();
        String mediaType = this.resource.getMediaType();
        if (result instanceof byte[]) {
            //if mediaType is xml related one, we return an e4x xml object
            if (mediaType != null) {
                if (mediaType.matches(".*[\\/].*[xX][mM][lL].*")) {
                    return context.newObject(this, "XML", new Object[] { new String((byte[]) result) });
                }
            }
            return new String((byte[]) result);
        } else if (result instanceof String[]) {
            String[] content = (String[]) result;
            return context.newArray(this, Arrays.copyOf(content, content.length, Object[].class));
        } else {
            return Context.toObject(result, this);
        }
    } catch (RegistryException e) {
        throw new ScriptException("Registry Exception while reading content property", e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) XMLObject(org.mozilla.javascript.xml.XMLObject) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 15 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class URIMatcherHostObject method jsFunction_match.

/**
     * Match function that takes the URI template as an argument
     *
     * @param cx
     * @param thisObj
     * @param args
     * @param funObj
     * @return
     * @throws ScriptException
     */
public static ScriptableObject jsFunction_match(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "match";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs("RhinoTopLevel", functionName, argsCount, false);
    }
    String template = (String) args[0];
    URIMatcherHostObject uriho = (URIMatcherHostObject) thisObj;
    Map<String, String> urlParts = new HashMap<String, String>();
    try {
        URITemplate uriTemplate = new URITemplate(template);
        boolean uriMatch = uriTemplate.matches(uriho.uriToBeMatched, urlParts);
        if (!uriMatch) {
            return null;
        }
    } catch (URITemplateException e) {
        throw new ScriptException(e);
    }
    ScriptableObject nobj = (ScriptableObject) cx.newObject(thisObj);
    for (Map.Entry<String, String> entry : urlParts.entrySet()) {
        nobj.put(entry.getKey(), nobj, entry.getValue());
    }
    uriho.uriParts = nobj;
    return nobj;
}
Also used : URITemplateException(org.wso2.uri.template.URITemplateException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) HashMap(java.util.HashMap) URITemplate(org.wso2.uri.template.URITemplate) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)83 IOException (java.io.IOException)15 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)9 URL (java.net.URL)8 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)8 MalformedURLException (java.net.MalformedURLException)7 ServletContext (javax.servlet.ServletContext)6 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)6 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 Context (org.mozilla.javascript.Context)5 File (java.io.File)4 StringReader (java.io.StringReader)4 Callable (java.util.concurrent.Callable)4 ExecutorService (java.util.concurrent.ExecutorService)4 FileItem (org.apache.commons.fileupload.FileItem)4 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)4 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)4