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

📄 cachemanager.java

📁  EasyDBO是一个超轻量级对象-关系映射(Object/Relation Mapping
💻 JAVA
字号:
package com.easyjf.cache;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.log4j.*;

//~--- JDK imports ------------------------------------------------------------

import java.util.*;

//~--- classes ----------------------------------------------------------------

/**
 * 
 * <p>
 * Title: Cache管理类,负责管理所有的Cache,负责与调用Cache的应用程序接口
 * </p>
 * 
 * <p>
 * Description: Cache管理类,负责管理所有的Cache,负责与调用Cache的应用程序接口
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company: EasyJF开源团队-EasyDBO项目组
 * </p>
 * 
 * @author 大峡
 * @version 1.0
 */
public final class CacheManager {
	private final static Logger logger = Logger.getLogger(CacheManager.class);

	private static CacheManager singleton;

	// ~--- fields -------------------------------------------------------------
	/**
	 * 配置文件
	 */
	private String configurationFileName = "/easyjf-cache.xml";

	private Hashtable caches = new Hashtable();

	/**
	 * XML 解析类
	 */
	private Config configuration;

	// Cache 接口类
	private ICache defaultCache;

	private int status;

	// ~--- constructors -------------------------------------------------------
	/**
	 * 
	 * @throws CacheException
	 */
	public CacheManager() throws CacheException {

		// default config will be done
		status = Status.STATUS_UNINITIALISED;
		configure();
		status = Status.STATUS_ALIVE;
	}

	/**
	 * 
	 * @param configuration
	 *            Config
	 * @throws CacheException
	 */
	public CacheManager(Config configuration) throws CacheException {
		status = Status.STATUS_UNINITIALISED;
		this.configuration = configuration;
		configure();
		status = Status.STATUS_ALIVE;
	}

	/**
	 * 
	 * @param configurationFileName
	 *            String
	 * @throws CacheException
	 */
	public CacheManager(String configurationFileName) throws CacheException {
		status = Status.STATUS_UNINITIALISED;
		this.configurationFileName = configurationFileName;
		configure();
		status = Status.STATUS_ALIVE;
	}

	// ~--- methods ------------------------------------------------------------
	/**
	 * 增加缓存
	 * 
	 * @param cache
	 *            ICache 缓存接口
	 * @throws IllegalStateException
	 * @throws CacheException
	 */
	public synchronized void addCache(ICache cache)
			throws IllegalStateException, CacheException {
		// 检查判词
		checkStatus();
		addCacheNoCheck(cache);
	}

	/**
	 * 增加缓存
	 * 
	 * @param cacheName
	 *            String缓存名
	 * @throws IllegalStateException
	 * @throws CacheException
	 */
	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对象!");
		}
	}

	/**
	 * 
	 * @param cache
	 *            ICache
	 * @throws IllegalStateException
	 * @throws CacheException
	 */
	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);
	}

	/**
	 * 
	 * @param cacheName
	 *            String
	 * @return boolean
	 * @throws IllegalStateException
	 */
	public synchronized boolean cacheExists(String cacheName)
			throws IllegalStateException {
		checkStatus();

		return (caches.get(cacheName) != null);
	}

	/**
	 * 
	 */
	private void checkStatus() {
		if (!(status == Status.STATUS_ALIVE)) {
			throw new IllegalStateException("The CacheManager is not alive.");
		}
	}

	/**
	 * 
	 * @throws CacheException
	 */
	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));
		}
	}

	/**
	 * 
	 * @return CacheManager
	 * @throws CacheException
	 */
	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;
		}
	}

	/**
	 * 
	 * @param configurationFileName
	 *            String
	 * @return CacheManager
	 * @throws CacheException
	 */
	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;
		}
	}

	/**
	 * 
	 * @param cacheName
	 *            String
	 * @throws IllegalStateException
	 * @throws CacheException
	 */
	public synchronized void removeCache(String cacheName)
			throws IllegalStateException, CacheException {
		checkStatus();

		EasyCache cache = (EasyCache) caches.remove(cacheName);

		if (cache != null) {
			cache.destroy();
		}
	}

	/**
	 * 
	 * @throws CacheException
	 */
	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;
			}
		}
	}

	// ~--- get methods --------------------------------------------------------
	/**
	 * 
	 * @param name
	 *            String
	 * @return ICache
	 * @throws IllegalStateException
	 */
	public synchronized ICache getCache(String name)
			throws IllegalStateException {
		checkStatus();

		return (ICache) caches.get(name);
	}

	/**
	 * 
	 * @return String[]
	 * @throws IllegalStateException
	 */
	public synchronized String[] getCacheNames() throws IllegalStateException {
		checkStatus();

		String[] list = new String[caches.size()];

		return (String[]) caches.keySet().toArray(list);
	}

	/**
	 * 
	 * @return Config
	 * @throws IllegalStateException
	 */
	Config getConfiguration() throws IllegalStateException {
		checkStatus();

		return configuration;
	}

	/**
	 * 
	 * @return ICache
	 */
	public ICache getDefaultCache() {
		return defaultCache;
	}

	/**
	 * 
	 * @return CacheManager
	 */
	public static CacheManager getInstance() {
		try {
			if (singleton == null) {
				CacheManager.create();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return singleton;
	}

	public int getStatus() {
		return status;
	}

	/**
	 * 
	 * @param defaultCache
	 *            ICache
	 */
	public void setDefaultCache(ICache defaultCache) {
		this.defaultCache = defaultCache;
	}
}

⌨️ 快捷键说明

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