Java Code Examples for org.ehcache.xml.XmlConfiguration#newCacheConfigurationBuilderFromTemplate()

The following examples show how to use org.ehcache.xml.XmlConfiguration#newCacheConfigurationBuilderFromTemplate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EHCacheTokenReplayCache.java    From cxf with Apache License 2.0 6 votes vote down vote up
public EHCacheTokenReplayCache(String configFile, Bus bus)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    if (bus == null) {
        bus = BusFactory.getThreadDefaultBus(true);
    }
    URL configFileURL = null;
    try {
        configFileURL =
                ResourceUtils.getClasspathResourceURL(configFile, EHCacheTokenReplayCache.class, bus);
    } catch (Exception ex) {
        // ignore
    }

    XmlConfiguration xmlConfig = new XmlConfiguration(getConfigFileURL(configFileURL));
    CacheConfigurationBuilder<String, EHCacheValue> configurationBuilder =
            xmlConfig.newCacheConfigurationBuilderFromTemplate(CACHE_KEY,
                    String.class, EHCacheValue.class);
    // Note, we don't require strong random values here
    String diskKey = CACHE_KEY + "-" + Math.abs(new Random().nextInt());
    cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache(CACHE_KEY, configurationBuilder)
            .with(CacheManagerBuilder.persistence(new File(System.getProperty("java.io.tmpdir"), diskKey))).build();

    cacheManager.init();
    cache = cacheManager.getCache(CACHE_KEY, String.class, EHCacheValue.class);

}
 
Example 2
Source File: EHCache3Manager.java    From javalite with Apache License 2.0 6 votes vote down vote up
public EHCache3Manager() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    URL url = getClass().getResource("/activejdbc-ehcache.xml");
    if(url == null){
        throw new InitException("You are using " + getClass().getName() + " but failed to provide a EHCache configuration file on classpath: activejdbc-ehcache.xml");
    }

    XmlConfiguration xmlConfiguration = new XmlConfiguration(url);

    cacheTemplate = xmlConfiguration.newCacheConfigurationBuilderFromTemplate("activejdbc", String.class, Object.class);

    if(cacheTemplate == null){
        throw new InitException("Please, provide a <cache-template name=\"activejdbc\"> element in  activejdbc-ehcache.xml file");
    }
    cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
    cacheManager.init();
}
 
Example 3
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void xmlTemplateSample() throws Exception {
  // tag::xmlTemplate[]
  XmlConfiguration xmlConfiguration = new XmlConfiguration(getClass().getResource("/configs/docs/template-sample.xml"));
  CacheConfigurationBuilder<Long, String> configurationBuilder = xmlConfiguration.newCacheConfigurationBuilderFromTemplate("example", Long.class, String.class); // <1>
  configurationBuilder = configurationBuilder.withResourcePools(ResourcePoolsBuilder.heap(1000)); // <2>
  // end::xmlTemplate[]
}
 
Example 4
Source File: EHCacheTokenStore.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EHCacheTokenStore(String key, Bus b, URL configFileURL) throws TokenStoreException {
    bus = b;
    if (bus != null) {
        b.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this);
    }

    this.key = key;
    try {
        XmlConfiguration xmlConfig = new XmlConfiguration(configFileURL);

        // Exclude the endpoint info bit added in TokenStoreUtils when getting the template name
        String template = key;
        if (template.contains("-")) {
            template = key.substring(0, key.lastIndexOf('-'));
        }

        CacheConfigurationBuilder<String, SecurityToken> configurationBuilder =
                xmlConfig.newCacheConfigurationBuilderFromTemplate(template,
                        String.class, SecurityToken.class);

        cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache(key, configurationBuilder).build();

        cacheManager.init();
        cache = cacheManager.getCache(key, String.class, SecurityToken.class);

    } catch (Exception e) {
        throw new TokenStoreException(e);
    }
}
 
Example 5
Source File: EHCacheSPStateManager.java    From cxf with Apache License 2.0 4 votes vote down vote up
public EHCacheSPStateManager(String configFile, Bus bus)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    if (bus == null) {
        bus = BusFactory.getThreadDefaultBus(true);
    }

    URL configFileURL = null;
    try {
        configFileURL =
            ResourceUtils.getClasspathResourceURL(configFile, EHCacheSPStateManager.class, bus);
    } catch (Exception ex) {
        // ignore
    }

    XmlConfiguration xmlConfig = new XmlConfiguration(getConfigFileURL(configFileURL));
    CacheConfigurationBuilder<String, RequestState> requestConfigurationBuilder =
            xmlConfig.newCacheConfigurationBuilderFromTemplate(REQUEST_CACHE_KEY,
                    String.class, RequestState.class);

    // Note, we don't require strong random values here
    String diskKey = REQUEST_CACHE_KEY + "-" + Math.abs(new Random().nextInt());
    requestCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .withCache(REQUEST_CACHE_KEY, requestConfigurationBuilder)
            .with(CacheManagerBuilder.persistence(new File(System.getProperty("java.io.tmpdir"), diskKey))).build();

    requestCacheManager.init();
    requestCache = requestCacheManager.getCache(REQUEST_CACHE_KEY, String.class, RequestState.class);

    CacheConfigurationBuilder<String, ResponseState> responseConfigurationBuilder =
            xmlConfig.newCacheConfigurationBuilderFromTemplate(RESPONSE_CACHE_KEY,
                    String.class, ResponseState.class);

    // Note, we don't require strong random values here
    diskKey = RESPONSE_CACHE_KEY + "-" + Math.abs(new Random().nextInt());
    responseCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .withCache(RESPONSE_CACHE_KEY, responseConfigurationBuilder)
            .with(CacheManagerBuilder.persistence(new File(System.getProperty("java.io.tmpdir"), diskKey))).build();

    responseCacheManager.init();
    responseCache = responseCacheManager.getCache(RESPONSE_CACHE_KEY, String.class, ResponseState.class);
}