Exemple #1
0
  @Override
  protected void parse() {
    defaultScript = getConfig().getString(PARAM_DEFAULT_SCRIPT, "");
    defaultDir = getConfig().getString(PARAM_DEFAULT_DIR, "");

    try {
      List<HierarchicalConfiguration> fields =
          ((HierarchicalConfiguration) getConfig()).configurationsAt(ALL_SCRIPTS_KEY);
      this.scripts = new HashSet<>(fields.size());
      List<String> tempListNames = new ArrayList<>(fields.size());
      for (HierarchicalConfiguration sub : fields) {
        String name = sub.getString(SCRIPT_NAME_KEY, "");
        try {
          if (!"".equals(name) && !tempListNames.contains(name)) {
            tempListNames.add(name);

            File file = new File(sub.getString(SCRIPT_FILE_KEY));
            if (!file.exists()) {
              logger.error("Script '" + file.getAbsolutePath() + "' does not exist");
              continue;
            }

            ScriptWrapper script =
                new ScriptWrapper(
                    sub.getString(SCRIPT_NAME_KEY),
                    sub.getString(SCRIPT_DESC_KEY),
                    sub.getString(SCRIPT_ENGINE_KEY),
                    sub.getString(SCRIPT_TYPE_KEY),
                    sub.getBoolean(SCRIPT_ENABLED_KEY),
                    file);

            script.setLoadOnStart(true); // Because it was saved ;)

            scripts.add(script);
          }
        } catch (Exception e) {
          logger.error("Error while loading the script: " + name, e);
        }
      }
    } catch (Exception e) {
      logger.error("Error while loading the scripts: " + e.getMessage(), e);
    }

    try {
      this.scriptDirs = new ArrayList<File>();
      for (Object dirName : getConfig().getList(SCRIPT_DIRS)) {
        File f = new File((String) dirName);
        if (!f.exists() || !f.isDirectory()) {
          logger.error("Not a valid script directory: " + dirName);
        } else {
          scriptDirs.add(f);
        }
      }

    } catch (Exception e) {
      logger.error("Error while loading the script dirs: " + e.getMessage(), e);
    }
    confirmRemoveDir = getConfig().getBoolean(SCRIPT_CONFIRM_REMOVE_DIR, true);
  }
Exemple #2
0
  public void saveScripts() {
    ((HierarchicalConfiguration) getConfig()).clearTree(ALL_SCRIPTS_KEY);

    int i = 0;
    for (ScriptWrapper script : scripts) {
      if (script.isLoadOnStart()) {
        String elementBaseKey = ALL_SCRIPTS_KEY + "(" + i + ").";
        getConfig().setProperty(elementBaseKey + SCRIPT_NAME_KEY, script.getName());
        getConfig().setProperty(elementBaseKey + SCRIPT_DESC_KEY, script.getDescription());
        getConfig().setProperty(elementBaseKey + SCRIPT_ENGINE_KEY, script.getEngineName());
        getConfig().setProperty(elementBaseKey + SCRIPT_TYPE_KEY, script.getTypeName());
        getConfig()
            .setProperty(elementBaseKey + SCRIPT_ENABLED_KEY, Boolean.valueOf(script.isEnabled()));
        getConfig()
            .setProperty(elementBaseKey + SCRIPT_FILE_KEY, script.getFile().getAbsolutePath());
        i++;
      }
    }
  }