示例#1
0
 // This code worked successfully on command-line but never as JUnit test
 // <script>:1: undefined method `+' for nil:NilClass (NoMethodError)
 // raised at "Object obj1 = engine1.eval("$Value + 2010.to_s");"
 // @Test
 public void testMultipleEngineStates() throws ScriptException {
   logger1.info("Multiple Engine States");
   ScriptEngine engine1;
   ScriptEngine engine2;
   synchronized (this) {
     System.setProperty("org.jruby.embed.localcontext.scope", "singlethread");
     System.setProperty("org.jruby.embed.localvariable.behavior", "global");
     ScriptEngineManager manager = new ScriptEngineManager();
     List<ScriptEngineFactory> factories = manager.getEngineFactories();
     ScriptEngineFactory factory = null;
     while (factories.iterator().hasNext()) {
       factory = factories.iterator().next();
       if ("ruby".equals(factory.getLanguageName())) {
         break;
       }
     }
     engine1 = factory.getScriptEngine();
     engine2 = factory.getScriptEngine();
   }
   engine1.put("Value", "value of the first engine");
   engine2.put("Value", new Double(-0.0149));
   Object obj1 = engine1.eval("$Value + 2010.to_s");
   Object obj2 = engine2.eval("$Value + 2010");
   assertNotSame(obj1, obj2);
   engine1 = null;
   engine2 = null;
 }
示例#2
0
  /**
   * Log available script engines at INFO level.
   *
   * @param logger the logger instance used to log available script engines.
   */
  public static void logAvailableScriptEngines(Logger logger) {
    if (!logger.isInfoEnabled()) {
      return;
    }

    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();

    StringBuilder buffer = new StringBuilder();
    buffer.append("Available script engines: ");

    int engineFactoriesCount = engineFactories.size();

    for (int i = 0; i < engineFactoriesCount; i++) {
      ScriptEngineFactory scriptEngineFactory = engineFactories.get(i);
      buffer.append(scriptEngineFactory.getEngineName());
      buffer.append(" (");
      buffer.append(scriptEngineFactory.getEngineVersion());
      buffer.append(")");

      if (i < engineFactoriesCount - 1) {
        buffer.append(", ");
      }
    }

    logger.info(buffer.toString());
  }
 public static void main(String[] args) {
   // create ScriptEngineManager
   ScriptEngineManager manager = new ScriptEngineManager();
   List<ScriptEngineFactory> factoryList = manager.getEngineFactories();
   for (ScriptEngineFactory factory : factoryList) {
     System.out.println(factory.getEngineName());
     System.out.println(factory.getLanguageName());
   }
 }
  private L2ScriptEngineManager() {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = scriptEngineManager.getEngineFactories();
    if (Config.SCRIPT_CACHE) {
      _cache = loadCompiledScriptCache();
    } else {
      _cache = null;
    }
    _log.info("Initializing Script Engine Manager");

    for (ScriptEngineFactory factory : factories) {
      try {
        ScriptEngine engine = factory.getScriptEngine();
        boolean reg = false;
        for (String name : factory.getNames()) {
          ScriptEngine existentEngine = _nameEngines.get(name);

          if (existentEngine != null) {
            double engineVer = Double.parseDouble(factory.getEngineVersion());
            double existentEngVer =
                Double.parseDouble(existentEngine.getFactory().getEngineVersion());

            if (engineVer <= existentEngVer) {
              continue;
            }
          }

          reg = true;
          _nameEngines.put(name, engine);
        }

        if (reg) {
          _log.info(
              "Script Engine: "
                  + factory.getEngineName()
                  + " "
                  + factory.getEngineVersion()
                  + " - Language: "
                  + factory.getLanguageName()
                  + " - Language Version: "
                  + factory.getLanguageVersion());
        }

        for (String ext : factory.getExtensions()) {
          if (!ext.equals("java") || factory.getLanguageName().equals("java")) {
            _extEngines.put(ext, engine);
          }
        }
      } catch (Exception e) {
        _log.warn("Failed initializing factory. ");
        e.printStackTrace();
      }
    }

    preConfigure();
  }
  /**
   * @return A list containing the short names of all scripting engines supported. An engie can have
   *     multiple names so in general the number of engine names is greather than the number of
   *     actual engines registered into the platform
   */
  public Set<String> getSuportedEngineNames() {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

    Set<String> engineNames = new HashSet<String>();
    for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
      engineNames.addAll(scriptEngineFactory.getNames());
    }
    return engineNames;
  }
  /** @return A list containing the names of all scripting languages supported */
  public Set<String> getSupportedLanguages() {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

    Set<String> languages = new HashSet<String>();
    for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
      languages.add(scriptEngineFactory.getLanguageName());
    }
    return languages;
  }
  public String getHelpText() {
    StringBuilder helpText =
        new StringBuilder(
            "Custom message content generated from a script using JSR 223. "
                + "Custom scripts should be placed in "
                + "$JENKINS_HOME/"
                + EMAIL_TEMPLATES_DIRECTORY
                + ". When using custom scripts, "
                + "the script filename WITH .py/.rb/etc	should be used for "
                + "the \""
                + SCRIPT_NAME_ARG
                + "\" argument.\n"
                + "templates and other items may be loaded using the\n"
                + "host.readFile(String fileName) function\n"
                + "the function will look in the resources for\n"
                + "the email-ext plugin first, and then in the $JENKINS_HOME/"
                + EMAIL_TEMPLATES_DIRECTORY
                + "\n"
                + "directory. No other directories will be searched.\n"
                + "<ul>\n"
                + "<li><i>"
                + SCRIPT_NAME_ARG
                + "</i> - the script name.<br>\n"
                + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Defaults to \""
                + DEFAULT_SCRIPT_NAME
                + "\".</li>\n"
                + "<li><i>"
                + SCRIPT_TEMPLATE_ARG
                + "</i> - the template filename.<br>\n"
                + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Defaults to \""
                + DEFAULT_TEMPLATE_NAME
                + "\"</li>\n"
                + "<li><i>"
                + SCRIPT_INIT_ARG
                + "</i> - true to run the language's init script.<br>\n"
                + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Defaults to "
                + DEFAULT_INIT_VALUE
                + "</li>\n"
                + "<li>Available Script Engines\n"
                + "<ul>\n");

    for (ScriptEngineFactory fact : scriptEngineManager.getEngineFactories()) {
      String extensions = join(fact.getExtensions(), ",");
      helpText.append(
          "<li><i>"
              + fact.getLanguageName()
              + "</i> - "
              + fact.getLanguageVersion()
              + " ("
              + extensions
              + ")</li>\n");
    }
    helpText.append("</ul></ul>\n");
    return helpText.toString();
  }
  private ScriptEngine getScriptEngineByName(String name) {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

    for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
      if (scriptEngineFactory.getNames().contains(name)) {
        return scriptEngineFactory.getScriptEngine();
      }
    }
    return null;
  }
  private ScriptEngine getScriptEngineByLanguage(String language) {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

    for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
      if (scriptEngineFactory.getLanguageName().equals(language)) {
        return scriptEngineFactory.getScriptEngine();
      }
    }
    return null;
  }
