use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class WebSocketHostObject method jsConstructor.
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws Exception {
int argsCount = args.length;
if (argsCount != 0) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
}
WebSocketHostObject who = new WebSocketHostObject();
who.contextFactory = cx.getFactory();
JaggeryContext currentContext = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
JaggeryContext asyncContext = new JaggeryContext();
asyncContext.setEngine(currentContext.getEngine());
asyncContext.setScope(currentContext.getScope());
asyncContext.setTenantDomain(currentContext.getTenantDomain());
asyncContext.addProperty(Constants.SERVLET_CONTEXT, currentContext.getProperty(Constants.SERVLET_CONTEXT));
asyncContext.addProperty(LogHostObject.LOG_LEVEL, currentContext.getProperty(LogHostObject.LOG_LEVEL));
asyncContext.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, currentContext.getProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER));
asyncContext.addProperty(Constants.JAGGERY_CORE_MANAGER, currentContext.getProperty(Constants.JAGGERY_CORE_MANAGER));
asyncContext.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>());
asyncContext.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>());
asyncContext.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
who.asyncContext = asyncContext;
return who;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class LogHostObject method jsConstructor.
/**
* Creates a new log object for the requested resource.
* logger name will be the resource name separated by a . (dot)
* i.e if resource is /foo/bar/mar.jag
* the loger name will be
* JAGGERY.foo.bar.mar
* <p/>
* by default the log level is set to debug
*
* @param cx
* @param args
* @param ctorObj
* @param inNewExpr
* @return
* @throws ScriptException
*/
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
int argsCount = args.length;
if (argsCount > 1) {
HostObjectUtil.invalidNumberOfArgs(HOSTOBJECT_NAME, HOSTOBJECT_NAME, argsCount, true);
}
String loggerName;
JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
if (argsCount == 1 && (args[0] instanceof String)) {
loggerName = (String) args[0];
} else {
String requestString = ((Stack<String>) context.getProperty(JAGGERY_INCLUDES_CALLSTACK)).peek();
loggerName = ROOT_LOGGER + requestString.replace(".jag", ":jag").replace(".js", ":js").replace("/", ".");
}
LogHostObject logObj = new LogHostObject();
Logger currentLogger = Logger.getLogger(loggerName);
String appLogLevel = (String) context.getProperty(LOG_LEVEL);
if (currentLogger.getLevel() == null) {
currentLogger.setLevel(Level.toLevel(appLogLevel));
}
logObj.logger = currentLogger;
return logObj;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext 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() }));
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.engine.JaggeryContext in project jaggery by wso2.
the class WebAppManager method getScriptCachingContext.
protected static ScriptCachingContext getScriptCachingContext(HttpServletRequest request, String scriptPath) throws ScriptException {
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
String tenantId = jaggeryContext.getTenantDomain();
String[] parts = getKeys(request.getContextPath(), scriptPath, scriptPath);
/**
* tenantId = tenantId
* context = webapp context
* path = relative path to the directory of *.js file
* cacheKey = name of the *.js file being cached
*/
ScriptCachingContext sctx = new ScriptCachingContext(tenantId, parts[0], parts[1], parts[2]);
ServletContext servletContext = request.getServletContext();
sctx.setSecurityDomain(new JaggerySecurityDomain(getNormalizedScriptPath(parts), servletContext));
long lastModified = getScriptLastModified(servletContext, scriptPath);
sctx.setSourceModifiedTime(lastModified);
return sctx;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class WebAppManager method clearInterval.
public static void clearInterval(final Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
JaggeryContext context = CommonManager.getJaggeryContext();
ServletContext servletContext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
String contextPath = servletContext.getContextPath();
RhinoTopLevel.clearTimeout(cx, thisObj, args, funObj);
List<String> taskIds = intervals.get(contextPath);
taskIds.remove(String.valueOf(args[0]));
}
Aggregations