package org.hibernate.ogm.org.hibernate.ogm.util;

import java.util.Map;

import org.infinispan.Cache;
import org.infinispan.manager.CacheManager;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.ogm.factory.CacheLifecycleManager;
import org.hibernate.ogm.grid.Key;

/**
 * Helper class to be refactored and abstracted from specific grid implementation
 * @author Emmanuel Bernard
 */
public class CacheManagerHelper {
	public static final String ENTITY_CACHE = "ENTITIES";

	public static CacheManager getCacheManager(SessionFactoryImplementor factory) {
		final SessionFactoryObserver sessionFactoryObserver = factory.getFactoryObserver();
		if ( sessionFactoryObserver instanceof CacheLifecycleManager ) {
			return ( (CacheLifecycleManager) factory).getCacheManager();
		}
		else {
			StringBuilder error = new StringBuilder("Cannot get CacheManager for OGM. ");
			if ( sessionFactoryObserver == null ) {
				error.append("SessionFactoryObserver not configured");
			}
			else {
				error.append("SessionFactoryObserver not of type " + CacheLifecycleManager.class);
			}
			throw new HibernateException( error.toString() );
		}
	}

	public static Cache<Key, Map<String, Object>> getEntityCache(SessionFactoryImplementor factory) {
		final CacheManager cacheManager = CacheManagerHelper.getCacheManager( factory );
		final Cache<Key, Map<String, Object>> cache = cacheManager.getCache( ENTITY_CACHE );
		return cache;
	}
}

@@ -655,32 +655,195 @@
 ntor factory) %7B%0A
+%09%09return getCacheLifecyleManager(factory).getCacheManager();%0A%09%7D%0A%0A%09public static CacheLifecycleManager getCacheLifecyleManager(SessionFactoryImplementor factory) %7B%0A
 %09%09final SessionF
@@ -912,16 +912,16 @@
 rver();%0A
-
 %09%09if ( s
@@ -988,18 +988,16 @@
 %09%09return
- (
  (CacheL
@@ -1017,34 +1017,30 @@
 er) 
-factory).getCacheManager()
+sessionFactoryObserver
 ;%0A%09%09

Improve helper class to extract metadata services