List of usage examples for javax.script ScriptEngineManager ScriptEngineManager
public ScriptEngineManager(ClassLoader loader)
ScriptEngineFactory
visible to the given ClassLoader
using the service provider mechanism.null
, the script engine factories that are bundled with the platform are loaded. From source file:org.jwebsocket.plugins.scripting.ScriptingPlugIn.java
private void execAppBeforeLoadChecks(final String aAppName, String aAppPath) throws Exception { // parsing app manifest File lManifestFile = new File(aAppPath + "/manifest.json"); if (!lManifestFile.exists() || !lManifestFile.canRead()) { String lMsg = "Unable to load '" + aAppName + "' application. Manifest file no found!"; mLog.error(lMsg);//w w w .j a v a 2s . co m throw new FileNotFoundException(lMsg); } // parsing app manifest file ObjectMapper lMapper = new ObjectMapper(); Map<String, Object> lTree = lMapper.readValue(lManifestFile, Map.class); Token lManifestJSON = TokenFactory.createToken(); lManifestJSON.setMap(lTree); // getting script language extension String lExt = lManifestJSON.getString(Manifest.LANGUAGE_EXT, "js"); // checking jWebSocket version Manifest.checkJwsVersion(lManifestJSON.getString(Manifest.JWEBSOCKET_VERSION, "1.0.0")); // checking jWebSocket plug-ins dependencies Manifest.checkJwsDependencies( lManifestJSON.getList(Manifest.JWEBSOCKET_PLUGINS_DEPENDENCIES, new ArrayList<String>())); // checking sandbox permissions dependency Manifest.checkPermissions(lManifestJSON.getList(Manifest.PERMISSIONS, new ArrayList()), mSettings.getAppPermissions(aAppName, aAppPath), aAppPath); // validating bootstrap file final File lBootstrap = new File(aAppPath + "/App." + lExt); if (!lBootstrap.exists() || !lBootstrap.canRead()) { String lMsg = "Unable to load '" + aAppName + "' application. Bootstrap file not found!"; mLog.error(lMsg); throw new FileNotFoundException(lMsg); } LocalLoader lClassLoader = new LocalLoader((URLClassLoader) ClassLoader.getSystemClassLoader()); ScriptEngineManager lManager = new ScriptEngineManager(lClassLoader); final ScriptEngine lScriptApp; final BaseScriptApp lApp; if ("js".equals(lExt)) { // making "nashorn" the default engine for JavaScript if (null != lManager.getEngineByName("nashorn")) { lScriptApp = lManager.getEngineByName("nashorn"); } else { lScriptApp = lManager.getEngineByExtension(lExt); } } else { lScriptApp = lManager.getEngineByExtension(lExt); } // creating the high level script app instance if ("js".equals(lExt)) { lApp = new JavaScriptApp(this, aAppName, aAppPath, lScriptApp, lClassLoader); } else { String lMsg = "The extension '" + lExt + "' is not currently supported!"; mLog.error(lMsg); throw new Exception(lMsg); } // loading application into security sandbox Tools.doPrivileged(mSettings.getAppPermissions(aAppName, aAppPath), new PrivilegedAction<Object>() { @Override public Object run() { try { // evaluating app content lScriptApp.eval(FileUtils.readFileToString(lBootstrap)); return null; } catch (Exception lEx) { String lAction = (mApps.containsKey(aAppName)) ? "reloaded" : "loaded"; String lMsg = "Script applicaton '" + aAppName + "' not " + lAction + " because it failed the 'before-load' checks: " + lEx.getMessage(); mLog.info(lMsg); throw new RuntimeException(lMsg); } } }); if (mLog.isDebugEnabled()) { mLog.debug(aAppName + "(" + lExt + ") application passed the 'before-load' checks successfully!"); } }
From source file:org.jwebsocket.plugins.scripting.ScriptingPlugIn.java
/** * Loads an script application.//from w ww . j a va2s . c o m * * @param aAppName The application name * @param aAppPath The application home path * @param aHotLoad * @return * @throws Exception */ private void loadApp(final String aAppName, String aAppPath, boolean aHotLoad) throws Exception { // notifying before app reload event here BaseScriptApp lScript = mApps.get(aAppName); if (null != lScript) { lScript.notifyEvent(BaseScriptApp.EVENT_BEFORE_APP_RELOAD, new Object[] { aHotLoad }); if (!aHotLoad) { destroyAppBeans(lScript); } } // parsing app manifest File lManifestFile = new File(aAppPath + "/manifest.json"); // parsing app manifest file ObjectMapper lMapper = new ObjectMapper(); Map<String, Object> lTree = lMapper.readValue(lManifestFile, Map.class); Token lManifestJSON = TokenFactory.createToken(); lManifestJSON.setMap(lTree); // getting script language extension String lExt = lManifestJSON.getString(Manifest.LANGUAGE_EXT, "js"); // validating bootstrap file final File lBootstrap = new File(aAppPath + "/App." + lExt); // support hot app load if (aHotLoad && mApps.containsKey(aAppName)) { try { // loading app mApps.get(aAppName).eval(lBootstrap.getPath()); } catch (ScriptException lEx) { mLog.error("Script applicaton '" + aAppName + "' failed to start: " + lEx.getMessage()); mApps.remove(aAppName); throw new ScriptException(lEx.getMessage()); } } else { LocalLoader lClassLoader = new LocalLoader((URLClassLoader) ClassLoader.getSystemClassLoader()); ScriptEngineManager lManager = new ScriptEngineManager(lClassLoader); final ScriptEngine lScriptApp; if ("js".equals(lExt)) { // making "nashorn" the default engine for JavaScript if (null != lManager.getEngineByName("nashorn")) { lScriptApp = lManager.getEngineByName("nashorn"); } else { lScriptApp = lManager.getEngineByExtension(lExt); } } else { lScriptApp = lManager.getEngineByExtension(lExt); } // crating the high level script app instance if ("js".equals(lExt)) { mApps.put(aAppName, new JavaScriptApp(this, aAppName, aAppPath, lScriptApp, lClassLoader)); } else { String lMsg = "The extension '" + lExt + "' is not currently supported!"; mLog.error(lMsg); throw new Exception(lMsg); } final BaseScriptApp lApp = mApps.get(aAppName); // loading application into security sandbox Tools.doPrivileged(mSettings.getAppPermissions(aAppName, aAppPath), new PrivilegedAction<Object>() { @Override public Object run() { try { // loading app lApp.eval(lBootstrap.getPath()); return null; } catch (Exception lEx) { mLog.error("Script applicaton '" + aAppName + "' failed to start: " + lEx.getMessage()); mApps.remove(aAppName); throw new RuntimeException(lEx); } } }); } // notifying app loaded event mApps.get(aAppName).notifyEvent(BaseScriptApp.EVENT_APP_LOADED, new Object[] { aHotLoad }); if (mLog.isDebugEnabled()) { mLog.debug(aAppName + "(" + lExt + ") application loaded successfully!"); } }