001    package org.apache.ojb.broker.core.proxy;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    import java.util.HashMap;
018    
019    import net.sf.cglib.proxy.Callback;
020    import net.sf.cglib.proxy.Enhancer;
021    import net.sf.cglib.proxy.Factory;
022    
023    /**
024     * @author andrew.clute
025     *
026     */
027    public class ProxyFactoryCGLIBImpl extends AbstractProxyFactory {
028    
029        HashMap proxyFactories = new HashMap();
030    
031        public Class getDefaultIndirectionHandlerClass() {
032            return IndirectionHandlerCGLIBImpl.class;
033        }
034    
035        /**
036         * Returns the class of the base class that the given IndirectionHandler must extend/implement
037         * 
038         */
039        public Class getIndirectionHandlerBaseClass() {
040            return IndirectionHandlerCGLIB.class;
041        }
042    
043        public OJBProxy createProxy(Class proxyClass, IndirectionHandler handler) throws Exception {
044    
045            Factory factory = (Factory)proxyFactories.get(proxyClass);
046            Object result = null;
047            if (factory == null) {
048                Class[] interfaces;
049                if (proxyClass.isInterface()) {
050                    interfaces = new Class[] { proxyClass, OJBProxy.class };
051                } else {
052                    interfaces = new Class[] { OJBProxy.class };
053                }
054    
055                result = (Factory)Enhancer.create(proxyClass, interfaces, (Callback)handler);
056                proxyFactories.put(proxyClass, result);
057            } else {
058                result = factory.newInstance((Callback)handler);
059            }
060            return (OJBProxy)result;
061        }
062    
063        public boolean isNormalOjbProxy(Object proxyOrObject) {
064            return super.isNormalOjbProxy(proxyOrObject) && (proxyOrObject instanceof Factory);
065        }
066    
067        public IndirectionHandler getDynamicIndirectionHandler(Object obj) {
068            return (IndirectionHandler)((Factory)obj).getCallbacks()[0];
069    
070        }
071    
072        public boolean interfaceRequiredForProxyGeneration() {
073            return false;
074        }
075        
076        
077    
078    }