package org.openas2.logging;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.openas2.OpenAS2Exception;
import org.openas2.Session;
import org.openas2.message.Message;
import org.openas2.params.CompositeParameters;
import org.openas2.params.DateParameters;
import org.openas2.params.ParameterParser;

public class FileLogger extends BaseLogger {
    public static final String PARAM_FILENAME = "filename";

    public void init(Session session, Map<String, String> parameters) throws OpenAS2Exception {
        super.init(session, parameters);
        // check if log file can be created
        getLogFile();
    }

    public void doLog(Level level, String msgText, Message as2Msg) {
        appendToFile(getFormatter().format(level, msgText + (as2Msg == null?"":as2Msg.getLogMsgID())));
    }

    protected String getShowDefaults() {
        return VALUE_SHOW_ALL;
    }

    protected void appendToFile(String text) {
        try {
            File logFile = getLogFile();
            FileWriter writer = new FileWriter(logFile, true);
            writer.write(text);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected File getLogFile() throws OpenAS2Exception {
        String filename = getParameter(PARAM_FILENAME, true);

        ParameterParser parser = createParser();
        filename = ParameterParser.parse(filename, parser);

        File logFile = new File(filename);

        if (!logFile.exists()) {
            File parentDir = logFile.getParentFile();
            if (!parentDir.exists()) {
                if (!parentDir.mkdirs()) {
                    String msg = "Could not create log directories for file \"" + logFile.getAbsolutePath()
                            + "\" for log file parameter \"" + filename + "\"";
                    throw new OpenAS2Exception(msg);
                }
            }
            try {
                if (!logFile.createNewFile()) {
                    String msg = "Could not create log file \"" + logFile.getAbsolutePath()
                            + "\" for log file parameter \"" + filename + "\"";
                    throw new OpenAS2Exception(msg);
                }
            } catch (IOException ioe) {
                String msg = "Could not create log file \"" + logFile.getAbsolutePath()
                        + "\" for log file parameter \"" + filename + "\": " + ioe.getMessage();
                throw new OpenAS2Exception(msg, ioe);
            }
        }

        return logFile;
    }

    protected ParameterParser createParser() {
        CompositeParameters compParams = new CompositeParameters(false);
        compParams.add("date", new DateParameters());

        return compParams;
    }

    protected void doLog(Throwable t, boolean terminated) {
        appendToFile(getFormatter().format(t, terminated));
    }
}
@@ -43,24 +43,58 @@
 a.io.File;%0D%0A
+import java.io.FileOutputStream;%0D%0A
 import java.
@@ -109,16 +109,16 @@
 riter;%0D%0A
-
 import j
@@ -510,16 +510,78 @@
 ename%22;%0D
+%0A    %0D%0A    private final Object fileWriteLock = new Object();%0D
 %0A%0D%0A    p
@@ -1114,191 +1114,281 @@
     
+%09%0D%0A
     
-try %7B%0D%0A            File logFile = getLogFile();%0D%0A            FileWriter writer = new FileWriter(l
+%09final byte%5B%5D msg = text.getBytes();%0D%0A    %09// one thread might have to wait a long time (several seconds) if it is busy.%0D%0A    %09synchronized(fileWriteLock) %7B %0D%0A    %09%09try (FileOutputStream fos = new FileOutputStream(getL
 ogFile
+()
 , true)
-;
+) %7B
 %0D%0A    
-        writer.write(text);%0D%0A            writer.close(
+%09%09%09fos.write(msg
 );%0D%0A    
     
@@ -1379,28 +1379,26 @@
 (msg);%0D%0A    
-    
+%09%09
 %7D catch (Exc
@@ -1414,24 +1414,19 @@
  %7B%0D%0A    
-        
+%09%09%09
 e.printS
@@ -1439,28 +1439,34 @@
 ace();%0D%0A    
+%09%09%7D%0D%0A
     
+%09
 %7D%0D%0A    %7D%0D%0A%0D%0A

Synchronize writes to log-file. (#109)