@Test public void testCreateCacheManagers() throws Exception { // no arg assertNotNull("create with no arg", EHCacheUtil.createCacheManager()); URL configURL = EHCacheUtil.class.getResource("/test-ehcache.xml"); assertNotNull(configURL); // string assertNotNull("create with string", EHCacheUtil.createCacheManager(configURL.getPath())); // url assertNotNull("create with url", EHCacheUtil.createCacheManager(configURL)); // inputstream assertNotNull("create with inputstream", EHCacheUtil.createCacheManager(configURL.openStream())); // config Configuration conf = ConfigurationFactory.parseConfiguration(configURL); assertNotNull(conf); assertNotNull("create with configuration", EHCacheUtil.createCacheManager(conf)); }
@Test public void testEHCacheCompatiblity() throws Exception { // get the default cache manager CacheManagerFactory factory = new DefaultCacheManagerFactory(); CacheManager manager = factory.getInstance(); assertEquals(Status.STATUS_ALIVE, manager.getStatus()); // create another unrelated cache manager Configuration conf = ConfigurationFactory.parseConfiguration(DefaultCacheManagerFactory.class.getResource("/test-ehcache.xml")); assertNotNull(conf); conf.setName("otherCache"); CacheManager other = CacheManager.create(conf); assertEquals(Status.STATUS_ALIVE, other.getStatus()); // shutdown this unrelated cache manager other.shutdown(); assertEquals(Status.STATUS_SHUTDOWN, other.getStatus()); // the default cache manager should be still running assertEquals(Status.STATUS_ALIVE, manager.getStatus()); factory.doStop(); // the default cache manger is shutdown assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus()); }
/** * Creates a unique cache manager. * * @param uniqueName the unique name, typically a test case class name * @return the unique cache manager, not null */ public static CacheManager createTestCacheManager(String uniqueName) { ArgumentChecker.notNull(uniqueName, "uniqueName"); if (UNIQUE_TEST_NAMES.putIfAbsent(uniqueName, uniqueName) != null) { throw new OpenGammaRuntimeException("CacheManager has already been created with unique name: " + uniqueName); } try { InputStream configStream = getTestEhCacheConfig(); Configuration config = ConfigurationFactory.parseConfiguration(configStream); config.setName(uniqueName); config.setUpdateCheck(false); return CacheManager.newInstance(config); } catch (CacheException ex) { throw new OpenGammaRuntimeException("Unable to create CacheManager", ex); } }
private static synchronized Configuration getEhCacheConfig() { String ehcacheConfigFile = DEFAULT_EHCACHE_CONFIG_FILE; String overrideEhcacheConfigFile = System.getProperty("ehcache.config"); // passed in by Ant if (overrideEhcacheConfigFile != null) { ehcacheConfigFile = overrideEhcacheConfigFile; System.err.println("Using ehcache.config from system property: " + ehcacheConfigFile); } else { System.err.println("Using default ehcache.config file name: " + ehcacheConfigFile); } try (InputStream resource = EHCacheUtils.class.getResourceAsStream(ehcacheConfigFile)) { Configuration config = ConfigurationFactory.parseConfiguration(resource); config.setUpdateCheck(false); return config; } catch (IOException ex) { throw new OpenGammaRuntimeException("Unable to read ehcache file", ex); } }
public void afterPropertiesSet() throws IOException, CacheException { log.info("Initializing EHCache CacheManager"); Configuration config = null; if (this.configLocation != null) { config = ConfigurationFactory .parseConfiguration(this.configLocation.getInputStream()); if (this.diskStoreLocation != null) { DiskStoreConfiguration dc = new DiskStoreConfiguration(); dc.setPath(this.diskStoreLocation.getFile().getAbsolutePath()); try { config.addDiskStore(dc); } catch (ObjectExistsException e) { log.warn("if you want to config distStore in spring," + " please remove diskStore in config file!", e); } } } if (config != null) { this.cacheManager = new CacheManager(config); } else { this.cacheManager = new CacheManager(); } if (this.cacheManagerName != null) { this.cacheManager.setName(this.cacheManagerName); } }
/** * Itializes the service by creating a manager object with a given configuration file. */ private void init( ) { Configuration configuration = ConfigurationFactory.parseConfiguration( ); configuration.setName( LUTECE_CACHEMANAGER_NAME ); _manager = CacheManager.create( configuration ); loadDefaults( ); loadCachesConfig( ); boolean bJmxMonitoring = AppPropertiesService.getProperty( PROPERTY_JMX_MONITORING, FALSE ).equals( TRUE ); if ( bJmxMonitoring ) { initJmxMonitoring( ); } }
public void afterPropertiesSet() throws Exception { Configuration configuration = ConfigurationFactory.parseConfiguration(); // Override configuration to make sure cache is stored in Airsonic home dir. File cacheDir = new File(SettingsService.getAirsonicHome(), "cache"); configuration.getDiskStoreConfiguration().setPath(cacheDir.getPath()); cacheManager = CacheManager.create(configuration); }
@Override public void afterPropertiesSet() throws CacheException { logger.info("Initializing EhCache CacheManager"); Configuration configuration = (this.configLocation != null ? EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration()); if (this.cacheManagerName != null) { configuration.setName(this.cacheManagerName); } if (this.shared) { // Old-school EhCache singleton sharing... // No way to find out whether we actually created a new CacheManager // or just received an existing singleton reference. this.cacheManager = CacheManager.create(configuration); } else if (this.acceptExisting) { // EhCache 2.5+: Reusing an existing CacheManager of the same name. // Basically the same code as in CacheManager.getInstance(String), // just storing whether we're dealing with an existing instance. synchronized (CacheManager.class) { this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName); if (this.cacheManager == null) { this.cacheManager = new CacheManager(configuration); } else { this.locallyManaged = false; } } } else { // Throwing an exception if a CacheManager of the same name exists already... this.cacheManager = new CacheManager(configuration); } }
public IndexedCollectionAdapter(BananaRama parent){ //This must be configurable in the future final Configuration conf = ConfigurationFactory .parseConfiguration(); conf.getDefaultCacheConfiguration() .getPersistenceConfiguration() .strategy(PersistenceConfiguration.Strategy.NONE); cacheManager = CacheManager.newInstance(conf); cache = cacheManager.addCacheIfAbsent(CACHE_NAME); this.parent = parent; }
public void afterPropertiesSet() throws Exception { Configuration configuration = ConfigurationFactory.parseConfiguration(); // Override configuration to make sure cache is stored in Subsonic home dir. File cacheDir = new File(SettingsService.getSubsonicHome(), "cache"); configuration.getDiskStoreConfiguration().setPath(cacheDir.getPath()); cacheManager = CacheManager.create(configuration); }
/** * {@inheritDoc} */ @Override public Configuration getConfiguration(Properties properties) throws CacheException { Configuration config = ConfigurationFactory.parseConfiguration(resolveEhcacheConfigFile()); postProcessConfiguration(config, properties); return config; }
@Produces @Singleton public CacheManager produce(Configuration configuration) { InputStream cacheConfigurationStream = EhServiceCacheManager.class.getResourceAsStream("cache.xml"); net.sf.ehcache.config.Configuration cacheConfiguration = ConfigurationFactory.parseConfiguration(cacheConfigurationStream); String cacheLocation = configuration.getCacheLocation(); if (cacheLocation!=null && !cacheLocation.isEmpty()) { log.info("Setting cache location to {}", cacheLocation); cacheConfiguration.getDiskStoreConfiguration().setPath(cacheLocation); } else log.trace("cache location {}", cacheConfiguration.getDiskStoreConfiguration().getPath()); return CacheManager.newInstance(cacheConfiguration); }
private void initializeEhcacheDiskStore(Environment environment) { DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration(); diskStoreConfiguration.setPath(environment.getProperty(HmpProperties.EHCACHE_DATA_DIR)); Configuration configuration = ConfigurationFactory.parseConfiguration(); configuration.addDiskStore(diskStoreConfiguration); CacheManager.newInstance(configuration); }
public void afterPropertiesSet() throws Exception { Configuration configuration = ConfigurationFactory.parseConfiguration(); // Override configuration to make sure cache is stored in Madsonic home dir. File cacheDir = new File(SettingsService.getMadsonicHome(), "cache"); configuration.getDiskStoreConfiguration().setPath(cacheDir.getPath()); cacheManager = CacheManager.create(configuration); }
public void afterPropertiesSet() throws IOException, CacheException { logger.info("Initializing EhCache CacheManager"); InputStream is = (this.configLocation != null ? this.configLocation.getInputStream() : null); try { // A bit convoluted for EhCache 1.x/2.0 compatibility. // To be much simpler once we require EhCache 2.1+ if (this.cacheManagerName != null) { if (this.shared && createWithConfiguration == null) { // No CacheManager.create(Configuration) method available before EhCache 2.1; // can only set CacheManager name after creation. this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create()); this.cacheManager.setName(this.cacheManagerName); } else { Configuration configuration = (is != null ? ConfigurationFactory.parseConfiguration(is) : ConfigurationFactory.parseConfiguration()); configuration.setName(this.cacheManagerName); if (this.shared) { this.cacheManager = (CacheManager) ReflectionUtils.invokeMethod(createWithConfiguration, null, configuration); } else { this.cacheManager = new CacheManager(configuration); } } } // For strict backwards compatibility: use simplest possible constructors... else if (this.shared) { this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create()); } else { this.cacheManager = (is != null ? new CacheManager(is) : new CacheManager()); } } finally { if (is != null) { is.close(); } } }
@Override public void afterPropertiesSet() throws IOException, CacheException { logger.info("Initializing EhCache CacheManager"); InputStream is = (this.configLocation != null ? this.configLocation.getInputStream() : null); try { Configuration configuration = (is != null ? ConfigurationFactory.parseConfiguration(is) : ConfigurationFactory.parseConfiguration()); if (this.cacheManagerName != null) { configuration.setName(this.cacheManagerName); } if (this.shared) { // Old-school EhCache singleton sharing... // No way to find out whether we actually created a new CacheManager // or just received an existing singleton reference. this.cacheManager = CacheManager.create(configuration); } else if (this.acceptExisting) { // EhCache 2.5+: Reusing an existing CacheManager of the same name. // Basically the same code as in CacheManager.getInstance(String), // just storing whether we're dealing with an existing instance. synchronized (CacheManager.class) { this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName); if (this.cacheManager == null) { this.cacheManager = new CacheManager(configuration); } else { this.locallyManaged = false; } } } else { // Throwing an exception if a CacheManager of the same name exists already... this.cacheManager = new CacheManager(configuration); } } finally { if (is != null) { is.close(); } } }
/** * Parses the configuration from the provided resource location * * @param pathToResource * the path to the resource * @return the parsed configuration * @throws CacheException * in case parsing failed */ private static Configuration parseConfiguration(String pathToResource) throws CacheException { URL confFileURL = SingletonEhCacheManagerFactory.class.getResource(pathToResource); if (confFileURL == null) { throw new CacheException("The path to the Ehcache " + "configuration file is not defined."); } return ConfigurationFactory.parseConfiguration(confFileURL); }
/** * Build an EhCache {@link CacheManager} from the default configuration. * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path * (that is, default EhCache initialization - as defined in the EhCache docs - will apply). * If no configuration file can be found, a fail-safe fallback configuration will be used. * @return the new EhCache CacheManager * @throws CacheException in case of configuration parsing failure */ public static CacheManager buildCacheManager() throws CacheException { return new CacheManager(ConfigurationFactory.parseConfiguration()); }
/** * Build an EhCache {@link CacheManager} from the default configuration. * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path * (that is, default EhCache initialization - as defined in the EhCache docs - will apply). * If no configuration file can be found, a fail-safe fallback configuration will be used. * @param name the desired name of the cache manager * @return the new EhCache CacheManager * @throws CacheException in case of configuration parsing failure */ public static CacheManager buildCacheManager(String name) throws CacheException { Configuration configuration = ConfigurationFactory.parseConfiguration(); configuration.setName(name); return new CacheManager(configuration); }
/** * Read the cache conifuration from the specified resource. * This function is intended to be overrideable to allow for programmatic * cache configuration. * @param configPath * @return Configuration * @throws IOException */ protected Configuration getConfiguration(String configPath) throws IOException { InputStream configStream = ResourceLoader.open(configPath); return ConfigurationFactory.parseConfiguration(configStream); }