KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > JndiProxyObjectFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.resource;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.NameNotFoundException JavaDoc;
33 import javax.naming.RefAddr JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.spi.ObjectFactory JavaDoc;
36 import javax.naming.spi.InitialContextFactory JavaDoc;
37
38
39 /**
40  * A proxy object factory for an external JNDI factory
41  */

42 public class JndiProxyObjectFactory implements ObjectFactory JavaDoc {
43
44     // for every external-jndi-resource there is an InitialContext
45
// created from the factory and environment properties
46
private static Hashtable JavaDoc contextMap = new Hashtable JavaDoc();
47
48     protected static Context JavaDoc removeInitialContext(String JavaDoc jndiName) {
49         return (Context JavaDoc) contextMap.remove(jndiName);
50     }
51
52     /**
53      * load the context factory
54      */

55     private Context JavaDoc loadInitialContext(String JavaDoc factoryClass, Hashtable JavaDoc env) {
56     Object JavaDoc factory = ResourceInstaller.loadObject(factoryClass);
57         if (factory == null) {
58             System.err.println("Cannot load external-jndi-resource " +
59                                    "factory-class '" + factoryClass + "'");
60                 return null;
61         } else if (! (factory instanceof
62                             javax.naming.spi.InitialContextFactory JavaDoc)) {
63
64                 System.err.println("external-jndi-resource factory-class '"
65                                   + factoryClass + "' must be of type "
66                                   + "javax.naming.spi.InitialContextFactory");
67                 return null;
68         }
69
70         Context JavaDoc context = null;
71         try {
72             context = ((InitialContextFactory JavaDoc)factory).getInitialContext(env);
73         } catch (NamingException JavaDoc ne) {
74             System.err.println("Exception thrown creating initial context " +
75                                    "for external JNDI factory '" +
76                                    factoryClass + "' " + ne.getMessage());
77         }
78
79     return context;
80     }
81
82     /**
83     * create the object instance from the factory
84     */

85     public Object JavaDoc getObjectInstance(Object JavaDoc obj,
86                     Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc environment)
87                     throws NamingException JavaDoc {
88
89         // name to lookup in the external factory
90
String JavaDoc jndiLookupName = "";
91         String JavaDoc jndiFactoryClass = null;
92         String JavaDoc bindName = null;
93
94         // get the target initial naming context and the lookup name
95
Reference JavaDoc ref = (Reference JavaDoc) obj;
96         Enumeration JavaDoc addrs = ref.getAll();
97         while (addrs.hasMoreElements()) {
98             RefAddr JavaDoc addr = (RefAddr JavaDoc) addrs.nextElement();
99
100             String JavaDoc prop = addr.getType();
101             if (prop.equals("jndiName")) {
102                 bindName = (String JavaDoc)addr.getContent();
103             }
104             else if (prop.equals("jndiLookupName")) {
105                 jndiLookupName = (String JavaDoc) addr.getContent();
106             }
107             else if (prop.equals("jndiFactoryClass")) {
108                 jndiFactoryClass = (String JavaDoc) addr.getContent();
109             }
110         }
111
112         if (bindName == null) {
113             throw new NamingException JavaDoc("JndiProxyObjectFactory: no bindName context info");
114         }
115
116         ProxyRefAddr contextAddr = (ProxyRefAddr)ref.get(bindName);
117         Hashtable JavaDoc env = null;
118         if (contextAddr == null ||
119             jndiFactoryClass == null ||
120             (env = (Hashtable JavaDoc)(contextAddr.getContent())) == null) {
121             throw new NamingException JavaDoc("JndiProxyObjectFactory: no info in the reference about the target context; contextAddr = " + contextAddr + " env = " + env + " factoryClass = " + jndiFactoryClass);
122     }
123
124         // Context of the external naming factory
125
Context JavaDoc context = (Context JavaDoc)contextMap.get(bindName);
126         if (context == null) {
127             synchronized (contextMap) {
128                 context = (Context JavaDoc)contextMap.get(bindName);
129                 if (context == null) {
130                     context = loadInitialContext(jndiFactoryClass, env);
131                     contextMap.put(bindName, context);
132                 }
133             }
134         }
135
136         if (context == null)
137             throw new NamingException JavaDoc("JndiProxyObjectFactory no InitialContext" + jndiFactoryClass);
138
139         // use the name to lookup in the external JNDI naming context
140
try {
141             return context.lookup(jndiLookupName);
142         } catch (NameNotFoundException JavaDoc e) {
143             throw new ExternalNameNotFoundException(e);
144         }
145     }
146 }
147
Popular Tags