KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jndi > enc > EnterpriseContextInvocationHandler


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6 package org.jfox.jndi.enc;
7
8 import java.lang.reflect.InvocationHandler JavaDoc;
9 import java.lang.reflect.Method JavaDoc;
10 import javax.naming.Context JavaDoc;
11 import javax.naming.LinkRef JavaDoc;
12 import javax.naming.OperationNotSupportedException JavaDoc;
13 import javax.naming.Reference JavaDoc;
14
15 import org.jfox.jndi.InitialContextHelper;
16
17 /**
18  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
19  */

20
21 public class EnterpriseContextInvocationHandler implements InvocationHandler JavaDoc {
22
23     private static Context JavaDoc initCtx = InitialContextHelper.getInitialContext();
24
25     private Context JavaDoc enterpriseContext = null;
26
27     public EnterpriseContextInvocationHandler(Context JavaDoc enterpriseContext) {
28         this.enterpriseContext = enterpriseContext;
29     }
30
31     // hold other methed except lookup
32
public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
33         String JavaDoc methodName = method.getName();
34         if(methodName.equals("toString")) {
35             return enterpriseContext.toString();
36         }
37         if(!methodName.equals("lookup")) {
38             throw new OperationNotSupportedException JavaDoc(methodName + " is not supported in EnterpriseContext, except only lookup!");
39         }
40
41         Object JavaDoc obj = method.invoke(enterpriseContext, args);
42         if(obj instanceof Reference JavaDoc) {
43             return lookupReference((Reference JavaDoc) obj);
44         }
45         else {
46             return obj;
47         }
48
49     }
50
51     public Context JavaDoc getEnterpriseContext() {
52         return enterpriseContext;
53     }
54
55     public Context JavaDoc getInitialContext() {
56         return initCtx;
57     }
58
59     /**
60      * @param ref
61      * @return
62      * @throws Exception
63      */

64     protected Object JavaDoc lookupReference(Reference JavaDoc ref) throws Exception JavaDoc {
65         //TODO: must bind EJB env reference as LinkRef
66
if(ref instanceof LinkRef JavaDoc) {
67             return getInitialContext().lookup(((LinkRef JavaDoc) ref).getLinkName());
68         }
69         else {
70             Object JavaDoc refAddr = ref.get(0).getContent();
71             return getInitialContext().lookup(refAddr.toString());
72         }
73     }
74 }
75
76
Popular Tags