use of org.jaggeryjs.scriptengine.engine.JavaScriptProperty 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.JavaScriptProperty in project jaggery by wso2.
the class WebAppManager method deploy.
public static void deploy(org.apache.catalina.Context context) throws ScriptException {
ServletContext ctx = context.getServletContext();
JaggeryContext sharedContext = new JaggeryContext();
Context cx = Context.getCurrentContext();
CommonManager.initContext(sharedContext);
sharedContext.addProperty(Constants.SERVLET_CONTEXT, ctx);
sharedContext.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(ctx));
sharedContext.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
String logLevel = (String) ctx.getAttribute(LogHostObject.LOG_LEVEL);
if (logLevel != null) {
sharedContext.addProperty(LogHostObject.LOG_LEVEL, logLevel);
}
ScriptableObject sharedScope = sharedContext.getScope();
JavaScriptProperty application = new JavaScriptProperty("application");
application.setValue(cx.newObject(sharedScope, "Application", new Object[] { ctx }));
application.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(sharedScope, application);
ctx.setAttribute(SHARED_JAGGERY_CONTEXT, sharedContext);
}
use of org.jaggeryjs.scriptengine.engine.JavaScriptProperty in project jaggery by wso2.
the class WebAppManager method defineProperties.
private static void defineProperties(Context cx, JaggeryContext context, ScriptableObject scope) {
JavaScriptProperty request = new JavaScriptProperty("request");
HttpServletRequest servletRequest = (HttpServletRequest) context.getProperty(SERVLET_REQUEST);
request.setValue(cx.newObject(scope, "Request", new Object[] { servletRequest }));
request.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(scope, request);
JavaScriptProperty response = new JavaScriptProperty("response");
HttpServletResponse servletResponse = (HttpServletResponse) context.getProperty(SERVLET_RESPONSE);
response.setValue(cx.newObject(scope, "Response", new Object[] { servletResponse }));
response.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(scope, response);
JavaScriptProperty session = new JavaScriptProperty("session");
session.setValue(cx.newObject(scope, "Session", new Object[] { servletRequest }));
RhinoEngine.defineProperty(scope, session);
JavaScriptProperty application = new JavaScriptProperty("application");
ServletContext servletConext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
application.setValue(cx.newObject(scope, "Application", new Object[] { servletConext }));
application.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(scope, application);
if (isWebSocket(servletRequest)) {
JavaScriptProperty websocket = new JavaScriptProperty("webSocket");
websocket.setValue(cx.newObject(scope, "WebSocket", new Object[0]));
websocket.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(scope, websocket);
}
}
use of org.jaggeryjs.scriptengine.engine.JavaScriptProperty in project jaggery by wso2.
the class WebAppSessionListener method sessionCreated.
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_CREATED_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();
}
Aggregations