clientresourcemanager.java

来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 310 行

JAVA
310
字号
/**
 * $Log: ClientResourceManager.java,v $
 * Revision 1.4  2003/02/02 13:30:15  mwulff
 * remote message is tested for empty string
 *
 * Revision 1.3  2003/01/18 21:59:11  mwulff
 * added an method log() that is used by the super class for logging
 *
 * Revision 1.2  2003/01/17 15:58:25  mwulff
 * no deals with messages that comes from the server and should be displayed
 * to the client
 *
 * Revision 1.1  2003/01/16 13:08:54  mwulff
 * initial version
 *
 * Revision 1.16  2002/12/20 18:49:31  mwulff
 * no message
 *
 * Revision 1.15  2002/12/15 16:51:22  mwulff
 * adjusted to reorganisation of the client logging system
 *
 * Revision 1.14  2002/12/13 20:05:54  mwulff
 * no message
 *
 * Revision 1.13  2002/12/12 11:36:39  mwulff
 * no message
 *
 * Revision 1.12  2002/12/05 21:26:06  mwulff
 * no message
 *
 * Revision 1.11  2002/12/01 21:37:44  mwulff
 * is now implemented as singleton
 *
 * Revision 1.10  2002/12/01 13:03:00  mwulff
 * is now implemented as singleton
 *
 * Revision 1.9  2002/11/30 11:52:31  mwulff
 * no message
 *
 * Revision 1.8  2002/11/22 16:31:23  mwulff
 * changed import statements because of reorganisation from other packages
 *
 * Revision 1.7  2002/11/17 17:48:24  mwulff
 * the field logConfPath is now set by JKFClient
 *
 * Revision 1.6  2002/11/16 12:26:08  mwulff
 * organized imports
 *
 * Revision 1.5  2002/11/15 18:20:16  mwulff
 * modified function loadResources()
 *
 * Revision 1.4  2002/11/08 19:15:40  mwulff
 * ClientResourceManager is now inherited from ResourceManager
 *
 * Revision 1.3  2002/11/07 16:23:28  mwulff
 * ClientResourceManager.configure is now called by JKFClient
 *
 * Revision 1.2  2002/11/05 19:44:50  mwulff
 * the parameters are now loaded from a xml configuration file, that must
 * be put in directory from where the application is started ($user.dir)
 *
 * Revision 1.1  2002/11/03 20:58:37  mwulff
 * initial version
 *
 */
package de.fhm.jkf.resource.cl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Level;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import de.fhm.jkf.comm.clsv.ResponseCommands;
import de.fhm.jkf.resource.clsv.ResourceManager;

/**
 * @author marten wulff
 * 
 * <br><br><center><table border="1" width="80%"><hr>
 * <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
 * <p>
 * Copyright (C) 2002 by Marten Wulff
 * <p>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * <p>
 * You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
 * GNU Lesser General Public License</a> along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 * <hr></table></center>
 */
public class ClientResourceManager extends ResourceManager {

	private static ClientResourceManager instance = null;
	private static String logName = ClientResourceManager.class.getName();	
	private static Object semaphore = new Object();

	// Element names
	private final String REQUEST_BROKER_ELEMENT = "requestBrokerServlet";
	private final String CBSIZE_ELEMENT = "communicationBufferSize";
	private final String COMPRESS_LIMIT_ELEMENT = "compressLimit";
	private final String LOGGING_ELEMENT = "logging";
	private final String FILE_LOGGER_ELEMENT = "file";
	private final String CONSOLE_LOGGER_ELEMENT = "console";

	// attributes 
	private final String LOG_LEVEL_ATTRIB = "level";
	private final String LOG_ENABLED = "enabled";

	// url of the requestBrokerServlet
	private URL requestBrokerURL = null;

	/*
	 * Constructor for ClientResourceManager. This class should not be 
	 * instatiated.
	 */
	private ClientResourceManager() {
		super();
	}

	/**
	 * Entrypoint for getting a reference to a 
	 * ClientResourceManager instance.
	 */
	public static ClientResourceManager instance() {
		if (instance == null) {
			synchronized (semaphore) {
				if (instance == null) {
					instance = new ClientResourceManager();
				}
			}
		}

		return instance;
	}

