Search in sources :

Example 16 with CacheManager

use of net.sf.ehcache.CacheManager in project cxf by apache.

the class EHCacheUtilTest method testCreateCacheManager.

@Test
public void testCreateCacheManager() {
    Configuration conf = ConfigurationFactory.parseConfiguration(EHCacheUtil.class.getResource("/cxf-test-ehcache.xml"));
    assertNotNull(conf);
    conf.setName("testCache");
    CacheManager manager1 = EHCacheUtil.createCacheManager(conf);
    assertNotNull(manager1);
    CacheManager manager2 = EHCacheUtil.createCacheManager();
    assertNotNull(manager2);
    manager1.shutdown();
    assertEquals(Status.STATUS_SHUTDOWN, manager1.getStatus());
    assertEquals(Status.STATUS_ALIVE, manager2.getStatus());
    manager2.shutdown();
    assertEquals(Status.STATUS_SHUTDOWN, manager2.getStatus());
}
Also used : Configuration(net.sf.ehcache.config.Configuration) CacheManager(net.sf.ehcache.CacheManager) Test(org.junit.Test)

Example 17 with CacheManager

use of net.sf.ehcache.CacheManager in project cxf by apache.

the class EHCacheUtil method createCacheManager.

public static CacheManager createCacheManager(String configFile, Bus bus) {
    if (bus == null) {
        bus = BusFactory.getThreadDefaultBus(true);
    }
    URL configFileURL = null;
    try {
        configFileURL = ResourceUtils.getClasspathResourceURL(configFile, DefaultEHCacheOAuthDataProvider.class, bus);
    } catch (Exception ex) {
    // ignore
    }
    CacheManager cacheManager = null;
    if (configFileURL == null) {
        cacheManager = createCacheManager();
    } else {
        Configuration conf = ConfigurationFactory.parseConfiguration(configFileURL);
        if (bus != null) {
            conf.setName(bus.getId());
            DiskStoreConfiguration dsc = conf.getDiskStoreConfiguration();
            if (dsc != null && "java.io.tmpdir".equals(dsc.getOriginalPath())) {
                String path = conf.getDiskStoreConfiguration().getPath() + File.separator + bus.getId();
                conf.getDiskStoreConfiguration().setPath(path);
            }
        }
        cacheManager = EHCacheUtil.createCacheManager(conf);
    }
    return cacheManager;
}
Also used : DiskStoreConfiguration(net.sf.ehcache.config.DiskStoreConfiguration) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Configuration(net.sf.ehcache.config.Configuration) DefaultEHCacheOAuthDataProvider(org.apache.cxf.rs.security.oauth2.provider.DefaultEHCacheOAuthDataProvider) CacheManager(net.sf.ehcache.CacheManager) DiskStoreConfiguration(net.sf.ehcache.config.DiskStoreConfiguration) URL(java.net.URL) CacheException(net.sf.ehcache.CacheException)

Example 18 with CacheManager

use of net.sf.ehcache.CacheManager in project cxf by apache.

the class JAXRSOAuth2TlsTest method startServers.

@BeforeClass
public static void startServers() throws Exception {
    CacheManager manager = EHCacheUtil.createCacheManager("cxf-oauth2-ehcache.xml", null);
    if (manager != null) {
        manager.clearAll();
    }
    assertTrue("server did not launch correctly", launchServer(BookServerOAuth2Tls.class, true));
}
Also used : CacheManager(net.sf.ehcache.CacheManager) BeforeClass(org.junit.BeforeClass)

Example 19 with CacheManager

use of net.sf.ehcache.CacheManager in project hibernate-orm by hibernate.

the class EhcacheRegionFactory method useNormalCacheManager.

/**
 * Locate the CacheManager during start-up.  protected to allow for subclassing
 * such as SingletonEhcacheRegionFactory
 */
