KickJava   Java API By Example, From Geeks To Geeks.

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


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.loader.EnvironmentLocal;
33
34 import java.lang.ref.SoftReference JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.logging.LogManager JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Implementation of the log manager.
43  */

44 public class LogManagerImpl extends LogManager JavaDoc {
45   private static final String JavaDoc LOG_LOCAL = "caucho.log.manager";
46
47   private static final HashMap JavaDoc<String JavaDoc,SoftReference JavaDoc<EnvironmentLogger>> _envLoggers
48     = new HashMap JavaDoc<String JavaDoc,SoftReference JavaDoc<EnvironmentLogger>>();
49
50   // custom loggers set by the user.
51
private static final EnvironmentLocal<HashMap JavaDoc<String JavaDoc,Logger JavaDoc>> _localLoggers
52     = new EnvironmentLocal<HashMap JavaDoc<String JavaDoc,Logger JavaDoc>>();
53     
54
55   public LogManagerImpl()
56   {
57   }
58
59   /**
60    * Adds a logger.
61    */

62   public synchronized boolean addLogger(Logger JavaDoc logger)
63   {
64     String JavaDoc name = logger.getName();
65
66     EnvironmentLogger envLogger = null;
67
68     SoftReference JavaDoc<EnvironmentLogger> loggerRef = _envLoggers.get(name);
69     if (loggerRef != null)
70       envLogger = loggerRef.get();
71
72     if (envLogger == null) {
73       envLogger = new EnvironmentLogger(name, logger.getResourceBundleName());
74
75       _envLoggers.put(name, new SoftReference JavaDoc<EnvironmentLogger>(envLogger));
76
77       EnvironmentLogger parent = buildParentTree(name);
78
79       if (parent != null)
80     envLogger.setParent(parent);
81     }
82
83     // handle custom logger
84
if (! logger.getClass().equals(Logger JavaDoc.class)) {
85       return envLogger.addLogger(logger);
86     }
87
88     return false;
89   }
90
91   /**
92    * Recursively builds the parents of the logger.
93    */

94   private EnvironmentLogger buildParentTree(String JavaDoc childName)
95   {
96     if (childName.equals(""))
97       return null;
98     
99     int p = childName.lastIndexOf('.');
100
101     String JavaDoc parentName;
102
103     if (p > 0)
104       parentName = childName.substring(0, p);
105     else
106       parentName = "";
107     
108     EnvironmentLogger parent = null;
109     
110     SoftReference JavaDoc<EnvironmentLogger> parentRef = _envLoggers.get(parentName);
111     if (parentRef != null)
112       parent = parentRef.get();
113
114     if (parent != null)
115       return parent;
116     else {
117       parent = new EnvironmentLogger(parentName, null);
118       _envLoggers.put(parentName, new SoftReference JavaDoc<EnvironmentLogger>(parent));
119
120       EnvironmentLogger grandparent = buildParentTree(parentName);
121
122       if (grandparent != null)
123         parent.setParent(grandparent);
124
125       return parent;
126     }
127   }
128
129   /**
130    * Returns the named logger.
131    */

132   public synchronized Logger JavaDoc getLogger(String JavaDoc name)
133   {
134     SoftReference JavaDoc<EnvironmentLogger> envLoggerRef = _envLoggers.get(name);
135     
136     EnvironmentLogger envLogger = null;
137     if (envLoggerRef != null)
138       envLogger = envLoggerRef.get();
139
140     if (envLogger == null)
141       return null;
142
143     Logger JavaDoc customLogger = envLogger.getLogger();
144
145     if (customLogger != null)
146       return customLogger;
147     else
148       return envLogger;
149   }
150
151   /**
152    * Returns an enumeration of loggers.
153    */

154   public Enumeration JavaDoc<String JavaDoc> getLoggerNames()
155   {
156     return Collections.enumeration(_envLoggers.keySet());
157   }
158
159   /**
160    * Returns an enumeration of loggers.
161    */

162   public void reset()
163   {
164   }
165 }
166
Popular Tags