KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > log > GeronimoLogFactory


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
18 package org.apache.geronimo.kernel.log;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogConfigurationException;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class GeronimoLogFactory extends LogFactory {
34     private final static Object JavaDoc factoryLock = new Object JavaDoc();
35     // todo this should use weak references
36
private static final HashMap JavaDoc instancesByClassLoader = new HashMap JavaDoc();
37
38     private static LogFactory logFactory = new BootstrapLogFactory();
39
40     public GeronimoLogFactory() {
41     }
42
43     public LogFactory getLogFactory() {
44         synchronized (factoryLock) {
45             return logFactory;
46         }
47     }
48
49     public void setLogFactory(LogFactory logFactory) {
50         // change the log factory
51
GeronimoLogFactory.logFactory = logFactory;
52
53         // update all known logs to use instances of the new factory
54
Set JavaDoc logs = getInstances();
55         for (Iterator JavaDoc iterator = logs.iterator(); iterator.hasNext();) {
56             GeronimoLog log = (GeronimoLog) iterator.next();
57             log.setLog(logFactory.getInstance(log.getName()));
58         }
59     }
60
61     public Set JavaDoc getInstances() {
62         synchronized (factoryLock) {
63             Set JavaDoc logs = new HashSet JavaDoc();
64             for (Iterator JavaDoc iterator = instancesByClassLoader.values().iterator(); iterator.hasNext();) {
65                 Map JavaDoc instanceMap = ((Map JavaDoc) iterator.next());
66                 logs.addAll(instanceMap.values());
67
68             }
69             return logs;
70         }
71     }
72
73     public Log getInstance(Class JavaDoc clazz) throws LogConfigurationException {
74         synchronized (factoryLock) {
75             return getInstance(clazz.getName());
76         }
77     }
78
79     public Log getInstance(String JavaDoc name) throws LogConfigurationException {
80         synchronized (factoryLock) {
81             // get the instances for the context classloader
82
ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
83             Map JavaDoc instances = (Map JavaDoc) instancesByClassLoader.get(contextClassLoader);
84             if (instances == null) {
85                 instances = new HashMap JavaDoc();
86                 instancesByClassLoader.put(contextClassLoader, instances);
87             }
88
89             // get the log
90
Log log = (Log) instances.get(name);
91             if (log == null) {
92                 log = new GeronimoLog(name, logFactory.getInstance(name));
93                 instances.put(name, log);
94             }
95             return log;
96         }
97     }
98
99     public void release() {
100         synchronized (factoryLock) {
101 // TODO rethink this - it works for now
102
// for (Iterator maps = instancesByClassLoader.values().iterator(); maps.hasNext();) {
103
// Map instances = (Map) maps.next();
104
// for (Iterator logs = instances.values().iterator(); logs.hasNext();) {
105
// GeronimoLog log = (GeronimoLog) logs.next();
106
// log.setLog(null);
107
//
108
// }
109
// }
110
instancesByClassLoader.clear();
111         }
112     }
113
114     public Object JavaDoc getAttribute(String JavaDoc name) {
115         synchronized (factoryLock) {
116             return logFactory.getAttribute(name);
117         }
118     }
119
120     public String JavaDoc[] getAttributeNames() {
121         synchronized (factoryLock) {
122             return logFactory.getAttributeNames();
123         }
124     }
125
126     public void removeAttribute(String JavaDoc name) {
127         synchronized (factoryLock) {
128             logFactory.removeAttribute(name);
129         }
130     }
131
132     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
133         synchronized (factoryLock) {
134             logFactory.setAttribute(name, value);
135         }
136     }
137 }
138
139
Popular Tags