001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.cache.jcache; 018 019import java.net.URI; 020import java.util.Properties; 021import javax.cache.CacheManager; 022import javax.cache.Caching; 023 024import org.springframework.beans.factory.BeanClassLoaderAware; 025import org.springframework.beans.factory.DisposableBean; 026import org.springframework.beans.factory.FactoryBean; 027import org.springframework.beans.factory.InitializingBean; 028 029/** 030 * {@link FactoryBean} for a JCache {@link javax.cache.CacheManager}, 031 * obtaining a pre-defined CacheManager by name through the standard 032 * JCache {@link javax.cache.Caching} class. 033 * 034 * <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0. 035 * 036 * @author Juergen Hoeller 037 * @since 3.2 038 * @see javax.cache.Caching#getCachingProvider() 039 * @see javax.cache.spi.CachingProvider#getCacheManager() 040 */ 041public class JCacheManagerFactoryBean 042 implements FactoryBean<CacheManager>, BeanClassLoaderAware, InitializingBean, DisposableBean { 043 044 private URI cacheManagerUri; 045 046 private Properties cacheManagerProperties; 047 048 private ClassLoader beanClassLoader; 049 050 private CacheManager cacheManager; 051 052 053 /** 054 * Specify the URI for the desired CacheManager. 055 * Default is {@code null} (i.e. JCache's default). 056 */ 057 public void setCacheManagerUri(URI cacheManagerUri) { 058 this.cacheManagerUri = cacheManagerUri; 059 } 060 061 /** 062 * Specify properties for the to-be-created CacheManager. 063 * Default is {@code null} (i.e. no special properties to apply). 064 * @see javax.cache.spi.CachingProvider#getCacheManager(URI, ClassLoader, Properties) 065 */ 066 public void setCacheManagerProperties(Properties cacheManagerProperties) { 067 this.cacheManagerProperties = cacheManagerProperties; 068 } 069 070 @Override 071 public void setBeanClassLoader(ClassLoader classLoader) { 072 this.beanClassLoader = classLoader; 073 } 074 075 @Override 076 public void afterPropertiesSet() { 077 this.cacheManager = Caching.getCachingProvider().getCacheManager( 078 this.cacheManagerUri, this.beanClassLoader, this.cacheManagerProperties); 079 } 080 081 082 @Override 083 public CacheManager getObject() { 084 return this.cacheManager; 085 } 086 087 @Override 088 public Class<?> getObjectType() { 089 return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class); 090 } 091 092 @Override 093 public boolean isSingleton() { 094 return true; 095 } 096 097 098 @Override 099 public void destroy() { 100 this.cacheManager.close(); 101 } 102 103}