KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > tuscany > CommonsLoggingMonitorFactory


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 package org.apache.servicemix.sca.tuscany;
18
19 import java.lang.ref.WeakReference JavaDoc;
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.WeakHashMap JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.tuscany.common.monitor.MonitorFactory;
29
30 public class CommonsLoggingMonitorFactory implements MonitorFactory {
31
32     private final Map JavaDoc<Class JavaDoc<?>, WeakReference JavaDoc<?>> proxies = new WeakHashMap JavaDoc<Class JavaDoc<?>, WeakReference JavaDoc<?>>();
33     
34     public CommonsLoggingMonitorFactory() {
35     }
36
37     public <T> T getMonitor(Class JavaDoc<T> monitorInterface) {
38         T proxy = getCachedMonitor(monitorInterface);
39         if (proxy == null) {
40             proxy = createMonitor(monitorInterface);
41             proxies.put(monitorInterface, new WeakReference JavaDoc<T>(proxy));
42         }
43         return proxy;
44     }
45
46     private <T>T getCachedMonitor(Class JavaDoc<T> monitorInterface) {
47         WeakReference JavaDoc<T> ref = (WeakReference JavaDoc<T>) proxies.get(monitorInterface);
48         return (ref != null) ? ref.get() : null;
49     }
50
51     private <T>T createMonitor(Class JavaDoc<T> monitorInterface) {
52         String JavaDoc className = monitorInterface.getName();
53         Log logger = LogFactory.getLog(className);
54         InvocationHandler JavaDoc handler = new LoggingHandler(logger);
55         return (T) Proxy.newProxyInstance(monitorInterface.getClassLoader(), new Class JavaDoc<?>[]{monitorInterface}, handler);
56     }
57
58     private static final class LoggingHandler implements InvocationHandler JavaDoc {
59         private final Log logger;
60
61         public LoggingHandler(Log logger) {
62             this.logger = logger;
63         }
64
65         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
66             String JavaDoc sourceMethod = method.getName();
67             if (logger.isDebugEnabled()) {
68                 // if the only argument is a Throwable use the special logger for it
69
if (args != null && args.length == 1 && args[0] instanceof Throwable JavaDoc) {
70                     logger.debug(sourceMethod, (Throwable JavaDoc) args[0]);
71                 } else {
72                     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
73                     sb.append(sourceMethod);
74                     sb.append("(");
75                     for (int i = 0; i < args.length; i++) {
76                         if (i > 0) {
77                             sb.append(", ");
78                         }
79                         sb.append(args[i]);
80                     }
81                     sb.append(")");
82                     logger.debug(sb.toString());
83                 }
84             }
85             return null;
86         }
87     }
88 }
89
Popular Tags