use of javax.script.ScriptEngineManager in project tomee by apache.
the class AbstractTomEEMojo method scriptCustomization.
private void scriptCustomization(final List<String> customizers, final String ext) throws MojoExecutionException {
if (customizers != null) {
final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
if (engine == null) {
throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
}
for (final String js : customizers) {
try {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("catalinaBase", catalinaBase.getAbsolutePath());
bindings.put("resolver", new Resolver() {
@Override
public File resolve(final String group, final String artifact, final String version, final String classifier, final String type) {
try {
return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type).resolved;
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public File resolve(final String group, final String artifact, final String version) {
try {
return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar").resolved;
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public File resolve(final String group, final String artifact, final String version, final String type) {
try {
return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type).resolved;
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
});
engine.eval(new StringReader(js), bindings);
} catch (final ScriptException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
}
use of javax.script.ScriptEngineManager in project bnd by bndtools.
the class Macro method _js.
public Object _js(String[] args) throws Exception {
verifyCommand(args, _js, null, 2, Integer.MAX_VALUE);
StringBuilder sb = new StringBuilder();
for (int i = 1; i < args.length; i++) sb.append(args[i]).append(';');
if (context == null) {
synchronized (this) {
if (engine == null)
engine = new ScriptEngineManager().getEngineByName("javascript");
}
context = engine.getContext();
bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("domain", domain);
String javascript = domain.mergeProperties("javascript", ";");
if (javascript != null && javascript.length() > 0) {
engine.eval(javascript, context);
}
context.setErrorWriter(stderr);
context.setWriter(stdout);
}
Object eval = engine.eval(sb.toString(), context);
StringBuffer buffer = stdout.getBuffer();
if (buffer.length() > 0) {
domain.error("Executing js: %s: %s", sb, buffer);
buffer.setLength(0);
}
if (eval != null) {
return toString(eval);
}
String out = stdout.toString();
stdout.getBuffer().setLength(0);
return out;
}
use of javax.script.ScriptEngineManager in project jgnash by ccavanaugh.
the class ExecuteJavaScriptAction method showAndWait.
public static void showAndWait() {
final ResourceBundle resources = ResourceUtils.getBundle();
final FileChooser fileChooser = configureFileChooser();
fileChooser.setTitle(resources.getString("Title.SelFile"));
final File file = fileChooser.showOpenDialog(MainView.getPrimaryStage());
if (file != null) {
Preferences pref = Preferences.userNodeForPackage(ExecuteJavaScriptAction.class);
pref.put(LAST_DIR, file.getParentFile().getAbsolutePath());
Platform.runLater(() -> {
try (final Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
new ScriptEngineManager().getEngineByName("nashorn").eval(reader);
} catch (IOException | ScriptException ex) {
Logger.getLogger(ExecuteJavaScriptAction.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
});
}
}
use of javax.script.ScriptEngineManager in project jgnash by ccavanaugh.
the class RunJavaScriptAction method actionPerformed.
@Override
public void actionPerformed(final ActionEvent e) {
final Preferences pref = Preferences.userNodeForPackage(RunJavaScriptAction.class);
JFileChooser chooser = new JFileChooser(pref.get(JAVASCRIPT_DIR, null));
chooser.setMultiSelectionEnabled(false);
if (chooser.showOpenDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
pref.put(JAVASCRIPT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
final Path path = chooser.getSelectedFile().toPath();
EventQueue.invokeLater(() -> {
try (final Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
new ScriptEngineManager().getEngineByName("nashorn").eval(reader);
} catch (IOException | ScriptException ex) {
Logger.getLogger(RunJavaScriptAction.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
});
}
}
Aggregations