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

📄 proxyservlet.java

📁 该HttpProxy用于从Applet或Swing界面中访问Ejb和服务端的JavaBean。
💻 JAVA
字号:
package com.nari.pmos.proxy.servlet;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import pbos.dao.DB.ConnectParameters;

import com.nari.pmos.proxy.common.InvokeEnv;
import com.nari.pmos.proxy.server.EjbProxy;
import com.nari.pmos.proxy.server.FrameBeanProxy;
import com.nari.pmos.proxy.server.IProxy;
import com.nari.pmos.proxy.server.LoginSrv;

/**
 * @author hujun
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ProxyServlet extends HttpServlet {

	private ServletConfig config = null;

	private InvokeEnv invokeEnv = null;

	private IProxy ejbProxy = null;

	private IProxy frmBeanProxy = null;// 2007-05-31

	private IProxy beanProxy = null;// 2007-05-31

	private final HashMap beanMap = new HashMap();

	private void readBeanConfig() {// 2007-06-01
		ServletContext ctx = this.getServletContext();
		try {
			// 使用dom解析bean-config.xml,装载代理类(类似Spring?)
			URL url = ctx.getResource("/WEB-INF/bean-config.xml");
			URI uri = new URI(url.toString());
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			org.w3c.dom.Document document = builder.parse(uri.toString());
			Element beanCfgElement = document.getDocumentElement();
			Element frmBeanElement = null;
			Element beanElement = null;
			Node node = null;
			Node node1 = null;
			if (beanCfgElement.getTagName().equals("bean-config")) {
				if (beanCfgElement.hasChildNodes()) {
					node = beanCfgElement.getFirstChild();
					do {
						if (node.getNodeType() == Node.ELEMENT_NODE
								&& node.getNodeName().equals("frm-bean")) {
							frmBeanElement = (Element) node;
							node1 = frmBeanElement.getFirstChild();
							do {
								if (node1.getNodeType() == Node.ELEMENT_NODE
										&& node1.getNodeName().equals("bean")) {
									beanElement = (Element) node1;
									String beanName = beanElement
											.getAttribute("name");
									String beanClassName = beanElement
											.getAttribute("class");
									try {
										// 创建这些类实例,并将此放入beanMap中
										Object beanObj = Class.forName(
												beanClassName).newInstance();
										this.beanMap.put(beanName, beanObj);
									} catch (Exception e) {
										System.out
												.println("beanClassName Exception: "
														+ beanClassName
														+ "  !!!");
									}
								}
								node1 = node1.getNextSibling();
							} while (node1 != null);

						} else if (node.getNodeType() == Node.ELEMENT_NODE
								&& node.getNodeName().equals("sess-bean")) {

						}
						node = node.getNextSibling();
					} while (node != null);
				}
			}

		} catch (Exception e) {
			System.out.println("readBeanConfig Exception !!!");
		}
	}

	public void init(ServletConfig c) throws ServletException {
		String s = "";
		super.init(c);
		this.config = c;
		// 在web.xml中设置servlet的参数
		if (invokeEnv == null)
			invokeEnv = new InvokeEnv();
		// 服务器名称
		try {
			s = config.getInitParameter("srv");
			if (s != null)
				invokeEnv.setSrvName(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [srv]---");
		}
		// url
		try {
			s = config.getInitParameter("url");
			if (s != null)
				invokeEnv.setProviderURL(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [url]---");
		}
		// 工厂名称
		try {
			s = config.getInitParameter("ncf");
			if (s != null)
				invokeEnv.setInitCtxFactory(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [ncf] ---");
		}
		// 访问模式 local, remote
		try {
			s = config.getInitParameter("mode");
			if (s != null)
				invokeEnv.setAccessMode(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [mode] ---");
		}
		// ejb jndi 前缀
		try {
			s = config.getInitParameter("prefix");
			if (s != null)
				invokeEnv.setJndiPrefix(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [prefix] ---");
		}
		// ejb jndi 后缀
		try {
			s = config.getInitParameter("postfix");
			if (s != null)
				invokeEnv.setJndiPostfix(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [postfix] ---");
		}
		try {
			s = config.getInitParameter("check");
			if (s != null)
				invokeEnv.setCheckLevel(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [check] ---");
		}
		// 数据源
		try {
			s = config.getInitParameter("data_src");
			if (s != null)
				invokeEnv.setDataSource(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [data_src] ---");
		}
		// Context Path
		try {
			s = config.getInitParameter("context_path");
			if (s != null)
				invokeEnv.setContextPath(s);
		} catch (Exception e) {
			System.out
					.println("--- ProxyServlet getInitParamter Error [context_path] ---");
		}
		// 创建fream bean
		readBeanConfig();
		// 设置数据源
		ConnectParameters.DataSource = new String(invokeEnv.getDataSource());

		// 设置全局web应用的环境变量 2007-04-21 增加,注意要在contextlisnter中清除全局变量
		ServletContext context = c.getServletContext();
		Object obj = context.getAttribute("PmosInvokeEnv");
		if (obj == null) {
			context.setAttribute("PmosInvokeEnv", invokeEnv);
		}
	}

	public void destroy() {
		try {
			beanMap.clear();
		} catch (Exception e) {
			System.out.println(" ProxyServlet beanMap.clear()");
			e.printStackTrace();
		}
		super.destroy();
	}

	public void service(HttpServletRequest req, HttpServletResponse res)
			throws IOException {

		String proxyMode = req.getParameter("proxymode");

		if (proxyMode.equalsIgnoreCase("ejb")) {
			if (ejbProxy == null) {
				ejbProxy = new EjbProxy();
				ejbProxy.setEnv(invokeEnv);
			}
			ejbProxy.doProxy(req, res);
		} else if (proxyMode.equalsIgnoreCase("frm_bean")) {// 2007-05-31 bean
			// of servlet
			if (frmBeanProxy == null) {
				FrameBeanProxy fbean = new FrameBeanProxy();
				frmBeanProxy = fbean;
				frmBeanProxy.setEnv(invokeEnv);
				fbean.setBeanMap(beanMap);
			}
			frmBeanProxy.doProxy(req, res);
		} else if (proxyMode.equalsIgnoreCase("bean")) {// 2007-05-31 bean of
			// http session
			if (beanProxy == null) {
				beanProxy = new EjbProxy();
				beanProxy.setEnv(invokeEnv);
			}
			beanProxy.doProxy(req, res);
		} else if (proxyMode.equalsIgnoreCase("logon")) {
			System.out.println("--- logon ---");
			LoginSrv loginSrv = new LoginSrv();
			loginSrv.setCmd("logon");
			loginSrv.doLogin(req, res);
		} else if (proxyMode.equalsIgnoreCase("logout")) {
			LoginSrv loginSrv = new LoginSrv();
			loginSrv.setCmd("logout");
			loginSrv.doLogin(req, res);
		}
	}
}

⌨️ 快捷键说明

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