Search in sources :

Example 11 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project sling by apache.

the class ScriptEngineManagerFactory method registerFactories.

@SuppressWarnings("unchecked")
private void registerFactories(final SlingScriptEngineManager mgr, final Bundle bundle) {
    URL url = bundle.getEntry(ENGINE_FACTORY_SERVICE);
    InputStream ins = null;
    try {
        ins = url.openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        String line;
        while ((line = reader.readLine()) != null) {
            if (!line.startsWith("#") && line.trim().length() > 0) {
                try {
                    Class<ScriptEngineFactory> clazz = (Class<ScriptEngineFactory>) bundle.loadClass(line);
                    ScriptEngineFactory spi = clazz.newInstance();
                    registerFactory(mgr, spi, null);
                } catch (Throwable t) {
                    log.error("Cannot register ScriptEngineFactory " + line, t);
                }
            }
        }
    } catch (IOException ioe) {
    // ignore
    } finally {
        if (ins != null) {
            try {
                ins.close();
            } catch (IOException ioe) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ScriptEngineFactory(javax.script.ScriptEngineFactory) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL)

Example 12 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project sling by apache.

the class ScriptEngineConsolePlugin method printConfiguration.

public void printConfiguration(final PrintWriter pw) {
    pw.println("Available Script Engines");
    pw.println("========================");
    ScriptEngineManager manager = scriptEngineManagerFactory.getScriptEngineManager();
    List<?> factories = manager.getEngineFactories();
    for (Iterator<?> fi = factories.iterator(); fi.hasNext(); ) {
        final ScriptEngineFactory factory = (ScriptEngineFactory) fi.next();
        pw.println();
        pw.print(factory.getEngineName());
        pw.print(" ");
        pw.println(factory.getEngineVersion());
        pw.println("-------------------------------------");
        pw.print("- Language : ");
        pw.print(factory.getLanguageName());
        pw.print(", ");
        pw.println(factory.getLanguageVersion());
        pw.print("- Extensions : ");
        printArray(pw, factory.getExtensions());
        pw.print("- MIME Types : ");
        printArray(pw, factory.getMimeTypes());
        pw.print("- Names : ");
        printArray(pw, factory.getNames());
    }
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager)

Example 13 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project sling by apache.

the class ScriptEngineManagerFactory method refreshScriptEngineManager.

/**
     * Refresh the script engine manager.
     */
private void refreshScriptEngineManager() {
    // create (empty) script engine manager
    final ClassLoader loader = getClass().getClassLoader();
    final SlingScriptEngineManager tmp = new SlingScriptEngineManager(loader);
    // register script engines from bundles
    synchronized (this.engineSpiBundles) {
        for (final Bundle bundle : this.engineSpiBundles) {
            registerFactories(tmp, bundle);
        }
    }
    // register script engines from registered services
    synchronized (this.engineSpiServices) {
        for (final Map.Entry<ScriptEngineFactory, Map<Object, Object>> factory : this.engineSpiServices.entrySet()) {
            registerFactory(tmp, factory.getKey(), factory.getValue());
        }
    }
    scriptEngineManager.setDelegatee(tmp);
    final List<ScriptEngineFactory> factories = tmp.getEngineFactories();
    for (final ScriptEngineFactory factory : factories) {
        log.info("ScriptEngine {}/{} is now handling {}, {}, {}.", new Object[] { factory.getEngineName(), factory.getEngineVersion(), factory.getExtensions(), factory.getMimeTypes(), factory.getNames() });
    }
}
Also used : SlingScriptEngineManager(org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager) Bundle(org.osgi.framework.Bundle) ScriptEngineFactory(javax.script.ScriptEngineFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project sling by apache.

the class ScriptingVariablesConsolePlugin method renderContent.

@Override
protected void renderContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final PrintWriter pw = response.getWriter();
    pw.append("<script type='text/javascript' src='").append(JS_RES_PATH).append("'></script>");
    pw.append("<div id='content'>");
    pw.append("<table class='content'  cellpadding='0' cellspacing='0' width='100%'>");
    pw.append("<tr><th colspan='3' class='content container'>Sling Scripting Variables</th></tr>");
    pw.append("<tr class='content'><td class='content' colspan='3'>Provide a resource path url and script engine (via extension) and then click on 'Retrieve Variables' to expose all script bindings variables for context 'request' which are available for that resource and script engine.</td></tr>");
    pw.append("<tr class='content'>");
    pw.append("<td class='content'>Resource Url (without selectors and extension)</td> ");
    pw.append("<td class='content' colspan='2'><input type ='text' name='form.path' placeholder='path' required='required' value='/' ");
    pw.append("class='input ui-state-default ui-corner-all inputText' size='50' pattern='^/{1}.*'></td></tr>");
    pw.append("<tr class='content'>");
    pw.append("<td class='content'>Script Engine</td> ");
    pw.append("<td class='content' colspan='2'><select name='form.extension'>");
    for (ScriptEngineFactory factory : scriptEngineManager.getEngineFactories()) {
        for (String extension : factory.getExtensions()) {
            pw.append("<option value='" + extension + "'>" + extension + " (" + factory.getEngineName() + ")</option>");
        }
        pw.append("<option value=''>all (unfiltered)</option>");
    }
    pw.append("</select> ");
    pw.append("<button type='button' id='submitButton'> Retrieve Variables </button></td></tr></table>");
    pw.append("<div id='response'></div>");
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) PrintWriter(java.io.PrintWriter)

Example 15 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project sling by apache.

the class SlingBindingsVariablesListJsonServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    JSONWriter jsonWriter = new JSONWriter(response.getWriter());
    jsonWriter.array();
    // get filter by engine selector
    String requestedExtension = request.getParameter(PARAMETER_EXTENSION);
    if (StringUtils.isNotBlank(requestedExtension)) {
        ScriptEngine selectedScriptEngine = scriptEngineManager.getEngineByExtension(requestedExtension);
        if (selectedScriptEngine == null) {
            throw new IllegalArgumentException("Invalid extension requested: " + requestedExtension);
        } else {
            writeBindingsToJsonWriter(jsonWriter, selectedScriptEngine.getFactory(), request, response);
        }
    } else {
        for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) {
            writeBindingsToJsonWriter(jsonWriter, engineFactory, request, response);
        }
    }
    jsonWriter.endArray();
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngineFactory (javax.script.ScriptEngineFactory)30 ScriptEngineManager (javax.script.ScriptEngineManager)11 ScriptEngine (javax.script.ScriptEngine)9 Test (org.junit.Test)9 QuickTest (com.hazelcast.test.annotation.QuickTest)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 ArrayList (java.util.ArrayList)4 URL (java.net.URL)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 JSONWriter (org.apache.felix.utils.json.JSONWriter)2 Result (com.buschmais.jqassistant.core.analysis.api.Result)1 RuleExecutorException (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)1 OSQLScriptEngineFactory (com.orientechnologies.orient.core.sql.OSQLScriptEngineFactory)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1