Example #1
0
 /**
  * Add all resources in a resource collection to the source list.
  *
  * @since Ant 1.7.1
  * @param collection the resource to load
  * @throws BuildException if a resource cannot be read
  */
 public void loadResources(ResourceCollection collection) {
   Iterator resources = collection.iterator();
   while (resources.hasNext()) {
     Resource resource = (Resource) resources.next();
     loadResource(resource);
   }
 }
Example #2
0
  /**
   * Returns list of mapped files, that should be transformed. Files can by specified via attributes
   * (srcFile, srcDir) or resources (FileSet, FileList, DirSet, etc). Mapped file represents input
   * and output file for transformation.
   *
   * @return list of mapped files
   */
  public List<MappedFile> getMappedFiles() {
    mappedFiles.clear();

    // one src file
    if (getSrcFile() != null) {
      addMappedFile(getSrcFile());
    }

    if (getSrcDir() != null) {
      addMappedFile(getSrcDir());
    }

    Iterator element = resources.iterator();
    while (element.hasNext()) {
      ResourceCollection rc = (ResourceCollection) element.next();
      if (rc instanceof FileSet && rc.isFilesystemOnly()) {
        FileSet fs = (FileSet) rc;
        File fromDir = fs.getDir(getProject());

        DirectoryScanner ds;
        try {
          ds = fs.getDirectoryScanner(getProject());
        } catch (BuildException ex) {
          log("Could not scan directory " + fromDir, ex, Project.MSG_ERR);
          continue;
        }

        for (String f : ds.getIncludedFiles()) {
          addMappedFile(new File(fromDir + System.getProperty("file.separator") + f), fromDir);
        }
      } else {
        if (!rc.isFilesystemOnly()) {
          log("Only filesystem resources are supported", Project.MSG_WARN);
          continue;
        }
        Iterator rcIt = rc.iterator();
        while (rcIt.hasNext()) {
          Resource r = (Resource) rcIt.next();
          if (!r.isExists()) {
            log("Could not find resource " + r.toLongString(), Project.MSG_VERBOSE);
            continue;
          }

          if (r instanceof FileResource) {
            FileResource fr = (FileResource) r;
            addMappedFile(fr.getFile(), fr.getBaseDir());
          } else {
            log(
                "Only file resources are supported (" + r.getClass().getSimpleName() + " found)",
                Project.MSG_WARN);
            continue;
          }
        }
      }
    }

    return mappedFiles;
  }
 /**
  * Set the source resource.
  *
  * @param a the resource to load as a single element Resource collection.
  * @since Ant 1.7
  */
 public synchronized void addConfigured(ResourceCollection a) {
   if (src != null) {
     throw new BuildException("only a single source is supported");
   }
   if (a.size() != 1) {
     throw new BuildException("only single-element resource collections are supported");
   }
   src = (Resource) a.iterator().next();
 }
Example #4
0
 /**
  * Sort the contained elements.
  *
  * @return a Collection of Resources.
  */
 protected synchronized Collection<Resource> getCollection() {
   ResourceCollection rc = getResourceCollection();
   Iterator<Resource> iter = rc.iterator();
   if (!(iter.hasNext())) {
     return Collections.emptySet();
   }
   List<Resource> result = (List<Resource>) CollectionUtils.asCollection(iter);
   Collections.sort(result, comp);
   return result;
 }
Example #5
0
 /**
  * Sort the contained elements.
  *
  * @return a Collection of Resources.
  */
 protected synchronized Collection getCollection() {
   ResourceCollection rc = getResourceCollection();
   Iterator iter = rc.iterator();
   if (!(iter.hasNext())) {
     return Collections.EMPTY_SET;
   }
   SortedBag b = new SortedBag(comp);
   while (iter.hasNext()) {
     b.add(iter.next());
   }
   return b;
 }
Example #6
0
  /**
   * Get the list of all Ant files we want to process. These can be project and antlib files.
   *
   * @return antFiles a list of ant files to be processed
   */
  public ArrayList getAntFiles(Project project, boolean homeOnly) {
    ArrayList antFiles = new ArrayList();

    Map targets = project.getTargets();
    Iterator targetsIter = targets.values().iterator();

    String projectHome = null;
    try {
      projectHome = new File(project.getProperty("helium.dir")).getCanonicalPath();

      while (targetsIter.hasNext()) {
        Target target = (Target) targetsIter.next();
        String projectPath = new File(target.getLocation().getFileName()).getCanonicalPath();

        if (!antFiles.contains(projectPath)) {
          if (homeOnly) {
            if (!projectPath.contains(projectHome)) {
              antFiles.add(projectPath);
            }
          } else antFiles.add(projectPath);
        }
      }

      if (rc != null) {
        Iterator extraFilesIter = rc.iterator();
        while (extraFilesIter.hasNext()) {
          FileResource f = (FileResource) extraFilesIter.next();
          String extrafile = f.getFile().getCanonicalPath();

          if (!antFiles.contains(f.toString()) && !f.getFile().getName().startsWith("test_")) {
            if (homeOnly) {
              if (!extrafile.contains(projectHome)) {
                antFiles.add(extrafile);
              }
            } else antFiles.add(extrafile);
          }
        }
      }

    } catch (Exception e) {
      log(e.getMessage(), Project.MSG_ERR);
      e.printStackTrace();
    }
    return antFiles;
  }
