001package org.cache2k; 002 003/* 004 * #%L 005 * cache2k API only package 006 * %% 007 * Copyright (C) 2000 - 2014 headissue GmbH, Munich 008 * %% 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as 011 * published by the Free Software Foundation, either version 3 of the 012 * License, or (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public 020 * License along with this program. If not, see 021 * <http://www.gnu.org/licenses/gpl-3.0.html>. 022 * #L% 023 */ 024 025import org.cache2k.spi.Cache2kExtensionProvider; 026 027import javax.naming.Context; 028import javax.naming.InitialContext; 029import java.lang.reflect.Constructor; 030import java.util.HashMap; 031import java.util.Iterator; 032import java.util.Map; 033import java.util.ServiceLoader; 034 035/** 036 * @author Jens Wilke; created: 2013-06-27 037 */ 038public abstract class CacheManager implements Iterable<Cache> { 039 040 protected final static String DEFAULT_MANAGER_NAME = "default"; 041 042 private static CacheManager defaultManager; 043 private static String defaultName = DEFAULT_MANAGER_NAME; 044 private static Map<String, CacheManager> name2manager = new HashMap<String, CacheManager>(); 045 046 static { 047 ServiceLoader<Cache2kExtensionProvider> _loader = 048 ServiceLoader.load(Cache2kExtensionProvider.class); 049 for (Cache2kExtensionProvider p : _loader) { 050 p.register(); 051 } 052 } 053 054 /** 055 * Name of the default cache manager, which is "default" by default. It is also possible 056 * to set the default manager name via JNDI context "java:comp/env" and name 057 * "org.cache2k.CacheManager.defaultName". 058 */ 059 public static String getDefaultName() { 060 return defaultName; 061 } 062 063 /** 064 * Reset the manager name once on application startup. 065 */ 066 public static void setDefaultName(String defaultName) { 067 if (defaultManager != null) { 068 throw new IllegalStateException("default CacheManager already created"); 069 } 070 CacheManager.defaultName = defaultName; 071 } 072 073 /** 074 * Get the default cache manager for the current class loader 075 */ 076 public synchronized static CacheManager getInstance() { 077 if (defaultManager != null && !defaultManager.isDestroyed()) { 078 return defaultManager; 079 } 080 try { 081 defaultManager = (CacheManager) getManagerClass().newInstance(); 082 } catch (Exception e) { 083 return implNotFound(e); 084 } 085 name2manager.put(defaultManager.getName(), defaultManager); 086 return defaultManager; 087 } 088 089 private static CacheManager implNotFound(Exception e) { 090 throw new Error("cache2k implementation not found, cache2k-core.jar missing?", e); 091 } 092 093 private static Class<?> getManagerClass() throws ClassNotFoundException { 094 return Class.forName("org.cache2k.impl.CacheManagerImpl"); 095 } 096 097 public synchronized static CacheManager getInstance(String _name) { 098 if (defaultName.equals(_name)) { 099 return getInstance(); 100 } 101 CacheManager m = name2manager.get(_name); 102 if (m != null) { return m; } 103 try { 104 Class<?> c = getManagerClass(); 105 Constructor<?> cc = c.getConstructor(String.class); 106 m = (CacheManager) cc.newInstance(_name); 107 } catch (Exception e) { 108 return implNotFound(e); 109 } 110 name2manager.put(_name, m); 111 return m; 112 } 113 114 public abstract String getName(); 115 116 public abstract Iterator<Cache> iterator(); 117 118 public abstract Cache getCache(String name); 119 120 /** Clear all caches associated to this cache manager */ 121 public abstract void clear(); 122 123 /** 124 * Destroy all caches associated to this cache manager. 125 */ 126 public abstract void destroy(); 127 128 public abstract boolean isDestroyed(); 129 130}