use of org.wso2.carbon.context.PrivilegedCarbonContext in project jaggery by wso2.
the class RhinoTopLevel method setTimeout.
public static String setTimeout(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 timeout;
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();
timeout = ((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.schedule(new Callable<Void>() {
public Void call() throws Exception {
//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 ctx = RhinoEngine.enterContext(factory);
RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, context);
callback.call(ctx, thisObj, thisObj, params);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
RhinoEngine.exitContext();
currentThread.setContextClassLoader(originalClassLoader);
}
return null;
}
}, timeout, TimeUnit.MILLISECONDS);
Map<String, ScheduledFuture> tasks = timeouts.get(context.getTenantDomain());
if (tasks == null) {
tasks = new HashMap<String, ScheduledFuture>();
timeouts.put(context.getTenantDomain(), tasks);
}
tasks.put(uuid, future);
return uuid;
}
use of org.wso2.carbon.context.PrivilegedCarbonContext 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;
}
Aggregations