From: <hib...@li...> - 2006-07-05 15:05:04
|
Author: ste...@jb... Date: 2006-07-05 11:03:52 -0400 (Wed, 05 Jul 2006) New Revision: 10084 Added: trunk/Hibernate3/src/org/hibernate/usertype/LoggableUserType.java Modified: trunk/Hibernate3/src/org/hibernate/type/ArrayType.java trunk/Hibernate3/src/org/hibernate/type/CollectionType.java trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java trunk/Hibernate3/src/org/hibernate/type/CustomType.java Log: HHH-1586 and HHH-1881 : custom collection type logging Modified: trunk/Hibernate3/src/org/hibernate/type/ArrayType.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/type/ArrayType.java 2006-07-04 17:58:50 UTC (rev 10083) +++ trunk/Hibernate3/src/org/hibernate/type/ArrayType.java 2006-07-05 15:03:52 UTC (rev 10084) @@ -57,7 +57,9 @@ } public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { - if (value==null) return "null"; + if ( value == null ) { + return "null"; + } int length = Array.getLength(value); List list = new ArrayList(length); Type elemType = getElementType(factory); Modified: trunk/Hibernate3/src/org/hibernate/type/CollectionType.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/type/CollectionType.java 2006-07-04 17:58:50 UTC (rev 10083) +++ trunk/Hibernate3/src/org/hibernate/type/CollectionType.java 2006-07-05 15:03:52 UTC (rev 10084) @@ -134,28 +134,33 @@ public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { + if ( value == null ) { + return "null"; + } + else if ( !Hibernate.isInitialized( value ) ) { + return "<uninitialized>"; + } + else { + return renderLoggableString( value, factory ); + } + } - if ( value == null ) return "null"; - - if ( Hibernate.isInitialized( value ) ) { - if ( getReturnedClass().isInstance(value) ) { - List list = new ArrayList(); - Type elemType = getElementType( factory ); - Iterator iter = getElementsIterator( value ); - while ( iter.hasNext() ) { - list.add( elemType.toLoggableString( iter.next(), factory ) ); - } - return list.toString(); - } - else { - // for DOM4J "collections" only - return ( (Element) value ).asXML(); //TODO: it would be better if this was done at the higher level by Printer - } + protected String renderLoggableString(Object value, SessionFactoryImplementor factory) + throws HibernateException { + if ( Element.class.isInstance( value ) ) { + // for DOM4J "collections" only + // TODO: it would be better if this was done at the higher level by Printer + return ( ( Element ) value ).asXML(); } else { - return "<uninitialized>"; + List list = new ArrayList(); + Type elemType = getElementType( factory ); + Iterator iter = getElementsIterator( value ); + while ( iter.hasNext() ) { + list.add( elemType.toLoggableString( iter.next(), factory ) ); + } + return list.toString(); } - } public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) Modified: trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java 2006-07-04 17:58:50 UTC (rev 10083) +++ trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java 2006-07-05 15:03:52 UTC (rev 10084) @@ -7,10 +7,13 @@ import org.hibernate.HibernateException; import org.hibernate.MappingException; +import org.hibernate.Hibernate; import org.hibernate.collection.PersistentCollection; import org.hibernate.engine.SessionImplementor; +import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.usertype.UserCollectionType; +import org.hibernate.usertype.LoggableUserType; /** * A custom type for mapping user-written classes that implement <tt>PersistentCollection</tt> @@ -22,27 +25,29 @@ public class CustomCollectionType extends CollectionType { private final UserCollectionType userType; + private final boolean customLogging; public CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML) { super(role, foreignKeyPropertyName, isEmbeddedInXML); - - if ( !UserCollectionType.class.isAssignableFrom(userTypeClass) ) { + + if ( !UserCollectionType.class.isAssignableFrom( userTypeClass ) ) { throw new MappingException( "Custom type does not implement UserCollectionType: " + userTypeClass.getName() ); } - + try { - userType = (UserCollectionType) userTypeClass.newInstance(); + userType = ( UserCollectionType ) userTypeClass.newInstance(); } - catch (InstantiationException ie) { + catch ( InstantiationException ie ) { throw new MappingException( "Cannot instantiate custom type: " + userTypeClass.getName() ); } - catch (IllegalAccessException iae) { + catch ( IllegalAccessException iae ) { throw new MappingException( "IllegalAccessException trying to instantiate custom type: " + userTypeClass.getName() ); } + customLogging = LoggableUserType.class.isAssignableFrom( userTypeClass ); } - - public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) + + public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) throws HibernateException { return userType.instantiate(session, persister); } @@ -68,10 +73,19 @@ public Object indexOf(Object collection, Object entity) { return userType.indexOf(collection, entity); } - + public Object replaceElements(Object original, Object target, Object owner, Map copyCache, SessionImplementor session) throws HibernateException { CollectionPersister cp = session.getFactory().getCollectionPersister( getRole() ); return userType.replaceElements(original, target, cp, owner, copyCache, session); } + + protected String renderLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { + if ( customLogging ) { + return ( ( LoggableUserType ) userType ).toLoggableString( value, factory ); + } + else { + return super.renderLoggableString( value, factory ); + } + } } Modified: trunk/Hibernate3/src/org/hibernate/type/CustomType.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/type/CustomType.java 2006-07-04 17:58:50 UTC (rev 10083) +++ trunk/Hibernate3/src/org/hibernate/type/CustomType.java 2006-07-05 15:03:52 UTC (rev 10084) @@ -21,9 +21,11 @@ import org.hibernate.usertype.EnhancedUserType; import org.hibernate.usertype.UserType; import org.hibernate.usertype.UserVersionType; +import org.hibernate.usertype.LoggableUserType; /** - * Adapts <tt>UserType</tt> to the generic <tt>Type</tt> interface. + * Adapts {@link UserType} to the generic {@link Type} interface, in order + * to isolate user code from changes in the internal Type contracts. * * @see org.hibernate.usertype.UserType * @author Gavin King @@ -33,40 +35,39 @@ private final UserType userType; private final String name; private final int[] types; + private final boolean customLogging; public CustomType(Class userTypeClass, Properties parameters) throws MappingException { + if ( !UserType.class.isAssignableFrom( userTypeClass ) ) { + throw new MappingException( + "Custom type does not implement UserType: " + + userTypeClass.getName() + ); + } + name = userTypeClass.getName(); - if ( !UserType.class.isAssignableFrom(userTypeClass) ) { - throw new MappingException( - "Custom type does not implement UserType: " + - userTypeClass.getName() - ); - } - try { - userType = (UserType) userTypeClass.newInstance(); + userType = ( UserType ) userTypeClass.newInstance(); } - catch (InstantiationException ie) { - throw new MappingException( - "Cannot instantiate custom type: " + - userTypeClass.getName() + catch ( InstantiationException ie ) { + throw new MappingException( + "Cannot instantiate custom type: " + + userTypeClass.getName() ); } - catch (IllegalAccessException iae) { - throw new MappingException( - "IllegalAccessException trying to instantiate custom type: " + - userTypeClass.getName() + catch ( IllegalAccessException iae ) { + throw new MappingException( + "IllegalAccessException trying to instantiate custom type: " + + userTypeClass.getName() ); } - /*if ( !Serializable.class.isAssignableFrom( userType.returnedClass() ) ) { - LogFactory.getLog(CustomType.class).warn("custom type does not implement Serializable: " + userTypeClass); - }*/ - TypeFactory.injectParameters(userType, parameters); + TypeFactory.injectParameters( userType, parameters ); types = userType.sqlTypes(); + customLogging = LoggableUserType.class.isAssignableFrom( userTypeClass ); } public int[] sqlTypes(Mapping pi) { @@ -85,7 +86,7 @@ return userType.equals(x, y); } - public boolean isEqual(Object x, Object y, EntityMode entityMode) + public boolean isEqual(Object x, Object y, EntityMode entityMode) throws HibernateException { return isEqual(x, y); } @@ -93,7 +94,7 @@ public int getHashCode(Object x, EntityMode entityMode) { return userType.hashCode(x); } - + public Object nullSafeGet( ResultSet rs, String[] names, @@ -114,7 +115,7 @@ } - public Object assemble(Serializable cached, SessionImplementor session, Object owner) + public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException { return userType.assemble(cached, owner); } @@ -125,20 +126,20 @@ } public Object replace( - Object original, + Object original, Object target, - SessionImplementor session, - Object owner, + SessionImplementor session, + Object owner, Map copyCache) throws HibernateException { return userType.replace(original, target, owner); } - + public void nullSafeSet( PreparedStatement st, Object value, int index, - boolean[] settable, + boolean[] settable, SessionImplementor session ) throws HibernateException, SQLException { @@ -173,7 +174,7 @@ return name; } - public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) + public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) throws HibernateException { return userType.deepCopy(value); } @@ -206,14 +207,22 @@ return fromXMLString( xml.getText(), factory ); } - public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) + public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { node.setText( toXMLString(value, factory) ); } - public String toLoggableString(Object value, SessionFactoryImplementor factory) + public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { - return value==null ? "null" : toXMLString(value, factory); + if ( value == null ) { + return "null"; + } + else if ( customLogging ) { + return ( ( LoggableUserType ) userType ).toLoggableString( value, factory ); + } + else { + return toXMLString( value, factory ); + } } public boolean[] toColumnNullness(Object value, Mapping mapping) { @@ -221,9 +230,9 @@ if (value!=null) Arrays.fill(result, true); return result; } - + public boolean isDirty(Object old, Object current, boolean[] checkable, SessionImplementor session) throws HibernateException { return checkable[0] && isDirty(old, current, session); } - + } Added: trunk/Hibernate3/src/org/hibernate/usertype/LoggableUserType.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/usertype/LoggableUserType.java 2006-07-04 17:58:50 UTC (rev 10083) +++ trunk/Hibernate3/src/org/hibernate/usertype/LoggableUserType.java 2006-07-05 15:03:52 UTC (rev 10084) @@ -0,0 +1,20 @@ +package org.hibernate.usertype; + +import org.hibernate.engine.SessionFactoryImplementor; + +/** + * Marker interface for user types which want to perform custom + * logging of their corresponding values + * + * @author Steve Ebersole + */ +public interface LoggableUserType { + /** + * Generate a loggable string representation of the collection (value). + * + * @param value The collection to be logged; guarenteed to be non-null and initialized. + * @param factory The factory. + * @return The loggable string representation. + */ + public String toLoggableString(Object value, SessionFactoryImplementor factory); +} |