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;
}
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);
}
}
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();
}
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);
}
}
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;
}
Aggregations