Coverage Report - org.apache.maven.plugin.invoker.FileLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
FileLogger
82%
18/22
33%
2/6
1,571
FileLogger$1
80%
4/5
N/A
1,571
 
 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  4
     private boolean shouldFinalize = true;
 41  
 
 42  
     private final Log log;
 43  
 
 44  
     public FileLogger( File outputFile )
 45  
         throws IOException
 46  
     {
 47  4
         this( outputFile, null );
 48  4
     }
 49  
 
 50  
     public FileLogger( File outputFile, Log log )
 51  
         throws IOException
 52  4
     {
 53  4
         this.log = log;
 54  4
         stream = new PrintStream( new FileOutputStream( outputFile  ) );
 55  
 
 56  4
         Runnable finalizer = new Runnable()
 57  
         {
 58  4
             public void run()
 59  
             {
 60  
                 try
 61  
                 {
 62  2
                     finalize();
 63  
                 }
 64  0
                 catch ( Throwable e )
 65  
                 {
 66  
                     // ignore
 67  2
                 }
 68  2
             }
 69  
         };
 70  
 
 71  4
         Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) );
 72  4
     }
 73  
 
 74  
     public PrintStream getPrintStream()
 75  
     {
 76  4
         return stream;
 77  
     }
 78  
 
 79  
     public void consumeLine( String line )
 80  
     {
 81  54
         stream.println( line );
 82  54
         stream.flush();
 83  
 
 84  54
         if ( log != null )
 85  
         {
 86  0
             log.info( line );
 87  
         }
 88  54
     }
 89  
 
 90  
     public void close()
 91  
     {
 92  4
         if ( stream != null )
 93  
         {
 94  4
             stream.flush();
 95  
         }
 96  
 
 97  4
         IOUtil.close( stream );
 98  4
     }
 99  
 
 100  
     public void finalize()
 101  
     {
 102  0
         if ( shouldFinalize )
 103  
         {
 104  0
             close();
 105  
         }
 106  0
     }
 107  
 }