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

📄 framebeanproxy.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 java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

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

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


public class FrameBeanProxy implements IProxy {

	private HashMap beanMap = null;

	private Object executeLocalMethod(String user, String pwd,
			String interfaceName, String methodName, Class[] classes,
			Object[] objects, String[] calsses_type)
			throws ApplicationException {
		boolean checkOK = false;
		String s = "";
		// 授权处理

		// === 因jboss323无法正确转换 int double,所以作如下处理 ===
		int n = classes.length;
		int i = 0;

		for (i = 0; i < n; i++) {
			if (classes[i].getName().equals("java.lang.Integer")
					&& calsses_type[i].equals("int"))
				classes[i] = int.class;
			else if (classes[i].getName().equals("java.lang.Double")
					&& calsses_type[i].equals("double"))
				classes[i] = double.class;
			else if (classes[i].getName().equals("java.lang.Boolean")
					&& calsses_type[i].equals("boolean"))
				classes[i] = boolean.class;
		}
		String iName = name2shortName(interfaceName);
		Object beanObj = beanMap.get(iName);
		// if(this.invokeEnv.getCheckLevel().equals("0")) checkOK = true;
		// //不作函数级的校验
		checkOK = true; // 不作函数级的校验
		if (checkOK) {
			try {
				Method m = beanObj.getClass().getDeclaredMethod(methodName,
						classes);
				try {
					Object r = m.invoke(beanObj, objects);
					// 日志处理
					return r;
				} catch (IllegalAccessException e) {
					System.out
							.println("--- checkOK IllegalAccessException ---");
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					System.out
							.println("--- checkOK InvocationTargetException ---");
					e.printStackTrace();
				}
			} catch (NoSuchMethodException e) {
				System.out
						.println("--- 999 checkOK NoSuchMethodException begin ---");
				e.printStackTrace();
			}
		} else {
			ApplicationException e = new ApplicationException("[" + user + " "
					+ s + "." + methodName
					+ "not pass the authentication or authorization!!!");
			throw e;
		}
		// 日志处理
		return null;
	}

	public String name2shortName(String name) {

		String[] s = name.split("\\.");
		int n = s.length;
		if (n > 0) {
			return s[n - 1];
		} else
			return name;
	}

	public void doProxy(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		// TODO Auto-generated method stub
		String user = "";
		String pwd = "";
		String interfaceName = "";
		String methodName = "";
		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("--- FrameBeanProxy read Object error, ClassNotFoundException ---");
			ex.printStackTrace();
		} catch (Exception e) {
			id = -1;
			System.out.println("--- FrameBeanProxy read Object error ---");
			e.printStackTrace();
		}

		// 读取到数据执行调用
		if (id == 1) {
			try {
				id = -2;
				r = executeLocalMethod(user, pwd, interfaceName, methodName,
						classes, args, classes_type);

				id = 2;
			} catch (ApplicationException ae) {
				System.out.println("--- FrameBeanProxy ApplicationException : "
						+ ae + " ---");
				ae.printStackTrace();
			} catch (Exception e) {
				System.out.println("--- FrameBeanProxy 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();
	}

	public void setEnv(InvokeEnv env) {
		// TODO Auto-generated method stub

	}

	public void setBeanMap(HashMap beanMap) {
		this.beanMap = beanMap;
	}

	public HashMap getBeanMap() {
		return this.beanMap;
	}

}

⌨️ 快捷键说明

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