1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Set;
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: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
32 */
33 public class GeronimoLogFactory extends LogFactory {
34 private final static Object factoryLock = new Object();
35
36 private static final HashMap instancesByClassLoader = new HashMap();
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
51 GeronimoLogFactory.logFactory = logFactory;
52
53
54 Set logs = getInstances();
55 for (Iterator iterator = logs.iterator(); iterator.hasNext();) {
56 GeronimoLog log = (GeronimoLog) iterator.next();
57 log.setLog(logFactory.getInstance(log.getName()));
58 }
59 }
60
61 public Set getInstances() {
62 synchronized (factoryLock) {
63 Set logs = new HashSet();
64 for (Iterator iterator = instancesByClassLoader.values().iterator(); iterator.hasNext();) {
65 Map instanceMap = ((Map) iterator.next());
66 logs.addAll(instanceMap.values());
67
68 }
69 return logs;
70 }
71 }
72
73 public Log getInstance(Class clazz) throws LogConfigurationException {
74 synchronized (factoryLock) {
75 return getInstance(clazz.getName());
76 }
77 }
78
79 public Log getInstance(String name) throws LogConfigurationException {
80 synchronized (factoryLock) {
81
82 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
83 Map instances = (Map) instancesByClassLoader.get(contextClassLoader);
84 if (instances == null) {
85 instances = new HashMap();
86 instancesByClassLoader.put(contextClassLoader, instances);
87 }
88
89
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
102
103
104
105
106
107
108
109
110 instancesByClassLoader.clear();
111 }
112 }
113
114 public Object getAttribute(String name) {
115 synchronized (factoryLock) {
116 return logFactory.getAttribute(name);
117 }
118 }
119
120 public String[] getAttributeNames() {
121 synchronized (factoryLock) {
122 return logFactory.getAttributeNames();
123 }
124 }
125
126 public void removeAttribute(String name) {
127 synchronized (factoryLock) {
128 logFactory.removeAttribute(name);
129 }
130 }
131
132 public void setAttribute(String name, Object value) {
133 synchronized (factoryLock) {
134 logFactory.setAttribute(name, value);
135 }
136 }
137 }
138