/**
  * Fired when a task finishes building, this adds the time taken and any error stacktrace to the
  * appropriate task element in the log.
  *
  * @param event An event with any relevant extra information. Will not be <code>null</code>.
  */
 public void taskFinished(BuildEvent event) {
   Task task = event.getTask();
   TimedElement taskElement = (TimedElement) tasks.get(task);
   if (taskElement != null) {
     long totalTime = System.currentTimeMillis() - taskElement.startTime;
     // taskElement.element.setAttribute(TIME_ATTR,
     // DefaultLogger.formatTime(totalTime));
     Target target = task.getOwningTarget();
     TimedElement targetElement = null;
     if (target != null) {
       targetElement = (TimedElement) targets.get(target);
     }
     if (targetElement == null) {
       buildElement.element.appendChild(taskElement.element);
     } else {
       targetElement.element.appendChild(taskElement.element);
     }
     Stack threadStack = getStack();
     if (!threadStack.empty()) {
       TimedElement poppedStack = (TimedElement) threadStack.pop();
       if (poppedStack != taskElement) {
         throw new RuntimeException(
             "Mismatch - popped element = "
                 + poppedStack
                 + " finished task element = "
                 + taskElement);
       }
     }
     tasks.remove(task);
   } else {
     throw new RuntimeException("Unknown task " + task + " not in " + tasks);
   }
 }
  /**
   * Fired when a target finishes building, this adds the time taken and any error stacktrace to the
   * appropriate target element in the log.
   *
   * @param event An event with any relevant extra information. Will not be <code>null</code>.
   */
  public void targetFinished(BuildEvent event) {
    System.out.println("target finished in cruise control HeliumCCLogger");
    Target target = event.getTarget();
    TimedElement targetElement = (TimedElement) targets.get(target);
    if (targetElement != null) {
      long totalTime = System.currentTimeMillis() - targetElement.startTime;
      // targetElement.element.setAttribute(TIME_ATTR,
      // DefaultLogger.formatTime(totalTime));

      TimedElement parentElement = null;
      Stack threadStack = getStack();
      if (!threadStack.empty()) {
        TimedElement poppedStack = (TimedElement) threadStack.pop();
        if (poppedStack != targetElement) {
          throw new RuntimeException(
              "Mismatch - popped element = "
                  + poppedStack
                  + " finished target element = "
                  + targetElement);
        }
        if (!threadStack.empty()) {
          parentElement = (TimedElement) threadStack.peek();
        }
      }
      if (parentElement == null) {
        buildElement.element.appendChild(targetElement.element);
      } else {
        parentElement.element.appendChild(targetElement.element);
      }
    }
    targets.remove(target);
  }
  /** @throws Exception If failed. */
  public void testAntGarTaskToString() throws Exception {
    String tmpDirName = GridTestProperties.getProperty("ant.gar.tmpdir");
    String srcDirName = GridTestProperties.getProperty("ant.gar.srcdir");
    String baseDirName = tmpDirName + File.separator + System.currentTimeMillis() + "_6";
    String metaDirName = baseDirName + File.separator + "META-INF";
    String garFileName = baseDirName + ".gar";

    // Make base and META-INF dir.
    boolean mkdir = new File(baseDirName).mkdirs();

    assert mkdir;

    mkdir = new File(metaDirName).mkdirs();

    assert mkdir;

    // Copy files to basedir
    U.copy(new File(srcDirName), new File(baseDirName), true);

    IgniteDeploymentGarAntTask garTask = new IgniteDeploymentGarAntTask();

    Project garProject = new Project();

    garProject.setName("Gar test project");

    garTask.setDestFile(new File(garFileName));
    garTask.setBasedir(new File(garFileName));
    garTask.setProject(garProject);
    garTask.setDescrdir(new File(garFileName));

    garTask.toString();
  }
