⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachemanager.java

📁 EasyDBO v0.4 是一个非常适合中小型软件数据库开发的数据持久层框架
💻 JAVA
字号:
package com.easyjf.cache;import org.apache.log4j.Logger;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Set;/** * Cache管理类,负责管理所有的Cache,负责与调用Cache的应用程序接口 * @author 蔡世友 * */public final class CacheManager {	private final static Logger logger = Logger.getLogger(CacheManager.class);           private static CacheManager singleton;    private Config configuration;      private String configurationFileName="/easyjf-cache.xml";       private Hashtable caches = new Hashtable();    private ICache defaultCache;        private int status;    public CacheManager(Config configuration) throws CacheException {        status = Status.STATUS_UNINITIALISED;        this.configuration = configuration;        configure();        status = Status.STATUS_ALIVE;    }        public CacheManager(String configurationFileName) throws CacheException {        status = Status.STATUS_UNINITIALISED;        this.configurationFileName = configurationFileName;        configure();        status = Status.STATUS_ALIVE;    }    public CacheManager() throws CacheException {        //default config will be done        status = Status.STATUS_UNINITIALISED;        configure();        status = Status.STATUS_ALIVE;    }       private synchronized void configure() throws CacheException {              if (configuration == null) {            try {            if (configurationFileName != null) {                configuration =Config.getInstance(configurationFileName);                                   }            } catch (Exception e) {                throw new CacheException("Cannot configure CacheManager: " + e.getMessage());            }        }               defaultCache = configuration.getDefaultCache();        Set configuredCacheKeys = configuration.getCacheKeySet();        for (Iterator iterator = configuredCacheKeys.iterator(); iterator.hasNext();) {            String name = (String) iterator.next();            addCacheNoCheck(configuration.getCache(name));        }    }    public static CacheManager create() throws CacheException {        synchronized (CacheManager.class) {            if (singleton == null) {                                   logger.debug("Creating new CacheManager with default config");                               singleton = new CacheManager();            } else {                                  logger.debug("Attempting to create an existing singleton. Existing singleton returned.");                          }            return singleton;        }    }    public static CacheManager getInstance()  {    	try{       if(singleton==null) CacheManager.create();    	}    	catch(Exception e)    	{    		e.printStackTrace();    	}       return singleton;    }    public static CacheManager create(String configurationFileName) throws CacheException {        synchronized (CacheManager.class) {            if (singleton == null) {                                    logger.debug("Creating new CacheManager with config file: " + configurationFileName);                                singleton = new CacheManager(configurationFileName);            }            return singleton;        }    }    public synchronized ICache getCache(String name) throws IllegalStateException {        checkStatus();        return (ICache) caches.get(name);    }      public synchronized void addCache(String cacheName) throws IllegalStateException, CacheException {        checkStatus();        if (caches.get(cacheName) != null) {            throw new CacheException("Cache " + cacheName + " already exists");        }        try{        ICache cache = null;          cache = (ICache) defaultCache.clone();                cache.setName(cacheName);        addCache(cache);        }        catch(CloneNotSupportedException e)        {        	throw new CacheException("无法colne对象!");        }    }    public synchronized void addCache(ICache cache) throws IllegalStateException,CacheException {        checkStatus();        addCacheNoCheck(cache);    }    private synchronized void addCacheNoCheck(ICache cache) throws IllegalStateException,CacheException {        if (caches.get(cache.getName()) != null) {throw new CacheException("Cache " + cache.getName() + " already exists");        }             caches.put(cache.getName(), cache);            }    public synchronized boolean cacheExists(String cacheName) throws IllegalStateException {        checkStatus();        return (caches.get(cacheName) != null);    }    public synchronized void removeCache(String cacheName) throws IllegalStateException,CacheException {        checkStatus();        EasyCache cache = (EasyCache) caches.remove(cacheName);        if (cache != null) {            cache.destroy();        }            }      public void shutdown()throws CacheException {        if (status==Status.STATUS_SHUTDOWN) {                            logger.warn("CacheManager already shutdown");                        return;        }        synchronized (CacheManager.class) {                      Enumeration allCaches = caches.elements();            while (allCaches.hasMoreElements()) {                ICache cache = (ICache) allCaches.nextElement();                if (cache != null) {                    cache.destroy();                }            }            status = Status.STATUS_SHUTDOWN;            if (this == singleton) {                singleton = null;            }        }    }     public synchronized String[] getCacheNames() throws IllegalStateException {        checkStatus();        String[] list = new String[caches.size()];        return (String[]) caches.keySet().toArray(list);    }    Config getConfiguration() throws IllegalStateException {        checkStatus();        return configuration;    }    private void checkStatus() {        if (!(status==Status.STATUS_ALIVE)) {            throw new IllegalStateException("The CacheManager is not alive.");        }    }    public int getStatus() {        return status;    }	public ICache getDefaultCache() {		return defaultCache;	}	public void setDefaultCache(ICache defaultCache) {		this.defaultCache = defaultCache;	}    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -