javax.cache.CacheManager Maven / Gradle / Ivy
/**
*
* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.cache;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class CacheManager {
protected static CacheManager instance = new CacheManager();
private final Map caches = Collections.synchronizedMap(new HashMap());
public static CacheManager getInstance() {
return instance;
}
public Cache getCache(String cacheName) {
return (Cache) caches.get(cacheName);
}
public void registerCache(String cacheName, Cache cache) {
caches.put(cacheName, cache);
}
public CacheFactory getCacheFactory() throws CacheException {
String name = getImplementationClassName(CacheFactory.class.getName());
if (name == null) {
throw new CacheException("No implementation of JCache can be found on the classpath");
}
Class type = null;
try {
type = Thread.currentThread().getContextClassLoader().loadClass(name);
}
catch (ClassNotFoundException e) {
try {
type = getClass().getClassLoader().loadClass(name);
}
catch (ClassNotFoundException e1) {
throw new CacheException("Could not find class name: " + name);
}
}
try {
return (CacheFactory) type.newInstance();
}
catch (Exception e) {
throw new CacheException("Could not instantiate instance of: " + name + ". Reason: " + e, e);
}
}
protected String getImplementationClassName(String interfaceType) {
try {
String name = System.getProperty(interfaceType);
if (name != null && name.length() > 0)
return name;
}
catch (SecurityException e) {
// ignore security exceptions disallowing us from accessing
// system properties
}
// lets try META-INF/services
try {
InputStream in = getResourceAsStream("META-INF/services/" + interfaceType);
if (in != null) {
BufferedReader r = new BufferedReader(new InputStreamReader(in));
try {
String line = r.readLine();
if (line != null) {
line = line.trim();
if (line.length() > 0) {
return line;
}
}
}
finally {
r.close();
}
}
}
catch (IOException e) {
// ignore errors reading the file
}
return null;
}
protected InputStream getResourceAsStream(String name) {
InputStream answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
if (answer == null) {
answer = getClass().getClassLoader().getResourceAsStream(name);
}
return answer;
}
}