/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.connector.maven.internal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.concurrent.Callable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ProcessOutputReader implements Callable<String> {

    private Log log = LogFactory.getLog(getClass());

    private InputStream inputStream;

    private StringWriter writer;

    private OutputStreamWriter logFileWriter;

    public ProcessOutputReader(InputStream inputStream) {
        this.inputStream = inputStream;
        writer = new StringWriter();
    }

    public ProcessOutputReader(InputStream inputStream, File logFile) throws FileNotFoundException {
        this(inputStream);
        FileOutputStream logFileOutputStream = new FileOutputStream(logFile);
        logFileWriter = new OutputStreamWriter(logFileOutputStream);
    }

    @Override
    public String call() throws IOException {
        log.debug("starting reading inputstream");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            writer.append(line + "\n");
            if (logFileWriter != null) {
                logFileWriter.write(line + "\n");
                logFileWriter.flush();
            }
        }
        log.debug("inputstream has ended. returning result");
        return writer.toString();
    }
}

@@ -1079,16 +1079,221 @@
 ctory;%0A%0A
+/**%0A * Utility class writing an inputStream (mostly from an process) always into a string and optionally into a log file, if%0A * you use the %7B@link #ProcessOutputReader(InputStream, File)%7D constructor.%0A */%0A
 public c
@@ -2152,16 +2152,271 @@
 ream));%0A
+        readInputStream(bufferedReader);%0A        closeResources();%0A        log.debug(%22inputstream has ended. returning result%22);%0A        return writer.toString();%0A    %7D%0A%0A    private void readInputStream(BufferedReader bufferedReader) throws IOException %7B%0A
         
@@ -2525,32 +2525,143 @@
 ne + %22%5Cn%22);%0A    
+        readToLogFile(line);%0A        %7D%0A    %7D%0A%0A    private void readToLogFile(String line) throws IOException %7B%0A
         if (logF
@@ -2673,36 +2673,32 @@
 iter != null) %7B%0A
-    
             logF
@@ -2731,36 +2731,32 @@
 %22);%0A            
-    
 logFileWriter.fl
@@ -2774,23 +2774,71 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-    %7D
+private void closeResources() throws IOException %7B
 %0A   
@@ -2845,37 +2845,38 @@
      log.debug(%22
-i
+I
 nput
+ 
 stream has ended
@@ -2881,66 +2881,129 @@
 ed. 
-returning result%22);%0A        return writer.toString
+cleanup resources%22);%0A        writer.close();%0A        if (logFileWriter != null) %7B%0A            logFileWriter.close
 ();%0A    
 %7D%0A%7D%0A
@@ -2998,12 +2998,23 @@
 ();%0A    
+   
-%7D
+ %7D%0A    %7D%0A
 %0A%7D%0A

fix problem of not closing resource streams