List of usage examples for javax.script ScriptException ScriptException
public ScriptException(Exception e)
ScriptException
wrapping an Exception
thrown by an underlying interpreter. From source file:org.danann.cernunnos.script.ScriptEvaluator.java
/** * Evaluates the script passed to the constructor, either using a CompiledScript if available * and supported by the ScriptEngine or directly with ScriptEngine.eval *//* w ww . j av a 2 s . com*/ public Object eval(ScriptContext scriptContext) throws ScriptException { if (this.compiledScript == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug( "No compiled script available, invoking ScriptEngine.eval(String, ScriptContext) for script:\n" + this.script); } return this.scriptEngine.eval(this.script, scriptContext); } try { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Compiled script available, invoking CompiledScript.eval(ScriptContext) for script:\n" + this.script); } return evalCompiledScriptWithContextMethod.invoke(this.compiledScript, scriptContext); } catch (Exception e) { if (e instanceof ScriptException) { throw (ScriptException) e; } throw new ScriptException(e); } }
From source file:com.seajas.search.attender.service.template.TemplateService.java
/** * Create a results template result./*www. j a va 2s . co m*/ * * @param language * @param queryString * @param query * @param uuid * @param searchResults * @return TemplateResult */ public TemplateResult createResults(final String language, final String queryString, final String query, final String subscriberUUID, final String profileUUID, final List<SearchResult> searchResults) throws ScriptException { Template template = templateDAO.findTemplate(TemplateType.Results, language); if (template == null) { logger.warn("Could not retrieve results template for language " + language + " - falling back to default language " + attenderService.getDefaultApplicationLanguage()); template = templateDAO.findTemplate(TemplateType.Confirmation, attenderService.getDefaultApplicationLanguage()); if (template == null) { logger.error("Could not retrieve fall-back results template for language " + attenderService.getDefaultApplicationLanguage() + " - not generating a template result"); return null; } } // Create an input document Document document; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setCoalescing(true); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.newDocument(); Element results = document.createElement("results"); results.setAttribute("templateLanguage", language); results.setAttribute("profileQuery", query); results.setAttribute("subscriberUUID", subscriberUUID); results.setAttribute("profileUUID", profileUUID); results.setAttribute("queryString", queryString); for (SearchResult searchResult : searchResults) results.appendChild(searchResult.toElement(document)); document.appendChild(results); // Plain text Transformer textTransformer = getTransformer(template, true); StringWriter textWriter = new StringWriter(); textTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); textTransformer.transform(new DOMSource(document), new StreamResult(textWriter)); // HTML Transformer htmlTransformer = getTransformer(template, false); StringWriter htmlWriter = new StringWriter(); htmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); htmlTransformer.transform(new DOMSource(document), new StreamResult(htmlWriter)); return new TemplateResult(textWriter.toString(), htmlWriter.toString()); } catch (ScriptException e) { throw e; } catch (Exception e) { throw new ScriptException(e); } }
From source file:org.danann.cernunnos.script.ScriptEvaluator.java
/** * Evaluates the script passed to the constructor, either using a CompiledScript if available * and supported by the ScriptEngine or directly with ScriptEngine.eval *//*from w ww .java2 s .c o m*/ public Object eval(Bindings bindings) throws ScriptException { if (this.compiledScript == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug( "No compiled script available, invoking ScriptEngine.eval(String, Bindings) for script:\n" + this.script); } return this.scriptEngine.eval(this.script, bindings); } try { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Compiled script available, invoking CompiledScript.eval(Bindings) for script:\n" + this.script); } return evalCompiledScriptWithBindingsMethod.invoke(this.compiledScript, bindings); } catch (Exception e) { if (e instanceof ScriptException) { throw (ScriptException) e; } throw new ScriptException(e); } }
From source file:com.seajas.search.attender.service.template.TemplateService.java
/** * Retrieve a transformer from the given template. * //from w w w .j a va 2 s . co m * @param template * @param asText * @return Transformer */ private Transformer getTransformer(final Template template, final Boolean asText) throws ScriptException { try { Transformer transformer = transformerCache.getTransformer(template.getId(), "attender-" + template.getType() + (asText ? "-text" : "-html"), template.getModificationDate()); if (transformer == null) transformer = transformerCache.putContent(template.getId(), "attender-" + template.getType() + (asText ? "-text" : "-html"), template.getModificationDate(), asText ? template.getTextContent() : template.getHtmlContent()); return transformer; } catch (TransformerConfigurationException e) { logger.error("Unable to generate a (cached) transformer from the given content", e); throw new ScriptException("Unable to generate a (cached) transformer from the given content"); } catch (TransformerFactoryConfigurationError e) { logger.error("Unable to generate a (cached) transformer from the given content", e); throw new ScriptException("Unable to generate a (cached) transformer from the given content"); } }
From source file:com.thinkbiganalytics.spark.repl.ScriptEngine.java
/** * Checks for a runtime exception.// w w w . ja va 2s.c om * * @throws ScriptException if an exception is found */ private void checkRuntimeError() throws ScriptException { Throwable exception = this.exception.get(); if (exception != null) { Throwables.propagateIfPossible(exception, ScriptException.class); if (exception instanceof Exception) { throw new ScriptException((Exception) exception); } else { throw new ScriptException(exception.getMessage()); } } }
From source file:com.seajas.search.contender.service.modifier.ModifierScriptProcessor.java
/** * Evaluate the given script./*from w ww . j a v a 2 s .c o m*/ * * @param settings * @param script * @param input * @param uri * @return String * @throws ScriptException */ private String evaluateScript(final WebResolverSettings settings, final ModifierScript script, String input, URI uri) throws ScriptException { final ScriptCacheEntry entry = scriptCache.acquireScript(script); try { final ScriptEngine engine = entry != null ? entry.getScript().getEngine() : engineManager.getEngineByName(script.getScriptLanguage().toLowerCase()); FeedScriptEvaluation evaluation = new FeedScriptEvaluation(settings); evaluation.setScriptResolver(new ScriptCacheResolver<FeedScript>() { @Override public FeedScript resolve(final Bindings bindings) throws ScriptException { for (Map.Entry<String, Object> binding : bindings.entrySet()) engine.put(binding.getKey(), binding.getValue()); if (entry != null) { if (logger.isTraceEnabled()) logger.trace("Executing compiled script with ID " + script.getId()); entry.getScript().eval(); } else { if (logger.isTraceEnabled()) logger.trace(String.format("Executing non-compiled script with ID %d and content '%s'", script.getId(), script.getScriptContent())); engine.eval(script.getScriptContent()); } return ((Invocable) engine).getInterface(FeedScript.class); } }); evaluation.setFeedURI(uri); evaluation.setFeedContent(input); evaluation.setEngine(engine); evaluation.setCacheResolver(new FeedScriptCacheResolver() { @Override public boolean isCached(final String resource) { logger.info("Cache requested URI: " + resource); String cacheKey = cacheService.createCompositeKey(resource, settings.getResultParameters()); return cacheService.isCached(cacheKey); } }); evaluation.setWebPages(new WebPages()); DefaultWebResolver resolver = new DefaultWebResolver(); resolver.setSettings(settings); resolver.setContenderService(contenderService); evaluation.setWebResolver(resolver); evaluation.init(); evaluation.run(); SyndFeed feed = evaluation.getFeed(); if (feed == null) throw new ScriptException("No result available"); try { WebFeeds.validate(feed, uri); SyndFeedOutput serializer = new SyndFeedOutput(); return serializer.outputString(feed, true); } catch (FeedException e) { throw new ScriptException("Can't serialize feed result: " + e); } } finally { scriptCache.releaseScript(script, entry); } }
From source file:com.aionemu.commons.scripting.AionScriptEngineManager.java
public void executeScript(File file) throws ScriptException, FileNotFoundException { String name = file.getName(); int lastIndex = name.lastIndexOf('.'); String extension;/* w w w . ja v a 2s. c o m*/ if (lastIndex != -1) { extension = name.substring(lastIndex + 1); } else { throw new ScriptException("Script file (" + name + ") doesnt has an extension that identifies the ScriptEngine to be used."); } ScriptEngine engine = getEngineByExtension(extension); if (engine == null) throw new ScriptException("No engine registered for extension (" + extension + ")"); this.executeScript(engine, file); }
From source file:com.aionemu.commons.scripting.AionScriptEngineManager.java
public void executeScript(String engineName, File file) throws FileNotFoundException, ScriptException { ScriptEngine engine = getEngineByName(engineName); if (engine == null) throw new ScriptException("No engine registered with name (" + engineName + ")"); this.executeScript(engine, file); }
From source file:org.wso2.carbon.apimgt.hostobjects.oidc.OIDCRelyingPartyObject.java
public static String jsFunction_getLoggedInUser(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { int argLength = args.length; if (argLength != 1 || !(args[0] instanceof String)) { throw new ScriptException("Invalid argument. Session id is missing."); }// ww w. ja v a2s .c om OIDCRelyingPartyObject relyingPartyObject = (OIDCRelyingPartyObject) thisObj; SessionInfo sessionInfo = relyingPartyObject.getSessionInfo((String) args[0]); String loggedInUser = null; if (sessionInfo != null && sessionInfo.getLoggedInUser() != null) { loggedInUser = sessionInfo.getLoggedInUser(); } return loggedInUser; }
From source file:com.aionemu.commons.scripting.AionScriptEngineManager.java
public Object eval(String engineName, String script, ScriptContext context) throws ScriptException { ScriptEngine engine = getEngineByName(engineName); if (engine == null) throw new ScriptException("No engine registered with name (" + engineName + ")"); return this.eval(engine, script, context); }