示例#10
0
 static {
   FACTORIES.addAll(MANAGER.getEngineFactories());
   // Sort factories alphabetically so that they appear in the UI consistently
   Collections.sort(
       FACTORIES,
       new Comparator<ScriptEngineFactory>() {
         @Override
         public int compare(ScriptEngineFactory sef1, ScriptEngineFactory sef2) {
           return sef1.getEngineName().compareTo(sef2.getEngineName());
         }
       });
 }
示例#11
0
  public ObjectPanel(ClientModel model) {
    super(model);

    model.addObjectListener(this);

    // get all installed script engines
    scriptExtensions = new HashSet<String>();
    ScriptEngineManager mgr = new ScriptEngineManager();
    for (ScriptEngineFactory sef : mgr.getEngineFactories()) {
      scriptExtensions.addAll(sef.getExtensions());
    }

    createGUI();
  }
  public void printInfo() {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();

    for (ScriptEngineFactory factory : factories) {
      System.out.println("ScriptEngineFactory Info");
      String engName = factory.getEngineName();
      String engVersion = factory.getEngineVersion();
      String langName = factory.getLanguageName();
      String langVersion = factory.getLanguageVersion();
      System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);
      List<String> engNames = factory.getNames();
      for (String name : engNames) {
        System.out.printf("\tEngine Alias: %s\n", name);
      }
      System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
    }
  }
