KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > client > ClientNamingContext


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.client;
20
21 import gcc.properties.*;
22 import gcc.util.*;
23 import gcc.rmi.iiop.ObjectRef;
24 import gcc.rmi.iiop.compiler.StubFactory;
25
26 import java.util.*;
27 import javax.naming.*;
28
29 public class ClientNamingContext implements Context, java.io.Serializable
30 {
31     public static ClientNamingContext getInstance(Hashtable env)
32     {
33         ClientNamingContext nc = (ClientNamingContext)_contextMap.get(env);
34         if (nc == null)
35         {
36             synchronized (_contextMap)
37             {
38                 nc = (ClientNamingContext)_contextMap.get(env);
39                 if (nc == null)
40                 {
41                     nc = new ClientNamingContext();
42                     nc.init(env);
43                     _contextMap.put(env, nc);
44                 }
45             }
46         }
47         return nc;
48     }
49
50     // -----------------------------------------------------------------------
51
// properties
52
// -----------------------------------------------------------------------
53

54     public static final StringProperty usernameProperty =
55         new StringProperty(SystemProperties.class, "java.naming.security.principal");
56
57     public static final StringProperty passwordProperty =
58         new StringProperty(SystemProperties.class, "java.naming.security.credentials");
59
60     public static final IntProperty idleConnectionTimeoutProperty =
61         new IntProperty(SystemProperties.class, "gcc.rmi.idleConnectionTimeout")
62         .defaultValue(60); // seconds
63

64     // -----------------------------------------------------------------------
65
// private data
66
// -----------------------------------------------------------------------
67

68     private static int _idleConnectionTimeout =
69         idleConnectionTimeoutProperty.getInt();
70
71     private static int _namingContextCacheTimeout =
72         SystemProperties.rmiNamingContextCacheTimeoutProperty.getInt();
73
74     private static HashMap _contextMap = new HashMap();
75
76     private static HashMap _hostListCache = new HashMap();
77
78     private static HashMap _multiHostMap = new HashMap();
79
80     private static Random _random = new Random();
81
82     private HashMap _cache = new HashMap();
83
84     private Hashtable _env;
85
86     private ConnectionPool _connectionPool;
87
88     private PropertyMap _connectionProperties;
89
90     private gcc.org.omg.CosNaming.NamingContext _serverNamingContext;
91     static private HashMap _nameMap = new HashMap();
92
93
94     private String _prefix;
95
96     private String _username;
97
98     private String _password;
99
100     // -----------------------------------------------------------------------
101
// public methods
102
// -----------------------------------------------------------------------
103

104     public ConnectionPool getConnectionPool()
105     {
106         return _connectionPool;
107     }
108
109     public PropertyMap getConnectionProperties()
110     {
111         return _connectionProperties;
112     }
113
114     public int getIdleConnectionTimeout()
115     {
116         return _idleConnectionTimeout;
117     }
118
119     public String getUsername()
120     {
121         return _username;
122     }
123
124     public String getPassword()
125     {
126         return _password;
127     }
128
129     // -----------------------------------------------------------------------
130
// public methods from interface javax.naming.Context
131
// -----------------------------------------------------------------------
132

133     public Object lookup(Name name) throws NamingException
134     {
135         return lookup(name.toString());
136     }
137
138     public Object lookup(String name) throws NamingException
139     {
140         if(name.startsWith("java:comp/env/"))
141         {
142             name = name.substring(14);
143         }
144
145         String newName = (String) _nameMap.get(name);
146         if(newName != null)
147         {
148             name = newName;
149         }
150
151         NameBinding nb = (NameBinding)_cache.get(name);
152         if (nb == null)
153         {
154             synchronized (_cache)
155             {
156                 nb = (NameBinding)_cache.get(name);
157                 if (nb != null && nb.hasExpired())
158                 {
159                     _cache.remove(name);
160                     nb = null;
161                 }
162                 if (nb == null)
163                 {
164                     nb = resolve(name);
165                     _cache.put(name, nb);
166                 }
167             }
168         }
169         return nb.object;
170     }
171
172     public HostList lookupHost(String name)
173     {
174         NameBinding nb = (NameBinding)_hostListCache.get(name);
175         if (nb == null)
176         {
177             synchronized (_hostListCache)
178             {
179                 nb = (NameBinding)_hostListCache.get(name);
180                 if (nb != null && nb.hasExpired())
181                 {
182                     _hostListCache.remove(name);
183                     nb = null;
184                 }
185                 if (nb == null)
186                 {
187                     nb = resolve_host(name);
188                     _hostListCache.put(name, nb);
189                 }
190             }
191         }
192         return (HostList)nb.object;
193     }
194
195     public static void bind(String bindName, String name) throws NamingException
196     {
197         _nameMap.put(bindName,name);
198     }
199
200     public void bind(Name name, Object obj) throws NamingException
201     {
202         throw new OperationNotSupportedException();
203     }
204
205     public void bind(String name, Object obj) throws NamingException
206     {
207         throw new OperationNotSupportedException();
208     }
209
210     public void rebind(Name name, Object obj) throws NamingException
211     {
212         throw new OperationNotSupportedException();
213     }
214
215     public void rebind(String name, Object obj) throws NamingException
216     {
217         throw new OperationNotSupportedException();
218     }
219
220     public void unbind(Name name) throws NamingException
221     {
222         throw new OperationNotSupportedException();
223     }
224
225     public void unbind(String name) throws NamingException
226     {
227         throw new OperationNotSupportedException();
228     }
229
230     public void rename(Name oldName, Name newName) throws NamingException
231     {
232         throw new OperationNotSupportedException();
233     }
234
235     public void rename(String oldName, String newName) throws NamingException
236     {
237         throw new OperationNotSupportedException();
238     }
239
240     public NamingEnumeration list(Name name) throws NamingException
241     {
242         // TODO: support this
243
throw new OperationNotSupportedException();
244     }
245
246     public NamingEnumeration list(String name) throws NamingException
247     {
248         // TODO: support this
249
throw new OperationNotSupportedException();
250     }
251
252     public NamingEnumeration listBindings(Name name) throws NamingException
253     {
254         // TODO: support this
255
throw new OperationNotSupportedException();
256     }
257
258     public NamingEnumeration listBindings(String name) throws NamingException
259     {
260         // TODO: support this
261
throw new OperationNotSupportedException();
262     }
263
264     public void destroySubcontext(Name name) throws NamingException
265     {
266         throw new OperationNotSupportedException();
267     }
268
269     public void destroySubcontext(String name) throws NamingException
270     {
271         throw new OperationNotSupportedException();
272     }
273
274     public Context createSubcontext(Name name) throws NamingException
275     {
276         throw new OperationNotSupportedException();
277     }
278
279     public Context createSubcontext(String name) throws NamingException
280     {
281         throw new OperationNotSupportedException();
282     }
283
284     public Object lookupLink(Name name) throws NamingException
285     {
286         // TODO: support this
287
throw new OperationNotSupportedException();
288     }
289
290     public Object lookupLink(String name) throws NamingException
291     {
292         // TODO: support this
293
throw new OperationNotSupportedException();
294     }
295
296     public NameParser getNameParser(Name name) throws NamingException
297     {
298         throw new OperationNotSupportedException();
299     }
300
301     public NameParser getNameParser(String name) throws NamingException
302     {
303         throw new OperationNotSupportedException();
304     }
305
306     public Name composeName(Name name, Name prefix) throws NamingException
307     {
308         // TODO: support this
309
throw new OperationNotSupportedException();
310     }
311
312     public String composeName(String name, String prefix) throws NamingException
313     {
314         // TODO: support this
315
throw new OperationNotSupportedException();
316     }
317
318     public Object addToEnvironment(String propName, Object propVal) throws NamingException
319     {
320         throw new OperationNotSupportedException();
321     }
322
323     public Object removeFromEnvironment(String propName) throws NamingException
324     {
325         throw new OperationNotSupportedException();
326     }
327
328     public Hashtable getEnvironment() throws NamingException
329     {
330         throw new OperationNotSupportedException();
331     }
332
333     public String getNameInNamespace() throws NamingException
334     {
335         throw new OperationNotSupportedException();
336     }
337
338     public void close() throws NamingException
339     {
340         throw new OperationNotSupportedException();
341     }
342
343     // -----------------------------------------------------------------------
344
// protected methods
345
// -----------------------------------------------------------------------
346

347     protected void init(Hashtable env)
348     {
349         _env = env;
350         Object urlObject = env.get(Context.PROVIDER_URL);
351         if (urlObject == null)
352         {
353             System.out.println( "ClientNamingContext.init(): TODO: urlObject was null, create one based on the current hostname." );
354             urlObject = SystemProperties.getInstance().getProperty("java.naming.provider.url",
355                 "iiop://" + "delafran-t30" + ":2000");
356         }
357         String url = urlObject.toString();
358         UrlInfo urlInfo = UrlInfo.getInstance(url);
359         _serverNamingContext = (gcc.org.omg.CosNaming.NamingContext)
360             StubFactory.getInstance().getStub(gcc.org.omg.CosNaming.NamingContext.class);
361         ObjectRef ncRef = (ObjectRef)_serverNamingContext;
362         ncRef.$setNamingContext(this);
363         ncRef.$setProtocol(urlInfo.getProtocol());
364         ncRef.$setHost("ns~" + urlInfo.getHost());
365         ncRef.$setPort(urlInfo.getPort());
366         ncRef.$setObjectKey(urlInfo.getObjectKey());
367         _connectionProperties = urlInfo.getProperties();
368         _connectionPool = ConnectionPool.getInstance(this);
369         Object u = env.get(Context.SECURITY_PRINCIPAL);
370         Object p = env.get(Context.SECURITY_CREDENTIALS);
371         if (u == null)
372         {
373             u = usernameProperty.getString();
374         }
375         if (p == null)
376         {
377             p = passwordProperty.getString();
378         }
379         _username = u != null ? u.toString() : null;
380         _password = p != null ? p.toString() : null;
381
382         /*
383         if (_serverNamingContext._is_a("IDL:gcc/rmi/iiop/NameService:1.0"))
384         {
385             _serverNamingContext = (gcc.rmi.iiop.NameService)
386                 StubFactory.getInstance().getStub(gcc.rmi.iiop.NameService.class);
387             ncRef = (ObjectRef)_serverNamingContext;
388             ncRef.$setNamingContext(this);
389             ncRef.$setProtocol(urlInfo.getProtocol());
390             ncRef.$setHost("ns~" + urlInfo.getHost());
391             ncRef.$setPort(urlInfo.getPort());
392             ncRef.$setObjectKey(urlInfo.getObjectKey());
393         }
394         */

395     }
396
397     protected NameBinding resolve(String name) throws NamingException
398     {
399         Object value = gcc.naming.NameService.getInitialContext().lookupReturnNullIfNotFound(name);
400         if (value != null)
401         {
402             NameBinding nb = new NameBinding();
403             nb.object = value;
404             nb.cacheTimeout = System.currentTimeMillis() + _namingContextCacheTimeout;
405             return nb;
406         }
407         try
408         {
409             gcc.org.omg.CosNaming.NameComponent[] resolveName =
410                 { new gcc.org.omg.CosNaming.NameComponent(name, "") };
411             org.omg.CORBA.Object object = _serverNamingContext.resolve(resolveName);
412             NameBinding nb = new NameBinding();
413             nb.object = object;
414             nb.cacheTimeout = System.currentTimeMillis() + _namingContextCacheTimeout;
415             return nb;
416         }
417         catch (gcc.org.omg.CosNaming.NamingContextPackage.NotFound notFound)
418         {
419             throw new NameNotFoundException(name);
420         }
421         catch (Exception ex)
422         {
423             throw new javax.naming.NamingException(ExceptionUtil.getStackTrace(ex));
424         }
425     }
426
427     protected NameBinding resolve_host(String host)
428     {
429         HostList list = null;
430         if (_serverNamingContext instanceof gcc.rmi.iiop.NameService)
431         {
432             gcc.rmi.iiop.NameService nameService = (gcc.rmi.iiop.NameService)_serverNamingContext;
433             list = new HostList(nameService.resolve_host(host));
434         }
435         NameBinding nb = new NameBinding();
436         nb.object = list;
437         //nb.cacheTimeout = System.currentTimeMillis()
438
// + (list != null ? list.getCacheTimeout() : _namingContextCacheTimeout);
439
return nb;
440     }
441 }
442
Popular Tags