bodewig     2004/11/02 08:19:48

  Modified:    src/main/org/apache/tools/ant/taskdefs Recorder.java
                        RecorderEntry.java
  Log:
  Make sure <record> lets go of files when stopped.
  
  PR: 26020
  Submitted by: Martijn Kruithof
  
  Revision  Changes    Path
  1.22      +11 -14    ant/src/main/org/apache/tools/ant/taskdefs/Recorder.java
  
  Index: Recorder.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Recorder.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Recorder.java     24 Jun 2004 11:16:41 -0000      1.21
  +++ Recorder.java     2 Nov 2004 16:19:48 -0000       1.22
  @@ -146,7 +146,15 @@
           RecorderEntry recorder = getRecorder(filename, getProject());
           // set the values on the recorder
           recorder.setMessageOutputLevel(loglevel);
  -        recorder.setRecordState(start);
  +        if (start != null) {
  +            if (start.booleanValue()) {
  +                recorder.reopenFile();
  +                recorder.setRecordState(start);
  +            } else {
  +                recorder.setRecordState(start);
  +                recorder.closeFile();
  +            }
  +        }
           recorder.setEmacsMode(emacsMode);
       }
   
  @@ -193,24 +201,13 @@
   
           if (o == null) {
               // create a recorder entry
  -            try {
                   entry = new RecorderEntry(name);
   
  -                PrintStream out = null;
  -
                   if (append == null) {
  -                    out = new PrintStream(
  -                        new FileOutputStream(name));
  +                    entry.openFile(false);
                   } else {
  -                    out = new PrintStream(
  -                        new FileOutputStream(name, append.booleanValue()));
  +                    entry.openFile(append.booleanValue());
                   }
  -                entry.setErrorPrintStream(out);
  -                entry.setOutputPrintStream(out);
  -            } catch (IOException ioe) {
  -                throw new BuildException("Problems creating a recorder 
entry",
  -                    ioe);
  -            }
               entry.setProject(proj);
               recorderEntries.put(name, entry);
           } else {
  
  
  
  1.19      +64 -7     
ant/src/main/org/apache/tools/ant/taskdefs/RecorderEntry.java
  
  Index: RecorderEntry.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/RecorderEntry.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- RecorderEntry.java        24 Jun 2004 11:16:41 -0000      1.18
  +++ RecorderEntry.java        2 Nov 2004 16:19:48 -0000       1.19
  @@ -16,8 +16,11 @@
    */
   package org.apache.tools.ant.taskdefs;
   
  +import java.io.FileOutputStream;
  +import java.io.IOException;
   import java.io.PrintStream;
   import org.apache.tools.ant.BuildEvent;
  +import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.BuildLogger;
   import org.apache.tools.ant.DefaultLogger;
   import org.apache.tools.ant.Project;
  @@ -28,7 +31,6 @@
    * This is a class that represents a recorder. This is the listener to the
    * build process.
    *
  - * @version 0.5
    * @since Ant 1.4
    */
   public class RecorderEntry implements BuildLogger, SubBuildListener {
  @@ -80,6 +82,7 @@
        */
       public void setRecordState(Boolean state) {
           if (state != null) {
  +            flush();
               record = state.booleanValue();
           }
       }
  @@ -93,6 +96,7 @@
       public void buildFinished(BuildEvent event) {
           log("< BUILD FINISHED", Project.MSG_DEBUG);
   
  +        if (record && out != null) {
           Throwable error = event.getException();
   
           if (error == null) {
  @@ -102,6 +106,7 @@
                    + StringUtils.LINE_SEP);
               error.printStackTrace(out);
           }
  +        }
           cleanup();
       }
   
  @@ -145,7 +150,7 @@
           String time = formatTime(System.currentTimeMillis() - 
targetStartTime);
   
           log(event.getTarget() + ":  duration " + time, Project.MSG_VERBOSE);
  -        out.flush();
  +        flush();
       }
   
   
  @@ -156,7 +161,7 @@
   
       public void taskFinished(BuildEvent event) {
           log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG);
  -        out.flush();
  +        flush();
       }
   
   
  @@ -191,10 +196,16 @@
        * @param level The verbosity level of the message.
        */
       private void log(String mesg, int level) {
  -        if (record && (level <= loglevel)) {
  +        if (record && (level <= loglevel) && out != null) {
               out.println(mesg);
           }
       }
  +    
  +    private void flush() {
  +        if (record && out != null) {
  +            out.flush();
  +        }
  +    }
   
   
       public void setMessageOutputLevel(int level) {
  @@ -205,6 +216,7 @@
   
   
       public void setOutputPrintStream(PrintStream output) {
  +        closeFile();
           out = output;
       }
   
  @@ -215,7 +227,7 @@
   
   
       public void setErrorPrintStream(PrintStream err) {
  -        out = err;
  +        setOutputPrintStream(err);
       }
   
   
  @@ -254,12 +266,57 @@
        * @since 1.6.2
        */
       public void cleanup() {
  -        out.flush();
  -        out.close();
  +        closeFile();
           if (project != null) {
               project.removeBuildListener(this);
           }
           project = null;
       }
  +    
  +    /**
  +     * Initially opens the file associated with this recorder.
  +     * Used by Recorder.
  +     * @param append Indicates if output must be appended to the logfile or 
that
  +     * the logfile should be overwritten.
  +     * @throws BuildException
  +     * @since 1.6.3
  +     */
  +    void openFile(boolean append) throws BuildException {
  +        openFileImpl(append);
  +    }
  +
  +    /**
  +     * Closes the file associated with this recorder.
  +     * Used by Recorder.
  +     * @since 1.6.3
  +     */
  +    void closeFile() {
  +        if (out != null) {
  +            out.close();
  +            out = null;
  +        }
  +    }
  +    
  +    /**
  +     * Re-opens the file associated with this recorder.
  +     * Used by Recorder.
  +     * @throws BuildException
  +     * @since 1.6.3
  +     */
  +    void reopenFile() throws BuildException {
  +        openFileImpl(true);
  +    }
  +
  +    private void openFileImpl(boolean append) throws BuildException {
  +        if (out == null) {
  +            try {
  +                out = new PrintStream(new FileOutputStream(filename, 
append));
  +            } catch (IOException ioe) {
  +                throw new BuildException("Problems opening file using a " +
  +                        "recorder entry", ioe);
  +            }
  +        }        
  +    }
  +
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to