arminw 2005/09/29 10:21:20
Modified: src/java/org/apache/ojb/broker/core/proxy Tag:
OJB_1_0_RELEASE ProxyHelper.java VirtualProxy.java
src/java/org/apache/ojb/broker/util/collections Tag:
OJB_1_0_RELEASE ManageableHashMap.java
Log:
don't use the default PB instance in OJB's code base (except as fallback),
because the 'default' PB is only optional and can return the wrong backend DB
Revision Changes Path
No revision
No revision
1.2.2.6 +39 -40
db-ojb/src/java/org/apache/ojb/broker/core/proxy/Attic/ProxyHelper.java
Index: ProxyHelper.java
===================================================================
RCS file:
/home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/Attic/ProxyHelper.java,v
retrieving revision 1.2.2.5
retrieving revision 1.2.2.6
diff -u -r1.2.2.5 -r1.2.2.6
--- ProxyHelper.java 5 Sep 2005 10:59:30 -0000 1.2.2.5
+++ ProxyHelper.java 29 Sep 2005 17:21:19 -0000 1.2.2.6
@@ -15,43 +15,36 @@
* limitations under the License.
*/
-import org.apache.ojb.broker.PBFactoryException;
-import org.apache.ojb.broker.PersistenceBrokerFactory;
-import org.apache.ojb.broker.PersistenceBrokerInternal;
-
import java.lang.ref.SoftReference;
+import org.apache.ojb.broker.PBFactoryException;
+
/**
* ProxyHelper used to get the real thing behind a proxy
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jakob Braeuchi</a>
* @version $Id$
*/
-public class ProxyHelper {
+public class ProxyHelper
+{
private static SoftReference proxyFactoryRef;
public synchronized static ProxyFactory getProxyFactory()
{
- if ((proxyFactoryRef == null) || (proxyFactoryRef.get() == null))
+ if((proxyFactoryRef == null) || (proxyFactoryRef.get() == null))
{
try
{
- proxyFactoryRef = new
SoftReference(getBroker().getProxyFactory());
+ proxyFactoryRef = new
SoftReference(AbstractProxyFactory.getProxyFactory());
}
- catch (PBFactoryException ex)
+ catch(PBFactoryException ex)
{
// seems we cannot get a broker; in that case we're
defaulting to the CGLib proxy factory
// (which also works for older JDKs) ie. for broker-less
mode (only metadata)
return new ProxyFactoryCGLIBImpl();
}
}
- return (ProxyFactory)proxyFactoryRef.get();
-
- }
-
- private static PersistenceBrokerInternal getBroker()
- {
- return
(PersistenceBrokerInternal)PersistenceBrokerFactory.defaultPersistenceBroker();
+ return (ProxyFactory) proxyFactoryRef.get();
}
/**
@@ -60,7 +53,8 @@
* @param objectOrProxy
* @return Object
*/
- public static final Object getRealObject(Object objectOrProxy) {
+ public static final Object getRealObject(Object objectOrProxy)
+ {
return getProxyFactory().getRealObject(objectOrProxy);
}
@@ -70,7 +64,8 @@
* @param objectOrProxy
* @return Object or null if the Handel is not materialized
*/
- public static final Object getRealObjectIfMaterialized(Object
objectOrProxy) {
+ public static final Object getRealObjectIfMaterialized(Object
objectOrProxy)
+ {
return getProxyFactory().getRealObjectIfMaterialized(objectOrProxy);
}
@@ -80,25 +75,28 @@
* @param objectOrProxy
* @return Class
*/
- public static final Class getRealClass(Object objectOrProxy) {
+ public static final Class getRealClass(Object objectOrProxy)
+ {
return getProxyFactory().getRealClass(objectOrProxy);
}
/**
* Determines whether the given object is an OJB proxy.
- *
+ *
* @return <code>true</code> if the object is an OJB proxy
*/
- public static boolean isNormalOjbProxy(Object proxyOrObject) {
+ public static boolean isNormalOjbProxy(Object proxyOrObject)
+ {
return getProxyFactory().isNormalOjbProxy(proxyOrObject);
}
/**
* Determines whether the given object is an OJB virtual proxy.
- *
+ *
* @return <code>true</code> if the object is an OJB virtual proxy
*/
- public static boolean isVirtualOjbProxy(Object proxyOrObject) {
+ public static boolean isVirtualOjbProxy(Object proxyOrObject)
+ {
return getProxyFactory().isVirtualOjbProxy(proxyOrObject);
}
@@ -106,29 +104,32 @@
* Returns <tt>true</tt> if the given object is a [EMAIL PROTECTED]
java.lang.reflect.Proxy}
* or a [EMAIL PROTECTED] VirtualProxy} instance.
*/
- public static boolean isProxy(Object proxyOrObject) {
+ public static boolean isProxy(Object proxyOrObject)
+ {
return getProxyFactory().isProxy(proxyOrObject);
}
/**
* Returns the invocation handler object of the given proxy object.
- *
+ *
* @param obj The object
* @return The invocation handler if the object is an OJB proxy, or
<code>null</code>
* otherwise
*/
- public static IndirectionHandler getIndirectionHandler(Object obj) {
+ public static IndirectionHandler getIndirectionHandler(Object obj)
+ {
return getProxyFactory().getIndirectionHandler(obj);
}
/**
* Determines whether the object is a materialized object, i.e. no proxy
or a
* proxy that has already been loaded from the database.
- *
+ *
* @param object The object to test
* @return <code>true</code> if the object is materialized
*/
- public static boolean isMaterialized(Object object) {
+ public static boolean isMaterialized(Object object)
+ {
return getProxyFactory().isMaterialized(object);
}
@@ -137,27 +138,25 @@
* then only the text "unmaterialized proxy for ..." is returned and the
proxy is NOT
* materialized. Otherwise, the normal toString method is called. This
useful e.g. for
* logging etc.
- *
+ *
* @param object The object for which a string representation shall be
generated
* @return The string representation
*/
- public static String toString(Object object) {
+ public static String toString(Object object)
+ {
return getProxyFactory().toString(object);
}
- /**
- * Return CollectionProxy for item is item is a CollectionProxy,
otherwise return null
- */
- public static CollectionProxy getCollectionProxy(Object item) {
+ /** Return CollectionProxy for item is item is a CollectionProxy,
otherwise return null */
+ public static CollectionProxy getCollectionProxy(Object item)
+ {
return getProxyFactory().getCollectionProxy(item);
}
- /**
- * Reports if item is a CollectionProxy.
- *
- * @todo Provide handling for pluggable collection proxy implementations
- */
- public static boolean isCollectionProxy(Object item) {
+ /** Reports if item is a CollectionProxy. */
+ public static boolean isCollectionProxy(Object item)
+ {
+ // TODO: Provide handling for pluggable collection proxy
implementations
return getProxyFactory().isCollectionProxy(item);
}
}
1.1.2.5 +2 -10
db-ojb/src/java/org/apache/ojb/broker/core/proxy/VirtualProxy.java
Index: VirtualProxy.java
===================================================================
RCS file:
/home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/VirtualProxy.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- VirtualProxy.java 17 Aug 2005 21:19:39 -0000 1.1.2.4
+++ VirtualProxy.java 29 Sep 2005 17:21:19 -0000 1.1.2.5
@@ -15,22 +15,15 @@
* limitations under the License.
*/
-
-
-
import java.io.Serializable;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBrokerException;
-import org.apache.ojb.broker.PersistenceBrokerFactory;
-import org.apache.ojb.broker.PersistenceBrokerInternal;
-
/**
* Proxy base class. can be used to implement lazy materialization
techniques.
*
- * @see org.apache.ojb.broker.ProxyExamples as a sample application.
* @author <a href="mailto:[EMAIL PROTECTED]">Thomas Mahler<a>
* @version $Id$
*/
@@ -57,8 +50,7 @@
*/
public VirtualProxy(PBKey key, Identity oid)
{
- indirectionHandler =
((PersistenceBrokerInternal)PersistenceBrokerFactory.defaultPersistenceBroker())
-
.getProxyFactory().createIndirectionHandler(key, oid);
+ indirectionHandler =
AbstractProxyFactory.getProxyFactory().createIndirectionHandler(key, oid);
}
/**
No revision
No revision
1.6.2.1 +25 -8
db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableHashMap.java
Index: ManageableHashMap.java
===================================================================
RCS file:
/home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableHashMap.java,v
retrieving revision 1.6
retrieving revision 1.6.2.1
diff -u -r1.6 -r1.6.2.1
--- ManageableHashMap.java 22 May 2004 09:51:25 -0000 1.6
+++ ManageableHashMap.java 29 Sep 2005 17:21:20 -0000 1.6.2.1
@@ -14,18 +14,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
+import java.util.HashMap;
+import java.util.Iterator;
+
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
-import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.metadata.ClassDescriptor;
-
-import java.util.HashMap;
-import java.util.Iterator;
+import org.apache.ojb.broker.metadata.FieldDescriptor;
+import org.apache.ojb.broker.metadata.MetadataException;
+import org.apache.ojb.broker.metadata.MetadataManager;
/**
- * Creates a Map where the primary key is the map key, and the object is the
map value
+ * Creates a Map where the primary key is the map key, and the object
+ * is the map value.
+ * <br/>
+ * <strong>Note:</strong> This implementation is limited in use, only
objects with
+ * single primary key field are allowed (composed PK's are illegal).
*/
public class ManageableHashMap extends HashMap implements
ManageableCollection
{
@@ -33,9 +40,19 @@
{
if (anObject != null)
{
- ClassDescriptor cd =
PersistenceBrokerFactory.defaultPersistenceBroker().getDescriptorRepository().getDescriptorFor(anObject.getClass());
- Object key =
cd.getPrimaryKey().getPersistentField().get(anObject);
- put(key,anObject);
+ ClassDescriptor cd =
MetadataManager.getInstance().getRepository().getDescriptorFor(anObject.getClass());
+ FieldDescriptor[] fields = cd.getPkFields();
+ if(fields.length > 1 || fields.length == 0)
+ {
+ throw new MetadataException("ManageableHashMap can only be
used for persistence capable objects with" +
+ " exactly one primiary key field defined in
metadata, for " + anObject.getClass() + " the" +
+ " PK field count is " + fields.length);
+ }
+ else
+ {
+ Object key = fields[0].getPersistentField().get(anObject);
+ put(key,anObject);
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]