db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/util ProxyHelper.java
Date Wed, 25 Dec 2002 23:26:09 GMT
arminw      2002/12/25 15:26:09

  Modified:    src/java/org/apache/ojb/broker/util ProxyHelper.java
  Log:
  add log, try to avoid costly Proxy.isProxyClass
  check by do a 'instanceof Proxy' check before -
  is this a valid solution?
  
  Revision  Changes    Path
  1.4       +89 -25    jakarta-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java
  
  Index: ProxyHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ProxyHelper.java	21 Dec 2002 16:34:30 -0000	1.3
  +++ ProxyHelper.java	25 Dec 2002 23:26:09 -0000	1.4
  @@ -55,7 +55,10 @@
    */
   
   import org.apache.ojb.broker.VirtualProxy;
  +import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.accesslayer.IndirectionHandler;
  +import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.broker.util.logging.LoggerFactory;
   
   //#ifdef JDK13
   import java.lang.reflect.Proxy;
  @@ -65,7 +68,6 @@
   */
   //#endif
   
  -
   /**
    * ProxyHelper used to get the real thing behind a proxy
    *
  @@ -74,6 +76,7 @@
    */
   public class ProxyHelper
   {
  +    private static Logger log = LoggerFactory.getLogger(ProxyHelper.class);
   
       /**
        * Get the real Object
  @@ -84,15 +87,40 @@
       public static final Object getRealObject(Object objectOrProxy)
       {
           IndirectionHandler handler;
  -
  -        if (Proxy.isProxyClass(objectOrProxy.getClass()))
  +        /*
  +        arminw
  +        TODO: Check it out. Because the isProxyClass method is costly and most objects
  +        aren't proxies, I add a instanceof check before. Is every Proxy a instance of Proxy?
  +        */
  +        if ((objectOrProxy instanceof Proxy) && Proxy.isProxyClass(objectOrProxy.getClass()))
           {
  -            handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  -            return handler.getRealSubject();
  +            try
  +            {
  +                handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  +                return handler.getRealSubject();
  +            }
  +            catch (IllegalArgumentException e)
  +            {
  +                log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
  +                throw new PersistenceBrokerException(e);
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +                log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
  +                throw e;
  +            }
           }
           else if (objectOrProxy instanceof VirtualProxy)
           {
  -            return ((VirtualProxy)objectOrProxy).getRealSubject();
  +            try
  +            {
  +                return ((VirtualProxy) objectOrProxy).getRealSubject();
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +                log.error("Could not retrieve real object for VirtualProxy: "+objectOrProxy);
  +                throw e;
  +            }
           }
           else
           {
  @@ -109,29 +137,54 @@
       public static final Object getRealObjectIfMaterialized(Object objectOrProxy)
       {
           IndirectionHandler handler;
  -
  -        if (Proxy.isProxyClass(objectOrProxy.getClass()))
  +        /*
  +        arminw
  +        TODO: Check it out. Because the isProxyClass method is costly and most objects
  +        aren't proxies, I add a instanceof check before. Is every Proxy a instance of Proxy?
  +        */
  +        if ((objectOrProxy instanceof Proxy) && Proxy.isProxyClass(objectOrProxy.getClass()))
           {
  -            handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  -            if (handler.alreadyMaterialized())
  +            try
               {
  -                return handler.getRealSubject();
  +                handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  +                if (handler.alreadyMaterialized())
  +                {
  +                    return handler.getRealSubject();
  +                }
  +                else
  +                {
  +                    return null;
  +                }
               }
  -            else
  +            catch (IllegalArgumentException e)
               {
  -                return null;
  +                log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
  +                throw new PersistenceBrokerException(e);
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +                log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
  +                throw e;
               }
           }
           else if (objectOrProxy instanceof VirtualProxy)
           {
  -            VirtualProxy proxy = (VirtualProxy)objectOrProxy;
  -            if (proxy.alreadyMaterialized())
  +            try
               {
  -                return proxy.getRealSubject();
  +                VirtualProxy proxy = (VirtualProxy)objectOrProxy;
  +                if (proxy.alreadyMaterialized())
  +                {
  +                    return proxy.getRealSubject();
  +                }
  +                else
  +                {
  +                    return null;
  +                }
               }
  -            else
  +            catch (PersistenceBrokerException e)
               {
  -                return null;
  +                log.error("Could not retrieve real object for VirtualProxy: "+objectOrProxy);
  +                throw e;
               }
           }
           else
  @@ -150,14 +203,27 @@
       {
           IndirectionHandler handler;
   
  -        if (Proxy.isProxyClass(objectOrProxy.getClass()))
  +        /*
  +        arminw
  +        TODO: Check it out. Because the isProxyClass method is costly and most objects
  +        aren't proxies, I add a instanceof check before. Is every Proxy a instance of Proxy?
  +        */
  +        if ((objectOrProxy instanceof Proxy) && Proxy.isProxyClass(objectOrProxy.getClass()))
           {
  -            handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  -            return handler.getIdentity().getObjectsClass();
  +            try
  +            {
  +                handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
  +                return handler.getIdentity().getObjectsClass();
  +            }
  +            catch (IllegalArgumentException e)
  +            {
  +                log.error("Could not retrieve real Class for Proxy: "+objectOrProxy);
  +                throw e;
  +            }
           }
           else if (objectOrProxy instanceof VirtualProxy)
           {
  -            handler = (IndirectionHandler) VirtualProxy.getIndirectionHandler((VirtualProxy)objectOrProxy);
  +            handler = (IndirectionHandler) VirtualProxy.getIndirectionHandler((VirtualProxy)
objectOrProxy);
               return handler.getIdentity().getObjectsClass();
           }
           else
  @@ -165,6 +231,4 @@
               return objectOrProxy.getClass();
           }
       }
  -
  -
   }
  
  
  

Mime
View raw message