Package javax.script

Examples of javax.script.ScriptEngine


  protected final Logger logger = LoggerFactory.getLogger(getClass());

  @Override
  public boolean execute(ScriptEngineManager engineManager,
      TransitionElement transition, Map<String, Object> conditions) {
    ScriptEngine engine = engineManager.getEngineByName(ScriptEngineConstants.AVIATOR);
   
    //创建执行表达式
    String expression = buildExpression(transition, conditions);
   
    if(logger.isDebugEnabled())
      logger.debug("Transition Expressopn:{} Start", expression);
    Bindings variables= engine.createBindings();
    if(conditions != null) {
      for (Map.Entry<String, Object> m : conditions.entrySet()) {  
        variables.put(m.getKey(), m.getValue());
       
        if(logger.isDebugEnabled())
          logger.debug("    Transition Condition:{} = {}", m.getKey(), m.getValue());
      }
    }
    if(logger.isDebugEnabled())
      logger.debug("Transition Expressopn End");
   
    try {
      Object result = engine.eval(expression, variables);
      if(result instanceof Boolean && (Boolean)result) {
        return true;
      } else
        return false;
    } catch (Exception e) {
View Full Code Here


  public ScriptInstance(String scriptlanguage) {
    languageId = scriptlanguage;
    logger.debug("the language id is " + scriptlanguage);

    ScriptEngineManager sm;
    ScriptEngine se;
    Bindings sb;
    try {
      sm = new ScriptEngineManager();
      se = sm.getEngineByName(getLanguageId());
      sb = se.getBindings(ScriptContext.ENGINE_SCOPE);
    }
    catch (Exception e) {
      throw new UnsupportedScriptLanguageException(e, "Scriptlanguage " + getLanguageId() + " is not supported.", scriptlanguage);
    }
    scriptengine = se;
View Full Code Here

  private ConfigService configService;

  public void parseConfig(ConfigFile configFile) {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    String content = configFile.getContent();
    engine.put("config", this);
    try {
      engine.eval(content);
    } catch (ScriptException e) {
      throw new IllegalStateException("There was an arror evaluating config script: " + configFile.getPath(), e);
    }

  }
View Full Code Here

    private Dictionary<String, String> props;
    private ComponentContext ctx;
    private final JmxScriptBindingsProvider jmxScriptBindingsProvider = new JmxScriptBindingsProvider();

    private void assertExpression(String expression, String languageExtension, boolean expected) throws Exception {
        final ScriptEngine rhino = new ScriptEngineManager().getEngineByExtension("js");
        assertNotNull("With the rhino jar in our classpath, we should get a js script engine", rhino);
        final ScriptEngineManager manager = Mockito.mock(ScriptEngineManager.class);
        Mockito.when(manager.getEngineByExtension(Matchers.same("ecma"))).thenReturn(rhino);
        final Field f = hc.getClass().getDeclaredField("scriptEngineManager");
        f.setAccessible(true);
View Full Code Here

    @Override
    public Result execute() {
        final FormattingResultLog resultLog = new FormattingResultLog();
        resultLog.debug("Checking expression [{}], language extension=[{}]",  expression, languageExtension);
        try {
            final ScriptEngine engine = scriptEngineManager.getEngineByExtension(languageExtension);
            if (engine == null) {
                resultLog.healthCheckError("No ScriptEngine available for extension {}", languageExtension);
            } else {
                // Set Bindings, with our ResultLog as a binding first, so that other bindings can use it
                final Bindings b = engine.createBindings();
                b.put(FormattingResultLog.class.getName(), resultLog);
                synchronized (bindingsValuesProviders) {
                    for(BindingsValuesProvider bvp : bindingsValuesProviders) {
                        log.debug("Adding Bindings provided by {}", bvp);
                        bvp.addBindings(b);
                    }
                }
                log.debug("All Bindings added: {}", b.keySet());

                final Object value = engine.eval(expression, b);
                if(value!=null && "true".equals(value.toString().toLowerCase())) {
                    resultLog.debug("Expression [{}] evaluates to true as expected", expression);
                } else {
                    resultLog.warn("Expression [{}] does not evaluate to true as expected, value=[{}]", expression, value);
                }
View Full Code Here

public class RhinoJavaScriptEngineTest extends TestCase {

    public void testPreserveScopeBetweenEvals() throws ScriptException {
        MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory();
        ScriptEngine engine = factory.getScriptEngine();
        Bindings context = new SimpleBindings();
        engine.eval("var f = 1", context);
        Object result = null;
        try {
            result = engine.eval("f += 1", context);
        } catch (ScriptException e) {
            TestCase.fail(e.getMessage());
        }
        assertTrue(result instanceof Double);
        assertEquals(2.0, result);
View Full Code Here

        Resource resource = (Resource) adaptable;
        String path = resource.getPath();
        String ext = path.substring(path.lastIndexOf('.') + 1);

        ScriptEngine engine = scriptEngineManager.getEngineByExtension(ext);
        if (engine != null) {
            final Collection<BindingsValuesProvider> bindingsValuesProviders =
                    bindingsValuesProviderTracker.getBindingsValuesProviders(engine.getFactory(), BINDINGS_CONTEXT);
            // unchecked cast
            return (AdapterType) new DefaultSlingScript(this.bundleContext,
                    resource, engine, bindingsValuesProviders, this.serviceCache);
        }
View Full Code Here

     *            name contains no dot, the entire name is considered the
     *            extension.
     */
    public String getMimeType(String name) {
        name = name.substring(name.lastIndexOf('.') + 1);
        ScriptEngine se = scriptEngineManager.getEngineByExtension(name);
        if (se != null) {
            List<?> mimeTypes = se.getFactory().getMimeTypes();
            if (mimeTypes != null && mimeTypes.size() > 0) {
                return String.valueOf(mimeTypes.get(0));
            }
        }

View Full Code Here

     * method returns <code>null</code>.
     *
     * @param mimeType The MIME type to be mapped to an extension.
     */
    public String getExtension(String mimeType) {
        ScriptEngine se = scriptEngineManager.getEngineByMimeType(mimeType);
        if (se != null) {
            List<?> extensions = se.getFactory().getExtensions();
            if (extensions != null && extensions.size() > 0) {
                return String.valueOf(extensions.get(0));
            }
        }

View Full Code Here

        // Log messages to verify which ScriptEngine is actually used
        // for our registered extensions
        if (log.isInfoEnabled()) {
            for (Object o : extensions) {
                final String ext = o.toString();
                final ScriptEngine e = tmp.getEngineByExtension(ext);
                if (e == null) {
                    log.warn("No ScriptEngine found for extension '{}' that was just registered", ext);
                } else {
                    log.info("Script extension '{}' is now handled by ScriptEngine '{}', version='{}', class='{}'", new Object[] { ext,
                            e.getFactory().getEngineName(), e.getFactory().getEngineVersion(), e.getClass().getName() });
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of javax.script.ScriptEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.