Java Code Examples for javax.script.SimpleBindings#put()

The following examples show how to use javax.script.SimpleBindings#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TomEEEmbeddedMojo.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void scriptCustomization(final List<String> customizers, final String ext, final String base) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    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", base);
            engine.eval(new StringReader(js), bindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
 
Example 2
Source File: NashornEngineUtil.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
public static SimpleHttpResponse process(SimpleHttpRequest httpRequest)
		throws NoSuchMethodException, ScriptException, IOException {
	CompiledScript compiled;
	Compilable engine;
	String scriptFilePath = "./src/main/resources/templates/js/script.js";
	
	engine = (Compilable) engineFactory.getScriptEngine();
	compiled = ((Compilable) engine).compile(Files.newBufferedReader(Paths.get(scriptFilePath),StandardCharsets.UTF_8));
	
	SimpleBindings global = new SimpleBindings();
	global.put("theReq", httpRequest);
	global.put("theResp", new SimpleHttpResponse());
	
	Object result = compiled.eval(global);
	SimpleHttpResponse resp = (SimpleHttpResponse) result;
	return resp;
}
 
Example 3
Source File: Assistant.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static Object evalObjectScript(String jsStr, JavaExecutor java, Object result, String baseUrl) {
    try {
        SimpleBindings bindings = new SimpleBindings();
        bindings.put("java", java);
        bindings.put("result", result);
        bindings.put("baseUrl", baseUrl);
        return Assistant.SCRIPT_ENGINE.eval(jsStr, bindings);
    } catch (Exception e) {
        Logger.e(TAG, jsStr, e);
    }
    return null;
}
 
Example 4
Source File: ScriptPatternSelector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public PatternFormatter[] getFormatters(final LogEvent event) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.putAll(configuration.getProperties());
    bindings.put("substitutor", configuration.getStrSubstitutor());
    bindings.put("logEvent", event);
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    if (object == null) {
        return defaultFormatters;
    }
    final PatternFormatter[] patternFormatter = formatterMap.get(object.toString());

    return patternFormatter == null ? defaultFormatters : patternFormatter;
}
 
Example 5
Source File: ScriptFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
                     final Throwable t) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", msg);
    bindings.put("parameters", null);
    bindings.put("throwable", t);
    bindings.putAll(configuration.getProperties());
    bindings.put("substitutor", configuration.getStrSubstitutor());
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
 
Example 6
Source File: ScriptFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
                     final Object... params) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", new SimpleMessage(msg));
    bindings.put("parameters", params);
    bindings.put("throwable", null);
    bindings.putAll(configuration.getProperties());
    bindings.put("substitutor", configuration.getStrSubstitutor());
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
 
Example 7
Source File: BaseJSMetaBuilder.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
private Object execute(MapAttribute wellKnowsAttributes) throws MetaException {
  try {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("attributes", wellKnowsAttributes);
    Invocable invocable = (Invocable)engines.getCachedEngine(javascriptPath);
    return invocable.invokeFunction("create", wellKnowsAttributes);
  } catch (ScriptException|NoSuchMethodException|IOException|URISyntaxException ex) {
    throw new MetaException("Error executing script.", ex);
  }
}
 
Example 8
Source File: ScriptCondition.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the script
 * 
 * @param baseDir
 * @param candidates
 * @return
 */
@SuppressWarnings("unchecked")
public List<PathWithAttributes> selectFilesToDelete(final Path basePath, final List<PathWithAttributes> candidates) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("basePath", basePath);
    bindings.put("pathList", candidates);
    bindings.putAll(configuration.getProperties());
    bindings.put("configuration", configuration);
    bindings.put("substitutor", configuration.getStrSubstitutor());
    bindings.put("statusLogger", LOGGER);
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return (List<PathWithAttributes>) object;
}
 
