KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > PathHandler


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.log;
31
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.WriteStream;
35
36 import java.io.IOException JavaDoc;
37 import java.util.logging.Handler JavaDoc;
38 import java.util.logging.LogRecord JavaDoc;
39
40 /**
41  * Resin's rotating path-based log.
42  */

43 public class PathHandler extends Handler JavaDoc {
44   private static final L10N L = new L10N(PathHandler.class);
45   
46   private Path _path;
47   private String JavaDoc _encoding;
48
49   public PathHandler(Path path)
50   {
51     _path = path;
52   }
53
54   /**
55    * Publishes the record.
56    */

57   public void publish(LogRecord JavaDoc record)
58   {
59     if (record.getLevel().intValue() < getLevel().intValue())
60       return;
61
62     WriteStream os = null;
63     synchronized (_path) {
64       try {
65     try {
66       os = _path.openAppend();
67     } catch (Throwable JavaDoc e) {
68       _path.getParent().mkdirs();
69       os = _path.openAppend();
70     }
71
72     if (_encoding != null)
73       os.setEncoding(_encoding);
74
75     String JavaDoc msg = record.getMessage();
76     os.println(msg);
77       } catch (Throwable JavaDoc e) {
78     e.printStackTrace();
79       } finally {
80     try {
81       if (os != null)
82         os.close();
83     } catch (IOException JavaDoc e) {
84     }
85       }
86     }
87   }
88
89   /**
90    * Flushes the buffer.
91    */

92   public void flush()
93   {
94   }
95
96   /**
97    * Closes the handler.
98    */

99   public void close()
100   {
101   }
102
103   /**
104    * Returns the hash code.
105    */

106   public int hashCode()
107   {
108     return _path.hashCode();
109   }
110
111   /**
112    * Test for equality.
113    */

114   public boolean equals(Object JavaDoc o)
115   {
116     if (this == o)
117       return true;
118     else if (! (o instanceof PathHandler))
119       return false;
120
121     PathHandler handler = (PathHandler) o;
122
123     return _path.equals(handler._path);
124   }
125
126   public String JavaDoc toString()
127   {
128     return "PathHandler[" + _path + "]";
129   }
130 }
131
Popular Tags