Beispiel #4
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;
  }
 /**
  * Fired when a target starts building, this pushes a timed element for the target onto the stack
  * of elements for the current thread, remembering the current time and the name of the target.
  *
  * @param event An event with any relevant extra information. Will not be <code>null</code>.
  */
 public void targetStarted(BuildEvent event) {
   System.out.println("target started in cruise control HeliumCCLogger");
   Target target = event.getTarget();
   TimedElement targetElement = new TimedElement();
   targetElement.startTime = System.currentTimeMillis();
   targetElement.element = doc.createElement(TARGET_TAG);
   targetElement.element.setAttribute(NAME_ATTR, target.getName());
   targets.put(target, targetElement);
   getStack().push(targetElement);
 }
Beispiel #6
0
  /**
   * Protected method for simplyfied addinng of mapped files
   *
   * @param file file to add
   * @param baseDir base directory for file
   */
  protected void addMappedFile(File file, File baseDir) {
    if (file.isFile()) {
      if (baseDir == null) {
        baseDir = file.getParentFile();
      }
      MappedFile mappedFile = new MappedFile();
      mappedFile.setFrom(file);
      String filename = file.getName();

      String[] names = getMapper().mapFileName(filename);
      // we don't use original filename if no mapping is available
      if (names == null || names.length == 0) {
        return;
      }

      File newFile = new File(file.getParent() + System.getProperty("file.separator") + names[0]);
      if (getDestDir() != null) {
        try {
          newFile =
              new File(
                  newFile
                      .getCanonicalPath()
                      .replace(baseDir.getCanonicalPath(), getDestDir().getCanonicalPath()));
        } catch (IOException ex) {
          log("Couldn't map file", ex, Project.MSG_WARN);
          return;
        }
      }
      mappedFile.setTo(newFile);
      mappedFiles.add(mappedFile);
    } else if (file.isDirectory()) {
      if (baseDir == null) {
        baseDir = file;
      }
      DirectoryScanner ds = new DirectoryScanner();
      ds.setBasedir(file);
      ds.scan();

      for (String fileName : ds.getIncludedFiles()) {
        addMappedFile(new File(file + System.getProperty("file.separator") + fileName), baseDir);
      }
    }
  }
  /**
   * Fired when a task starts building, this pushes a timed element for the task onto the stack of
   * elements for the current thread, remembering the current time and the name of the task.
   *
   * @param event An event with any relevant extra information. Will not be <code>null</code>.
   */
  public void taskStarted(BuildEvent event) {
    TimedElement taskElement = new TimedElement();
    taskElement.startTime = System.currentTimeMillis();
    taskElement.element = doc.createElement(TASK_TAG);

    Task task = event.getTask();
    String name = event.getTask().getTaskName();
    if (name == null) {
      name = "";
    }
    taskElement.element.setAttribute(NAME_ATTR, name);
    taskElement.element.setAttribute(LOCATION_ATTR, event.getTask().getLocation().toString());
    tasks.put(task, taskElement);
    getStack().push(taskElement);
  }
  /** @throws Exception If failed. */
  @SuppressWarnings({"TypeMayBeWeakened"})
  public void testCorrectAntGarTask() throws Exception {
    String tmpDirName = GridTestProperties.getProperty("ant.gar.tmpdir");
    String srcDirName = GridTestProperties.getProperty("ant.gar.srcdir");
    String baseDirName = tmpDirName + File.separator + System.currentTimeMillis() + "_0";
    String metaDirName = baseDirName + File.separator + "META-INF";
    String garFileName = baseDirName + ".gar";
    String garDescDirName =
        U.resolveIgnitePath(GridTestProperties.getProperty("ant.gar.descriptor.dir"))
                .getAbsolutePath()
            + File.separator
            + "ignite.xml";

    // Make base and META-INF dir.
    boolean mkdir = new File(baseDirName).mkdirs();

    assert mkdir;

    mkdir = new File(metaDirName).mkdirs();

    assert mkdir;

    // Make Gar file
    U.copy(new File(garDescDirName), new File(metaDirName + File.separator + "ignite.xml"), true);

    // Copy files to basedir
    U.copy(new File(srcDirName), new File(baseDirName), true);

    IgniteDeploymentGarAntTask garTask = new IgniteDeploymentGarAntTask();

    Project garProject = new Project();

    garProject.setName("Gar test project");

    garTask.setDestFile(new File(garFileName));
    garTask.setBasedir(new File(baseDirName));
    garTask.setProject(garProject);

    garTask.execute();

    File garFile = new File(garFileName);

    assert garFile.exists();

    boolean res = checkStructure(garFile, true);

    assert res;
  }
  /** @throws Exception If failed. */
  public void testAntGarTaskWithDoubleP2PDescriptor() throws Exception {
    String tmpDirName = GridTestProperties.getProperty("ant.gar.tmpdir");
    String srcDirName = GridTestProperties.getProperty("ant.gar.srcdir");
    String baseDirName = tmpDirName + File.separator + System.currentTimeMillis() + "_2";
    String metaDirName = baseDirName + File.separator + "META-INF";
    String garFileName = baseDirName + ".gar";
    String garDescrDirName =
        U.resolveIgnitePath(GridTestProperties.getProperty("ant.gar.descriptor.dir"))
                .getAbsolutePath()
            + File.separator
            + "ignite.xml";

    // Make base and META-INF dir.
    boolean mkdir = new File(baseDirName).mkdirs();

    assert mkdir;

    mkdir = new File(metaDirName).mkdirs();

    assert mkdir;

    // Make Gar file
    U.copy(new File(garDescrDirName), new File(metaDirName + File.separator + "ignite.xml"), true);

    // Copy files to basedir
    U.copy(new File(srcDirName), new File(baseDirName), true);

    IgniteDeploymentGarAntTask garTask = new IgniteDeploymentGarAntTask();

    Project garProject = new Project();

    garProject.setName("Gar test project");

    garTask.setDestFile(new File(garFileName));
    garTask.setDescrdir(new File(garDescrDirName));
    garTask.setBasedir(new File(baseDirName));
    garTask.setProject(garProject);

    try {
      garTask.execute();

      assert false;
    } catch (BuildException e) {
      if (log().isInfoEnabled()) log().info(e.getMessage());
    }
  }
 public void testspawn() {
   project.executeTarget("init");
   if (project.getProperty("test.can.run") == null) {
     return;
   }
   myBuild = new MonitoredBuild(new File(System.getProperty("root"), BUILD_FILE), "spawn");
   logFile = FILE_UTILS.createTempFile("spawn", "log", project.getBaseDir(), false, false);
   // this is guaranteed by FileUtils#createTempFile
   assertTrue("log file not existing", !logFile.exists());
   // make the spawned process run 4 seconds
   myBuild.setTimeToWait(TIME_TO_WAIT);
   myBuild.setLogFile(logFile.getAbsolutePath());
   myBuild.addBuildListener(new MonitoredBuildListener());
   myBuild.start();
   GregorianCalendar startwait = new GregorianCalendar();
   // this loop runs parallel to the build
   while (!buildFinished) {
     try {
       Thread.sleep(10);
     } catch (InterruptedException e) {
       System.out.println("my sleep was interrupted");
     }
     GregorianCalendar now = new GregorianCalendar();
     // security
     if (now.getTime().getTime() - startwait.getTime().getTime() > MAX_BUILD_TIME) {
       System.out.println(
           "aborting wait, too long "
               + (now.getTime().getTime() - startwait.getTime().getTime())
               + "milliseconds");
       break;
     }
   }
   // now wait until the spawned process is finished
   try {
     Thread.sleep((TIME_TO_WAIT) * 1000 + SECURITY_MARGIN);
   } catch (InterruptedException e) {
     System.out.println("my sleep was interrupted");
   }
   // time of the build in milli seconds
   long elapsed = myBuild.getTimeElapsed();
   assertTrue(
       "we waited more than the process lasted", TIME_TO_WAIT * 1000 + SECURITY_MARGIN > elapsed);
   logFile = new File(logFile.getAbsolutePath());
   assertTrue("log file found after spawn", logFile.exists());
 }
  /**
   * Fired when a message is logged, this adds a message element to the most appropriate parent
   * element (task, target or build) and records the priority and text of the message.
   *
   * @param event An event with any relevant extra information. Will not be <code>null</code>.
   */
  public void messageLogged(BuildEvent event) {

    if (buildElement == null) {
      buildElement = new TimedElement();
      buildElement.startTime = System.currentTimeMillis();
      buildElement.element = doc.createElement(BUILD_TAG);
    }
    int priority = event.getPriority();
    if (priority > msgOutputLevel) {
      return;
    }
    Element messageElement = doc.createElement(MESSAGE_TAG);

    String name = "debug";
    switch (event.getPriority()) {
      case MSG_ERR:
        name = "error";
        break;
      case MSG_WARN:
        name = "warn";
        break;
      case MSG_INFO:
        name = "info";
        break;
      default:
        name = "debug";
        break;
    }
    messageElement.setAttribute(PRIORITY_ATTR, name);

    Throwable ex = event.getException();
    if (MSG_DEBUG <= msgOutputLevel && ex != null) {
      Text errText = doc.createCDATASection(StringUtils.getStackTrace(ex));
      Element stacktrace = doc.createElement(STACKTRACE_TAG);
      stacktrace.appendChild(errText);
      buildElement.element.appendChild(stacktrace);
    }
    Text messageText = doc.createCDATASection(event.getMessage());
    messageElement.appendChild(messageText);

    TimedElement parentElement = null;

    Task task = event.getTask();

    Target target = event.getTarget();
    if (task != null) {
      parentElement = getTaskElement(task);
    }
    if (parentElement == null && target != null) {
      parentElement = (TimedElement) targets.get(target);
    }

    /*
     * if (parentElement == null) { Stack threadStack = (Stack)
     * threadStacks.get(Thread.currentThread()); if (threadStack != null) {
     * if (!threadStack.empty()) { parentElement = (TimedElement)
     * threadStack.peek(); } } }
     */

    if (parentElement != null) {
      parentElement.element.appendChild(messageElement);
    } else {
      buildElement.element.appendChild(messageElement);
    }
  }
  /**
   * Fired when the build finishes, this adds the time taken and any error stacktrace to the build
   * element and writes the document to disk.
   *
   * @param event An event with any relevant extra information. Will not be <code>null</code>.
   */
  public void buildFinished(BuildEvent event) {
    long totalTime = System.currentTimeMillis() - buildElement.startTime;
    // buildElement.element.setAttribute(TIME_ATTR,
    // DefaultLogger.formatTime(totalTime));

    System.out.println("Build finished in cruise control HeliumCCLogger");

    if (event.getException() != null) {
      System.out.println("Build finished exception occured in cruise control HeliumCCLogger");
      System.out.println("Build finished exception is:-----" + event.getException().toString());
      buildElement.element.setAttribute(ERROR_ATTR, event.getException().toString());
      // print the stacktrace in the build file it is always useful...
      // better have too much info than not enough.
      Throwable t = event.getException();
      Text errText = doc.createCDATASection(StringUtils.getStackTrace(t));
      Element stacktrace = doc.createElement(STACKTRACE_TAG);
      stacktrace.appendChild(errText);
      buildElement.element.appendChild(stacktrace);
    }
    String outFilename = event.getProject().getProperty("HeliumCCLogger.file");
    System.out.println("Build finished writing to log file1" + outFilename);
    if (outFilename == null) {
      outFilename = "log.xml";
    }
    System.out.println("Build finished writing to log file2");
    String xslUri = event.getProject().getProperty("ant.HeliumCCLogger.stylesheet.uri");
    if (xslUri == null) {
      xslUri = "log.xsl";
    }
    System.out.println("Build finished writing to log file3");
    Writer out = null;
    try {
      // specify output in UTF8 otherwise accented characters will blow
      // up everything
      System.out.println("Build finished writing to log file3");

      OutputStream stream = outStream;
      if (stream == null) {
        stream = new FileOutputStream(outFilename);
      }
      out = new OutputStreamWriter(stream, "UTF8");
      System.out.println("Build finished writing to log file4");

      out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
      if (xslUri.length() > 0) {
        out.write("<?xml-stylesheet type=\"text/xsl\" href=\"" + xslUri + "\"?>\n\n");
      }
      System.out.println("Build finished writing to log file5");
      (new DOMElementWriter()).write(buildElement.element, out, 0, "\t");
      out.flush();
    } catch (IOException exc) {
      System.out.println("Build finished writing to log file6");
      throw new BuildException("Unable to write log file", exc);
    } finally {
      System.out.println("Build finished writing to log file7");

      if (out != null) {
        try {
          out.close();
        } catch (IOException e) {
          // ignore
          e = null;
        }
      }
    }
    buildElement = null;
  }