示例#13
0
  /**
   * Get a list of available scripting engines for executing scripts.
   *
   * @return a list of available scripting engines for executing scripts.
   */
  public List<String> getAvailableScriptEngines() {
    if (availableScriptEngines == null) {
      availableScriptEngines = new ArrayList<>();
      ScriptEngineManager manager = getScriptEngineManager();
      if (manager != null) {
        List<ScriptEngineFactory> factoryList = manager.getEngineFactories();
        for (ScriptEngineFactory factory : factoryList) {
          availableScriptEngines.addAll(factory.getNames());
        }
      }

      if (availableScriptEngines.isEmpty()) {
        useRhino = true;
        engineName = "Rhino";
        availableScriptEngines.add(engineName);
      }
    }
    return availableScriptEngines;
  }
  private void initScript() {
    String scriptName = "src" + File.separator + "jscripts" + File.separator + "CreateWorld.js";
    List<ScriptEngineFactory> list = factory.getEngineFactories();
    File scriptFile = new File(scriptName);

    try {
      FileReader fileReader = new FileReader(scriptFile);
      engine.eval(fileReader);
      fileReader.close();
    } catch (FileNotFoundException e1) {
      System.out.println(scriptName + " not found " + e1);
    } catch (IOException e2) {
      System.out.println("IO issue detected " + scriptName + e2);
    } catch (ScriptException e3) {
      System.out.println("ScriptException in " + scriptName + e3);
    } catch (NullPointerException e4) {
      System.out.println("Null pointer in" + scriptName + e4);
    }
    addGameWorldObject((SceneNode) engine.get("worldAxis"));
  }
  @Override
  protected PicoContainer createContainerFromScript(
      final PicoContainer parentContainer, final Object assemblyScope) {
    Reader reader = null;
    ScriptEngine engine = null;
    final ScriptEngineManager mgr = new ScriptEngineManager();
    final ClassLoader oldClassLoader =
        AccessController.doPrivileged(
            new PrivilegedAction<ClassLoader>() {

              public ClassLoader run() {
                return Thread.currentThread().getContextClassLoader();
              }
            });
    final ClassLoader newClassLoader = this.getClassLoader();
    try {
      if (applyCustomClassLoader()) {
        AccessController.doPrivileged(
            new PrivilegedAction<Void>() {
              public Void run() {
                Thread.currentThread().setContextClassLoader(newClassLoader);
                return null;
              }
            });
      }

      engine = mgr.getEngineByName(engineName);
      if (engine == null) {
        final StringBuilder message =
            new StringBuilder(
                "Could not find a script engine named: '"
                    + engineName
                    + "' all script engines in your classpath are:\n");
        for (final ScriptEngineFactory eachFactory : mgr.getEngineFactories()) {
          message.append(
              "\t Engine named '"
                  + eachFactory.getEngineName()
                  + "' which supports the language '"
                  + eachFactory.getLanguageName()
                  + "' with short names '"
                  + Arrays.toString(eachFactory.getNames().toArray())
                  + "'\n");
        }

        throw new PicoCompositionException(message.toString());
      }

      final Bindings bindings = engine.createBindings();
      bindings.put("parent", parentContainer);
      bindings.put("assemblyScope", assemblyScope);
      applyOtherBindings(bindings);

      reader = this.getScriptReader();

      PicoContainer result = (PicoContainer) engine.eval(reader, bindings);
      if (result == null) {
        result = (PicoContainer) bindings.get("pico");
        if (result == null) {
          // Todo: remove as a deprecated variable name
          result = (PicoContainer) bindings.get("nano");
          if (result == null) {
            throw new PicoCompositionException(
                "Script completed successfully, but did not return any value, nor did it declare a variable named 'pico'");
          }
        }
      }
      return result;
    } catch (final ClassCastException e) {
      throw new ScriptedPicoContainerMarkupException(
          "The return type of the script must be of type PicoContainer", e);
    } catch (final IOException e) {
      throw new ScriptedPicoContainerMarkupException(
          "IOException encountered, message -'" + e.getMessage() + "'", e);
    } catch (final ScriptException e) {
      throw new ScriptedPicoContainerMarkupException(
          "Error executing composition script under engine '"
              + engine.getFactory().getEngineName()
              + "'",
          e);
    } finally {
      if (applyCustomClassLoader()) {
        AccessController.doPrivileged(
            new PrivilegedAction<Void>() {

              public Void run() {
                Thread.currentThread().setContextClassLoader(oldClassLoader);
                return null;
              }
            });
      }

      if (reader != null) {
        try {
          reader.close();
        } catch (final IOException e) {
          // Ignore
        }
      }
    }
  }
 public ScriptLanguageParameter(ScriptEngineManager engineManager) {
   for (ScriptEngineFactory factory : engineManager.getEngineFactories()) {
     languages.add(factory.getLanguageName());
   }
 }