Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ProxyHelper |
|
| 1.3333333333333333;1.333 |
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 | ||
18 | import java.lang.ref.SoftReference; | |
19 | import java.lang.ref.WeakReference; | |
20 | import java.lang.ref.Reference; | |
21 | ||
22 | import org.apache.ojb.broker.PBFactoryException; | |
23 | ||
24 | /** | |
25 | * ProxyHelper used to get the real thing behind a proxy | |
26 | * | |
27 | * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a> | |
28 | * @version $Id: ProxyHelper.java,v 1.1 2007-08-24 22:17:32 ewestfal Exp $ | |
29 | */ | |
30 | public class ProxyHelper | |
31 | { | |
32 | private static Reference proxyFactoryRef; | |
33 | ||
34 | public synchronized static ProxyFactory getProxyFactory() | |
35 | { | |
36 | if((proxyFactoryRef == null) || (proxyFactoryRef.get() == null)) | |
37 | { | |
38 | try | |
39 | { | |
40 | /* | |
41 | TODO: Check usage of WeakReference | |
42 | arminw: Changed Soft- to WeakReference. If in AbstractProxyFactory the | |
43 | the ProxyFactory instance is freed this class will take care of that. | |
44 | */ | |
45 | proxyFactoryRef = new WeakReference(AbstractProxyFactory.getProxyFactory()); | |
46 | } | |
47 | catch(PBFactoryException ex) | |
48 | { | |
49 | // seems we cannot get a broker; in that case we're defaulting to the CGLib proxy factory | |
50 | // (which also works for older JDKs) ie. for broker-less mode (only metadata) | |
51 | return new ProxyFactoryCGLIBImpl(); | |
52 | } | |
53 | } | |
54 | return (ProxyFactory) proxyFactoryRef.get(); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Get the real Object | |
59 | * | |
60 | * @param objectOrProxy | |
61 | * @return Object | |
62 | */ | |
63 | public static final Object getRealObject(Object objectOrProxy) | |
64 | { | |
65 | return getProxyFactory().getRealObject(objectOrProxy); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Get the real Object for already materialized Handler | |
70 | * | |
71 | * @param objectOrProxy | |
72 | * @return Object or null if the Handel is not materialized | |
73 | */ | |
74 | public static final Object getRealObjectIfMaterialized(Object objectOrProxy) | |
75 | { | |
76 | return getProxyFactory().getRealObjectIfMaterialized(objectOrProxy); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Get the real Class | |
81 | * | |
82 | * @param objectOrProxy | |
83 | * @return Class | |
84 | */ | |
85 | public static final Class getRealClass(Object objectOrProxy) | |
86 | { | |
87 | return getProxyFactory().getRealClass(objectOrProxy); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Determines whether the given object is an OJB proxy. | |
92 | * | |
93 | * @return <code>true</code> if the object is an OJB proxy | |
94 | */ | |
95 | public static boolean isNormalOjbProxy(Object proxyOrObject) | |
96 | { | |
97 | return getProxyFactory().isNormalOjbProxy(proxyOrObject); | |
98 | } | |
99 | ||
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 | } |