View Javadoc

1   // 
2   // Copyright (c) 2003,2004 , Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.logging;
29  
30  import java.io.BufferedWriter;
31  import java.io.IOException;
32  import java.io.Writer;
33  
34  import org.apache.log4j.FileAppender;
35  import org.apache.log4j.helpers.LogLog;
36  import org.objectledge.filesystem.FileSystem;
37  import org.objectledge.filesystem.UnsupportedCharactersInFilePathException;
38  
39  /***
40   * A derivate of log4j.FileAppender that accepts paths within Ledge file system.
41   *
42   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
43   * @version $Id: LedgeFileAppender.java,v 1.4 2004/12/22 08:34:55 rafal Exp $
44   */
45  public class LedgeFileAppender extends FileAppender
46  {
47      private FileSystem fileSystem;
48      
49      /***
50       * Creates new LedgeFileAppender instance.
51       * 
52       * @param fileSystem Ledge FileSystem object.
53       */
54      public LedgeFileAppender(FileSystem fileSystem)
55      {
56          this.fileSystem = fileSystem;
57      }
58  
59      /***
60       * <p>
61       * Sets and <i>opens </i> the file where the log output will go. The specified file must be
62       * writable.
63       * 
64       * <p>
65       * If there was already an opened file, then the previous file is closed first.
66       * 
67       * <p>
68       * <b>Do not use this method directly. To configure a FileAppender or one of its subclasses, set
69       * its properties one by one and then call activateOptions. </b>
70       * 
71       * @param fileName The path to the log file.
72       * @param append If true will append to fileName. Otherwise will truncate fileName.
73       * @param bufferedIO If true buffered IO will be used.
74       * @param bufferSize the size of buffer in bytes.
75       * @throws IOException if setting up the file IO fails.
76       */
77      public synchronized void setFile(String fileName, boolean append, boolean bufferedIO,
78          int bufferSize) throws IOException
79      {
80          LogLog.debug("setFile called: " + fileName + ", " + append);
81  
82          // It does not make sense to have immediate flush and bufferedIO.
83          if(bufferedIO)
84          {
85              setImmediateFlush(false);
86          }
87  
88          reset();
89          try
90          {
91              fileSystem.mkdirs(FileSystem.directoryPath(fileName));
92          }
93          catch(UnsupportedCharactersInFilePathException e)
94          {
95              throw (IOException) (new IOException().initCause(e));
96          }
97          Writer fw = createWriter(fileSystem.getOutputStream(fileName, append));
98          if(bufferedIO)
99          {
100             fw = new BufferedWriter(fw, bufferSize);
101         }
102         this.setQWForFiles(fw);
103         this.fileName = fileName;
104         this.fileAppend = append;
105         this.bufferedIO = bufferedIO;
106         this.bufferSize = bufferSize;
107         writeHeader();
108         LogLog.debug("setFile ended");
109     }    
110 }