Example #1
0
 /**
  * Add a Parameter.
  *
  * @param param a <code>Parameter</code> value
  */
 public void addConfiguredParam(Parameter param) {
   parameters.put(param.getName(), param.getValue());
 }
Example #2
0
  /**
   * Main entry point for this task. It creates transformation specified by classname attribute,
   * passes all parameters and executes it.
   *
   * @throws BuildException if an error occured
   */
  @Override
  public void execute() throws BuildException {
    if (getClassname() == null) {
      throw new BuildException("Classname of transformation is not set.");
    }

    if (getSrcFile() != null && getSrcDir() != null) {
      throw new BuildException("Both srcFile and srcDir were set.");
    }

    if (getDestFile() != null && getDestDir() != null) {
      throw new BuildException("Both destFile and destDir were set.");
    }

    if (!resources.isEmpty() && (getSrcFile() != null || getSrcDir() != null)) {
      throw new BuildException("Resources can't be combined with srcFile or srcDir.");
    }

    initClassloader();

    try {
      TransformationLoader loader =
          new TransformationLoader(Thread.currentThread().getContextClassLoader());
      Transformation transObj = null;
      try {
        transObj = loader.getInstance(getClassname());
      } catch (InstantiationException ex) {
        throw new BuildException("Transformation couldn't be instanciated", ex);
      } catch (IllegalAccessException ex) {
        throw new BuildException("Transformation couldn't be accessed", ex);
      }

      if (transObj == null) {
        throw new BuildException("Transformation " + getClassname() + " couldn't be loaded");
      }

      List<MappedFile> files = getMappedFiles();
      if (files.isEmpty()) {
        log(
            "No files specified, " + transObj.getClass().getSimpleName() + " skipped",
            Project.MSG_INFO);
        return;
      }
      for (MappedFile mf : files) {
        log("Mapped file " + mf.getFrom() + " to " + mf.getTo(), Project.MSG_VERBOSE);
      }

      transObj.setParam("baseDir", getProject().getBaseDir().getPath());
      transObj.setMappedFiles(files);
      transObj.setParams(parameters);

      for (String key : parameters.keySet())
        log(
            "Parsed parameter "
                + key
                + " = "
                + parameters.get(key)
                + " ("
                + parameters.get(key).getClass().getName()
                + ")",
            Project.MSG_VERBOSE);

      try {
        log(
            "Executing transformation "
                + transObj.getClass().getSimpleName()
                + " on "
                + mappedFiles.size()
                + " files");
        transObj.execute();
      } catch (TransformationException ex) {
        log("Transformation has failed", ex, Project.MSG_ERR);
        if (isFailonerror()) {
          throw new BuildException("Transformation has failed.", ex);
        }
      }
    } finally {
      restoreClassloader();
    }
  }