KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > client > ClientNamingContext


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.rmi.iiop.client;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.Name JavaDoc;
26 import javax.naming.NameNotFoundException JavaDoc;
27 import javax.naming.NameParser JavaDoc;
28 import javax.naming.NamingEnumeration JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.naming.OperationNotSupportedException JavaDoc;
31
32 import org.apache.geronimo.interop.properties.IntProperty;
33 import org.apache.geronimo.interop.properties.PropertyMap;
34 import org.apache.geronimo.interop.properties.StringProperty;
35 import org.apache.geronimo.interop.properties.SystemProperties;
36 import org.apache.geronimo.interop.rmi.iiop.ObjectRef;
37 import org.apache.geronimo.interop.rmi.iiop.compiler.StubFactory;
38 import org.apache.geronimo.interop.util.ExceptionUtil;
39 import org.omg.CosNaming.NameComponent JavaDoc;
40 import org.omg.CosNaming.NamingContext JavaDoc;
41 import org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc;
42
43 public class ClientNamingContext implements Context JavaDoc, java.io.Serializable JavaDoc
44 {
45
46     public static ClientNamingContext getInstance(Hashtable JavaDoc env)
47     {
48         ClientNamingContext nc = (ClientNamingContext) contextMap.get(env);
49         if (nc == null)
50         {
51             synchronized (contextMap)
52             {
53                 nc = (ClientNamingContext) contextMap.get(env);
54                 if (nc == null)
55                 {
56                     nc = new ClientNamingContext();
57                     nc.init(env);
58                     contextMap.put(env, nc);
59                 }
60             }
61         }
62         return nc;
63     }
64
65
66     public static final IntProperty idleConnectionTimeoutProperty =
67         new IntProperty(SystemProperties.class, "idleConnectionTimeout")
68         .defaultValue(60); // 60 seconds
69

70     public static final IntProperty lookupCacheTimeoutProperty =
71         new IntProperty(SystemProperties.class, "lookupCacheTimeout")
72         .defaultValue(600); // 10 minutes
73

74     public static final StringProperty usernameSystemProperty =
75         new StringProperty(SystemProperties.class, "java.naming.security.principal");
76
77     public static final StringProperty passwordSystemProperty =
78         new StringProperty(SystemProperties.class, "java.naming.security.credentials");
79
80     private static long idleConnectionTimeout;
81
82     private static long lookupCacheTimeout;
83
84     private static int socketTimeout;
85
86     private static HashMap JavaDoc contextMap = new HashMap JavaDoc();
87
88     private static HashMap JavaDoc hostListCache = new HashMap JavaDoc();
89
90 // private ArrayList requestKeys;
91

92     private HashMap JavaDoc cache = new HashMap JavaDoc();
93
94     private Hashtable JavaDoc env;
95
96     private ConnectionPool connectionPool;
97
98     private PropertyMap connectionProperties;
99
100     static private HashMap JavaDoc nameMap = new HashMap JavaDoc();
101
102     private String JavaDoc username;
103
104     private String JavaDoc password;
105
106     private String JavaDoc namePrefix;
107
108     private NamingContext JavaDoc serverNamingContext;
109
110     public ConnectionPool getConnectionPool() {
111         return connectionPool;
112     }
113
114     public PropertyMap getConnectionProperties()
115     {
116         return connectionProperties;
117     }
118
119     public long getIdleConnectionTimeout()
120     {
121         return idleConnectionTimeout;
122     }
123
124     public String JavaDoc getUsername()
125     {
126         return username;
127     }
128
129     public String JavaDoc getPassword()
130     {
131         return password;
132     }
133
134     // -----------------------------------------------------------------------
135
// public methods from interface javax.naming.Context
136
// -----------------------------------------------------------------------
137

138     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
139         return lookup(name.toString());
140     }
141
142     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
143         if (name.startsWith("java:comp/env/")) {
144             name = name.substring(14);
145         }
146
147         String JavaDoc newName = (String JavaDoc) nameMap.get(name);
148         if (newName != null) {
149             name = newName;
150         }
151
152         NameBinding nb = (NameBinding) cache.get(name);
153         if (nb == null) {
154             synchronized (cache) {
155                 nb = (NameBinding) cache.get(name);
156                 if (nb != null && nb.hasExpired()) {
157                     cache.remove(name);
158                     nb = null;
159                 }
160                 if (nb == null) {
161                     nb = resolve(name);
162                     cache.put(name, nb);
163                 }
164             }
165         }
166         return nb.object;
167     }
168
169     public HostList lookupHost(String JavaDoc name) {
170         NameBinding nb = (NameBinding) hostListCache.get(name);
171         if (nb == null) {
172             synchronized (hostListCache) {
173                 nb = (NameBinding) hostListCache.get(name);
174                 if (nb != null && nb.hasExpired()) {
175                     hostListCache.remove(name);
176                     nb = null;
177                 }
178                 if (nb == null) {
179                     hostListCache.put(name, nb);
180                 }
181             }
182         }
183         return (HostList) nb.object;
184     }
185
186     public static void bind(String JavaDoc bindName, String JavaDoc name) throws NamingException JavaDoc {
187         nameMap.put(bindName, name);
188     }
189
190     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
191         throw new OperationNotSupportedException JavaDoc();
192     }
193
194     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
195         throw new OperationNotSupportedException JavaDoc();
196     }
197
198     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
199         throw new OperationNotSupportedException JavaDoc();
200     }
201
202     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
203         throw new OperationNotSupportedException JavaDoc();
204     }
205
206     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
207         throw new OperationNotSupportedException JavaDoc();
208     }
209
210     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
211         throw new OperationNotSupportedException JavaDoc();
212     }
213
214     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
215         throw new OperationNotSupportedException JavaDoc();
216     }
217
218     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
219         throw new OperationNotSupportedException JavaDoc();
220     }
221
222     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
223         throw new OperationNotSupportedException JavaDoc();
224     }
225
226     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
227         throw new OperationNotSupportedException JavaDoc();
228     }
229
230     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
231         throw new OperationNotSupportedException JavaDoc();
232     }
233
234     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
235         throw new OperationNotSupportedException JavaDoc();
236     }
237
238     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
239         throw new OperationNotSupportedException JavaDoc();
240     }
241
242     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
243         throw new OperationNotSupportedException JavaDoc();
244     }
245
246     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
247         throw new OperationNotSupportedException JavaDoc();
248     }
249
250     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
251         throw new OperationNotSupportedException JavaDoc();
252     }
253
254     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
255         throw new OperationNotSupportedException JavaDoc();
256     }
257
258     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
259         throw new OperationNotSupportedException JavaDoc();
260     }
261
262     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
263         throw new OperationNotSupportedException JavaDoc();
264     }
265
266     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
267         throw new OperationNotSupportedException JavaDoc();
268     }
269
270     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
271         throw new OperationNotSupportedException JavaDoc();
272     }
273
274     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
275         throw new OperationNotSupportedException JavaDoc();
276     }
277
278     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
279         throw new OperationNotSupportedException JavaDoc();
280     }
281
282     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
283         throw new OperationNotSupportedException JavaDoc();
284     }
285
286     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
287         throw new OperationNotSupportedException JavaDoc();
288     }
289
290     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
291         throw new OperationNotSupportedException JavaDoc();
292     }
293
294     public void close() throws NamingException JavaDoc {
295         throw new OperationNotSupportedException JavaDoc();
296     }
297
298     protected void init(Hashtable JavaDoc env)
299     {
300         this.env = env;
301         Object JavaDoc urlObject = env.get(Context.PROVIDER_URL);
302         if (urlObject == null) {
303             urlObject = SystemProperties.getInstance().getProperty("java.naming.provider.url",
304                                                                    "iiop://" + "delafran-t30" + ":2000");
305         }
306         String JavaDoc url = urlObject.toString();
307         UrlInfo urlInfo = UrlInfo.getInstance(url);
308         serverNamingContext = (NamingContext JavaDoc)
309                 StubFactory.getInstance().getStub(NamingContext JavaDoc.class);
310
311         namePrefix = urlInfo.getNamePrefix();
312
313         ObjectRef ncRef = (ObjectRef) serverNamingContext;
314         ncRef.$setNamingContext(this);
315         ncRef.$setProtocol(urlInfo.getProtocol());
316         ncRef.$setHost("ns~" + urlInfo.getHost());
317         ncRef.$setPort(urlInfo.getPort());
318         ncRef.$setObjectKey(urlInfo.getObjectKey());
319         connectionPool = ConnectionPool.getInstance(this);
320         Object JavaDoc u = env.get(Context.SECURITY_PRINCIPAL);
321         Object JavaDoc p = env.get(Context.SECURITY_CREDENTIALS);
322         if (u == null)
323         {
324             u = usernameSystemProperty.getString();
325         }
326         if (p == null)
327         {
328             p = passwordSystemProperty.getString();
329         }
330         username = u != null ? u.toString() : null;
331         password = p != null ? p.toString() : null;
332
333         PropertyMap props = urlInfo.getProperties();
334         props.putAll(env);
335         PropertyMap copyProps = new PropertyMap();
336         copyProps.putAll(props);
337         for (Iterator JavaDoc i = copyProps.entrySet().iterator(); i.hasNext();)
338         {
339             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
340             String JavaDoc property = (String JavaDoc)entry.getKey();
341             Object JavaDoc value = entry.getValue();
342
343             String JavaDoc startsWith = "org.apache.geronimo.interop.rmi.";
344             if (property.startsWith(startsWith))
345             {
346                 int replace = startsWith.length();
347                 props.remove(property);
348                 props.put(property.substring(replace), value);
349             }
350         }
351         for (Iterator JavaDoc i = SystemProperties.getInstance().entrySet().iterator(); i.hasNext();)
352         {
353             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
354             String JavaDoc property = (String JavaDoc)entry.getKey();
355             Object JavaDoc value = entry.getValue();
356             if (property.startsWith("djc."))
357             {
358                 props.put(property.substring(4), value);
359             }
360         }
361         connectionProperties = props;
362         idleConnectionTimeout = 1000 * idleConnectionTimeoutProperty.getInt(url, props);
363         lookupCacheTimeout = 1000 * lookupCacheTimeoutProperty.getInt(url, props);
364     }
365
366     protected NameBinding resolve(String JavaDoc name) throws NamingException JavaDoc
367     {
368         Object JavaDoc value = org.apache.geronimo.interop.naming.NameService.getInitialContext().lookupReturnNullIfNotFound(name);
369         if (value != null)
370         {
371             NameBinding nb = new NameBinding();
372             nb.object = value;
373             nb.cacheTimeout = System.currentTimeMillis() + lookupCacheTimeout;
374             return nb;
375         }
376         try
377         {
378             NameComponent JavaDoc[] resolveName =
379                 { new NameComponent JavaDoc(namePrefix + name, "") };
380             org.omg.CORBA.Object JavaDoc object = serverNamingContext.resolve(resolveName);
381             NameBinding nb = new NameBinding();
382             nb.object = object;
383             nb.cacheTimeout = System.currentTimeMillis() + lookupCacheTimeout;
384             return nb;
385         }
386         catch (NotFound JavaDoc notFound)
387         {
388             throw new NameNotFoundException JavaDoc(name);
389         }
390         catch (Exception JavaDoc ex)
391         {
392             throw new javax.naming.NamingException JavaDoc(ExceptionUtil.getStackTrace(ex));
393         }
394     }
395 }
396
Popular Tags