use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class CommonManager method print.
/**
* JaggeryMethod responsible of writing to the output stream
*/
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "print";
JaggeryContext jaggeryContext = getJaggeryContext();
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs("RhinoTopLevel", functionName, argsCount, false);
}
OutputStream out = (OutputStream) jaggeryContext.getProperty(CommonManager.JAGGERY_OUTPUT_STREAM);
if (args[0] instanceof StreamHostObject) {
InputStream in = ((StreamHostObject) args[0]).getStream();
if (in instanceof FileInputStream) {
//if form file we will use channel since it's faster
ReadableByteChannel inputChannel = null;
WritableByteChannel outputChannel = null;
FileChannel fc = null;
try {
inputChannel = Channels.newChannel(in);
outputChannel = Channels.newChannel(out);
fc = (FileChannel) inputChannel;
fc.transferTo(0, fc.size(), outputChannel);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} finally {
clearResources(fc, inputChannel, outputChannel, in, out);
}
} else {
try {
IOUtils.copy(in, out);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} finally {
clearResources(in, out);
}
}
} else {
try {
out.write(HostObjectUtil.serializeObject(args[0]).getBytes());
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(e.getMessage(), e);
}
throw new ScriptException(e);
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class CommonManager method require.
public static ScriptableObject require(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
String functionName = "require";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
}
String moduleName = (String) args[0];
JaggeryContext context = getJaggeryContext();
//RhinoEngine engine = context.getEngine();
//ScriptableObject scope = context.getScope();
CommonManager manager = (CommonManager) context.getProperty(Constants.JAGGERY_CORE_MANAGER);
ModuleManager moduleManager = manager.getModuleManager();
JavaScriptModule module = moduleManager.getModule(moduleName);
if (module == null) {
String msg = "A module cannot be found with the specified name : " + moduleName;
log.error(msg);
throw new ScriptException(msg);
}
ScriptableObject object = (ScriptableObject) cx.newObject(thisObj);
object.setPrototype(thisObj);
object.setParentScope(thisObj);
exposeModule(cx, object, module);
return object;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class JaggeryDeployerManager method processJaggeryApp.
/**
* Process the jaggery.conf file of jaggery apps
*
* @param context context of the jaggery app
*/
public static void processJaggeryApp(Context context, Path appBase) {
JSONObject jaggeryConfigObj = readJaggeryConfig(context, appBase);
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setAuthConstraint(true);
SecurityCollection securityCollection = new SecurityCollection();
securityCollection.setName(SECURITYCOLLECTION_NAME);
securityCollection.setDescription(SECURITYCOLLECTION_DESCRIPTION);
securityConstraint.addCollection(securityCollection);
initJaggeryappDefaults(context, jaggeryConfigObj, securityConstraint);
try {
WebAppManager.getEngine().enterContext();
WebAppManager.deploy(context);
setDisplayName(context, jaggeryConfigObj);
if (jaggeryConfigObj != null) {
addSessionCreatedListners(context, (JSONArray) jaggeryConfigObj.get(JaggeryCoreConstants.JaggeryConfigParams.SESSION_CREATED_LISTENER_SCRIPTS));
addSessionDestroyedListners(context, (JSONArray) jaggeryConfigObj.get(JaggeryCoreConstants.JaggeryConfigParams.SESSION_DESTROYED_LISTENER_SCRIPTS));
executeScripts(context, (JSONArray) jaggeryConfigObj.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS));
addUrlMappings(context, jaggeryConfigObj);
}
} catch (ScriptException e) {
log.error(e.getMessage(), e);
try {
context.destroy();
} catch (LifecycleException e1) {
log.error(e1.getMessage(), e1);
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException 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();
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class CommandLineManager method include_once.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "include_once";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
CommonManager.include_once(cx, thisObj, args, funObj);
return;
}
ScriptableObject scope = jaggeryContext.getScope();
RhinoEngine engine = jaggeryContext.getEngine();
if (fileURL.startsWith("/")) {
fileURL = includesCallstack.firstElement() + fileURL;
} else {
fileURL = FilenameUtils.getFullPath(parent) + fileURL;
}
fileURL = FilenameUtils.normalize(fileURL);
if (includesCallstack.search(fileURL) != -1) {
return;
}
if (includedScripts.get(fileURL) != null) {
return;
}
try {
ScriptReader source = new ScriptReader(new FileInputStream(fileURL));
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
engine.exec(source, scope, null);
includesCallstack.pop();
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
Aggregations