運用反射實現ejb動態委派 (轉)
每個bean可能會有很多方法,一般我們透過一個delegate來sessionbean中的方法,而非直接呼叫sessionbean,delegate中只是簡單的對每個相對應的sessionbean的public方法的簡單封裝,在呼叫的時候省去了每次對home的查詢和的create,但是可能我們的bean會有很多方法,如果每個bean都寫這樣一個delegate,這樣工作量就會很大,而且也不便於以後的移植,比如說,原來使用ejb實現,現在要改用jdo直接操作,而透過運用的reflect技術,就能較好地實現這些要求。首先,定義了一個FacadeDelegate的抽象類,用來實現對sessionbean的home的查詢,程式碼如下::namespace prefix = o ns = "urn:schemas--com::office" />
import javax.ejb.*;
import testejb.util.common.*;
import testejb.util.re.*;
public abstract class FacadeDelegate{
private static String type = Resource.RemoteType;
public FacadeDelegate() {
}
public EJBHome getHome(String jindiName,Class className)
{
EJBHome home = null;
ServerLocatorAdapter adapter = ServerLocatorAdapter.getInstance();
try
{
home = (EJBHome)adapter.getHome(type, jindiName, className);
}
catch(Exception e)
{
System.err.println(e.getMessage() + jindiName + className.toString());
}
return home;
}
}
其中ServerLocatorAdapter是一個用來根據是local還是remote呼叫ejb物件而透過不同的方法查詢home的類,如果type為local則呼叫LocalServerLocate中的方法,如果type為remote則呼叫RemoteServerLocate中的方法,獲得home。程式碼如下:
import java.util.*;
import java.lang.reflect.*;
import testejb.util.resource.*;
public class ServerLocatorAdapter {
private Map cache;//用來快取home
private static ServerLocatorAdapter me;
public static ServerLocatorAdapter getInstance()
{
if(me == null)
me = new ServerLocatorAdapter();
return me;
}
//取得home
public getHome(String type,String jndiHomeName,Class className) throws Exception
{
Object home = null;
if(cache.containsKey(jndiHomeName))
return cache.get(jndiHomeName);
if(Resource.LocalType.equals(type))
{
home = getLocalHome(jndiHomeName,className);
cache.put(jndiHomeName,home);
return home;
}
if(Resource.RemoteType.equals(type))
{
home = getRemoteHome(jndiHomeName,className);
cache.put(jndiHomeName,home);
return home;
}
return home;
}
//取得local home
private Object getLocalHome(String jndiHomeName,Class className) throws Exception
{
Class myClass = Class.forName(Resource.LocalClass);
// Resource. LocalClass =”testejb.util.common. LocalServerLocator
Method method = myClass.getMethod(Resource.LocalConstractMethod,null);
// Resource. LocalConstractMethod =” getInstance”
LocalServerLocator local = null;
local = (LocalServerLocator)method.invoke(myClass,null);
return local.getLocalHome(jndiHomeName,className);
}
//取得remote home
private Object getRemoteHome(String jndiHomeName,Class className) throws Exception
{
Class myClass = Class.forName(Resource.RemoteClass);
// Resource.RemoteClass =”testejb.util.common.RemoteServerLocator”
Method method = myClass.getMethod(Resource.RemoteConstractMethod,null);
// Resource.RemoteConstractMethod=” getInstance”
RemoteServerLocator remote = null;
remote = (RemoteServerLocator)method.invoke(myClass,null);
return remote.getHome(jndiHomeName,className);
}
private ServerLocatorAdapter() {
// 為cache提供執行緒的保證
cache = Collections.synchronizedMap(new HashMap());
}
}
其中Resource為資源類,其中透過對的讀取,取得一些指定的配置資訊。
RemoteServerLocator和LocalServerLocator是兩個根據不同的呼叫方式取得home藉口的具體實現類,程式碼如下:
LocalServerLocator:
import javax.naming.*;
import javax..PortableRemoteObject;
import java.util.*;
import javax.ejb.*;
public class LocalServerLocator {
private Context ic;
private Map cache;//快取home
private static LocalServerLocator me;
public static LocalServerLocator getInstance()
{
if(me == null)
{
try
{
me = new LocalServerLocator();
}
catch(Exception e)
{
System.err.println(e.getCause());
System.err.println(e.getMessage());
}
}
return me;
}
public EJBLocalHome getLocalHome(String jndiHomeName, Class className) throws Exception {
EJBLocalHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBLocalHome) cache.get(jndiHomeName);
} else {
Object obf = ic.lookup(jndiHomeName);
home = (EJBLocalHome) objref;
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
System.err.println(jndiHomeName);
throw ne;
} catch (Exception e) {
throw e;
}
return home;
}
private LocalServerLocator() throws Exception{
try
{
ic = new InitialContext();
// 為cache提供執行緒安全的保證
cache = Collections.synchronizedMap(new HashMap());
}
catch(NamingException ne)
{
throw ne;
}
catch(Exception e)
{
throw e;
}
}
}
RemoteServerLocator
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import javax.ejb.*;
public class RemoteServerLocator{
private Context ic;
private Map cache;
private static RemoteServerLocator me;
public static RemoteServerLocator getInstance()
{
if(me == null)
{
try
{
me = new RemoteServerLocator();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
return me;
}
public EJBHome getHome(String jndiHomeName, Class className) throws Exception {
EJBHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBHome) cache.get(jndiHomeName);
} else {
Object objref = ic.lookup(jndiHomeName);
Object obj = PortableRemoteObject.narrow(objref, className);
home = (EJBHome) obj;
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
System.err.println(jndiHomeName);
throw ne;
} catch (Exception e) {
throw e;
}
return home;
}
private RemoteServerLocator() throws Exception{
try {
ic = getInitialContext();
// 為cache提供執行緒安全的保證
cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException ne) {
throw ne;
} catch (Exception e) {
throw e;
}
}
private javax.naming.Context getInitialContext() throws NamingException {
java.util.Hashtable JNDIPa= new java.util.Hashtable();
JNDIParm.put(Context.PROVR_URL, "your server address");
JNDIParm.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
return new InitialContext(JNDIParm);
}
}
對上面這些呼叫機制有個瞭解之後,下面就是來具體的實現動態委派了,再此定義了一個FacadeDelegateImp類,繼承了FacadeDelegate類。先看一下程式碼,然後對此作解釋,這樣比較清楚一些
import testejb.delegate.common.*;
import javax.ejb.*;
import java.lang.reflect.*;
import java.util.*;
public class FacadeDelegateImp extends FacadeDelegate {
private static FacadeDelegateImp me;
private Map cache;
private HashMap methodMap;
private Object object;
public static FacadeDelegateImp getInstance()
{
if(me == null)
me = new FacadeDelegateImp();
return me;
}
//init方法是在呼叫invoke之前對要呼叫的sessionbean進行初始化
public void init(String jindiName, String className) {
EJBHome home = null;
if(cache.containsKey(jindiName))
home = (EJBHome)cache.get(jindiName);
else
{
try
{
home = super.getHome(jindiName, Class.forName(className));//呼叫父類的的方法取得home
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
cache.put(jindiName,className);
}
try
{
object = home.getClass().getMethod("create", null).invoke(home, null);//呼叫home的//create方法,取得ejbObject
methodMap = new HashMap();
//將ejbObject中所有的方法存入methodMap中
Method[] aryMethod = object.getClass().getMethods();
if(aryMethod != null && aryMethod.length > 0)
{
for (int i = 0; i < aryMethod.length; i++) {
methodMap.put(aryMethod[i].getName(), aryMethod[i]);
}
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
//此init方法是對一般java類初始化
public void init(String className,Object[] args)
{
boolean flage = false;
if(cache.get(className) != null)
object = cache.get(className);
else
{
try {
Class myClass = Class.forName(className);
if (args != null && args.length > 0) {
Class[] type = new Class[args.length];
for (int i = 0; i < type.length; i++) {
type[i] = args[i].getClass();
}
Constructor constructor = myClass.getConstructor(type);
object = constructor.newInstance(args);
cache.put(className, object);
}
else {
object = myClass.newInstance();
cache.put(className, object);
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
Method[] methods = object.getClass().getMethods();
methodMap = new HashMap();
for(int i = 0; i< methods.length; i++)
methodMap.put(methods[i].getName(),methods[i]);
}
public Object invoke(String method, Object[] args,String jindiName, String className)
{
if("init".equals(method))
{
this.init(jindiName, className);
return null;
}
if(methodMap == null)
this.init(jindiName, className);
Method tmpMethod = (Method)methodMap.get(method);
if(tmpMethod != null)
{
try
{
return tmpMethod.invoke(object, args);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
return null;
}
public Object invoke(String method, Object[] args, String className)
{
if("init".equals(method))
{
this.init(className,args);
return null;
}
if(methodMap == null)
System.err.println("not init");
Method tmpMethod = (Method)methodMap.get(method);
if(tmpMethod != null)
{
try
{
return tmpMethod.invoke(object, args);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
return null;
}
private FacadeDelegateImp() {
cache = Collections.synchronizedMap(new HashMap());
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-958837/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- java反射——實現程式的動態性Java反射
- 反射-動態代理的概述和實現反射
- 有沒有用EJB(CMP)實現動態查詢的方法?
- C#動態執行函式:利用反射實現C#函式反射
- 00024.06 反射:反射的應用2:動態建立物件反射物件
- c++ web框架實現之靜態反射實現C++Web框架反射
- 用PHP動態生成虛擬現實VRML網頁 (轉)PHPVR網頁
- 反射的妙用:C#通過反射動態生成型別繼承介面並實現反射C#型別繼承
- 在 Java 中運用動態掛載實現 Bug 的熱修復Java
- 關於用ejb實現負載平衡負載
- JAVA的反射機制==>用反射分析類的實現Java反射
- 使用iptable實現動態防火牆(轉)防火牆
- BCB中實現動態建立元件 (轉)元件
- css3實現線條環形動態運動效果CSSS3
- Flask 運用Xterm實現互動終端Flask
- PHP 反射之動態代理PHP反射
- java 反射和動態代理Java反射
- 反射,註解,動態代理,依賴注入控制反轉反射依賴注入
- SQL 如何實現動態的行列轉置SQL
- webwork下面如何實現動態轉發Web
- iOS動態庫和靜態庫的運用iOS
- Protobuf_動態訊息-反射反射
- 輕鬆實現SINDOWS 98動態選單 (轉)
- WIN下動態註冊碼實現方法 (轉)
- MFC抽象類之動態實現技術 (轉)抽象
- Oracle元件實現動態Web資料庫(轉)Oracle元件Web資料庫
- FSM狀態機及C#反射實現邏輯C#反射
- [.net 物件導向程式設計進階] (20) 反射(Reflection)(上)利用反射技術實現動態程式設計物件程式設計反射
- 用C實現動態擴容的string
- EJB實用原理和實戰示例
- 在VB中實現窗體的動態效果 (轉)
- 利用模板實現動態的繼承體系 (轉)繼承
- Java動態代理和反射機制Java反射
- 【譯】11. Java反射——動態代理Java反射
- 四則運算實現 (轉)
- Kettle實現動態SQLSQL
- 動態棧的實現
- 使用動態SQL語句實現簡單的行列轉置(動態產生列)SQL