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

📄 serverresourcemanager.java

📁 主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32.zip后就可以使用。
💻 JAVA
字号:
/**
 * $Log: ServerResourceManager.java,v $
 * Revision 1.3  2003/02/12 16:24:03  mwulff
 * element lookuphandler is no longer supported insted jndiproperties is used
 * to specify properties for the ejb lookup
 *
 * Revision 1.2  2003/01/18 21:59:56  mwulff
 * added an method log() that is used by the super class for logging
 *
 * Revision 1.1  2003/01/16 13:08:09  mwulff
 * initial version
 *
 * Revision 1.11  2002/12/20 18:49:12  mwulff
 * no message
 *
 * Revision 1.10  2002/12/14 22:41:33  mwulff
 * no message
 *
 * Revision 1.9  2002/12/09 18:04:35  mwulff
 * no message
 *
 * Revision 1.8  2002/12/01 21:35:56  mwulff
 * is now implemented as singleton
 *
 * Revision 1.7  2002/11/30 11:52:11  mwulff
 * no message
 *
 * Revision 1.6  2002/11/22 16:29:28  mwulff
 * code reorganisation
 *
 * Revision 1.5  2002/11/21 10:56:36  mwulff
 * added some comments
 *
 * Revision 1.4  2002/11/17 17:45:40  mwulff
 * removed field logConfPath and corresponding getter and setter methods
 *
 * Revision 1.3  2002/11/15 18:17:54  mwulff
 * added funtionallity parsing and storing lookuphandlers and ejbs
 *
 * Revision 1.2  2002/11/08 19:14:30  mwulff
 * ServerResourceManager is now inherited from ResourceManager
 *
 * Revision 1.1  2002/11/07 16:23:55  mwulff
 * initial version
 *
 * Revision 1.2  2002/11/05 19:53:28  mwulff
 * added a Logger
 *
 * Revision 1.1  2002/11/03 21:01:44  mwulff
 * initial version
 */

package de.fhm.jkf.resource.sv;

import java.io.IOException;
import java.util.Hashtable;

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

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