	synchronized void loadResources() {
		// don't load twice
		if (dataLoaded) {
			return;
		}

		// check if an existing configuration file has been specified
		if (!configExists()) {
			System.err.println(
				"Jkf configuration file : " + confFile + " doesn't exist");
			System.err.println("Shutting down ...");
			System.exit(-1);
		}

		try {
			// parse the jkf configuration	
			DocumentBuilder parser =
				DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = parser.parse(confFile);

			// initialize the client logging system
			NodeList tmp = doc.getElementsByTagName(LOGGING_ELEMENT);
			initClientLogging(tmp);

			// initialize the url of the RequestBrokerServlet		
			tmp = doc.getElementsByTagName(REQUEST_BROKER_ELEMENT);
			initRBServletParam(tmp);

			// initialize the size of the communication buffers
			tmp = doc.getElementsByTagName(CBSIZE_ELEMENT);
			initCBSizeParam(tmp);

			// initialize the compress limit
			tmp = doc.getElementsByTagName(COMPRESS_LIMIT_ELEMENT);
			initCPLimitParam(tmp);

		} catch (ParserConfigurationException pce) {
			System.err.println(
				"Error configuring xml parser: " + pce.getMessage());
			System.err.println("Shutting down ...");
			System.exit(-1);
		} catch (SAXException se) {
			System.err.println(
				"Error reading jkf configuration: " + se.getMessage());
			System.err.println("Shutting down ...");
			System.exit(-1);
		} catch (IOException ioe) {
			System.err.println(
				"Error reading jkf configuration: " + ioe.getMessage());
			System.err.println("Shutting down ...");
			System.exit(-1);
		}

		dataLoaded = true;
	}

	private void initClientLogging(NodeList nodes) {
		if (nodes.getLength() > 0) {

			Element loggingElement = null;
			Element consoleElement = null;
			Element fileElement = null;

			NodeList tmp = null;
			boolean enabled = false;
			Level level = null;

			loggingElement = (Element) nodes.item(0);

			// check for console appender
			tmp = loggingElement.getElementsByTagName(CONSOLE_LOGGER_ELEMENT);
			if (tmp.getLength() > 0) {
				consoleElement = (Element) tmp.item(0);
				enabled =
					(Boolean.valueOf(consoleElement.getAttribute(LOG_ENABLED)))
						.booleanValue();
				if (enabled) {
					JKFClientLogger.addConsoleAppender();
					level =
						Level.toLevel(
							consoleElement.getAttribute(LOG_LEVEL_ATTRIB));
					JKFClientLogger.setConsoleThreshold(level);
				}
			}

			// check for file appender
			tmp = loggingElement.getElementsByTagName(FILE_LOGGER_ELEMENT);
			if (tmp.getLength() > 0) {
				fileElement = (Element) tmp.item(0);
				enabled =
					(Boolean.valueOf(fileElement.getAttribute(LOG_ENABLED)))
						.booleanValue();
				if (enabled) {
					JKFClientLogger.addFileAppender();
					level =
						Level.toLevel(
							fileElement.getAttribute(LOG_LEVEL_ATTRIB));
					JKFClientLogger.setFileThreshold(level);
				}
			}

		}
	}

	private void initRBServletParam(NodeList nodes) {
		if (nodes.getLength() > 0) {
			Node node = nodes.item(0);
			try {
				requestBrokerURL = new URL(node.getFirstChild().getNodeValue());
			} catch (MalformedURLException mue) {
				System.err.println(
					"[JKF]: Error setting URL for RequestBrokerServlet, url is malformed!");
				System.err.println("[JKF]: Shutting down ...");
				System.exit(-1);
			}
		}
	}

	/**
	 * Returns the url for the RequestBrokerServet
	 * @return String
	 */
	public URL getRequestBrokerURL() {
		return requestBrokerURL;
	}

	public void interpretResponseCommands(ResponseCommands commands) {
		int logLevel = commands.getLogLevel();
		JKFClientLogger.setRemoteThreshold(
			Level.toLevel(logLevel, JKFClientLogger.defaultRemoteLevel));

		String remoteMessage = commands.getResponseMessage();
		if (remoteMessage != null && !remoteMessage.equals("")) {
			JOptionPane.showMessageDialog(null, remoteMessage);
		}
	}
	
	/**
	 * Implemented abstract method for logging from parent class.
	 * 
	 * @param message 	a message that should be logged in mode debug
	 */
	protected void log(Object message) {
		if (JKFClientLogger.isDebugEnabled()) {
			JKFClientLogger.debug(logName + " - " + message);
		}
	}

	// only for testing
	public static void main(String[] args) {
		// configure jkf
		JKFClient.configure();
	}
}

⌨️ 快捷键说明

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