KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > jmx > JMXProxyManager


1 /**
2  *
3  * Copyright 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 package org.apache.geronimo.kernel.jmx;
18
19 import java.util.IdentityHashMap JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import net.sf.cglib.proxy.Callback;
23 import net.sf.cglib.proxy.Enhancer;
24 import net.sf.cglib.proxy.MethodInterceptor;
25 import org.apache.geronimo.kernel.Kernel;
26 import org.apache.geronimo.kernel.basic.ProxyMethodInterceptor;
27 import org.apache.geronimo.kernel.proxy.ProxyFactory;
28 import org.apache.geronimo.kernel.proxy.ProxyManager;
29
30 /**
31  * @version $Rev$ $Date$
32  */

33 public class JMXProxyManager implements ProxyManager {
34     private final Kernel kernel;
35     private final IdentityHashMap JavaDoc interceptors = new IdentityHashMap JavaDoc();
36
37     public JMXProxyManager(Kernel kernel) {
38         this.kernel = kernel;
39     }
40
41     public synchronized ProxyFactory createProxyFactory(Class JavaDoc type) {
42         assert type != null: "type is null";
43         return new ManagedProxyFactory(type);
44     }
45
46     public synchronized Object JavaDoc createProxy(ObjectName JavaDoc target, Class JavaDoc type) {
47         assert type != null: "type is null";
48         assert target != null: "target is null";
49
50         return createProxyFactory(type).createProxy(target);
51     }
52
53     public synchronized void destroyProxy(Object JavaDoc proxy) {
54         if (proxy == null) {
55             return;
56         }
57
58         ProxyMethodInterceptor methodInterceptor = (ProxyMethodInterceptor) interceptors.remove(proxy);
59         if (methodInterceptor != null) {
60             methodInterceptor.destroy();
61         }
62     }
63
64     public boolean isProxy(Object JavaDoc proxy) {
65         return interceptors.containsKey(proxy);
66     }
67
68     public synchronized ObjectName JavaDoc getProxyTarget(Object JavaDoc proxy) {
69         ProxyMethodInterceptor methodInterceptor = (ProxyMethodInterceptor) interceptors.remove(proxy);
70         if (methodInterceptor == null) {
71             return null;
72         }
73         return methodInterceptor.getObjectName();
74     }
75
76     private class ManagedProxyFactory implements ProxyFactory {
77         private final Class JavaDoc type;
78         private final Enhancer enhancer;
79
80         public ManagedProxyFactory(Class JavaDoc type) {
81             enhancer = new Enhancer();
82             enhancer.setSuperclass(type);
83             enhancer.setCallbackType(MethodInterceptor.class);
84             enhancer.setUseFactory(false);
85             this.type = enhancer.createClass();
86         }
87
88         public synchronized Object JavaDoc createProxy(ObjectName JavaDoc target) {
89             assert target != null: "target is null";
90
91             JMXProxyMethodInterceptor interceptor = new JMXProxyMethodInterceptor(type, kernel, target);
92
93             // @todo trap CodeGenerationException indicating missing no-arg ctr
94
enhancer.setCallbacks(new Callback[]{interceptor});
95             Object JavaDoc proxy = enhancer.create();
96
97             interceptors.put(proxy, interceptor);
98             return proxy;
99         }
100     }
101 }
102
Popular Tags