import de.fhm.jkf.comm.sv.EJBData;
import de.fhm.jkf.comm.sv.JndiProperties;
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 ServerResourceManager extends ResourceManager {

	// logging
	private static Logger logger =
		Logger.getLogger(ServerResourceManager.class);

	private static ServerResourceManager instance = null;
	private static Object semaphore = new Object();

	private static RemoteManager remoteManager = RemoteManager.instance();

	// Element names
	private final String CBS_ELEMENT = "communicationBufferSize";
	private final String CL_ELEMENT = "compressLimit";
	private final String EJB_ELEMENT = "ejb";
	private final String JNDIPROP_ELEMENT = "jndiproperties";
	private final String JNDIPROPS_REF_ELEMENT = "jndiproperties-ref";
	private final String HOME_INTERFACE_ELEMENT = "homeInterface";
	private final String ADMIN_ELEMENT = "admin";
	private final String REMOTE_LOG_ELEMENT = "remoteLogLevel";

	// Attributes
	private final String JNDI_NAME_ATTRIB = "jndiname";
	private final String NAME_ATTRIB = "name";
	private final String ADMIN_USER_ATTRIB = "user";
	private final String ADMIN_PASSWD_ATTRIB = "pw";
	private final String REMOTE_LOG_VALUE_ATTRIB = "value";
	private final String FACTORY_ATTRIB = "factory";
	private final String PROVIDER_URL_ATTRIB = "providerUrl";
	private final String PACKAGE_PREFIXES_ATTRIB = "packagePrefixes";

	// storage for all configured lookuphandlers
	// key = name of the lookuphandler 
	// value = lookuphandler object
	private Hashtable lookupHandler = new Hashtable();

	// storage for all declared ejb
	// key = jndi name of the bean
	// value = EJBData object
	private Hashtable ejbDataTable = new Hashtable();

	private String user = null;
	private String passwd = null;

	/**
	 * Method for getting a reference to a ServerResourceManager <br>
	 * instance.
	 * 
	 * @return ServerResourceManager  a instance
	 */
	public static ServerResourceManager instance() {
		if (instance == null) {
			synchronized (semaphore) {
				if (instance == null) {
					instance = new ServerResourceManager();
				}
			}
		}

		return instance;
	}

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

	/**
	 * Method for loading all resources that are needed
	 * by the jkf framework. This method is only called
	 * at initialisation by the JKFBroker class, which 
	 * resides in the same package. The resource data
	 * is only loaded once.
	 */
	synchronized void loadResources() {
		// don't load twice
		if (dataLoaded) {
			return;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("Loading resources from " + confFile + " ...");
		}

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

			// parse conf.xml
			Document doc = parser.parse(confFile);

			// init the size of the communication buffer
			NodeList tmp = doc.getElementsByTagName(CBS_ELEMENT);
			initCBSizeParam(tmp);

			// init the compress limit
			tmp = doc.getElementsByTagName(CL_ELEMENT);
			initCPLimitParam(tmp);

			// init all defined jndi properties
			tmp = doc.getElementsByTagName(JNDIPROP_ELEMENT);
			initJndiProperties(tmp);

			// init all defined ejbs
			tmp = doc.getElementsByTagName(EJB_ELEMENT);
			initEJBParams(tmp);

			// initialize management user and pw
			tmp = doc.getElementsByTagName(ADMIN_ELEMENT);
			initAdmin(tmp);

			// initialize the remote log level
			tmp = doc.getElementsByTagName(REMOTE_LOG_ELEMENT);
			initRemoteLogLevel(tmp);
		} catch (ParserConfigurationException pce) {
			logger.error("Error reading JKF configuration", pce);
		} catch (SAXException se) {
			logger.error("Error reading JKF configuration", se);
		} catch (IOException ioe) {
			logger.error("Error reading JKF configuration", ioe);
		}

		dataLoaded = true;
	}

	/**
	 * xml file has following design:<br>
	 * <jndiProperties 
	 * 		name = "refName"
	 * 		factory = "someFactory"
	 * 		providerUrl = "url"
	 * 		packagePrefixes = "some prefixes"/>
	 */
	private void initJndiProperties(NodeList nodes) {

		if (logger.isDebugEnabled()) {
			logger.debug("Initialising JndiProperties ...");
		}

		Element jndiPropsElement = null;
		String refName = null;
		JndiProperties jndiProps = null;

		for (int i = 0, n = nodes.getLength(); i < n; i++) {
			jndiPropsElement = (Element) nodes.item(i);
			refName = jndiPropsElement.getAttribute(NAME_ATTRIB);

			// instantiate a the JndiProperties
			try {
				jndiProps = new JndiProperties();				
				
				jndiProps.setFactory(jndiPropsElement.getAttribute(FACTORY_ATTRIB));
				jndiProps.setProviderUrl(jndiPropsElement.getAttribute(PROVIDER_URL_ATTRIB));
				jndiProps.setPackagePrefixes(jndiPropsElement.getAttribute(PACKAGE_PREFIXES_ATTRIB));
				
				// register the JndiProperties
				if (logger.isDebugEnabled()) {
					logger.debug(
						"Registering JndiProperties ("
							+ refName);
					}
					
				lookupHandler.put(refName, jndiProps);

			} catch (Exception ex) {
				logger.error(
					"Error registering JndiProperties " + refName,
					ex);
			}

		}
	}


	/**
	 * xml file has following design:
	 * <ejb jndiname="xyz">
	 * 		<lookuphandler-ref name="zyx"/>
	 * 	</ejb>
	 * 
	 */
	private void initEJBParams(NodeList nodes) {
		Element ejbElement = null;
		String jndiName = null;

		Element jndiRefElement = null;
		NodeList jndiRef = null;
		JndiProperties props = null;
		String refName = null;

		Element homeInterfaceElement = null;
		String homeInterfaceName = null;

		// iterate over all nodes from type <ejb> and reqister 
		// the according ejb with its JndiProperties.
		// The jndiName of the ejb is used as key and the value
		// is the JndiProperties.
		for (int i = 0, n = nodes.getLength(); i < n; i++) {
			EJBData ejbData = new EJBData();

			// get the jndi name of the ejb and store it in an 
			// EJBData Object.
			ejbElement = (Element) nodes.item(i);
			jndiName = ejbElement.getAttribute(JNDI_NAME_ATTRIB);
			ejbData.setJndiName(jndiName);

			// if there is specified a JndiProperties for this ejb
			// use it
			jndiRef =
				ejbElement.getElementsByTagName(JNDIPROPS_REF_ELEMENT);
			if (jndiRef.getLength() > 0) {
				jndiRefElement = (Element) jndiRef.item(0);
				refName = jndiRefElement.getAttribute(NAME_ATTRIB);
				props = (JndiProperties) lookupHandler.get(refName);

				// if the user specified a JndiProperties that wasn't previously
				// registered, print a error message and use a DefaultLookupHandler
				if (props == null) {
					logger.error(
						"JndiProperties with name "
							+ refName
							+ " is not registered. "
							+ "Using default jndi properties for "
							+ jndiName);
				} else {
					ejbData.setJndiProperties(props);
				}
			}

			// get the homeInterface class name
			homeInterfaceElement =
				(Element) ejbElement.getElementsByTagName(
					HOME_INTERFACE_ELEMENT).item(
					0);
			homeInterfaceName =
				homeInterfaceElement.getFirstChild().getNodeValue();
			ejbData.setHomeInterface(homeInterfaceName);

			// store the jndi name and the corresponding EJBData Object							
			ejbDataTable.put(jndiName, ejbData);
		}
	}

	private void initAdmin(NodeList nodes) {
		if (nodes.getLength() > 0) {
			Element adminElement = (Element) nodes.item(0);
			String pUser = adminElement.getAttribute(ADMIN_USER_ATTRIB);
			if (pUser == null
				|| pUser.trim().equals("")
				|| pUser.trim().length() < 6) {
				return;
			} else {
				user = pUser;
			}

			String pPasswd = adminElement.getAttribute(ADMIN_PASSWD_ATTRIB);
			if (pPasswd == null
				|| pPasswd.trim().equals("")
				|| pPasswd.trim().length() < 6) {
				user = null;
				return;
			} else {
				passwd = pPasswd;
				if (user.equals(passwd)) {
					user = null;
					passwd = null;
					return;
				}
			}
		}
	}

	private void initRemoteLogLevel(NodeList nodes) {
		if (nodes.getLength() > 0) {
			Element remoteLevel = (Element) nodes.item(0);
			remoteManager.setClientLogLevel(
				Level.toLevel(
					remoteLevel.getAttribute(REMOTE_LOG_VALUE_ATTRIB)));
		}
	}

	public boolean checkLogin(String pUser, String pPasswd) {
		// control if both user and password has been specified
		// in jkf_server_conf.xml and therefore has been set.
		if (user == null || passwd == null) {
			return false;
		}

		if (pUser.equals(user) && pPasswd.equals(passwd)) {
			return true;
		}

		return false;
	}

	/**
	 * Returns the EJBData resource mapped to this jndi name
	 */
	public EJBData getEJBData(String jndiName) {
		return (EJBData) ejbDataTable.get(jndiName);
	}
	
	protected void log(Object message) {
		if(logger.isDebugEnabled()) {
			logger.debug(message);
		}
	}

}

⌨️ 快捷键说明

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