/** Tries to load the class specified otherwise defaults to null */ public Map<String, CacheManagerPeerListener> createCachePeerListeners() { String className = null; Map<String, CacheManagerPeerListener> cacheManagerPeerListeners = new HashMap<String, CacheManagerPeerListener>(); List<FactoryConfiguration> cacheManagerPeerListenerFactoryConfigurations = configuration.getCacheManagerPeerListenerFactoryConfigurations(); boolean first = true; for (FactoryConfiguration factoryConfiguration : cacheManagerPeerListenerFactoryConfigurations) { if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null) { LOG.fine( "No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener."); return null; } else { CacheManagerPeerListenerFactory cacheManagerPeerListenerFactory = (CacheManagerPeerListenerFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); CacheManagerPeerListener cacheManagerPeerListener = cacheManagerPeerListenerFactory.createCachePeerListener(cacheManager, properties); cacheManagerPeerListeners.put( cacheManagerPeerListener.getScheme(), cacheManagerPeerListener); } } return cacheManagerPeerListeners; }
@Test public void testOverwritesCacheManager() throws NoSuchFieldException, IllegalAccessException { URL resource = ClassLoaderUtil.getStandardClassLoader().getResource("hibernate-config/ehcache.xml"); config.setProperty("net.sf.ehcache.configurationResourceName", "file://" + resource.getFile()); config.setProperty("net.sf.ehcache.cacheManagerName", "overwrittenCacheManagerName"); SessionFactory sessionFactory = config.buildSessionFactory(); final Field cache_managers_map = CacheManager.class.getDeclaredField("CACHE_MANAGERS_MAP"); cache_managers_map.setAccessible(true); assertThat(((Map) cache_managers_map.get(null)).get("tc"), nullValue()); assertThat( ((Map) cache_managers_map.get(null)).get("overwrittenCacheManagerName"), notNullValue()); sessionFactory.close(); }
@Test public void testLoadingFromOutsideTheClasspath() { URL resource = ClassLoaderUtil.getStandardClassLoader().getResource("hibernate-config/ehcache.xml"); config.setProperty("net.sf.ehcache.configurationResourceName", "file://" + resource.getFile()); SessionFactory sessionFactory = null; try { sessionFactory = config.buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); fail("This should have succeeded"); } assertNotNull("Session factory should have been successfully created!", sessionFactory); sessionFactory.close(); }
/** * Tries to load the class specified otherwise defaults to null. * * @param factoryConfiguration */ private static CacheLoader createCacheLoader( CacheConfiguration.CacheLoaderFactoryConfiguration factoryConfiguration, Ehcache cache) { String className = null; CacheLoader cacheLoader = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null) { LOG.fine("CacheLoader factory not configured. Skipping..."); } else { CacheLoaderFactory factory = (CacheLoaderFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); cacheLoader = factory.createCacheLoader(cache, properties); } return cacheLoader; }
/** * Tries to load the class specified. * * @return If there is none returns null. */ public final CacheManagerEventListener createCacheManagerEventListener() throws CacheException { String className = null; FactoryConfiguration cacheManagerEventListenerFactoryConfiguration = configuration.getCacheManagerEventListenerFactoryConfiguration(); if (cacheManagerEventListenerFactoryConfiguration != null) { className = cacheManagerEventListenerFactoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.fine("No CacheManagerEventListenerFactory class specified. Skipping..."); return null; } else { CacheManagerEventListenerFactory factory = (CacheManagerEventListenerFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( cacheManagerEventListenerFactoryConfiguration.properties, cacheManagerEventListenerFactoryConfiguration.getPropertySeparator()); return factory.createCacheManagerEventListener(properties); } }
/** * Tries to create a CacheLoader from the configuration using the factory specified. * * @return The CacheExceptionHandler, or null if it could not be found. */ public final CacheExceptionHandler createCacheExceptionHandler( CacheConfiguration.CacheExceptionHandlerFactoryConfiguration factoryConfiguration) throws CacheException { String className = null; CacheExceptionHandler cacheExceptionHandler = null; if (factoryConfiguration != null) { className = factoryConfiguration.getFullyQualifiedClassPath(); } if (className == null || className.length() == 0) { LOG.fine("No CacheExceptionHandlerFactory class specified. Skipping..."); } else { CacheExceptionHandlerFactory factory = (CacheExceptionHandlerFactory) ClassLoaderUtil.createNewInstance(className); Properties properties = PropertyUtil.parseProperties( factoryConfiguration.getProperties(), factoryConfiguration.getPropertySeparator()); return factory.createExceptionHandler(properties); } return cacheExceptionHandler; }