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    
018    import java.lang.ref.SoftReference;
019    import java.lang.ref.WeakReference;
020    import java.lang.ref.Reference;
021    
022    import org.apache.ojb.broker.PBFactoryException;
023    
024    /**
025     * ProxyHelper used to get the real thing behind a proxy
026     *
027     * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
028     * @version $Id: ProxyHelper.java,v 1.1 2007-08-24 22:17:32 ewestfal Exp $
029     */
030    public class ProxyHelper
031    {
032        private static Reference proxyFactoryRef;
033    
034        public synchronized static ProxyFactory getProxyFactory()
035        {
036            if((proxyFactoryRef == null) || (proxyFactoryRef.get() == null))
037            {
038                try
039                {
040                    /*
041                    TODO: Check usage of WeakReference
042                    arminw: Changed Soft- to WeakReference. If in AbstractProxyFactory the
043                    the ProxyFactory instance is freed this class will take care of that.
044                    */
045                    proxyFactoryRef = new WeakReference(AbstractProxyFactory.getProxyFactory());
046                }
047                catch(PBFactoryException ex)
048                {
049                    // seems we cannot get a broker; in that case we're defaulting to the CGLib proxy factory
050                    // (which also works for older JDKs) ie. for broker-less mode (only metadata)
051                    return new ProxyFactoryCGLIBImpl();
052                }
053            }
054            return (ProxyFactory) proxyFactoryRef.get();
055        }
056    
057        /**
058         * Get the real Object
059         *
060         * @param objectOrProxy
061         * @return Object
062         */
063        public static final Object getRealObject(Object objectOrProxy)
064        {
065            return getProxyFactory().getRealObject(objectOrProxy);
066        }
067    
068        /**
069         * Get the real Object for already materialized Handler
070         *
071         * @param objectOrProxy
072         * @return Object or null if the Handel is not materialized
073         */
074        public static final Object getRealObjectIfMaterialized(Object objectOrProxy)
075        {
076            return getProxyFactory().getRealObjectIfMaterialized(objectOrProxy);
077        }
078    
079        /**
080         * Get the real Class
081         *
082         * @param objectOrProxy
083         * @return Class
084         */
085        public static final Class getRealClass(Object objectOrProxy)
086        {
087            return getProxyFactory().getRealClass(objectOrProxy);
088        }
089    
090        /**
091         * Determines whether the given object is an OJB proxy.
092         *
093         * @return <code>true</code> if the object is an OJB proxy
094         */
095        public static boolean isNormalOjbProxy(Object proxyOrObject)
096        {
097            return getProxyFactory().isNormalOjbProxy(proxyOrObject);
098        }
099    
100        /**
101         * Determines whether the given object is an OJB virtual proxy.
102         *
103         * @return <code>true</code> if the object is an OJB virtual proxy
104         */
105        public static boolean isVirtualOjbProxy(Object proxyOrObject)
106        {
107            return getProxyFactory().isVirtualOjbProxy(proxyOrObject);
108        }
109    
110        /**
111         * Returns <tt>true</tt> if the given object is a {@link java.lang.reflect.Proxy}
112         * or a {@link VirtualProxy} instance.
113         */
114        public static boolean isProxy(Object proxyOrObject)
115        {
116            return getProxyFactory().isProxy(proxyOrObject);
117        }
118    
119        /**
120         * Returns the invocation handler object of the given proxy object.
121         *
122         * @param obj The object
123         * @return The invocation handler if the object is an OJB proxy, or <code>null</code>
124         *         otherwise
125         */
126        public static IndirectionHandler getIndirectionHandler(Object obj)
127        {
128            return getProxyFactory().getIndirectionHandler(obj);
129        }
130    
131        /**
132         * Determines whether the object is a materialized object, i.e. no proxy or a
133         * proxy that has already been loaded from the database.
134         *
135         * @param object The object to test
136         * @return <code>true</code> if the object is materialized
137         */
138        public static boolean isMaterialized(Object object)
139        {
140            return getProxyFactory().isMaterialized(object);
141        }
142    
143        /**
144         * Materialization-safe version of toString. If the object is a yet-unmaterialized proxy,
145         * then only the text "unmaterialized proxy for ..." is returned and the proxy is NOT
146         * materialized. Otherwise, the normal toString method is called. This useful e.g. for
147         * logging etc.
148         *
149         * @param object The object for which a string representation shall be generated
150         * @return The string representation
151         */
152        public static String toString(Object object)
153        {
154            return getProxyFactory().toString(object);
155        }
156    
157        /** Return CollectionProxy for item is item is a CollectionProxy, otherwise return null */
158        public static CollectionProxy getCollectionProxy(Object item)
159        {
160            return getProxyFactory().getCollectionProxy(item);
161        }
162    
163        /** Reports if item is a CollectionProxy. */
164        public static boolean isCollectionProxy(Object item)
165        {
166            // TODO: Provide handling for pluggable collection proxy implementations
167            return getProxyFactory().isCollectionProxy(item);
168        }
169    }