Ejemplo n.º 1
0
  /**
   * Initializes default class loader. It inherits from classloader, which was used to load this
   * task, therefore build-in classes and resources are loaded automatically.
   *
   * <p>Notice: task's classpath has priority over default classpath, therefore we are able to
   * easily override resources (templates)
   */
  protected void initClassloader() {
    // NOTICE: we should use the same classloader, which was used to load this task
    // otherwise we won't be able to cast instances
    // (that were loaded via different class loaders)

    // we are introducting workaround,
    // that tries to add classpath for current task to original class loader
    // and stores original classpath
    // after the task is executed, classpath is restored

    ClassLoader classLoader = getClass().getClassLoader();
    if (classLoader instanceof AntClassLoader) {
      // add defined classpath
      AntClassLoader antClassLoader = (AntClassLoader) classLoader;
      antClassLoader.setParentFirst(false);
      antClassLoader.setProject(getProject());
      savedClasspath = antClassLoader.getClasspath();

      Path cp = getClasspath();
      // add defined classpath to original path
      cp.add(new Path(getProject(), savedClasspath));
      antClassLoader.setClassPath(cp);
    } else {
      // create new class loader
      classLoader =
          new AntClassLoader(getClass().getClassLoader(), getProject(), getClasspath(), false);
    }

    // set this class loader as new class loader for whole thread
    // ContextClassLoader is used in StringTemplate's PathGroupLoader and other classes
    Thread.currentThread().setContextClassLoader(classLoader);
  }
Ejemplo n.º 2
0
  /**
   * Adds the contents of this class path element to the given class path.
   *
   * @param classPath the class path to be extended.
   * @param output specifies whether this is an output entry or not.
   */
  public void appendClassPathEntriesTo(ClassPath classPath, boolean output) {
    File baseDir = getProject().getBaseDir();
    String[] fileNames;

    if (isReference()) {
      // Get the referenced path or file set.
      Object referencedObject = getCheckedRef(DataType.class, DataType.class.getName());

      if (referencedObject instanceof Path) {
        Path path = (Path) referencedObject;

        // Get the names of the files in the referenced path.
        fileNames = path.list();
      } else if (referencedObject instanceof AbstractFileSet) {
        AbstractFileSet fileSet = (AbstractFileSet) referencedObject;

        // Get the names of the existing input files in the referenced file set.
        DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
        baseDir = scanner.getBasedir();
        fileNames = scanner.getIncludedFiles();
      } else {
        throw new BuildException(
            "The refid attribute doesn't point to a <path> element or a <fileset> element");
      }
    } else {
      // Get the names of the files in this path.
      fileNames = list();
    }

    if (output) {
      if (fileNames.length != 1) {
        throw new BuildException(
            "The <outjar> element must specify exactly one file or directory ["
                + fileNames.length
                + "]");
      }
    }
    // else
    // {
    //    if (fileNames.length < 1)
    //    {
    //        throw new BuildException("The <injar> element must specify at least one file or
    // directory");
    //    }
    // }

    for (int index = 0; index < fileNames.length; index++) {
      // Create a new class path entry, with the proper file name and
      // any filters.
      String fileName = fileNames[index];
      File file = new File(fileName);

      ClassPathEntry entry =
          new ClassPathEntry(file.isAbsolute() ? file : new File(baseDir, fileName), output);
      entry.setFilter(ListUtil.commaSeparatedList(filter));
      entry.setApkFilter(ListUtil.commaSeparatedList(apkFilter));
      entry.setJarFilter(ListUtil.commaSeparatedList(jarFilter));
      entry.setAarFilter(ListUtil.commaSeparatedList(aarFilter));
      entry.setWarFilter(ListUtil.commaSeparatedList(warFilter));
      entry.setEarFilter(ListUtil.commaSeparatedList(earFilter));
      entry.setZipFilter(ListUtil.commaSeparatedList(zipFilter));

      // Add it to the class path.
      classPath.add(entry);
    }
  }