Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » event » def » [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.event.def;
   26   
   27   import java.io.Serializable;
   28   
   29   import org.slf4j.Logger;
   30   import org.slf4j.LoggerFactory;
   31   import org.hibernate.HibernateException;
   32   import org.hibernate.cache.CacheKey;
   33   import org.hibernate.cache.entry.CollectionCacheEntry;
   34   import org.hibernate.collection.PersistentCollection;
   35   import org.hibernate.engine.CollectionEntry;
   36   import org.hibernate.engine.PersistenceContext;
   37   import org.hibernate.engine.SessionFactoryImplementor;
   38   import org.hibernate.event.InitializeCollectionEvent;
   39   import org.hibernate.event.InitializeCollectionEventListener;
   40   import org.hibernate.engine.SessionImplementor;
   41   import org.hibernate.persister.collection.CollectionPersister;
   42   import org.hibernate.pretty.MessageHelper;
   43   
   44   /**
   45    * @author Gavin King
   46    */
   47   public class DefaultInitializeCollectionEventListener implements InitializeCollectionEventListener {
   48   
   49   	private static final Logger log = LoggerFactory.getLogger(DefaultInitializeCollectionEventListener.class);
   50   
   51   	/**
   52   	 * called by a collection that wants to initialize itself
   53   	 */
   54   	public void onInitializeCollection(InitializeCollectionEvent event)
   55   	throws HibernateException {
   56   
   57   		PersistentCollection collection = event.getCollection();
   58   		SessionImplementor source = event.getSession();
   59   
   60   		CollectionEntry ce = source.getPersistenceContext().getCollectionEntry(collection);
   61   		if (ce==null) throw new HibernateException("collection was evicted");
   62   		if ( !collection.wasInitialized() ) {
   63   			if ( log.isTraceEnabled() ) {
   64   				log.trace(
   65   						"initializing collection " +
   66   						MessageHelper.collectionInfoString( ce.getLoadedPersister(), ce.getLoadedKey(), source.getFactory() )
   67   					);
   68   			}
   69   
   70   			log.trace("checking second-level cache");
   71   			final boolean foundInCache = initializeCollectionFromCache(
   72   					ce.getLoadedKey(),
   73   					ce.getLoadedPersister(),
   74   					collection,
   75   					source
   76   				);
   77   
   78   			if (foundInCache) {
   79   				log.trace("collection initialized from cache");
   80   			}
   81   			else {
   82   				log.trace("collection not cached");
   83   				ce.getLoadedPersister().initialize( ce.getLoadedKey(), source );
   84   				log.trace("collection initialized");
   85   
   86   				if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
   87   					source.getFactory().getStatisticsImplementor().fetchCollection( 
   88   							ce.getLoadedPersister().getRole() 
   89   						);
   90   				}
   91   			}
   92   		}
   93   	}
   94   
   95   	/**
   96   	 * Try to initialize a collection from the cache
   97   	 *
   98   	 * @param id The id of the collection of initialize
   99   	 * @param persister The collection persister
  100   	 * @param collection The collection to initialize
  101   	 * @param source The originating session
  102   	 * @return true if we were able to initialize the collection from the cache;
  103   	 * false otherwise.
  104   	 */
  105   	private boolean initializeCollectionFromCache(
  106   			Serializable id,
  107   			CollectionPersister persister,
  108   			PersistentCollection collection,
  109   			SessionImplementor source) {
  110   
  111   		if ( !source.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( source ) ) {
  112   			log.trace( "disregarding cached version (if any) of collection due to enabled filters ");
  113   			return false;
  114   		}
  115   
  116   		final boolean useCache = persister.hasCache() && 
  117   				source.getCacheMode().isGetEnabled();
  118   
  119   		if ( !useCache ) {
  120   			return false;
  121   		}
  122   		else {
  123   			
  124   			final SessionFactoryImplementor factory = source.getFactory();
  125   
  126   			final CacheKey ck = new CacheKey( 
  127   					id, 
  128   					persister.getKeyType(), 
  129   					persister.getRole(), 
  130   					source.getEntityMode(), 
  131   					source.getFactory() 
  132   				);
  133   			Object ce = persister.getCacheAccessStrategy().get( ck, source.getTimestamp() );
  134   			
  135   			if ( factory.getStatistics().isStatisticsEnabled() ) {
  136   				if ( ce == null ) {
  137   					factory.getStatisticsImplementor().secondLevelCacheMiss(
  138   							persister.getCacheAccessStrategy().getRegion().getName()
  139   					);
  140   				}
  141   				else {
  142   					factory.getStatisticsImplementor().secondLevelCacheHit(
  143   							persister.getCacheAccessStrategy().getRegion().getName()
  144   					);
  145   				}
  146   
  147   				
  148   			}
  149   			
  150   			if (ce==null) {
  151   				return false;
  152   			}
  153   			else {
  154   
  155   				CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure()
  156   						.destructure(ce, factory);
  157   			
  158   				final PersistenceContext persistenceContext = source.getPersistenceContext();
  159   				cacheEntry.assemble(
  160   						collection, 
  161   						persister,  
  162   						persistenceContext.getCollectionOwner(id, persister)
  163   					);
  164   				persistenceContext.getCollectionEntry(collection).postInitialize(collection);
  165   				//addInitializedCollection(collection, persister, id);
  166   				return true;
  167   			}
  168   			
  169   		}
  170   	}
  171   
  172   
  173   }

Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » event » def » [javadoc | source]