db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/core/proxy AbstractProxyFactory.java ProxyHelper.java
Date Mon, 10 Oct 2005 00:38:24 GMT
arminw      2005/10/09 17:38:24

  Modified:    src/java/org/apache/ojb/broker/core/proxy Tag:
                        OJB_1_0_RELEASE AbstractProxyFactory.java
                        ProxyHelper.java
  Log:
  make AbstractProxyFactory#getProxyFactory a singleton
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.2.2.3   +309 -175  db-ojb/src/java/org/apache/ojb/broker/core/proxy/AbstractProxyFactory.java
  
  Index: AbstractProxyFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/AbstractProxyFactory.java,v
  retrieving revision 1.2.2.2
  retrieving revision 1.2.2.3
  diff -u -r1.2.2.2 -r1.2.2.3
  --- AbstractProxyFactory.java	29 Sep 2005 17:16:58 -0000	1.2.2.2
  +++ AbstractProxyFactory.java	10 Oct 2005 00:38:24 -0000	1.2.2.3
  @@ -35,49 +35,61 @@
   /**
    * Abstract implementation for the ProxyFactory
    *
  - * @version $Id:
  - *
    * @author andrew.clute
  - *
  + * @version $Id:
    */
  -public abstract class AbstractProxyFactory implements ProxyFactory {
  +public abstract class AbstractProxyFactory implements ProxyFactory
  +{
  +    /*
  +    arminw:
  +    Do we need a serializable ProxyFactory implementation? If yes, we have to fix this
class.
  +    TODO: ProxyFactory is declared as Serializable but Constructor class is not, but used
without transient keyword.
  +    */
  +
  +    private static Logger log = LoggerFactory.getLogger(AbstractProxyFactory.class);
  +    private static transient ProxyFactory singleton;
   
  -    private static Logger         log = LoggerFactory.getLogger(AbstractProxyFactory.class);
       /** The indirection handler class */
  -    private Class                 _indirectionHandlerClass;
  +    private Class _indirectionHandlerClass;
       /** The constructor used for creating indirection handler instances (shortcut) */
       private transient Constructor _indirectionHandlerConstructor;
       /** The constructor used for creating list proxies */
  -    private Constructor           _listProxyConstructor;
  +    private Constructor _listProxyConstructor;
       /** The constructor used for creating set proxies */
  -    private Constructor           _setProxyConstructor;
  +    private Constructor _setProxyConstructor;
       /** The constructor used for creating collection proxies */
  -    private Constructor           _collectionProxyConstructor;
  +    private Constructor _collectionProxyConstructor;
   
  -    private static ProxyConfiguration getProxyConfiguration() {
  -        return (ProxyConfiguration)OjbConfigurator.getInstance().getConfigurationFor(null);
  +    private static ProxyConfiguration getProxyConfiguration()
  +    {
  +        return (ProxyConfiguration) OjbConfigurator.getInstance().getConfigurationFor(null);
       }
   
       /**
        * Returns the constructor of the indirection handler class.
  -     * 
  +     *
        * @return The constructor for indirection handlers
        */
  -    private Constructor getIndirectionHandlerConstructor() {
  -        if (_indirectionHandlerConstructor == null) {
  -            Class[] paramType = { PBKey.class, Identity.class };
  +    private synchronized Constructor getIndirectionHandlerConstructor()
  +    {
  +        if(_indirectionHandlerConstructor == null)
  +        {
  +            Class[] paramType = {PBKey.class, Identity.class};
   
  -            try {
  +            try
  +            {
                   _indirectionHandlerConstructor = getIndirectionHandlerClass().getConstructor(paramType);
  -            } catch (NoSuchMethodException ex) {
  +            }
  +            catch(NoSuchMethodException ex)
  +            {
                   throw new MetadataException("The class "
  -                                            + _indirectionHandlerClass.getName()
  -                                            + " specified for IndirectionHandlerClass"
  -                                            + " is required to have a public constructor
with signature ("
  -                                            + PBKey.class.getName()
  -                                            + ", "
  -                                            + Identity.class.getName()
  -                                            + ").");
  +                        + _indirectionHandlerClass.getName()
  +                        + " specified for IndirectionHandlerClass"
  +                        + " is required to have a public constructor with signature ("
  +                        + PBKey.class.getName()
  +                        + ", "
  +                        + Identity.class.getName()
  +                        + ").");
               }
           }
           return _indirectionHandlerConstructor;
  @@ -85,11 +97,13 @@
   
       /**
        * Returns the indirection handler class.
  -     * 
  +     *
        * @return The class for indirection handlers
        */
  -    public Class getIndirectionHandlerClass() {
  -        if (_indirectionHandlerClass == null) {
  +    public Class getIndirectionHandlerClass()
  +    {
  +        if(_indirectionHandlerClass == null)
  +        {
               setIndirectionHandlerClass(getProxyConfiguration().getIndirectionHandlerClass());
           }
   
  @@ -98,11 +112,13 @@
   
       /**
        * Sets the indirection handler class.
  -     * 
  +     *
        * @param indirectionHandlerClass The class for indirection handlers
        */
  -    public void setIndirectionHandlerClass(Class indirectionHandlerClass) {
  -        if (indirectionHandlerClass == null) {
  +    public void setIndirectionHandlerClass(Class indirectionHandlerClass)
  +    {
  +        if(indirectionHandlerClass == null)
  +        {
               //throw new MetadataException("No IndirectionHandlerClass specified.");
               /**
                * andrew.clute
  @@ -111,34 +127,43 @@
                */
               indirectionHandlerClass = getDefaultIndirectionHandlerClass();
           }
  -        if (indirectionHandlerClass.isInterface()
  -            || Modifier.isAbstract(indirectionHandlerClass.getModifiers())
  -            || !getIndirectionHandlerBaseClass().isAssignableFrom(indirectionHandlerClass))
{
  +        if(indirectionHandlerClass.isInterface()
  +                || Modifier.isAbstract(indirectionHandlerClass.getModifiers())
  +                || !getIndirectionHandlerBaseClass().isAssignableFrom(indirectionHandlerClass))
  +        {
               throw new MetadataException("Illegal class "
  -                                        + indirectionHandlerClass.getName()
  -                                        + " specified for IndirectionHandlerClass. Must
be a concrete subclass of "
  -                                        + getIndirectionHandlerBaseClass().getName());
  +                    + indirectionHandlerClass.getName()
  +                    + " specified for IndirectionHandlerClass. Must be a concrete subclass
of "
  +                    + getIndirectionHandlerBaseClass().getName());
           }
           _indirectionHandlerClass = indirectionHandlerClass;
       }
   
       /**
        * Creates a new indirection handler instance.
  -     * 
  +     *
        * @param brokerKey The associated {@link PBKey}.
  -     * @param id              The subject's ids
  +     * @param id The subject's ids
        * @return The new instance
        */
  -    public IndirectionHandler createIndirectionHandler(PBKey brokerKey, Identity id) {
  -        Object args[] = { brokerKey, id };
  -
  -        try {
  -            return (IndirectionHandler)getIndirectionHandlerConstructor().newInstance(args);
  -        } catch (InvocationTargetException ex) {
  +    public IndirectionHandler createIndirectionHandler(PBKey brokerKey, Identity id)
  +    {
  +        Object args[] = {brokerKey, id};
  +
  +        try
  +        {
  +            return (IndirectionHandler) getIndirectionHandlerConstructor().newInstance(args);
  +        }
  +        catch(InvocationTargetException ex)
  +        {
               throw new PersistenceBrokerException("Exception while creating a new indirection
handler instance", ex);
  -        } catch (InstantiationException ex) {
  +        }
  +        catch(InstantiationException ex)
  +        {
               throw new PersistenceBrokerException("Exception while creating a new indirection
handler instance", ex);
  -        } catch (IllegalAccessException ex) {
  +        }
  +        catch(IllegalAccessException ex)
  +        {
               throw new PersistenceBrokerException("Exception while creating a new indirection
handler instance", ex);
           }
       }
  @@ -146,60 +171,69 @@
       /**
        * Retrieves the constructor that is used by OJB to create instances of the given collection
proxy
        * class.
  -     * 
  +     *
        * @param proxyClass The proxy class
  -     * @param baseType   The required base type of the proxy class
  -     * @param typeDesc   The type of collection proxy
  +     * @param baseType The required base type of the proxy class
  +     * @param typeDesc The type of collection proxy
        * @return The constructor
        */
  -    private static Constructor retrieveCollectionProxyConstructor(Class proxyClass, Class
baseType, String typeDesc) {
  -        if (proxyClass == null) {
  +    private static Constructor retrieveCollectionProxyConstructor(Class proxyClass, Class
baseType, String typeDesc)
  +    {
  +        if(proxyClass == null)
  +        {
               throw new MetadataException("No " + typeDesc + " specified.");
           }
  -        if (proxyClass.isInterface() || Modifier.isAbstract(proxyClass.getModifiers())
|| !baseType.isAssignableFrom(proxyClass)) {
  +        if(proxyClass.isInterface() || Modifier.isAbstract(proxyClass.getModifiers()) ||
!baseType.isAssignableFrom(proxyClass))
  +        {
               throw new MetadataException("Illegal class "
  -                                        + proxyClass.getName()
  -                                        + " specified for "
  -                                        + typeDesc
  -                                        + ". Must be a concrete subclass of "
  -                                        + baseType.getName());
  +                    + proxyClass.getName()
  +                    + " specified for "
  +                    + typeDesc
  +                    + ". Must be a concrete subclass of "
  +                    + baseType.getName());
           }
   
  -        Class[] paramType = { PBKey.class, Class.class, Query.class };
  +        Class[] paramType = {PBKey.class, Class.class, Query.class};
   
  -        try {
  +        try
  +        {
               return proxyClass.getConstructor(paramType);
  -        } catch (NoSuchMethodException ex) {
  +        }
  +        catch(NoSuchMethodException ex)
  +        {
               throw new MetadataException("The class "
  -                                        + proxyClass.getName()
  -                                        + " specified for "
  -                                        + typeDesc
  -                                        + " is required to have a public constructor with
signature ("
  -                                        + PBKey.class.getName()
  -                                        + ", "
  -                                        + Class.class.getName()
  -                                        + ", "
  -                                        + Query.class.getName()
  -                                        + ").");
  +                    + proxyClass.getName()
  +                    + " specified for "
  +                    + typeDesc
  +                    + " is required to have a public constructor with signature ("
  +                    + PBKey.class.getName()
  +                    + ", "
  +                    + Class.class.getName()
  +                    + ", "
  +                    + Query.class.getName()
  +                    + ").");
           }
       }
   
       /**
        * Returns the list proxy class.
  -     * 
  +     *
        * @return The class used for list proxies
        */
  -    public Class getListProxyClass() {
  +    public Class getListProxyClass()
  +    {
           return getListProxyConstructor().getDeclaringClass();
       }
   
       /**
        * Returns the constructor of the list proxy class.
  -     * 
  +     *
        * @return The constructor for list proxies
        */
  -    private Constructor getListProxyConstructor() {
  -        if (_listProxyConstructor == null) {
  +    private Constructor getListProxyConstructor()
  +    {
  +        if(_listProxyConstructor == null)
  +        {
               setListProxyClass(getProxyConfiguration().getListProxyClass());
           }
           return _listProxyConstructor;
  @@ -212,26 +246,30 @@
        *
        * @param listProxyClass The proxy class
        */
  -    public void setListProxyClass(Class listProxyClass) {
  +    public void setListProxyClass(Class listProxyClass)
  +    {
           _listProxyConstructor = retrieveCollectionProxyConstructor(listProxyClass, List.class,
"ListProxyClass");
       }
   
       /**
        * Returns the set proxy class.
  -     * 
  +     *
        * @return The class used for set proxies
        */
  -    public Class getSetProxyClass() {
  +    public Class getSetProxyClass()
  +    {
           return getSetProxyConstructor().getDeclaringClass();
       }
   
       /**
        * Returns the constructor of the set proxy class.
  -     * 
  +     *
        * @return The constructor for set proxies
        */
  -    private Constructor getSetProxyConstructor() {
  -        if (_setProxyConstructor == null) {
  +    private Constructor getSetProxyConstructor()
  +    {
  +        if(_setProxyConstructor == null)
  +        {
               setSetProxyClass(getProxyConfiguration().getSetProxyClass());
           }
           return _setProxyConstructor;
  @@ -242,26 +280,30 @@
        *
        * @param setProxyClass The proxy class
        */
  -    public void setSetProxyClass(Class setProxyClass) {
  +    public void setSetProxyClass(Class setProxyClass)
  +    {
           _setProxyConstructor = retrieveCollectionProxyConstructor(setProxyClass, Set.class,
"SetProxyClass");
       }
   
       /**
        * Returns the collection proxy class.
  -     * 
  +     *
        * @return The class used for collection proxies
        */
  -    public Class getCollectionProxyClass() {
  +    public Class getCollectionProxyClass()
  +    {
           return getCollectionProxyConstructor().getDeclaringClass();
       }
   
       /**
        * Returns the constructor of the generic collection proxy class.
  -     * 
  +     *
        * @return The constructor for collection proxies
        */
  -    private Constructor getCollectionProxyConstructor() {
  -        if (_collectionProxyConstructor == null) {
  +    private Constructor getCollectionProxyConstructor()
  +    {
  +        if(_collectionProxyConstructor == null)
  +        {
               setCollectionProxyClass(getProxyConfiguration().getCollectionProxyClass());
           }
           return _collectionProxyConstructor;
  @@ -272,51 +314,67 @@
        *
        * @param collectionProxyClass The proxy class
        */
  -    public void setCollectionProxyClass(Class collectionProxyClass) {
  +    public void setCollectionProxyClass(Class collectionProxyClass)
  +    {
           _collectionProxyConstructor = retrieveCollectionProxyConstructor(collectionProxyClass,
Collection.class, "CollectionProxyClass");
           // we also require the class to be a subclass of ManageableCollection
  -        if (!ManageableCollection.class.isAssignableFrom(collectionProxyClass)) {
  +        if(!ManageableCollection.class.isAssignableFrom(collectionProxyClass))
  +        {
               throw new MetadataException("Illegal class "
  -                                        + collectionProxyClass.getName()
  -                                        + " specified for CollectionProxyClass. Must be
a concrete subclass of "
  -                                        + ManageableCollection.class.getName());
  +                    + collectionProxyClass.getName()
  +                    + " specified for CollectionProxyClass. Must be a concrete subclass
of "
  +                    + ManageableCollection.class.getName());
           }
       }
   
       /**
        * Determines which proxy to use for the given collection class (list, set or generic
collection proxy).
  -     * 
  +     *
        * @param collectionClass The collection class
        * @return The constructor of the proxy class
        */
  -    private Constructor getCollectionProxyConstructor(Class collectionClass) {
  -        if (List.class.isAssignableFrom(collectionClass)) {
  +    private Constructor getCollectionProxyConstructor(Class collectionClass)
  +    {
  +        if(List.class.isAssignableFrom(collectionClass))
  +        {
               return getListProxyConstructor();
  -        } else if (Set.class.isAssignableFrom(collectionClass)) {
  +        }
  +        else if(Set.class.isAssignableFrom(collectionClass))
  +        {
               return getSetProxyConstructor();
  -        } else {
  +        }
  +        else
  +        {
               return getCollectionProxyConstructor();
           }
       }
   
       /**
        * Create a Collection Proxy for a given query.
  -     * 
  -     * @param brokerKey       The key of the persistence broker
  -     * @param query           The query
  +     *
  +     * @param brokerKey The key of the persistence broker
  +     * @param query The query
        * @param collectionClass The class to build the proxy for
        * @return The collection proxy
        */
  -    public ManageableCollection createCollectionProxy(PBKey brokerKey, Query query, Class
collectionClass) {
  -        Object args[] = { brokerKey, collectionClass, query };
  -
  -        try {
  -            return (ManageableCollection)getCollectionProxyConstructor(collectionClass).newInstance(args);
  -        } catch (InstantiationException ex) {
  +    public ManageableCollection createCollectionProxy(PBKey brokerKey, Query query, Class
collectionClass)
  +    {
  +        Object args[] = {brokerKey, collectionClass, query};
  +
  +        try
  +        {
  +            return (ManageableCollection) getCollectionProxyConstructor(collectionClass).newInstance(args);
  +        }
  +        catch(InstantiationException ex)
  +        {
               throw new PersistenceBrokerException("Exception while creating a new collection
proxy instance", ex);
  -        } catch (InvocationTargetException ex) {
  +        }
  +        catch(InvocationTargetException ex)
  +        {
               throw new PersistenceBrokerException("Exception while creating a new collection
proxy instance", ex);
  -        } catch (IllegalAccessException ex) {
  +        }
  +        catch(IllegalAccessException ex)
  +        {
               throw new PersistenceBrokerException("Exception while creating a new collection
proxy instance", ex);
           }
       }
  @@ -327,33 +385,49 @@
        * @param objectOrProxy
        * @return Object
        */
  -    public final Object getRealObject(Object objectOrProxy) {
  -        if (isNormalOjbProxy(objectOrProxy)) {
  +    public final Object getRealObject(Object objectOrProxy)
  +    {
  +        if(isNormalOjbProxy(objectOrProxy))
  +        {
               String msg;
   
  -            try {
  +            try
  +            {
                   return getIndirectionHandler(objectOrProxy).getRealSubject();
  -            } catch (ClassCastException e) {
  +            }
  +            catch(ClassCastException e)
  +            {
                   // shouldn't happen but still ...
                   msg = "The InvocationHandler for the provided Proxy was not an instance
of " + IndirectionHandler.class.getName();
                   log.error(msg);
                   throw new PersistenceBrokerException(msg, e);
  -            } catch (IllegalArgumentException e) {
  +            }
  +            catch(IllegalArgumentException e)
  +            {
                   msg = "Could not retrieve real object for given Proxy: " + objectOrProxy;
                   log.error(msg);
                   throw new PersistenceBrokerException(msg, e);
  -            } catch (PersistenceBrokerException e) {
  +            }
  +            catch(PersistenceBrokerException e)
  +            {
                   log.error("Could not retrieve real object for given Proxy: " + objectOrProxy);
                   throw e;
               }
  -        } else if (isVirtualOjbProxy(objectOrProxy)) {
  -            try {
  -                return ((VirtualProxy)objectOrProxy).getRealSubject();
  -            } catch (PersistenceBrokerException e) {
  +        }
  +        else if(isVirtualOjbProxy(objectOrProxy))
  +        {
  +            try
  +            {
  +                return ((VirtualProxy) objectOrProxy).getRealSubject();
  +            }
  +            catch(PersistenceBrokerException e)
  +            {
                   log.error("Could not retrieve real object for VirtualProxy: " + objectOrProxy);
                   throw e;
               }
  -        } else {
  +        }
  +        else
  +        {
               return objectOrProxy;
           }
       }
  @@ -364,37 +438,53 @@
        * @param objectOrProxy
        * @return Object or null if the Handel is not materialized
        */
  -    public Object getRealObjectIfMaterialized(Object objectOrProxy) {
  -        if (isNormalOjbProxy(objectOrProxy)) {
  +    public Object getRealObjectIfMaterialized(Object objectOrProxy)
  +    {
  +        if(isNormalOjbProxy(objectOrProxy))
  +        {
               String msg;
   
  -            try {
  +            try
  +            {
                   IndirectionHandler handler = getIndirectionHandler(objectOrProxy);
   
                   return handler.alreadyMaterialized() ? handler.getRealSubject() : null;
  -            } catch (ClassCastException e) {
  +            }
  +            catch(ClassCastException e)
  +            {
                   // shouldn't happen but still ...
                   msg = "The InvocationHandler for the provided Proxy was not an instance
of " + IndirectionHandler.class.getName();
                   log.error(msg);
                   throw new PersistenceBrokerException(msg, e);
  -            } catch (IllegalArgumentException e) {
  +            }
  +            catch(IllegalArgumentException e)
  +            {
                   msg = "Could not retrieve real object for given Proxy: " + objectOrProxy;
                   log.error(msg);
                   throw new PersistenceBrokerException(msg, e);
  -            } catch (PersistenceBrokerException e) {
  +            }
  +            catch(PersistenceBrokerException e)
  +            {
                   log.error("Could not retrieve real object for given Proxy: " + objectOrProxy);
                   throw e;
               }
  -        } else if (isVirtualOjbProxy(objectOrProxy)) {
  -            try {
  -                VirtualProxy proxy = (VirtualProxy)objectOrProxy;
  +        }
  +        else if(isVirtualOjbProxy(objectOrProxy))
  +        {
  +            try
  +            {
  +                VirtualProxy proxy = (VirtualProxy) objectOrProxy;
   
                   return proxy.alreadyMaterialized() ? proxy.getRealSubject() : null;
  -            } catch (PersistenceBrokerException e) {
  +            }
  +            catch(PersistenceBrokerException e)
  +            {
                   log.error("Could not retrieve real object for VirtualProxy: " + objectOrProxy);
                   throw e;
               }
  -        } else {
  +        }
  +        else
  +        {
               return objectOrProxy;
           }
       }
  @@ -405,13 +495,16 @@
        * @param objectOrProxy
        * @return Class
        */
  -    public Class getRealClass(Object objectOrProxy) {
  +    public Class getRealClass(Object objectOrProxy)
  +    {
           IndirectionHandler handler;
   
  -        if (isNormalOjbProxy(objectOrProxy)) {
  +        if(isNormalOjbProxy(objectOrProxy))
  +        {
               String msg;
   
  -            try {
  +            try
  +            {
                   handler = getIndirectionHandler(objectOrProxy);
                   /*
                    arminw:
  @@ -419,44 +512,54 @@
                    */
                   // return handler.getIdentity().getObjectsTopLevelClass();
                   return handler.getIdentity().getObjectsRealClass();
  -            } catch (ClassCastException e) {
  +            }
  +            catch(ClassCastException e)
  +            {
                   // shouldn't happen but still ...
                   msg = "The InvocationHandler for the provided Proxy was not an instance
of " + IndirectionHandler.class.getName();
                   log.error(msg);
                   throw new PersistenceBrokerException(msg, e);
  -            } catch (IllegalArgumentException e) {
  +            }
  +            catch(IllegalArgumentException e)
  +            {
                   msg = "Could not retrieve real object for given Proxy: " + objectOrProxy;
                   log.error(msg);
                   throw new PersistenceBrokerException(msg, e);
               }
  -        } else if (isVirtualOjbProxy(objectOrProxy)) {
  -            handler = VirtualProxy.getIndirectionHandler((VirtualProxy)objectOrProxy);
  +        }
  +        else if(isVirtualOjbProxy(objectOrProxy))
  +        {
  +            handler = VirtualProxy.getIndirectionHandler((VirtualProxy) objectOrProxy);
               /*
                arminw:
                think we should return the real class
                */
               // return handler.getIdentity().getObjectsTopLevelClass();
               return handler.getIdentity().getObjectsRealClass();
  -        } else {
  +        }
  +        else
  +        {
               return objectOrProxy.getClass();
           }
       }
   
       /**
        * Determines whether the given object is an OJB proxy.
  -     * 
  +     *
        * @return <code>true</code> if the object is an OJB proxy
        */
  -    public boolean isNormalOjbProxy(Object proxyOrObject) {
  +    public boolean isNormalOjbProxy(Object proxyOrObject)
  +    {
           return proxyOrObject instanceof OJBProxy;
       }
   
       /**
        * Determines whether the given object is an OJB virtual proxy.
  -     * 
  +     *
        * @return <code>true</code> if the object is an OJB virtual proxy
        */
  -    public boolean isVirtualOjbProxy(Object proxyOrObject) {
  +    public boolean isVirtualOjbProxy(Object proxyOrObject)
  +    {
           return proxyOrObject instanceof VirtualProxy;
       }
   
  @@ -464,7 +567,8 @@
        * Returns <tt>true</tt> if the given object is a {@link java.lang.reflect.Proxy}
        * or a {@link VirtualProxy} instance.
        */
  -    public boolean isProxy(Object proxyOrObject) {
  +    public boolean isProxy(Object proxyOrObject)
  +    {
           return isNormalOjbProxy(proxyOrObject) || isVirtualOjbProxy(proxyOrObject);
       }
   
  @@ -476,19 +580,27 @@
   
       /**
        * 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 IndirectionHandler getIndirectionHandler(Object obj) {
  -        if (obj == null) {
  +    public IndirectionHandler getIndirectionHandler(Object obj)
  +    {
  +        if(obj == null)
  +        {
               return null;
  -        } else if (isNormalOjbProxy(obj)) {
  +        }
  +        else if(isNormalOjbProxy(obj))
  +        {
               return getDynamicIndirectionHandler(obj);
  -        } else if (isVirtualOjbProxy(obj)) {
  -            return VirtualProxy.getIndirectionHandler((VirtualProxy)obj);
  -        } else {
  +        }
  +        else if(isVirtualOjbProxy(obj))
  +        {
  +            return VirtualProxy.getIndirectionHandler((VirtualProxy) obj);
  +        }
  +        else
  +        {
               return null;
           }
   
  @@ -497,23 +609,26 @@
       /**
        * 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 boolean isMaterialized(Object object) {
  +    public boolean isMaterialized(Object object)
  +    {
           IndirectionHandler handler = getIndirectionHandler(object);
   
  -        return handler == null ? true : handler.alreadyMaterialized();
  +        return handler == null || handler.alreadyMaterialized();
       }
   
  -    /**
  -     * Return CollectionProxy for item is item is a CollectionProxy, otherwise return null
  -     */
  -    public CollectionProxy getCollectionProxy(Object item) {
  -        if (isCollectionProxy(item)) {
  -            return (CollectionProxy)item;
  -        } else {
  +    /** Return CollectionProxy for item is item is a CollectionProxy, otherwise return
null */
  +    public CollectionProxy getCollectionProxy(Object item)
  +    {
  +        if(isCollectionProxy(item))
  +        {
  +            return (CollectionProxy) item;
  +        }
  +        else
  +        {
               return null;
           }
       }
  @@ -523,7 +638,8 @@
        *
        * TODO: Provide handling for pluggable collection proxy implementations
        */
  -    public boolean isCollectionProxy(Object item) {
  +    public boolean isCollectionProxy(Object item)
  +    {
           return (item instanceof CollectionProxy);
       }
   
  @@ -532,30 +648,48 @@
        * 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 proxy The object for which a string representation shall be generated
        * @return The string representation
        */
  -
  -    public String toString(Object proxy) {
  +    public String toString(Object proxy)
  +    {
           IndirectionHandler handler = getIndirectionHandler(proxy);
  -        if ((handler != null) && handler.alreadyMaterialized()) {
  +        if((handler != null) && handler.alreadyMaterialized())
  +        {
               return "unmaterialized proxy for " + handler.getIdentity();
  -        } else {
  +        }
  +        else
  +        {
               return proxy.toString();
           }
       }
   
  -    public static ProxyFactory getProxyFactory() {
  -        Class proxyFactoryClass = null;
  -        try {
  -            proxyFactoryClass = getProxyConfiguration().getProxyFactoryClass();
  -            return (ProxyFactory)proxyFactoryClass.newInstance();
  -        } catch (InstantiationException e) {
  -            throw new MetadataException("Illegal class " + proxyFactoryClass.getName()
+ " specified for ProxyFactoryClass.");
  -        } catch (IllegalAccessException e) {
  -            throw new MetadataException("Illegal class " + proxyFactoryClass.getName()
+ " specified for ProxyFactoryClass.");
  +    public synchronized static ProxyFactory getProxyFactory()
  +    {
  +        /*
  +        TODO: Check usage of singleton.
  +        arminw: Think the ProxyFactory instance can be a singleton, because the proxy stuff
  +        will never change while runtime.
  +        */
  +        if(singleton == null)
  +        {
  +            Class proxyFactoryClass = null;
  +            try
  +            {
  +                proxyFactoryClass = getProxyConfiguration().getProxyFactoryClass();
  +                singleton = (ProxyFactory) proxyFactoryClass.newInstance();
  +            }
  +            catch(InstantiationException e)
  +            {
  +                throw new MetadataException("Illegal class " + proxyFactoryClass.getName()
+ " specified for ProxyFactoryClass.");
  +            }
  +            catch(IllegalAccessException e)
  +            {
  +                throw new MetadataException("Illegal class " + proxyFactoryClass.getName()
+ " specified for ProxyFactoryClass.");
  +            }
           }
  +        return singleton;
       }
   
   }
  
  
  
  1.2.2.7   +10 -3     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.6
  retrieving revision 1.2.2.7
  diff -u -r1.2.2.6 -r1.2.2.7
  --- ProxyHelper.java	29 Sep 2005 17:21:19 -0000	1.2.2.6
  +++ ProxyHelper.java	10 Oct 2005 00:38:24 -0000	1.2.2.7
  @@ -16,6 +16,8 @@
    */
   
   import java.lang.ref.SoftReference;
  +import java.lang.ref.WeakReference;
  +import java.lang.ref.Reference;
   
   import org.apache.ojb.broker.PBFactoryException;
   
  @@ -27,7 +29,7 @@
    */
   public class ProxyHelper
   {
  -    private static SoftReference proxyFactoryRef;
  +    private static Reference proxyFactoryRef;
   
       public synchronized static ProxyFactory getProxyFactory()
       {
  @@ -35,7 +37,12 @@
           {
               try
               {
  -                proxyFactoryRef = new SoftReference(AbstractProxyFactory.getProxyFactory());
  +                /*
  +                TODO: Check usage of WeakReference
  +                arminw: Changed Soft- to WeakReference. If in AbstractProxyFactory the
  +                the ProxyFactory instance is freed this class will take care of that.
  +                */
  +                proxyFactoryRef = new WeakReference(AbstractProxyFactory.getProxyFactory());
               }
               catch(PBFactoryException ex)
               {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message