View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import java.io.IOException;
20  import java.io.Writer;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.apache.logging.log4j.core.StringLayout;
24  
25  /**
26   * Manages a Writer so that it can be shared by multiple Appenders and will
27   * allow appenders to reconfigure without requiring a new writer.
28   */
29  public class WriterManager extends AbstractManager {
30  
31      /**
32       * Creates a Manager.
33       *
34       * @param name The name of the stream to manage.
35       * @param data The data to pass to the Manager.
36       * @param factory The factory to use to create the Manager.
37       * @param <T> The type of the WriterManager.
38       * @return A WriterManager.
39       */
40      public static <T> WriterManager getManager(final String name, final T data,
41                                                   final ManagerFactory<? extends WriterManager, T> factory) {
42          return AbstractManager.getManager(name, factory, data);
43      }
44      protected final StringLayout layout;
45  
46      private volatile Writer writer;
47  
48      public WriterManager(final Writer writer, final String streamName, final StringLayout layout,
49              final boolean writeHeader) {
50          super(null, streamName);
51          this.writer = writer;
52          this.layout = layout;
53          if (writeHeader && layout != null) {
54              final byte[] header = layout.getHeader();
55              if (header != null) {
56                  try {
57                      this.writer.write(new String(header, layout.getCharset()));
58                  } catch (final IOException e) {
59                      logError("Unable to write header", e);
60                  }
61              }
62          }
63      }
64  
65      protected synchronized void closeWriter() {
66          final Writer w = writer; // access volatile field only once per method
67          try {
68              w.close();
69          } catch (final IOException ex) {
70              logError("Unable to close stream", ex);
71          }
72      }
73  
74      /**
75       * Flushes any buffers.
76       */
77      public synchronized void flush() {
78          try {
79              writer.flush();
80          } catch (final IOException ex) {
81              final String msg = "Error flushing stream " + getName();
82              throw new AppenderLoggingException(msg, ex);
83          }
84      }
85  
86      protected Writer getWriter() {
87          return writer;
88      }
89  
90      /**
91       * Returns the status of the stream.
92       * @return true if the stream is open, false if it is not.
93       */
94      public boolean isOpen() {
95          return getCount() > 0;
96      }
97  
98      /**
99       * Default hook to write footer during close.
100      */
101     @Override
102     public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
103         writeFooter();
104         closeWriter();
105         return true;
106     }
107 
108     protected void setWriter(final Writer writer) {
109         final byte[] header = layout.getHeader();
110         if (header != null) {
111             try {
112                 writer.write(new String(header, layout.getCharset()));
113                 this.writer = writer; // only update field if writer.write() succeeded
114             } catch (final IOException ioe) {
115                 logError("Unable to write header", ioe);
116             }
117         } else {
118             this.writer = writer;
119         }
120     }
121 
122     /**
123      * Some output streams synchronize writes while others do not. Synchronizing here insures that
124      * log events won't be intertwined.
125      * @param str the string to write
126      * @throws AppenderLoggingException if an error occurs.
127      */
128     protected synchronized void write(final String str)  {
129         try {
130             writer.write(str);
131         } catch (final IOException ex) {
132             final String msg = "Error writing to stream " + getName();
133             throw new AppenderLoggingException(msg, ex);
134         }
135     }
136 
137     /**
138      * Writes the footer.
139      */
140     protected void writeFooter() {
141         if (layout == null) {
142             return;
143         }
144         final byte[] footer = layout.getFooter();
145         if (footer != null && footer.length > 0) {
146             write(new String(footer, layout.getCharset()));
147         }
148     }
149 }