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