You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/03/08 18:05:49 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/util ProxyHelper.java

tomdz       2004/03/08 09:05:49

  Modified:    src/java/org/apache/ojb/broker/util ProxyHelper.java
  Log:
  Added static toString helper method which does not materialize a proxy; should be used for logging instead of Object.toString
  
  Revision  Changes    Path
  1.23      +38 -1     db-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java
  
  Index: ProxyHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- ProxyHelper.java	8 Feb 2004 13:19:47 -0000	1.22
  +++ ProxyHelper.java	8 Mar 2004 17:05:49 -0000	1.23
  @@ -375,6 +375,13 @@
           return isNormalOjbProxy(proxyOrObject) || isVirtualOjbProxy(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)
       {
           if (obj == null)
  @@ -395,10 +402,40 @@
           }
       }
   
  +    /**
  +     * 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)
       {
           IndirectionHandler handler = getIndirectionHandler(object);
   
           return handler == null ? true : handler.alreadyMaterialized();
  +    }
  +
  +    /**
  +     * Materialization-safe version of toString. If the object is a yet-unmaterialized proxy,
  +     * 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)
  +    {
  +        IndirectionHandler handler = getIndirectionHandler(object);
  +
  +        if ((handler != null) && handler.alreadyMaterialized())
  +        {
  +            return "unmaterialized proxy for " + handler.getIdentity();
  +        }
  +        else
  +        {
  +            return object.toString();
  +        }
       }
   }
  
  
  

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