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