KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > core > proxy > ProxyFactoryCGLIBImpl


1 package org.apache.ojb.broker.core.proxy;
2
3 /* Copyright 2002-2005 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 import java.util.HashMap JavaDoc;
18
19 import net.sf.cglib.proxy.Callback;
20 import net.sf.cglib.proxy.Enhancer;
21 import net.sf.cglib.proxy.Factory;
22
23 /**
24  * @author andrew.clute
25  *
26  */

27 public class ProxyFactoryCGLIBImpl extends AbstractProxyFactory {
28
29     HashMap JavaDoc proxyFactories = new HashMap JavaDoc();
30
31     public Class JavaDoc getDefaultIndirectionHandlerClass() {
32         return IndirectionHandlerCGLIBImpl.class;
33     }
34
35     /**
36      * Returns the class of the base class that the given IndirectionHandler must extend/implement
37      *
38      */

39     public Class JavaDoc getIndirectionHandlerBaseClass() {
40         return IndirectionHandlerCGLIB.class;
41     }
42
43     public OJBProxy createProxy(Class JavaDoc proxyClass, IndirectionHandler handler) throws Exception JavaDoc {
44
45         Factory factory = (Factory)proxyFactories.get(proxyClass);
46         Object JavaDoc result = null;
47         if (factory == null) {
48             Class JavaDoc[] interfaces;
49             if (proxyClass.isInterface()) {
50                 interfaces = new Class JavaDoc[] { proxyClass, OJBProxy.class };
51             } else {
52                 interfaces = new Class JavaDoc[] { OJBProxy.class };
53             }
54
55             result = (Factory)Enhancer.create(proxyClass, interfaces, (Callback)handler);
56             proxyFactories.put(proxyClass, result);
57         } else {
58             result = factory.newInstance((Callback)handler);
59         }
60         return (OJBProxy)result;
61     }
62
63     public boolean isNormalOjbProxy(Object JavaDoc proxyOrObject) {
64         return super.isNormalOjbProxy(proxyOrObject) && (proxyOrObject instanceof Factory);
65     }
66
67     public IndirectionHandler getDynamicIndirectionHandler(Object JavaDoc obj) {
68         return (IndirectionHandler)((Factory)obj).getCallbacks()[0];
69
70     }
71
72     public boolean interfaceRequiredForProxyGeneration() {
73         return false;
74     }
75     
76     
77
78 }
79
Popular Tags