Example 9
Source File: ResourceOverridingRequestWrapper.java    From sling-org-apache-sling-models-impl with Apache License 2.0 5 votes vote down vote up
ResourceOverridingRequestWrapper(SlingHttpServletRequest wrappedRequest, Resource resource,
                                        AdapterManager adapterManager, SlingModelsScriptEngineFactory scriptEngineFactory,
                                        BindingsValuesProvidersByContext bindingsValuesProvidersByContext) {
    super(wrappedRequest);
    this.resource = resource;
    this.adapterManager = adapterManager;

    SlingBindings existingBindings = (SlingBindings) wrappedRequest.getAttribute(SlingBindings.class.getName());

    SimpleBindings bindings = new SimpleBindings();
    if (existingBindings != null) {
        bindings.put(SLING, existingBindings.getSling());
        bindings.put(RESPONSE, existingBindings.getResponse());
        bindings.put(READER, existingBindings.getReader());
        bindings.put(OUT, existingBindings.getOut());
        bindings.put(LOG, existingBindings.getLog());
    }
    bindings.put(REQUEST, this);
    bindings.put(RESOURCE, resource);
    bindings.put(SlingModelsScriptEngineFactory.RESOLVER, resource.getResourceResolver());

    scriptEngineFactory.invokeBindingsValuesProviders(bindingsValuesProvidersByContext, bindings);

    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);

    this.bindings = slingBindings;

}
 
Example 10
Source File: MySearchBookFragment.java    From a with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, String baseUrl) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", getAnalyzeRule());
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 11
Source File: FindBookPresenter.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, String baseUrl) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", getAnalyzeRule());
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 12
Source File: CheckSourceTask.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, String baseUrl) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", new AnalyzeRule(null));
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 13
Source File: AnalyzeRule.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, Object result) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", this);
    bindings.put("result", result);
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 14
Source File: MyFindBookPresenter.java    From a with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, String baseUrl) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", getAnalyzeRule());
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 15
Source File: FindBookPresenter.java    From a with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, String baseUrl) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", getAnalyzeRule());
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 16
Source File: CheckSourceTask.java    From a with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, String baseUrl) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", new AnalyzeRule(null));
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 17
Source File: AnalyzeRule.java    From a with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, Object result) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("java", this);
    bindings.put("result", result);
    bindings.put("baseUrl", baseUrl);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 18
Source File: AnalyzeUrl.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, Object result) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("result", result);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}
 
Example 19
Source File: WkfTrackingService.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Root method to access the service. It creates WkfTracking record. WkfTracking is linked with
 * record of model and workflow of model.
 *
 * @param model Model having workflow.
 * @param modelId Record id of model to track.
 * @param status Current wkfStatus of record.
 * @throws ClassNotFoundException
 */
@CallMethod
public void track(Long wkfId, Object object) {

  if (object != null) {

    SimpleBindings ctx = null;

    object = EntityHelper.getEntity(object);
    Model model = (Model) object;

    if (object instanceof MetaJsonRecord) {
      log.debug("Meta json record context");
      MetaJsonRecord metaJsonRecord = (MetaJsonRecord) object;
      log.debug(
          "Json id: {}, Json model: {}", metaJsonRecord.getId(), metaJsonRecord.getJsonModel());
      ctx = new JsonContext((MetaJsonRecord) object);
      ctx.put("id", metaJsonRecord.getId());
      ctx.put("jsonModel", metaJsonRecord.getJsonModel());
    } else {
      ctx = new Context(model.getId(), object.getClass());
    }
    ctx.put("wkfId", wkfId);
    WkfTracking wkfTracking = getWorkflowTracking(ctx, object.getClass().getName());

    if (wkfTracking == null) {
      return;
    }

    String wkfFieldName = Beans.get(WkfService.class).getWkfFieldInfo(wkfTracking.getWkf())[0];
    String selection =
        "wkf."
            + Inflector.getInstance().dasherize(wkfTracking.getWkf().getName()).replace("_", ".");
    selection +=
        "." + Inflector.getInstance().dasherize(wkfFieldName).replace("_", ".") + ".select";

    Object status = null;
    status = ctx.get(wkfFieldName);
    log.debug("Status value: {}", status);

    if (status == null) {
      return;
    }

    Option item = MetaStore.getSelectionItem(selection, status.toString());

    log.debug("Fetching option {} from selection {}", status, selection);
    if (item == null) {
      return;
    }

    durationHrs = BigDecimal.ZERO;
    WkfTrackingLine trackingLine = updateTrackingLine(wkfTracking, item.getTitle());
    if (trackingLine != null) {
      updateTrackingTotal(wkfTracking, item.getTitle());
      updateTrackingTime(wkfTracking, item.getTitle());
    }
  }
}
 
Example 20
Source File: AnalyzeUrl.java    From a with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 执行JS
 */
private Object evalJS(String jsStr, Object result) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("result", result);
    return SCRIPT_ENGINE.eval(jsStr, bindings);
}