Return-Path: Mailing-List: contact ojb-dev-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list ojb-dev@jakarta.apache.org Received: (qmail 1240 invoked by uid 97); 25 Dec 2002 23:27:33 -0000 Received: (qmail 1236 invoked by uid 98); 25 Dec 2002 23:27:33 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Received: (qmail 1217 invoked from network); 25 Dec 2002 23:27:31 -0000 Received: from daedalus.apache.org (HELO apache.org) (63.251.56.142) by nagoya.betaversion.org with SMTP; 25 Dec 2002 23:27:31 -0000 Received: (qmail 10599 invoked by uid 500); 25 Dec 2002 23:26:10 -0000 Received: (qmail 10592 invoked from network); 25 Dec 2002 23:26:10 -0000 Received: from icarus.apache.org (63.251.56.143) by daedalus.apache.org with SMTP; 25 Dec 2002 23:26:10 -0000 Received: (qmail 3101 invoked by uid 1510); 25 Dec 2002 23:26:09 -0000 Date: 25 Dec 2002 23:26:09 -0000 Message-ID: <20021225232609.3100.qmail@icarus.apache.org> From: arminw@apache.org To: jakarta-ojb-cvs@apache.org Subject: cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/util ProxyHelper.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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(); } } - - }