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

📄 serversetting.java

📁 用mina框架编写的转发代理服务程序,含自动更新功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.frontMachine.setting;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.frontMachine.client.DownloadType;
import com.frontMachine.util.ExceptionLoger;


public class ServerSetting 
{
	Map remoteHostMap = new HashMap();
//  转发服务器端口号	
	int port_zf = 8888;
//	更新服务器端口号	
	int port_ud = 7777;
//	更新服务器端口号	
	int port_icrd_ud = 7777;
//	系统最大处理线程数
	int maxThreads = 1;
//	空闲时间
	private int idleTime=10;
//	超时时间
	private long maxWait = 5000L;
//	从更新服务器下载的时间间隔
	private long dlInterval = 60000L;
//	接收缓冲取的大小
	private int recBufSize=1024;
//	下载更新文件缓冲区大小
	private int downloadBufSize=1024;
//	发送数据包大小
	private int sendPartSize=1024;
//  下载版本号	
	private String pos_edition="20070101";
	private String icreader_edition = "20070101";
//	解码数据包用的字符集	
	private String deCharset="GB2312";
//	是否记录pos终端上送的日志
	private String terminal_log = "false";
//	数据包格式的正则表达式
	private String regExp="^.*$";

	private static ServerSetting instance = null;
	private static SAXReader reader = null;
	private static Document doc =null;
	
	
	private ServerSetting() 
	{
		init();
	}

	public static ServerSetting getInstance() 
	{
		if (instance == null) 
		{
			instance = new ServerSetting();
		}

		return instance;
	}
	
	private void updateValue(Element element, String newText) 
	{
		PrintWriter out = null;
		XMLWriter writer = null;
		if (element.isTextOnly()) 
		{
			element.clearContent();  
			element.addText(newText);    
		}
		try
		{
			File f=new File("./config.xml");
			//File f=new File("./bin/config.xml");
			out=new PrintWriter(f);
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("GBK");
			writer = new XMLWriter(out,format);
			writer.write(doc);
			writer.flush();
		}
		catch(Exception ex)
		{
			ExceptionLoger.ExLog("ServerSetting::updateValue:更新配置文件失败!!");
			ExceptionLoger.ExLog(ex);
		}
		finally
		{
			try{
				writer.close();
				out.close();
			}catch(Exception e){}
		}
		return;
	}

	
	private void init() 
	{
		try 
		{
			reader = new SAXReader();
			doc = reader.read(getClass().getResourceAsStream("/config.xml"));
			Element root = doc.getRootElement();
			
			port_zf = Integer.parseInt(root.selectSingleNode("port_zf").getStringValue());
			port_ud=Integer.parseInt(root.selectSingleNode("port_ud").getStringValue());
			port_icrd_ud=Integer.parseInt(root.selectSingleNode("port_icrd_ud").getStringValue());
			maxThreads = Integer.parseInt(root.selectSingleNode("max_threads").getStringValue());
			maxWait = Long.parseLong(root.selectSingleNode("max_wait").getStringValue());
			dlInterval = Long.parseLong(root.selectSingleNode("download_interval").getStringValue());
			idleTime=Integer.parseInt(root.selectSingleNode("idle_time").getStringValue());
			recBufSize=Integer.parseInt(root.selectSingleNode("receive_buffer_size").getStringValue());
			downloadBufSize=Integer.parseInt(root.selectSingleNode("download_buffer_size").getStringValue());
			sendPartSize=Integer.parseInt(root.selectSingleNode("send_part_size").getStringValue());
			pos_edition=root.selectSingleNode("pos_download_edition").getStringValue();
			icreader_edition = root.selectSingleNode("icreader_download_edition").getStringValue();
			deCharset=root.selectSingleNode("decoder_charset").getStringValue();
			terminal_log = root.selectSingleNode("terminal_log").getStringValue();
			regExp = root.selectSingleNode("package_regular_expression").getStringValue();
			
			List despoints = doc.getRootElement().selectNodes("remotehost_list/remotehost");
			Iterator iter=despoints.iterator();	
			while(iter.hasNext())
			{
				Element ele=(Element)iter.next();
				RemoteHost remoteHost = new RemoteHost(ele.attributeValue("ip"), 
													   Integer.parseInt(ele.attributeValue("port")),
													   ele.attributeValue("description"));
				remoteHostMap.put(ele.attributeValue("code"), remoteHost);
			}
		} 
		catch (Exception e) 
		{
			ExceptionLoger.ExLog(e);
		}

	}
	public void reflashSettingParms()
	{
		init();
		System.out.println("The ServerSetting Parms Has Been Reflashed!!");
	}
		
	public int getReceiveBufferSize()
	{
		return recBufSize;
	}
	
	public int getDownloadBufSize()
	{
		return downloadBufSize;
	}
	
	public int getSendPartSize()
	{
		return sendPartSize;
	}
	
	public String getEdition(int dltype)
	{
		switch(dltype)
		{
			case DownloadType.DOWNLOADTYPE_POS :
				synchronized(this.pos_edition)
				{
					return pos_edition;
				}
			case DownloadType.DOWNLOADTYPE_ICREADER :
				synchronized(this.icreader_edition)
				{
					return icreader_edition;
				}
			default:
				return "";
		}
	}
	
	public void setEdition(String edition,int dltype)
	{
		switch(dltype)
		{
			case DownloadType.DOWNLOADTYPE_POS :
				synchronized(this.pos_edition)
				{
					this.pos_edition=edition;
					Element edt=doc.getRootElement().element("pos_download_edition");
					updateValue(edt,pos_edition);
				}
				break;
			case DownloadType.DOWNLOADTYPE_ICREADER :
				synchronized(this.icreader_edition)
				{
					this.icreader_edition=edition;
					Element edt=doc.getRootElement().element("icreader_download_edition");
					updateValue(edt,icreader_edition);
				}
				break;
		   default:		
		}
	}
	
	public Map getRemoteHostMap() 
	{
		return remoteHostMap;
	}

	public int getPort_zf() 
	{
		return port_zf;
	}
	public int getPort_ud()
	{
		return port_ud;
	}
	public int getPort_icrd_ud()
	{
		return port_icrd_ud;
	}

	public long getMaxWait() 
	{
		return maxWait;
	}
	
	public long getDlIntterval()
	{
		return dlInterval;
	}
	
	public int getIdleTime()
	{
		return idleTime;
	}

	public int getMaxThreads() 
	{
		return maxThreads;
	}
	
	public String getDeCharset()
	{

⌨️ 快捷键说明

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