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

📄 ejbproxy.java

📁 该HttpProxy用于从Applet或Swing界面中访问Ejb和服务端的JavaBean。
💻 JAVA
字号:
/**
 * @作者: 胡俊
 * @创建时间: 2008-03-05
 * @更新时间: 2008-03-05
 * @描述: 
 * 
 */
package com.nari.pmos.proxy.server;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nari.pmos.proxy.common.ApplicationException;
import com.nari.pmos.proxy.common.EjbInvoke;
import com.nari.pmos.proxy.common.InvokeEnv;


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

	private InvokeEnv invokeEnv = null;

	private EjbInvoke ejbInvoke = null;

	/**
	 * 
	 */
	public EjbProxy() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void setEnv(InvokeEnv env) {
		invokeEnv = env;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nari.pmos.proxy.servlet.IProxy#doProxy(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public void doProxy(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		// TODO Auto-generated method stub
		String user = "";
		String pwd = "";
		String interfaceName = "";
		String methodName = "";
		String s = "";
		Integer N;
		// 使用反射机制
		Class[] classes = null;
		String[] classes_type = null;
		Object[] args = null;
		Object r = null;

		int i = 0, n = 0, id = 0;
		/*
		 * try{ HttpSession hSess = req.getSession(); Object obj =
		 * hSess.getAttribute("loginfo"); }catch(Exception e) {
		 * System.out.println("--- PmosEjbServlet getSession() error ---!");
		 * e.printStackTrace(); }
		 */
		// 读取数据
		ObjectInputStream in = new ObjectInputStream(req.getInputStream());
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteOut);

		try {
			user = (String) in.readObject(); // user account
			pwd = (String) in.readObject(); // user pwd
			interfaceName = (String) in.readObject();
			methodName = (String) in.readObject();
			N = (Integer) in.readObject();
			n = N.intValue();
			// System.out.println("--- EjbProxy step 1 ---");
			if (n > 0) {
				classes = new Class[n];
				classes_type = new String[n];
				args = new Object[n];
			} else {
				classes = new Class[0];
				classes_type = new String[0];
				args = null;
			}
			for (i = 0; i < n; i++) {
				classes[i] = (Class) in.readObject();
			}
			for (i = 0; i < n; i++) {
				args[i] = (Object) in.readObject();
			}
			for (i = 0; i < n; i++) {
				classes_type[i] = (String) in.readObject();
			}
			id = 1;
			// System.out.println("--- EjbProxy step 2 ---");
		} catch (ClassNotFoundException ex) {
			id = -1;
			System.out
					.println("--- EjbProxy read Object error, ClassNotFoundException ---");
			ex.printStackTrace();
		} catch (Exception e) {
			id = -1;
			System.out.println("--- EjbProxy read Object error ---");
			e.printStackTrace();
		}

		// 读取到数据执行调用
		if (id == 1) {
			// System.out.println("--- EjbProxy step 3 ---");
			try {
				id = -2;
				// 采用local方式
				if (ejbInvoke == null) {
					ejbInvoke = new EjbInvoke();
					ejbInvoke.setInvokeEnv(invokeEnv);
				}
				r = ejbInvoke.executeLocalMethod(user, pwd, interfaceName,
						methodName, classes, args, classes_type);

				id = 2;
			} catch (ApplicationException ae) {
				System.out.println("--- EjbProxy ApplicationException : " + ae
						+ " ---");
				ae.printStackTrace();
			} catch (Exception e) {
				System.out.println("--- EjbProxy Exception : " + e + " ---");
				e.printStackTrace();
			}
		} else {
			id = -1;
		}
		// 写并返回数据
		if (id == 2) {
			if (r != null) {// 正确执行并返回一个对象
				out.writeObject(new Integer(1));
				out.writeObject(r);
			} else {
				out.writeObject(new Integer(0));
			}
		} else {
			out.writeObject(new Integer(id));
		}
		// Flush 数据缓冲区
		out.flush();
		byte buf[] = byteOut.toByteArray();

		// 设置属性
		res.setContentType("application/octet-stream");
		res.setHeader("Cache-Control", "no-cache");
		res.setContentLength(buf.length);

		// 发送数据
		DataOutputStream dataOut = new DataOutputStream(res.getOutputStream());
		dataOut.write(buf);
		dataOut.flush();
		dataOut.close();
	}
}

⌨️ 快捷键说明

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