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

📄 serverconfig.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.framework.server.helper;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;

import com.cownew.PIS.framework.common.multisql.TargetDBEnum;
import com.cownew.PIS.framework.common.services.ACInfo;
import com.cownew.ctk.common.ExceptionUtils;
import com.cownew.ctk.common.StringUtils;
import com.cownew.ctk.constant.StringConst;
import com.cownew.ctk.io.ResourceUtils;

public class ServerConfig
{
	private int sessionTimeOut;

	private String[] beanFiles;

	private ACInfo[] acInfos;

	private String metaDataPath;

	private String entityCacheFile;

	private boolean metaCacheEnabled;

	private int multiSQLCacheSize;

	private String jMSConnectionFactory;

	private String jMSContextFactory;

	private String jMSUrl;

	private String jMSJndiName;

	private static ServerConfig instance = null;

	private ServerConfig()
	{
		super();
	};

	public static ServerConfig getInstance()
	{
		if (instance == null)
		{
			instance = new ServerConfig();
			try
			{
				instance.initConfig();
			} catch (Exception e)
			{
				ExceptionUtils.toRuntimeException(e);
			}
		}

		return instance;
	}

	protected void initConfig() throws Exception
	{
		InputStream beansXFStream = null;
		try
		{
			beansXFStream = getClass().getResourceAsStream(
					"/com/cownew/PIS/framework/server/ServerConfig.xml");
			SAXReader reader = new SAXReader();
			reader.setValidation(false);
			Document doc = reader.read(new InputStreamReader(beansXFStream,
					StringConst.UTF8));
			loadBasic(doc);

			loadBeanFilesDef(doc);
			loadACDef(doc);
			loadMetaDataProp(doc);
			loadMultiSQLProp(doc);
			loadJMSConfig(doc);

		} finally
		{
			ResourceUtils.close(beansXFStream);
		}
	}

	private void loadJMSConfig(Document doc)
	{
		jMSContextFactory = doc.selectSingleNode("//Config/JMS/ContextFactory")
				.getText().trim();
		jMSUrl = doc.selectSingleNode("//Config/JMS/URL").getText().trim();
		jMSJndiName = doc.selectSingleNode("//Config/JMS/JndiName").getText().trim();
		jMSConnectionFactory = doc.selectSingleNode(
				"//Config/JMS/ConnectionFactory").getText().trim();
	}

	//加载基本选项
	private void loadBasic(Document doc)
	{
		String timoutStr = doc.selectSingleNode("//Config/SessionTimeOut")
				.getText().trim();
		sessionTimeOut = Integer.parseInt(timoutStr);
	}

	//加载多数据库支持的配置
	private void loadMultiSQLProp(Document doc)
	{
		String sqlCacheSizeStr = doc.selectSingleNode(
				"//Config/MultiSQL/CacheSize").getText().trim();
		multiSQLCacheSize = Integer.parseInt(sqlCacheSizeStr);
	}

	//加载元数据相关配置
	private void loadMetaDataProp(Document doc)
	{
		metaDataPath = doc.selectSingleNode("//Config/MetaData/MetaDataPath")
				.getText().trim();
		entityCacheFile = doc.selectSingleNode(
				"//Config/MetaData/EntityCacheFile").getText().trim();
		String cacheState = doc.selectSingleNode(
				"//Config/MetaData/CacheEnabled").getText().trim();
		if (StringUtils.isEmpty(cacheState)
				|| cacheState.equalsIgnoreCase("false")
				|| cacheState.equalsIgnoreCase("no")
				|| cacheState.equalsIgnoreCase("off"))
		{
			metaCacheEnabled = false;
		} else
		{
			metaCacheEnabled = true;
		}
	}

	//加载帐套定义配置
	private void loadACDef(Document doc)
	{
		List acItemList = doc.selectNodes("//Config/ACItems/ACItem");
		acInfos = new ACInfo[acItemList.size()];
		for (int i = 0, n = acItemList.size(); i < n; i++)
		{
			DefaultElement beanElement = (DefaultElement) acItemList.get(i);

			String name = beanElement.attributeValue("name");
			String displayName = beanElement.getText();
			String dataSourceName = beanElement
					.attributeValue("dataSourceName");
			String targetDB = beanElement.attributeValue("targetDB");
			ACInfo info = new ACInfo(name, displayName, dataSourceName,
					getTargetDBEnum(targetDB));

			acInfos[i] = info;
		}
	}

	/**
	 * 加载remoting配置文件
	 * @param doc
	 */
	private void loadBeanFilesDef(Document doc)
	{
		List beanList = doc.selectNodes("//Config/BeanFiles/File");
		beanFiles = new String[beanList.size()];
		for (int i = 0, n = beanList.size(); i < n; i++)
		{
			DefaultElement beanElement = (DefaultElement) beanList.get(i);
			beanFiles[i] = beanElement.getText().trim();
		}
	}

	//将配置文件中数据库类型转化为TargetDBEnum
	private static TargetDBEnum getTargetDBEnum(String value)
	{
		value = value.toUpperCase();
		if (value.equals("MSSQLSERVER") || value.equals("SQLSERVER")
				|| value.equals("MSSQL"))
		{
			return TargetDBEnum.MSSQLSERVER;
		} else if (value.equals("DB2"))
		{
			return TargetDBEnum.DB2;
		} else if (value.equals("ORACLE"))
		{
			return TargetDBEnum.ORACLE;
		} else if (value.equals("MYSQL"))
		{
			return TargetDBEnum.MYSQL;
		}
		throw new AssertionError("unkown target db:" + value);
	}

	/**
	 * 得到会话超时时间(分钟)
	 * @return
	 */
	public int getSessionTimeOut()
	{
		return sessionTimeOut;
	}

	/**
	 * Remoting定义文件
	 * @return
	 */
	public String[] getBeanFiles()
	{
		return beanFiles;
	}

	/**
	 * 得到帐套定义
	 * @return
	 */
	public ACInfo[] getAcInfos()
	{
		return acInfos;
	}

	/**
	 * 元数据缓存文件位置
	 * @return
	 */
	public String getEntityCacheFile()
	{
		return entityCacheFile;
	}

	/**
	 * 元数据的路径
	 * @return
	 */
	public String getMetaDataPath()
	{
		return metaDataPath;
	}

	/**
	 * 元数据缓存开关
	 * @return
	 */
	public boolean isMetaCacheEnabled()
	{
		return metaCacheEnabled;
	}

	/**
	 * 多数据库翻译引擎缓存大小
	 * @return
	 */
	public int getMultiSQLCacheSize()
	{
		return multiSQLCacheSize;
	}

	public String getJMSConnectionFactory()
	{
		return jMSConnectionFactory;
	}

	public String getJMSContextFactory()
	{
		return jMSContextFactory;
	}

	public String getJMSJndiName()
	{
		return jMSJndiName;
	}

	public String getJMSUrl()
	{
		return jMSUrl;
	}

}

⌨️ 快捷键说明

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