public HazelcastInstance getHazelcastInstance(Settings settings) { final RegionFactory rf = settings.getRegionFactory(); if (rf == null) { logger.log(Level.SEVERE, "Hibernate 2nd level cache has not been enabled!"); return null; } if (rf instanceof RegionFactoryCacheProviderBridge) { final CacheProvider provider = ((RegionFactoryCacheProviderBridge) rf).getCacheProvider(); if (provider instanceof HazelcastCacheProvider) { return ((HazelcastCacheProvider) provider).getHazelcastInstance(); } logger.log(Level.WARNING, "Current 2nd level cache implementation is not HazelcastCacheProvider!"); } else if (rf instanceof HazelcastCacheRegionFactory) { return ((HazelcastCacheRegionFactory) rf).getHazelcastInstance(); } else { logger.log(Level.WARNING, "Current 2nd level cache implementation is not HazelcastCacheRegionFactory!"); } return null; }
public HazelcastInstance getHazelcastInstance(Settings settings) { final RegionFactory rf = settings.getRegionFactory(); if (rf == null) { logger.log(Level.SEVERE, "Hibernate 2nd level cache has not been enabled!"); return null; } if (rf instanceof RegionFactoryCacheProviderBridge) { final CacheProvider provider = ((RegionFactoryCacheProviderBridge) rf).getCacheProvider(); if (provider instanceof HazelcastCacheProvider) { return ((HazelcastCacheProvider) provider).getHazelcastInstance(); } logger.log(Level.WARNING, "Current 2nd level cache implementation is not HazelcastCacheProvider!"); } else if (rf instanceof AbstractHazelcastCacheRegionFactory) { return ((AbstractHazelcastCacheRegionFactory) rf).getHazelcastInstance(); } else { logger.log(Level.WARNING, "Current 2nd level cache implementation is not HazelcastCacheRegionFactory!"); } return null; }
/** * Standard constructor. */ public LocalRegionFactoryProxy() { RegionFactory rf = (RegionFactory) LocalSessionFactoryBean.getConfigTimeRegionFactory(); // absolutely needs thread-bound RegionFactory to initialize if (rf == null) { throw new IllegalStateException("No Hibernate RegionFactory found - " + "'cacheRegionFactory' property must be set on LocalSessionFactoryBean"); } this.regionFactory = rf; }
@Test @SuppressWarnings("serial") public void testLocalSessionFactoryBeanWithCacheRegionFactory() throws Exception { final RegionFactory regionFactory = new NoCachingRegionFactory(null); final List invocations = new ArrayList(); LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { @Override protected Configuration newConfiguration() { return new Configuration() { @Override public Configuration addInputStream(InputStream is) { try { is.close(); } catch (IOException ex) { } invocations.add("addResource"); return this; } }; } @Override protected SessionFactory newSessionFactory(Configuration config) { assertEquals(LocalRegionFactoryProxy.class.getName(), config.getProperty(Environment.CACHE_REGION_FACTORY)); assertSame(regionFactory, LocalSessionFactoryBean.getConfigTimeRegionFactory()); invocations.add("newSessionFactory"); return null; } }; sfb.setCacheRegionFactory(regionFactory); sfb.afterPropertiesSet(); assertTrue(sfb.getConfiguration() != null); assertEquals("newSessionFactory", invocations.get(0)); }
/** * Tries to extract <code>HazelcastInstance</code> from <code>SessionFactoryImplementor</code>. * * @param sessionFactory Hibernate <code>SessionFactory</code> to extract settings from * @return currently used <code>HazelcastInstance</code> or null if an error occurs. */ public static HazelcastInstance getHazelcastInstance(final SessionFactoryImplementor sessionFactory) { final Settings settings = sessionFactory.getSettings(); final RegionFactory rf = settings.getRegionFactory(); if (rf instanceof AbstractHazelcastCacheRegionFactory) { return ((AbstractHazelcastCacheRegionFactory) rf).getHazelcastInstance(); } else { LOGGER.warning("Current 2nd level cache implementation is not HazelcastCacheRegionFactory!"); } return null; }
public AccessType getDefaultAccessType() { try { Method method = RegionFactory.class.getMethod("getDefaultAccessType"); return (AccessType) ReflectionUtils.invokeMethod(method, this.regionFactory); } catch (NoSuchMethodException ex) { throw new IllegalStateException("getDefaultAccessType requires Hibernate 3.5+"); } }
@Override public void afterPropertiesSet() throws Exception { // Attempt to get the same EHCache cache manager from the session factory try { // Dirty little trick using reflection RegionFactory regionFactory = sessionFactory.getSettings().getRegionFactory(); ehCacheManager = (net.sf.ehcache.CacheManager) FieldUtils.readField(regionFactory, "manager", true); } catch (Exception e) { // It was not possible. Fallback to the default cache manager ehCacheManager = new net.sf.ehcache.CacheManager(); cleanUpEhCache = true; } }
/** * Set the Hibernate RegionFactory to use for the SessionFactory. * Allows for using a Spring-managed RegionFactory instance. * <p>Note: If this is set, the Hibernate settings should not define a * cache provider to avoid meaningless double configuration. * @see org.hibernate.cache.RegionFactory */ public void setCacheRegionFactory(RegionFactory cacheRegionFactory) { this.cacheRegionFactory = cacheRegionFactory; }