Home » Hibernate-3.3.2.GA » org.hibernate » intercept » [javadoc | source]

    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.intercept;
   26   
   27   import org.hibernate.engine.SessionImplementor;
   28   import org.hibernate.intercept.cglib.CGLIBHelper;
   29   import org.hibernate.intercept.javassist.JavassistHelper;
   30   
   31   import java.util.Set;
   32   
   33   /**
   34    * Helper class for dealing with enhanced entity classes.
   35    *
   36    * @author Steve Ebersole
   37    */
   38   public class FieldInterceptionHelper {
   39   
   40   	// VERY IMPORTANT!!!! - This class needs to be free of any static references
   41   	// to any CGLIB or Javassist classes.  Otherwise, users will always need both
   42   	// on their classpaths no matter which (if either) they use.
   43   	//
   44   	// Another option here would be to remove the Hibernate.isPropertyInitialized()
   45   	// method and have the users go through the SessionFactory to get this information.
   46   
   47   	private FieldInterceptionHelper() {
   48   	}
   49   
   50   	public static boolean isInstrumented(Class entityClass) {
   51   		Class[] definedInterfaces = entityClass.getInterfaces();
   52   		for ( int i = 0; i < definedInterfaces.length; i++ ) {
   53   			if ( "net.sf.cglib.transform.impl.InterceptFieldEnabled".equals( definedInterfaces[i].getName() )
   54   			     || "org.hibernate.bytecode.javassist.FieldHandled".equals( definedInterfaces[i].getName() ) ) {
   55   				return true;
   56   			}
   57   		}
   58   		return false;
   59   	}
   60   
   61   	public static boolean isInstrumented(Object entity) {
   62   		return entity != null && isInstrumented( entity.getClass() );
   63   	}
   64   
   65   	public static FieldInterceptor extractFieldInterceptor(Object entity) {
   66   		if ( entity == null ) {
   67   			return null;
   68   		}
   69   		Class[] definedInterfaces = entity.getClass().getInterfaces();
   70   		for ( int i = 0; i < definedInterfaces.length; i++ ) {
   71   			if ( "net.sf.cglib.transform.impl.InterceptFieldEnabled".equals( definedInterfaces[i].getName() ) ) {
   72   				// we have a CGLIB enhanced entity
   73   				return CGLIBHelper.extractFieldInterceptor( entity );
   74   			}
   75   			else if ( "org.hibernate.bytecode.javassist.FieldHandled".equals( definedInterfaces[i].getName() ) ) {
   76   				// we have a Javassist enhanced entity
   77   				return JavassistHelper.extractFieldInterceptor( entity );
   78   			}
   79   		}
   80   		return null;
   81   	}
   82   
   83   	public static FieldInterceptor injectFieldInterceptor(
   84   			Object entity,
   85   	        String entityName,
   86   	        Set uninitializedFieldNames,
   87   	        SessionImplementor session) {
   88   		if ( entity != null ) {
   89   			Class[] definedInterfaces = entity.getClass().getInterfaces();
   90   			for ( int i = 0; i < definedInterfaces.length; i++ ) {
   91   				if ( "net.sf.cglib.transform.impl.InterceptFieldEnabled".equals( definedInterfaces[i].getName() ) ) {
   92   					// we have a CGLIB enhanced entity
   93   					return CGLIBHelper.injectFieldInterceptor( entity, entityName, uninitializedFieldNames, session );
   94   				}
   95   				else if ( "org.hibernate.bytecode.javassist.FieldHandled".equals( definedInterfaces[i].getName() ) ) {
   96   					// we have a Javassist enhanced entity
   97   					return JavassistHelper.injectFieldInterceptor( entity, entityName, uninitializedFieldNames, session );
   98   				}
   99   			}
  100   		}
  101   		return null;
  102   	}
  103   
  104   	public static void clearDirty(Object entity) {
  105   		FieldInterceptor interceptor = extractFieldInterceptor( entity );
  106   		if ( interceptor != null ) {
  107   			interceptor.clearDirty();
  108   		}
  109   	}
  110   
  111   	public static void markDirty(Object entity) {
  112   		FieldInterceptor interceptor = extractFieldInterceptor( entity );
  113   		if ( interceptor != null ) {
  114   			interceptor.dirty();
  115   		}
  116   	}
  117   }

Home » Hibernate-3.3.2.GA » org.hibernate » intercept » [javadoc | source]