KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > naming > NamingContext


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.naming;
19
20 import java.util.HashMap JavaDoc;
21 import javax.naming.NameNotFoundException JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.Context JavaDoc;
24
25 import org.apache.geronimo.interop.adapter.Adapter;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 public class NamingContext {
30
31     private final Log log = LogFactory.getLog(NamingContext.class);
32
33     public static final NamingContext getInstance(Class JavaDoc baseClass) {
34         NamingContext context;
35         synchronized (contextMap) {
36             context = (NamingContext) contextMap.get(baseClass);
37             if (context == null) {
38                 context = new NamingContext();
39                 contextMap.put(baseClass, context);
40                 context.init(baseClass);
41             }
42         }
43         return context;
44     }
45
46     private static ThreadLocal JavaDoc current = new ThreadLocal JavaDoc();
47     private static HashMap JavaDoc contextMap = new HashMap JavaDoc();
48     private static boolean quiet = false; // TODO: Configure
49
private static boolean verbose = true; // TODO: Configure
50
private String JavaDoc logContext;
51     private HashMap JavaDoc map = new HashMap JavaDoc();
52     private HashMap JavaDoc failedBindings = new HashMap JavaDoc();
53
54     public static final NamingContext getCurrent() {
55         return (NamingContext) current.get();
56     }
57
58     public static final NamingContext push(NamingContext that) {
59         NamingContext restore = getCurrent();
60         current.set(that);
61         return restore;
62     }
63
64     public static void pop(NamingContext restore) {
65         current.set(restore);
66     }
67
68     public HashMap JavaDoc getMap() {
69         return map;
70     }
71
72     public Object JavaDoc lookup(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
73
74         log.debug( "NameContext.lookup(): name = " + name + ", prefix = " + prefix );
75
76         if (prefix != null) {
77             name += prefix + "/" + name;
78         }
79
80         // Note: this part of the method is performance critical. Please
81
// refrain from using string concatenation, synchronization and
82
// other slow calls here. All possible initialization should
83
// be performed in 'init' so as to permit this method to be as
84
// fast as possible (i.e. a simple unsynchronized HashMap lookup).
85

86         Object JavaDoc value = map.get(name);
87
88         if (value == null) {
89             value = dynamicLookup(name);
90             if (value != null) {
91                 map.put(name, value); // TODO: allow refresh.
92
}
93         }
94
95         // If it is corbaname type bind, give it one more chance to bind
96
// if not already bound.
97

98         if (value == null)
99         {
100             value = tryBindCorbaName(name);
101         }
102
103         if (value == null) {
104             NameNotFoundException JavaDoc notFound = new NameNotFoundException JavaDoc(name.length() == 0 ? formatEmptyName() : name);
105             if (!quiet) {
106                 NameServiceLog.getInstance().warnNameNotFound(logContext, notFound);
107             }
108             throw notFound;
109         } else {
110             return value;
111         }
112     }
113
114     public Object JavaDoc lookupReturnNullIfNotFound(String JavaDoc name, String JavaDoc prefix) {
115         if (prefix != null) {
116             name += prefix + "/" + name;
117         }
118         return map.get(name);
119     }
120
121     protected void init(Class JavaDoc baseClass) {
122         // TODO: Nothing really to do as this would init all the env-prop res-ref ... from a component
123
// this logic isn't required for the CORBA container.
124
}
125
126     protected synchronized void bindAdapter(Adapter adp) {
127         String JavaDoc names[] = adp.getBindNames();
128         for( int i=0; i<names.length; i++ ) {
129             log.debug( "NameContext.bindAdapter(): name[" + i + "] = " + names[i] + ", adp = " + adp );
130             map.put(names[i], adp);
131         }
132     }
133
134     protected synchronized void unbindAdapter( Adapter adp ) {
135         String JavaDoc names[] = adp.getBindNames();
136         for( int i=0; i<names.length; i++ )
137         {
138             log.debug( "NameContext.bindAdapter(): name[" + i + "] = " + names[i] + ", adp = " + adp );
139             map.remove( names[i] );
140         }
141     }
142
143     protected boolean adapterExists(String JavaDoc name) {
144         System.out.println("TODO: NamingComponent.componentExists(): name = " + name);
145         return false;
146     }
147
148     /*
149      * The allows the server to bind an object whose name beings with "lookup=".
150      * The lookup= instructs the name service to perform a lookup on another name
151      * service.
152      */

153     protected Object JavaDoc bindCorbaName(String JavaDoc name, String JavaDoc value)
154     {
155         String JavaDoc url = value.substring("lookup=".length());
156
157         /*
158          * value will only have the following two patterns:
159          *
160          * lookup=corbaname...
161          * lookup=corbaloc...
162          *
163          * These are placed into the URL that is sent to the context factory.
164          * The context factory then determine how to perform a lookup on a
165          * given corbaname or corbaloc url.
166          */

167
168         java.util.Properties JavaDoc p = new java.util.Properties JavaDoc();
169
170         /*
171          * corbaname and corbaloc urls are not supported by the OpenEJB name service
172          */

173         p.put(Context.INITIAL_CONTEXT_FACTORY, "" ); // org.openejb.client.RemoteInitialContextFactory ??
174
p.put(Context.PROVIDER_URL, url);
175
176         Context JavaDoc initialContext = null;
177         Object JavaDoc object = null;
178         try
179         {
180             initialContext = new javax.naming.InitialContext JavaDoc(p);
181             object = initialContext.lookup("");
182         }
183         catch (javax.naming.NamingException JavaDoc ne)
184         {
185             failedBindings(name, value);
186             NameServiceLog.getInstance().warnBindFailed(logContext, name, url, ne);
187             return null;
188         }
189         catch (java.lang.IllegalArgumentException JavaDoc ie)
190         {
191             NameServiceLog.getInstance().warnBindFailed(logContext, name, url, ie);
192             return null;
193         }
194         catch (Exception JavaDoc ex)
195         {
196             failedBindings(name, value);
197             NameServiceLog.getInstance().warnBindFailed(logContext, name, url, ex);
198             return null;
199         }
200
201         if (object == null)
202         {
203             NameServiceLog.getInstance().warnIllegalBindValue(logContext, Object JavaDoc.class, name, url);
204             return null;
205         }
206
207         map.put(name, object);
208
209         return object;
210     }
211
212     protected Object JavaDoc dynamicLookup(String JavaDoc name) {
213         return null;
214     }
215
216     protected String JavaDoc formatEmptyName() {
217         return "formatEmptyName:";
218     }
219
220     // bind for corbaname failed at server startup. We will try to bind once
221
// again.
222
private Object JavaDoc tryBindCorbaName(String JavaDoc name)
223     {
224         Object JavaDoc obj = null;
225         Object JavaDoc val = failedBindings.get(name);
226         if( val != null)
227         {
228             obj = bindCorbaName(name, (String JavaDoc)val);
229         }
230         return obj;
231     }
232
233     /**
234      * If corbaname bindings fail, give it one more chance at the time of
235      * lookup
236      */

237     private void failedBindings(String JavaDoc name, String JavaDoc value)
238     {
239         Object JavaDoc val = failedBindings.get(name);
240         if( val == null)
241         {
242             failedBindings.put(name, value);
243         }
244         else
245         {
246             //If the binding already exists in the map, then we have already given
247
//it one more chance to bind. Time to remove it.
248
failedBindings.remove(name);
249         }
250     }
251
252 }
253
Popular Tags