View Javadoc

1   package org.apache.maven.plugin.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.logging.Log;
23  import org.apache.maven.shared.invoker.InvocationOutputHandler;
24  import org.codehaus.plexus.util.IOUtil;
25  
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.io.PrintStream;
30  
31  /**
32   * @version $Id: FileLogger.java 655038 2008-05-10 10:12:02Z bentmann $
33   */
34  public class FileLogger
35      implements InvocationOutputHandler
36  {
37  
38      private PrintStream stream;
39  
40      private boolean shouldFinalize = true;
41  
42      private final Log log;
43  
44      public FileLogger( File outputFile )
45          throws IOException
46      {
47          this( outputFile, null );
48      }
49  
50      public FileLogger( File outputFile, Log log )
51          throws IOException
52      {
53          this.log = log;
54          stream = new PrintStream( new FileOutputStream( outputFile  ) );
55  
56          Runnable finalizer = new Runnable()
57          {
58              public void run()
59              {
60                  try
61                  {
62                      finalize();
63                  }
64                  catch ( Throwable e )
65                  {
66                      // ignore
67                  }
68              }
69          };
70  
71          Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) );
72      }
73  
74      public PrintStream getPrintStream()
75      {
76          return stream;
77      }
78  
79      public void consumeLine( String line )
80      {
81          stream.println( line );
82          stream.flush();
83  
84          if ( log != null )
85          {
86              log.info( line );
87          }
88      }
89  
90      public void close()
91      {
92          if ( stream != null )
93          {
94              stream.flush();
95          }
96  
97          IOUtil.close( stream );
98      }
99  
100     public void finalize()
101     {
102         if ( shouldFinalize )
103         {
104             close();
105         }
106     }
107 }