protected static CacheManager useNormalCacheManager(SessionFactoryOptions settings, Map properties) {
    try {
        String configurationResourceName = null;
        if (properties != null) {
            configurationResourceName = (String) properties.get(EHCACHE_CONFIGURATION_RESOURCE_NAME);
        }
        if (configurationResourceName == null || configurationResourceName.length() == 0) {
            final Configuration configuration = ConfigurationFactory.parseConfiguration();
            setCacheManagerNameIfNeeded(settings, configuration, properties);
            return new CacheManager(configuration);
        } else {
            final URL url = loadResource(configurationResourceName, settings);
            final Configuration configuration = HibernateEhcacheUtils.loadAndCorrectConfiguration(url);
            setCacheManagerNameIfNeeded(settings, configuration, properties);
            return new CacheManager(configuration);
        }
    } catch (net.sf.ehcache.CacheException e) {
        if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " + "CacheManager using the diskStorePath")) {
            throw new CacheException("Attempt to restart an already started EhCacheRegionFactory. " + "Use sessionFactory.close() between repeated calls to buildSessionFactory. " + "Consider using SingletonEhCacheRegionFactory. Error from ehcache was: " + e.getMessage());
        } else {
            throw new CacheException(e);
        }
    }
}
Also used : Configuration(net.sf.ehcache.config.Configuration) CacheException(org.hibernate.cache.CacheException) CacheManager(net.sf.ehcache.CacheManager) URL(java.net.URL)

Example 20 with CacheManager

use of net.sf.ehcache.CacheManager in project openmrs-core by openmrs.

the class OpenmrsClassLoader method destroyInstance.

/**
 * Destroy the current instance of the classloader. Note**: After calling this and after the new
 * service is set up, All classes using this instance should be flushed. This would allow all
 * java classes that were loaded by the old instance variable to be gc'd and modules to load in
 * new java classes
 *
 * @see #flushInstance()
 */
public static void destroyInstance() {
    // remove all thread references to this class
    // Walk up all the way to the root thread group
    ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
    ThreadGroup parent;
    while ((parent = rootGroup.getParent()) != null) {
        rootGroup = parent;
    }
    log.info("this classloader hashcode: " + OpenmrsClassLoaderHolder.INSTANCE.hashCode());
    // Shut down and remove all cache managers.
    List<CacheManager> knownCacheManagers = CacheManager.ALL_CACHE_MANAGERS;
    while (!knownCacheManagers.isEmpty()) {
        CacheManager cacheManager = CacheManager.ALL_CACHE_MANAGERS.get(0);
        try {
            // This shuts down and removes the cache manager.
            cacheManager.shutdown();
            // Just in case the the timer does not stop, set the cacheManager
            // timer to null because it references this class loader.
            Field field = cacheManager.getClass().getDeclaredField("cacheManagerTimer");
            field.setAccessible(true);
            field.set(cacheManager, null);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    OpenmrsClassScanner.destroyInstance();
    OpenmrsClassLoaderHolder.INSTANCE = null;
}
Also used : Field(java.lang.reflect.Field) CacheManager(net.sf.ehcache.CacheManager) APIException(org.openmrs.api.APIException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) SchedulerException(org.openmrs.scheduler.SchedulerException)

Aggregations

CacheManager (net.sf.ehcache.CacheManager)102 Cache (net.sf.ehcache.Cache)55 ClassPathResource (org.springframework.core.io.ClassPathResource)21 Element (net.sf.ehcache.Element)20 Configuration (net.sf.ehcache.config.Configuration)18 Test (org.junit.Test)18 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)17 MarkupCache (org.apache.wicket.markup.MarkupCache)10 CacheException (net.sf.ehcache.CacheException)9 IOException (java.io.IOException)7 Ehcache (net.sf.ehcache.Ehcache)7 UpdatingSelfPopulatingCache (net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache)6 URL (java.net.URL)5 BlockingCache (net.sf.ehcache.constructs.blocking.BlockingCache)5 SelfPopulatingCache (net.sf.ehcache.constructs.blocking.SelfPopulatingCache)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)4 PersistenceConfiguration (net.sf.ehcache.config.PersistenceConfiguration)4 Around (org.aspectj.lang.annotation.Around)4 Before (org.junit.Before)4 File (java.io.File)3