KickJava   Java API By Example, From Geeks To Geeks.

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


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
7 package org.jfox.jndi.enc;
8
9 import java.util.Hashtable JavaDoc;
10 import javax.naming.Context JavaDoc;
11 import javax.naming.Name JavaDoc;
12 import javax.naming.spi.ObjectFactory JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class EnterpriseContextFactory implements ObjectFactory JavaDoc {
19     public final static EnterpriseType WEB = new EnterpriseType("web");
20     public final static EnterpriseType EJB = new EnterpriseType("ejb");
21
22     public final static String JavaDoc ENC_JNDI_NAME = "java:comp/env";
23
24     /**
25      * store current thread runtime type, EJB or WEB
26      */

27     private static ThreadLocal JavaDoc tl = new ThreadLocal JavaDoc();
28     private final static String JavaDoc WEB_ENC_OBJECT_FACTORY_CLASS = "org.jfox.deployment.web.WebEnterpriseContextFactory";
29     private final static String JavaDoc EJB_ENC_OBJECT_FACTORY_CLASS = "org.jfox.ejb.naming.EJBEnterpriseContextFactory";
30
31     public static EnterpriseType enterWeb() {
32         EnterpriseType type = (EnterpriseType) tl.get();
33         tl.set(WEB);
34         return type;
35     }
36
37     public static EnterpriseType enterEjb() {
38         EnterpriseType type = (EnterpriseType) tl.get();
39         tl.set(EJB);
40         return type;
41     }
42
43     public static void enter(EnterpriseType type) {
44         tl.set(type);
45     }
46
47     public static boolean isEnterpriseContext(Name JavaDoc name) {
48         if(name.toString().startsWith(ENC_JNDI_NAME) && name.toString().length() > (ENC_JNDI_NAME.length() + 1)) {
49             return true;
50         }
51         return false;
52     }
53
54     public static Name JavaDoc getEnterpriseContextSuffix(Name JavaDoc name) {
55         if(isEnterpriseContext(name)) {
56             return name.getSuffix(2);
57         }
58         else {
59             return name;
60         }
61     }
62
63     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc environment) throws Exception JavaDoc {
64         EnterpriseType type = (EnterpriseType) tl.get();
65         if(type.equals(WEB)) {
66             ObjectFactory JavaDoc of = (ObjectFactory JavaDoc) Thread.currentThread().getContextClassLoader().loadClass(WEB_ENC_OBJECT_FACTORY_CLASS).newInstance();
67             return of.getObjectInstance(obj, name, nameCtx, environment);
68         }
69         else if(type.equals(EJB)) {
70             ObjectFactory JavaDoc of = (ObjectFactory JavaDoc) Thread.currentThread().getContextClassLoader().loadClass(EJB_ENC_OBJECT_FACTORY_CLASS).newInstance();
71             return of.getObjectInstance(obj, name, nameCtx, environment);
72         }
73         else {
74             throw new RuntimeException JavaDoc("can only use java:comp/env in ejb or web application");
75         }
76
77     }
78
79     public static class EnterpriseType {
80
81         private String JavaDoc type = "ejb";
82
83         public EnterpriseType(String JavaDoc type) {
84             this.type = type;
85         }
86
87         public String JavaDoc getType() {
88             return type;
89         }
90
91         public boolean equals(Object JavaDoc obj) {
92             return type.equals(((EnterpriseType) obj).type);
93         }
94
95         public int hashCode() {
96             return type.hashCode();
97         }
98     }
99
100     public static void main(String JavaDoc[] args) {
101
102     }
103 }
104
Popular Tags