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 javax.naming.Context; 026import javax.naming.InitialContext; 027import java.util.Iterator; 028 029/** 030 * @author Jens Wilke; created: 2013-06-27 031 */ 032public abstract class CacheManager implements Iterable<Cache> { 033 034 private static CacheManager defaultManager; 035 private static String defaultName = "default"; 036 037 static { 038 try { 039 Context ctx = new InitialContext(); 040 ctx = (Context) ctx.lookup("java:comp/env"); 041 String _name = 042 (String) ctx.lookup("org.cache2k.CacheManager.defaultName"); 043 if (_name != null) { 044 defaultName = _name; 045 } 046 } catch (Exception ignore) { 047 } 048 } 049 050 /** 051 * Name of the default cache manager, which is "default" by default. It is also possible 052 * to set the default manager name via JNDI context "java:comp/env" and name 053 * "org.cache2k.CacheManager.defaultName". 054 */ 055 public static String getDefaultName() { 056 return defaultName; 057 } 058 059 /** 060 * Reset the manager name once on application startup. 061 */ 062 public static void setDefaultName(String defaultName) { 063 if (defaultManager != null) { 064 throw new IllegalStateException("default CacheManager already created"); 065 } 066 CacheManager.defaultName = defaultName; 067 } 068 069 /** 070 * Get the default cache manager for the class loader 071 */ 072 public synchronized static CacheManager getInstance() { 073 if (defaultManager != null && !defaultManager.isDestroyed()) { 074 return defaultManager; 075 } 076 try { 077 defaultManager = (CacheManager) 078 Class.forName("org.cache2k.impl.CacheManagerImpl").newInstance(); 079 } catch (Exception e) { 080 throw new LinkageError("cache2k implementation not found, cache2k-core.jar missing?", e); 081 } 082 return defaultManager; 083 } 084 085 public abstract String getName(); 086 087 public abstract Iterator<Cache> iterator(); 088 089 /** Clear all caches associated to this cache manager */ 090 public abstract void clear(); 091 092 /** 093 * Destroy all caches associated to this cache manager. 094 */ 095 public abstract void destroy(); 096 097 public abstract boolean isDestroyed(); 098 099}