Java Code Examples for javax.cache.spi.CachingProvider#getDefaultURI()

The following examples show how to use javax.cache.spi.CachingProvider#getDefaultURI() . 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: RepositoryCacheManager.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Reference(optional = true)
void setCachingProvider(CachingProvider cachingProvider) {
    if (cachingProvider == null) {
        throw new IllegalArgumentException("CachingProvider must not be null");
    } else if (cachingProvider.getDefaultClassLoader() == null) {
        throw new IllegalArgumentException("CachingProvider default ClassLoader must not be null");
    } else if (cachingProvider.getDefaultProperties() == null) {
        throw new IllegalArgumentException("CachingProvider default Properties must not be null");
    } else if (cachingProvider.getDefaultURI() == null) {
        throw new IllegalArgumentException("CachingProvider default URI must not be null");
    }

    this.cachingProvider = cachingProvider;
    this.classLoaderReference = new WeakReference<>(cachingProvider.getDefaultClassLoader());
    this.properties = cachingProvider.getDefaultProperties();
    this.uri = cachingProvider.getDefaultURI();
}
 
Example 2
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testReuseCacheManagerGetCache() throws Exception {
    CachingProvider provider = Caching.getCachingProvider();
    URI uri = provider.getDefaultURI();

    CacheManager cacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
    assertFalse(cacheManager.isClosed());
    cacheManager.close();
    assertTrue(cacheManager.isClosed());

    try {
        cacheManager.getCache("nonExistent", null, null);
        fail();
    } catch (IllegalStateException e) {
        //expected
    }

    CacheManager otherCacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
    assertFalse(otherCacheManager.isClosed());

    assertNotSame(cacheManager, otherCacheManager);
}
 
Example 3
Source File: JCacheOAuthDataProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static CacheManager createCacheManager(String configFile, Bus bus) {
    if (bus == null) {
        bus = BusFactory.getThreadDefaultBus(true);
    }

    CachingProvider provider = Caching.getCachingProvider();

    URI configFileURI;
    try {
        configFileURI = getClasspathResourceURL(configFile, JCacheOAuthDataProvider.class, bus).toURI();
    } catch (Exception ex) {
        configFileURI = provider.getDefaultURI();
    }

    return provider.getCacheManager(configFileURI, Thread.currentThread().getContextClassLoader());
}
 
Example 4
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * The URI of the CacheManager returned by {@link CachingProvider#getCacheManager(java.net.URI, ClassLoader)}
 * is the same as the URI used in the invocation.
 */
@Test
public void getCacheManagerSameURI() {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);
  URI uri = provider.getDefaultURI();

  CacheManager manager = provider.getCacheManager(uri, contextLoader);
  assertEquals(uri, manager.getURI());

  // using a different ClassLoader
  ClassLoader otherLoader = new MyClassLoader(contextLoader);
  CachingProvider otherProvider = Caching.getCachingProvider(otherLoader);
  assertNotSame(provider, otherProvider);

  CacheManager otherManager = otherProvider.getCacheManager(uri, contextLoader);
  assertNotSame(manager, otherManager);
  assertEquals(uri, otherManager.getURI());
}
 
Example 5
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Close all CacheManagers from a CachingProvider, each CacheManager being
 * based on a different ClassLoader.
 */
@Test
public void closeAllCacheManagers() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close();

  assertNotSame(manager1, provider.getCacheManager(uri, loader1));
  assertNotSame(manager2, provider.getCacheManager(uri, loader2));
  assertNotSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 6
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Closing a single CacheManager from a CachingProvider when there are
 * multiple available across different ClassLoaders.
 */
@Test
public void closeCacheManager() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close(manager2.getURI(), loader2);

  assertSame(manager1, provider.getCacheManager(uri, loader1));
  assertNotSame(manager2, provider.getCacheManager(uri, loader2));
  assertSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 7
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testReuseCacheManager() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  URI uri = provider.getDefaultURI();

  CacheManager cacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(cacheManager.isClosed());
  cacheManager.close();
  assertTrue(cacheManager.isClosed());

  try {
    cacheManager.createCache("Dog", new MutableConfiguration());
    fail();
  } catch (IllegalStateException e) {
    //expected
  }

  CacheManager otherCacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(otherCacheManager.isClosed());

  assertNotSame(cacheManager, otherCacheManager);
}
 
Example 8
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * https://github.com/jsr107/jsr107tck/issues/104
 * Changed in 1.1, don't do getCache(..., null, null)
 */
@Test
public void testReuseCacheManagerGetCache() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  URI uri = provider.getDefaultURI();

  CacheManager cacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(cacheManager.isClosed());
  cacheManager.close();
  assertTrue(cacheManager.isClosed());

  try {
    cacheManager.getCache("nonExistent", Integer.class, Integer.class);
    fail();
  } catch (IllegalStateException e) {
    //expected
  }

  CacheManager otherCacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(otherCacheManager.isClosed());

  assertNotSame(cacheManager, otherCacheManager);
}
 
Example 9
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to close CacheManagers using URIs and/or ClassLoaders that don't
 * have associated CacheManagers.
 */
@Test
public void closeClassLoader() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close(contextLoader);
  provider.close(provider.getDefaultURI(), contextLoader);
  provider.close(provider.getDefaultURI(), contextLoader);
  provider.close(provider.getDefaultURI(), contextLoader);

  assertSame(manager1, provider.getCacheManager(uri, loader1));
  assertSame(manager2, provider.getCacheManager(uri, loader2));
  assertSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 10
Source File: CacheManagerManagementTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains a CacheManager using a string-based name from the default
 * CachingProvider.
 *
 * @return a CacheManager
 * @throws Exception
 */
public static CacheManager getCacheManager() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();

  URI uri = provider.getDefaultURI();

  return Caching.getCachingProvider().getCacheManager(uri, provider.getDefaultClassLoader());
}
 
Example 11
Source File: CachingTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Multiple invocations of {@link CachingProvider#getCacheManager(java.net.URI, ClassLoader)} with the same name
 * return the same CacheManager instance
 */
@Test
public void getCacheManager_URI() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();

  URI uri = provider.getDefaultURI();

  CacheManager manager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertNotNull(manager);
  assertSame(manager, provider.getCacheManager(uri, provider.getDefaultClassLoader()));

  assertEquals(uri, manager.getURI());
}