Example #1
0
  /**
   * Makes this instance in effect a reference to another FilterChain instance.
   *
   * <p>You must not set another attribute or nest elements inside this element if you make it a
   * reference.
   *
   * @param r the reference to which this instance is associated
   * @exception BuildException if this instance already has been configured.
   */
  public void setRefid(Reference r) throws BuildException {
    if (!filterReaders.isEmpty()) {
      throw tooManyAttributes();
    }
    // change this to get the objects from the other reference
    Object o = r.getReferencedObject(getProject());
    if (o instanceof FilterChain) {
      FilterChain fc = (FilterChain) o;
      filterReaders = fc.getFilterReaders();
    } else {
      String msg = r.getRefId() + " doesn\'t refer to a FilterChain";
      throw new BuildException(msg);
    }

    super.setRefid(r);
  }
  /**
   * Assemble the reader
   *
   * @return the assembled reader
   * @exception BuildException if an error occurs
   */
  public Reader getAssembledReader() throws BuildException {
    if (primaryReader == null) {
      throw new BuildException("primaryReader must not be null.");
    }

    Reader instream = primaryReader;
    final int filterReadersCount = filterChains.size();
    final Vector finalFilters = new Vector();
    final ArrayList /*<AntClassLoader>*/ classLoadersToCleanUp =
        new ArrayList /*<AntClassLoader>*/();

    for (int i = 0; i < filterReadersCount; i++) {
      final FilterChain filterchain = (FilterChain) filterChains.elementAt(i);
      final Vector filterReaders = filterchain.getFilterReaders();
      final int readerCount = filterReaders.size();
      for (int j = 0; j < readerCount; j++) {
        finalFilters.addElement(filterReaders.elementAt(j));
      }
    }

    final int filtersCount = finalFilters.size();

    if (filtersCount > 0) {
      boolean success = false;
      try {
        for (int i = 0; i < filtersCount; i++) {
          Object o = finalFilters.elementAt(i);

          if (o instanceof AntFilterReader) {
            instream =
                expandReader(
                    (AntFilterReader) finalFilters.elementAt(i), instream, classLoadersToCleanUp);
          } else if (o instanceof ChainableReader) {
            setProjectOnObject(o);
            instream = ((ChainableReader) o).chain(instream);
            setProjectOnObject(instream);
          }
        }
        success = true;
      } finally {
        if (!success && classLoadersToCleanUp.size() > 0) {
          cleanUpClassLoaders(classLoadersToCleanUp);
        }
      }
    }
    final Reader finalReader = instream;
    return classLoadersToCleanUp.size() == 0
        ? finalReader
        : new FilterReader(finalReader) {
          public void close() throws IOException {
            FileUtils.close(in);
            cleanUpClassLoaders(classLoadersToCleanUp);
          }

          protected void finalize() throws Throwable {
            try {
              close();
            } finally {
              super.finalize();
            }
          }
        };
  }