Example #7
0
  public void execute() throws BuildException {
    if (id == null) {
      throw new BuildException("id attribute is mandatory for buildpath", getLocation());
    }

    if (cache == null) {
      cache =
          utils.normalise(
              getProject().getProperty("q.project.targetDir") + "/build-sequence/" + id + ".txt");
    }

    // Collect all of the project files
    List projects = new ArrayList();

    for (Iterator i = projectCollections.iterator(); i.hasNext(); ) {
      ResourceCollection rc = (ResourceCollection) i.next();

      for (Iterator j = rc.iterator(); j.hasNext(); ) {
        Resource resource = (Resource) j.next();
        Assert.isTrue(
            resource instanceof FileResource, getLocation(), "Collections of files are expected");
        projects.add(((FileResource) resource).getFile());
      }
    }

    List seq = null;

    if (sequence) {
      // See if the build sequence is already generated and is up to date
      UpToDate upToDate = new UpToDate();
      upToDate.setTargetFile(cache);
      upToDate.setProject(getProject());

      for (Iterator i = projects.iterator(); i.hasNext(); ) {
        File file = (File) i.next();
        upToDate.addSrcfiles(utils.toFileSet(file));
      }

      // Load or generate the sequence
      if (upToDate.eval()) {
        log("Loading existing build configuration as it is up to date", Project.MSG_VERBOSE);
        seq = loadSequence(cache);

        if (!matches(projects, seq)) {
          log(
              "Invalidating loaded sequence as project list no longer matches",
              Project.MSG_VERBOSE);
          seq = null;
        }
      }

      if (seq == null) {
        log("Generating build sequence", Project.MSG_VERBOSE);
        seq = generateSequence(projects);
        saveSequence(seq, cache);
      }
    } else {
      seq = projects;
    }

    // Convert the sequence into a path and set it as a reference
    Path path = new Path(getProject());

    for (Iterator i = seq.iterator(); i.hasNext(); ) {
      File file = (File) i.next();
      Path element = new Path(getProject(), file.getParentFile().getAbsolutePath());
      path.add(element);
    }

    getProject().addReference(id, path);
  }
