01: package org.apache.ojb.broker.core.proxy;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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;
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 proxyFactories = new HashMap();
30:
31: public Class 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 getIndirectionHandlerBaseClass() {
40: return IndirectionHandlerCGLIB.class;
41: }
42:
43: public OJBProxy createProxy(Class proxyClass,
44: IndirectionHandler handler) throws Exception {
45:
46: Factory factory = (Factory) proxyFactories.get(proxyClass);
47: Object result = null;
48: if (factory == null) {
49: Class[] interfaces;
50: if (proxyClass.isInterface()) {
51: interfaces = new Class[] { proxyClass, OJBProxy.class };
52: } else {
53: interfaces = new Class[] { OJBProxy.class };
54: }
55:
56: result = (Factory) Enhancer.create(proxyClass, interfaces,
57: (Callback) handler);
58: proxyFactories.put(proxyClass, result);
59: } else {
60: result = factory.newInstance((Callback) handler);
61: }
62: return (OJBProxy) result;
63: }
64:
65: public boolean isNormalOjbProxy(Object proxyOrObject) {
66: return super .isNormalOjbProxy(proxyOrObject)
67: && (proxyOrObject instanceof Factory);
68: }
69:
70: public IndirectionHandler getDynamicIndirectionHandler(Object obj) {
71: return (IndirectionHandler) ((Factory) obj).getCallbacks()[0];
72:
73: }
74:
75: public boolean interfaceRequiredForProxyGeneration() {
76: return false;
77: }
78:
79: }
|