~ubuntu-branches/ubuntu/natty/tomcat6/natty-proposed

« back to all changes in this revision

Viewing changes to java/org/apache/juli/FileHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2010-05-21 13:51:15 UTC
  • mfrom: (2.2.12 sid)
  • Revision ID: james.westby@ubuntu.com-20100521135115-qfwnf24lzvi3644v
Tags: 6.0.26-2
* debian/tomcat6.{postinst,prerm}: Respect TOMCAT6_USER and TOMCAT6_GROUP
  as defined in /etc/default/tomcat6 when setting directory permissions and
  authbind configuration (Closes: #581018, LP: #557300)
* debian/tomcat6.postinst: Use group "tomcat6" instead of "adm" for
  permissions in /var/lib/tomcat6, so that group "adm" doesn't get write
  permissions over /var/lib/tomcat6/webapps (LP: #569118)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import java.io.PrintWriter;
27
27
import java.io.UnsupportedEncodingException;
28
28
import java.sql.Timestamp;
 
29
import java.util.concurrent.locks.ReadWriteLock;
 
30
import java.util.concurrent.locks.ReentrantReadWriteLock;
29
31
import java.util.logging.ErrorManager;
30
32
import java.util.logging.Filter;
31
33
import java.util.logging.Formatter;
40
42
 * named {prefix}.{date}.{suffix} in a configured directory, with an
41
43
 * optional preceding timestamp.
42
44
 *
43
 
 * @version $Revision: 899160 $ $Date: 2010-01-14 12:13:46 +0100 (Do, 14. Jan 2010) $
 
45
 * @version $Revision: 919742 $ $Date: 2010-03-06 12:15:07 +0100 (Sa, 06. Mär 2010) $
44
46
 */
45
47
 
46
48
public class FileHandler
96
98
     * The PrintWriter to which we are currently logging, if any.
97
99
     */
98
100
    private volatile PrintWriter writer = null;
99
 
    
100
 
    /**
101
 
     * Log buffer size
102
 
     */
103
 
    private int bufferSize = 0;
 
101
 
 
102
 
 
103
    /**
 
104
     * Lock used to control access to the writer.
 
105
     */
 
106
    protected ReadWriteLock writerLock = new ReentrantReadWriteLock();
 
107
 
 
108
 
 
109
    /**
 
110
     * Log buffer size.
 
111
     */
 
112
    private int bufferSize = -1;
104
113
 
105
114
 
106
115
    // --------------------------------------------------------- Public Methods
122
131
        String tsString = ts.toString().substring(0, 19);
123
132
        String tsDate = tsString.substring(0, 10);
124
133
 
 
134
        writerLock.readLock().lock();
125
135
        // If the date has changed, switch log files
126
136
        if (!date.equals(tsDate)) {
127
 
            synchronized (this) {
 
137
            // Update to writeLock before we switch
 
138
            writerLock.readLock().unlock();
 
139
            writerLock.writeLock().lock();
 
140
            try {
 
141
                // Make sure another thread hasn't already done this
128
142
                if (!date.equals(tsDate)) {
129
143
                    closeWriter();
130
144
                    date = tsDate;
131
145
                    openWriter();
132
146
                }
133
 
            }
134
 
        }
135
 
 
136
 
        String result = null;
137
 
        try {
138
 
            result = getFormatter().format(record);
139
 
        } catch (Exception e) {
140
 
            reportError(null, e, ErrorManager.FORMAT_FAILURE);
141
 
            return;
142
 
        }
143
 
        
144
 
        try {
145
 
            PrintWriter writer = this.writer;
146
 
            if (writer!=null) {
147
 
                writer.write(result);
148
 
                if (bufferSize < 0) {
149
 
                    writer.flush();
 
147
                // Down grade to read-lock. This ensures the writer remains valid
 
148
                // until the log message is written
 
149
                writerLock.readLock().lock();
 
150
            } finally {
 
151
                writerLock.writeLock().unlock();
 
152
            }
 
153
        }
 
154
 
 
155
        try {
 
156
            String result = null;
 
157
            try {
 
158
                result = getFormatter().format(record);
 
159
            } catch (Exception e) {
 
160
                reportError(null, e, ErrorManager.FORMAT_FAILURE);
 
161
                return;
 
162
            }
 
163
 
 
164
            try {
 
165
                if (writer!=null) {
 
166
                    writer.write(result);
 
167
                    if (bufferSize < 0) {
 
168
                        writer.flush();
 
169
                    }
 
170
                } else {
 
171
                    reportError("FileHandler is closed or not yet initialized, unable to log ["+result+"]", null, ErrorManager.WRITE_FAILURE);
150
172
                }
151
 
            } else {
152
 
                reportError("FileHandler is closed or not yet initialized, unable to log ["+result+"]", null, ErrorManager.WRITE_FAILURE);
 
173
            } catch (Exception e) {
 
174
                reportError(null, e, ErrorManager.WRITE_FAILURE);
 
175
                return;
153
176
            }
154
 
        } catch (Exception e) {
155
 
            reportError(null, e, ErrorManager.WRITE_FAILURE);
156
 
            return;
 
177
        } finally {
 
178
            writerLock.readLock().unlock();
157
179
        }
158
 
        
159
180
    }
160
181
    
161
182
    
171
192
 
172
193
    protected void closeWriter() {
173
194
        
 
195
        writerLock.writeLock().lock();
174
196
        try {
175
 
            PrintWriter writer = this.writer;
176
 
            this.writer = null;
177
197
            if (writer == null)
178
198
                return;
179
199
            writer.write(getFormatter().getTail(this));
183
203
            date = "";
184
204
        } catch (Exception e) {
185
205
            reportError(null, e, ErrorManager.CLOSE_FAILURE);
 
206
        } finally {
 
207
            writerLock.writeLock().unlock();
186
208
        }
187
 
        
188
209
    }
189
210
 
190
211
 
193
214
     */
194
215
    public void flush() {
195
216
 
 
217
        writerLock.readLock().lock();
196
218
        try {
197
 
            PrintWriter writer = this.writer;
198
 
            if (writer==null)
 
219
            if (writer == null)
199
220
                return;
200
221
            writer.flush();
201
222
        } catch (Exception e) {
202
223
            reportError(null, e, ErrorManager.FLUSH_FAILURE);
 
224
        } finally {
 
225
            writerLock.readLock().unlock();
203
226
        }
204
227
        
205
228
    }
225
248
            prefix = getProperty(className + ".prefix", "juli.");
226
249
        if (suffix == null)
227
250
            suffix = getProperty(className + ".suffix", ".log");
228
 
        String sBufferSize = getProperty(className + ".bufferSize", "8192");
 
251
        String sBufferSize = getProperty(className + ".bufferSize", String.valueOf(bufferSize));
229
252
        try {
230
253
            bufferSize = Integer.parseInt(sBufferSize);
231
254
        } catch (NumberFormatException ignore) {
297
320
        dir.mkdirs();
298
321
 
299
322
        // Open the current log file
 
323
        writerLock.writeLock().lock();
300
324
        try {
301
325
            String pathname = dir.getAbsolutePath() + File.separator +
302
326
                prefix + date + suffix;
310
334
        } catch (Exception e) {
311
335
            reportError(null, e, ErrorManager.OPEN_FAILURE);
312
336
            writer = null;
 
337
        } finally {
 
338
            writerLock.writeLock().unlock();
313
339
        }
314
340
 
315
341
    }