Example #8
0
  /**
   * Index the fileset.
   *
   * @exception IOException if Lucene I/O exception TODO: refactor!!!!!
   */
  private void indexDocs() throws IOException {
    Date start = new Date();

    boolean create = overwrite;
    // If the index directory doesn't exist,
    // create it and force create mode
    if (indexDir.mkdirs() && !overwrite) {
      create = true;
    }

    FSDirectory dir = FSDirectory.open(indexDir);
    try {
      Searcher searcher = null;
      boolean checkLastModified = false;
      if (!create) {
        try {
          searcher = new IndexSearcher(dir, true);
          checkLastModified = true;
        } catch (IOException ioe) {
          log("IOException: " + ioe.getMessage());
          // Empty - ignore, which indicates to index all
          // documents
        }
      }

      log("checkLastModified = " + checkLastModified, Project.MSG_VERBOSE);

      IndexWriterConfig conf =
          new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer)
              .setOpenMode(create ? OpenMode.CREATE : OpenMode.APPEND);
      LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
      lmp.setUseCompoundFile(useCompoundIndex);
      lmp.setMergeFactor(mergeFactor);
      IndexWriter writer = new IndexWriter(dir, conf);
      int totalFiles = 0;
      int totalIndexed = 0;
      int totalIgnored = 0;
      try {

        for (int i = 0; i < rcs.size(); i++) {
          ResourceCollection rc = rcs.elementAt(i);
          if (rc.isFilesystemOnly()) {
            Iterator resources = rc.iterator();
            while (resources.hasNext()) {
              Resource r = (Resource) resources.next();
              if (!r.isExists() || !(r instanceof FileResource)) {
                continue;
              }

              totalFiles++;

              File file = ((FileResource) r).getFile();

              if (!file.exists() || !file.canRead()) {
                throw new BuildException(
                    "File \"" + file.getAbsolutePath() + "\" does not exist or is not readable.");
              }

              boolean indexIt = true;

              if (checkLastModified) {
                Term pathTerm = new Term("path", file.getPath());
                TermQuery query = new TermQuery(pathTerm);
                ScoreDoc[] hits = searcher.search(query, null, 1).scoreDocs;

                // if document is found, compare the
                // indexed last modified time with the
                // current file
                // - don't index if up to date
                if (hits.length > 0) {
                  Document doc = searcher.doc(hits[0].doc);
                  String indexModified = doc.get("modified").trim();
                  if (indexModified != null) {
                    long lastModified = 0;
                    try {
                      lastModified = DateTools.stringToTime(indexModified);
                    } catch (ParseException e) {
                      // if modified time is not parsable, skip
                    }
                    if (lastModified == file.lastModified()) {
                      // TODO: remove existing document
                      indexIt = false;
                    }
                  }
                }
              }

              if (indexIt) {
                try {
                  log("Indexing " + file.getPath(), Project.MSG_VERBOSE);
                  Document doc = handler.getDocument(file);

                  if (doc == null) {
                    totalIgnored++;
                  } else {
                    // Add the path of the file as a field named "path".  Use a Keyword field, so
                    // that the index stores the path, and so that the path is searchable
                    doc.add(
                        new Field(
                            "path", file.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                    // Add the last modified date of the file a field named "modified".  Use a
                    // Keyword field, so that it's searchable, but so that no attempt is made
                    // to tokenize the field into words.
                    doc.add(
                        new Field(
                            "modified",
                            DateTools.timeToString(
                                file.lastModified(), DateTools.Resolution.MILLISECOND),
                            Field.Store.YES,
                            Field.Index.NOT_ANALYZED));

                    writer.addDocument(doc);
                    totalIndexed++;
                  }
                } catch (DocumentHandlerException e) {
                  throw new BuildException(e);
                }
              }
            }
            // for j
          }
          // if (fs != null)
        }
        // for i

        writer.optimize();
      }
      // try
      finally {
        // always make sure everything gets closed,
        // no matter how we exit.
        writer.close();
        if (searcher != null) {
          searcher.close();
        }
      }

      Date end = new Date();

      log(
          totalIndexed
              + " out of "
              + totalFiles
              + " indexed ("
              + totalIgnored
              + " ignored) in "
              + (end.getTime() - start.getTime())
              + " milliseconds");
    } finally {
      dir.close();
    }
  }
Example #9
0
  /**
   * Tells which sources should be reprocessed based on the last modification date of targets.
   *
   * @param logTo where to send (more or less) interesting output.
   * @param source ResourceCollection.
   * @param mapper filename mapper indicating how to find the target Resources.
   * @param targets object able to map a relative path as a Resource.
   * @param granularity The number of milliseconds leeway to give before deciding a target is out of
   *     date.
   * @return ResourceCollection.
   * @since Ant 1.7
   */
  public static ResourceCollection selectOutOfDateSources(
      ProjectComponent logTo,
      ResourceCollection source,
      FileNameMapper mapper,
      ResourceFactory targets,
      long granularity) {
    if (source.size() == 0) {
      logTo.log("No sources found.", Project.MSG_VERBOSE);
      return Resources.NONE;
    }
    source = Union.getInstance(source);
    logFuture(logTo, source, granularity);

    Union result = new Union();
    for (Iterator iter = source.iterator(); iter.hasNext(); ) {
      Resource sr = (Resource) iter.next();
      String srName = sr.getName();
      srName = srName == null ? srName : srName.replace('/', File.separatorChar);

      String[] targetnames = null;
      try {
        targetnames = mapper.mapFileName(srName);
      } catch (Exception e) {
        logTo.log("Caught " + e + " mapping resource " + sr, Project.MSG_VERBOSE);
      }
      if (targetnames == null || targetnames.length == 0) {
        logTo.log(sr + " skipped - don\'t know how to handle it", Project.MSG_VERBOSE);
        continue;
      }
      Union targetColl = new Union();
      for (int i = 0; i < targetnames.length; i++) {
        targetColl.add(targets.getResource(targetnames[i].replace(File.separatorChar, '/')));
      }
      // find the out-of-date targets:
      Restrict r = new Restrict();
      r.add(
          new And(
              new ResourceSelector[] {
                Type.FILE,
                new Or(new ResourceSelector[] {NOT_EXISTS, new Outdated(sr, granularity)})
              }));
      r.add(targetColl);
      if (r.size() > 0) {
        result.add(sr);
        Resource t = (Resource) (r.iterator().next());
        logTo.log(
            sr.getName()
                + " added as "
                + t.getName()
                + (t.isExists() ? " is outdated." : " doesn\'t exist."),
            Project.MSG_VERBOSE);
        continue;
      }
      // log uptodateness of all targets:
      logTo.log(
          sr.getName()
              + " omitted as "
              + targetColl.toString()
              + (targetColl.size() == 1 ? " is" : " are ")
              + " up to date.",
          Project.MSG_VERBOSE);
    }
    return result;
  }