Beispiel #13
0
  /**
   * Construct the command line for parallel execution.
   *
   * @param srcFiles The filenames to add to the commandline
   * @param baseDir filenames are relative to this dir
   */
  protected String[] getCommandline(String[] srcFiles, File baseDir) {
    if (targetFilePos == null) {
      return super.getCommandline(srcFiles, baseDir);
    }

    Vector targets = new Vector();
    Hashtable addedFiles = new Hashtable();
    for (int i = 0; i < srcFiles.length; i++) {
      String[] subTargets = mapper.mapFileName(srcFiles[i]);
      if (subTargets != null) {
        for (int j = 0; j < subTargets.length; j++) {
          String name = (new File(destDir, subTargets[j])).getAbsolutePath();
          if (!addedFiles.contains(name)) {
            targets.addElement(name);
            addedFiles.put(name, name);
          }
        }
      }
    }
    String[] targetFiles = new String[targets.size()];
    targets.copyInto(targetFiles);

    String[] orig = cmdl.getCommandline();
    String[] result = new String[orig.length + srcFiles.length + targetFiles.length];

    int srcIndex = orig.length;
    if (srcFilePos != null) {
      srcIndex = srcFilePos.getPosition();
    }
    int targetIndex = targetFilePos.getPosition();

    if (srcIndex < targetIndex || (srcIndex == targetIndex && srcIsFirst)) {
      // 0 --> srcIndex
      System.arraycopy(orig, 0, result, 0, srcIndex);

      // srcIndex --> targetIndex
      System.arraycopy(orig, srcIndex, result, srcIndex + srcFiles.length, targetIndex - srcIndex);

      // targets are already absolute file names
      System.arraycopy(targetFiles, 0, result, targetIndex + srcFiles.length, targetFiles.length);

      // targetIndex --> end
      System.arraycopy(
          orig,
          targetIndex,
          result,
          targetIndex + srcFiles.length + targetFiles.length,
          orig.length - targetIndex);
    } else {
      // 0 --> targetIndex
      System.arraycopy(orig, 0, result, 0, targetIndex);

      // targets are already absolute file names
      System.arraycopy(targetFiles, 0, result, targetIndex, targetFiles.length);

      // targetIndex --> srcIndex
      System.arraycopy(
          orig, targetIndex, result, targetIndex + targetFiles.length, srcIndex - targetIndex);

      // srcIndex --> end
      System.arraycopy(
          orig,
          srcIndex,
          result,
          srcIndex + srcFiles.length + targetFiles.length,
          orig.length - srcIndex);
      srcIndex += targetFiles.length;
    }

    for (int i = 0; i < srcFiles.length; i++) {
      result[srcIndex + i] = (new File(baseDir, srcFiles[i])).getAbsolutePath();
    }
    return result;
  }