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; }
public static String jsFunction_readAll( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "readAll"; int argsCount = args.length; if (argsCount != 0) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } FileHostObject fho = (FileHostObject) thisObj; return fho.file.readAll(); }
public static Scriptable jsFunction_getStream( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "getStream"; int argsCount = args.length; if (argsCount != 0) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } FileHostObject fho = (FileHostObject) thisObj; return fho.context.newObject(thisObj, "Stream", new Object[] {fho.file.getInputStream()}); }
public static void jsFunction_close( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "close"; int argsCount = args.length; if (argsCount != 0) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } FileHostObject fho = (FileHostObject) thisObj; fho.file.close(); }
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( CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError( CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false); } String moduleId = (String) args[0]; int dotIndex = moduleId.lastIndexOf("."); if (moduleId.length() == dotIndex + 1) { String msg = "Invalid file path for require method : " + moduleId; log.error(msg); throw new ScriptException(msg); } JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); Map<String, ScriptableObject> requiredModules = (Map<String, ScriptableObject>) jaggeryContext.getProperty(Constants.JAGGERY_REQUIRED_MODULES); ScriptableObject object = requiredModules.get(moduleId); if (object != null) { return object; } if (dotIndex == -1) { object = CommonManager.require(cx, thisObj, args, funObj); initModule(cx, jaggeryContext, moduleId, object); } else { object = (ScriptableObject) cx.newObject(thisObj); object.setPrototype(thisObj); object.setParentScope(null); String ext = moduleId.substring(dotIndex + 1); if (ext.equalsIgnoreCase("json")) { object = executeScript(jaggeryContext, object, moduleId, true, true, false); } else if (ext.equalsIgnoreCase("js")) { object = executeScript(jaggeryContext, object, moduleId, false, true, false); } else if (ext.equalsIgnoreCase("jag")) { object = executeScript(jaggeryContext, object, moduleId, false, false, false); } else { String msg = "Unsupported file type for require() method : ." + ext; log.error(msg); throw new ScriptException(msg); } } requiredModules.put(moduleId, object); return object; }
public static void jsFunction_write( Context cx, Scriptable thisObj, Object[] args, Function funObj) // NOPMD throws ScriptException { String functionName = "write"; int argsCount = args.length; if (argsCount != 1) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } String data = HostObjectUtil.serializeObject(args[0]); FileHostObject fho = (FileHostObject) thisObj; fho.file.write(data); }
public static String jsFunction_read( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "read"; int argsCount = args.length; if (argsCount != 1) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } if (!(args[0] instanceof Number)) { HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false); } FileHostObject fho = (FileHostObject) thisObj; int count = ((Number) args[0]).intValue(); return fho.file.read(count); }
public static void jsFunction_open( Context cx, Scriptable thisObj, Object[] args, Function funObj) // NOPMD throws ScriptException { String functionName = "open"; int argsCount = args.length; if (argsCount != 1) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false); } FileHostObject fho = (FileHostObject) thisObj; fho.file.open((String) args[0]); }
public static boolean jsFunction_saveAs( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "saveAs"; int argsCount = args.length; if (argsCount != 1) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false); } FileHostObject fho = (FileHostObject) thisObj; return fho.file.saveAs((String) args[0]); }
public static String jsFunction_getContentType( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "getContentType"; int argsCount = args.length; if (argsCount != 0) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false); } FileHostObject fho = (FileHostObject) thisObj; if (!mimeMapLoaded) { FileTypeMap.setDefaultFileTypeMap(loadMimeMap()); mimeMapLoaded = true; } return fho.file.getContentType(); }
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( CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError( CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false); } String param = (String) args[0]; int dotIndex = param.lastIndexOf("."); if (param.length() == dotIndex + 1) { String msg = "Invalid file path for require method : " + param; log.error(msg); throw new ScriptException(msg); } if (dotIndex == -1) { ScriptableObject object = CommonManager.require(cx, thisObj, args, funObj); initModule(cx, CommonManager.getJaggeryContext(), param, object); return object; } JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); ScriptableObject object = (ScriptableObject) cx.newObject(thisObj); object.setPrototype(thisObj); object.setParentScope(null); String ext = param.substring(dotIndex + 1); if (ext.equalsIgnoreCase("json")) { return executeScript(jaggeryContext, object, param, true, true, false); } else if (ext.equalsIgnoreCase("js")) { return executeScript(jaggeryContext, object, param, false, true, false); } else if (ext.equalsIgnoreCase("jag")) { return executeScript(jaggeryContext, object, param, false, false, false); } else { String msg = "Unsupported file type for require() method : ." + ext; log.error(msg); throw new ScriptException(msg); } }
public static Scriptable jsConstructor( Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException { int argsCount = args.length; if (argsCount != 1) { HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true); } FileHostObject fho = new FileHostObject(); Object obj = cx.getThreadLocal(JAVASCRIPT_FILE_MANAGER); if (obj instanceof JavaScriptFileManager) { fho.manager = (JavaScriptFileManager) obj; } else { fho.manager = new JavaScriptFileManagerImpl(); } fho.file = fho.manager.getJavaScriptFile(args[0]); fho.file.construct(); fho.context = cx; return fho; }
/** 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(); try { byte[] buffer = new byte[BYTE_BUFFER_SIZE]; int count; while ((count = in.read(buffer)) != -1) { out.write(buffer, 0, count); } in.close(); } catch (IOException e) { log.debug(e.getMessage(), e); throw new ScriptException(e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { log.debug(e.getMessage(), e); } } } } else { try { out.write(HostObjectUtil.serializeObject(args[0]).getBytes()); } catch (IOException e) { log.debug(e.getMessage(), e); throw new ScriptException(e); } } }
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); String parent = includesCallstack.lastElement(); String fileURL = (String) args[0]; if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) { CommonManager.include_once(cx, thisObj, args, funObj); return; } executeScript(jaggeryContext, jaggeryContext.getScope(), fileURL, false, false, true); }
public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "include"; int argsCount = args.length; if (argsCount != 1 && argsCount != 2) { HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError( HOST_OBJECT_NAME, functionName, "1", "string", args[0], false); } if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) { HostObjectUtil.invalidArgsError( HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false); } JaggeryContext jaggeryContext = getJaggeryContext(); RhinoEngine engine = jaggeryContext.getEngine(); if (engine == null) { log.error("Rhino Engine in Jaggery context is null"); throw new ScriptException("Rhino Engine in Jaggery context is null"); } Stack<String> includesCallstack = getCallstack(jaggeryContext); Map<String, Boolean> includedScripts = getIncludes(jaggeryContext); String parent = includesCallstack.lastElement(); String fileURL = (String) args[0]; if (isHTTP(fileURL) || isHTTP(parent)) { if (!isHTTP(fileURL)) { fileURL = parent + fileURL; } if (includesCallstack.search(fileURL) != -1) { return; } ScriptReader source; ScriptableObject scope; if (argsCount == 2) { scope = (ScriptableObject) args[1]; } else { scope = jaggeryContext.getScope(); } // this is a remote file url try { URL url = new URL(fileURL); url.openConnection(); source = new ScriptReader(url.openStream()); includedScripts.put(fileURL, true); includesCallstack.push(fileURL); engine.exec(source, scope, null); includesCallstack.pop(); } catch (MalformedURLException e) { String msg = "Malformed URL. function : import, url : " + fileURL; log.warn(msg, e); throw new ScriptException(msg, e); } catch (IOException e) { String msg = "IO exception while importing content from url : " + fileURL; log.warn(msg, e); throw new ScriptException(msg, e); } } else { String msg = "Unsupported file include : " + fileURL; throw new ScriptException(msg); } }