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) {
}
}
}
}
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());
}
}
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() });
}
}
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>");
}
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();
}
Aggregations