KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > JNDIContext


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: JNDIContext.java 2475 2006-02-21 00:18:55Z dblevins $
44  */

45 package org.openejb.client;
46
47 import java.io.Serializable JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.net.URI JavaDoc;
50 import java.net.URISyntaxException JavaDoc;
51
52 import javax.naming.ConfigurationException JavaDoc;
53 import javax.naming.Context JavaDoc;
54 import javax.naming.InvalidNameException JavaDoc;
55 import javax.naming.Name JavaDoc;
56 import javax.naming.NameNotFoundException JavaDoc;
57 import javax.naming.NameParser JavaDoc;
58 import javax.naming.NamingEnumeration JavaDoc;
59 import javax.naming.NamingException JavaDoc;
60 import javax.naming.spi.InitialContextFactory JavaDoc;
61 /**
62  * JNDI client
63  *
64  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
65  * @since 11/25/2001
66  */

67 public class JNDIContext implements Serializable JavaDoc, InitialContextFactory JavaDoc, Context JavaDoc, RequestMethods, ResponseCodes {
68     
69     private transient String JavaDoc tail = "/";
70     private transient ServerMetaData server;
71     private transient ClientMetaData client;
72     private transient Hashtable JavaDoc env;
73
74     /**
75      * Constructs this JNDI context for the client.
76      *
77      * Opens a socket connection with the NamingServer.
78      * Initializes object output/input streams for writing/reading
79      * objects with the naming server.
80      * Authenticates the user's information with the NamingServer
81      *
82      * @param environment
83      * @exception NamingException
84      */

85     JNDIContext(Hashtable JavaDoc environment) throws NamingException JavaDoc{
86         init( environment );
87     }
88
89     public JNDIContext(){
90     }
91     
92     /*
93      * A neater version of clone
94      */

95     public JNDIContext(JNDIContext that){
96         this.tail = that.tail;
97         this.server = that.server;
98         this.client = that.client;
99         this.env = (Hashtable JavaDoc)that.env.clone();
100     }
101
102     /**
103      * Initializes this JNDI context for the client.
104      *
105      * Opens a socket connection with the NamingServer.
106      * Initializes object output/input streams for writing/reading
107      * objects with the naming server.
108      * Authenticates the user's information with the NamingServer
109      *
110      * @param environment
111      * @exception NamingException
112      */

113     public void init(Hashtable JavaDoc environment) throws NamingException JavaDoc{
114     }
115
116
117     private JNDIResponse request(JNDIRequest req) throws Exception JavaDoc {
118         return (JNDIResponse) Client.request(req, new JNDIResponse(), server);
119     }
120
121     public static void print(String JavaDoc s){
122         //System.out.println();
123
}
124     public static void println(String JavaDoc s){
125         //System.out.print(s+'\n');
126
}
127
128     //TODO:0:Write authentication module
129
protected AuthenticationResponse requestAuthorization(AuthenticationRequest req) throws java.rmi.RemoteException JavaDoc {
130         return (AuthenticationResponse) Client.request(req, new AuthenticationResponse(), server);
131     }
132
133     //-------------------------------------------------------------//
134
// InitialContextFactory implementation //
135
//-------------------------------------------------------------//
136

137     /**
138       * Creates an Initial Context for beginning name resolution.
139       * Special requirements of this context are supplied
140       * using <code>environment</code>.
141       *<p>
142       * The environment parameter is owned by the caller.
143       * The implementation will not modify the object or keep a reference
144       * to it, although it may keep a reference to a clone or copy.
145       *
146       * @param environment The possibly null environment
147       * specifying information to be used in the creation
148       * of the initial context.
149       * @return A non-null initial context object that implements the Context
150       * interface.
151       * @exception NamingException If cannot create an initial context.
152       */

153     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment) throws NamingException JavaDoc{
154         if ( environment == null )
155             throw new NamingException JavaDoc("Invalid Argument, hashtable cannot be null.");
156         else
157             env = (Hashtable JavaDoc)environment.clone();
158
159         String JavaDoc userID = (String JavaDoc) env.get(Context.SECURITY_PRINCIPAL);
160         String JavaDoc psswrd = (String JavaDoc) env.get(Context.SECURITY_CREDENTIALS);
161         Object JavaDoc serverURI = env.get(Context.PROVIDER_URL);
162
163         if (serverURI == null) serverURI = "foo://localhost:4201";
164         if (userID == null) userID = "anonymous";
165         if (psswrd == null) psswrd = "anon";
166
167         String JavaDoc uriString = (String JavaDoc) serverURI;
168         URI JavaDoc location = null;
169         try {
170             location = new URI JavaDoc(uriString);
171         } catch (Exception JavaDoc e) {
172             if (uriString.indexOf("://") == -1) {
173                 try {
174                     location = new URI JavaDoc("foo://"+uriString);
175                 } catch (URISyntaxException JavaDoc giveUp) {
176                     // Was worth a try, let's give up and throw the original exception.
177
throw (ConfigurationException JavaDoc)new ConfigurationException JavaDoc("Context property value error for "+Context.PROVIDER_URL + " :"+e.getMessage()).initCause(e);
178                 }
179             }
180         }
181         this.server = new ServerMetaData(location);
182         //TODO:1: Either aggressively initiate authentication or wait for the
183
// server to send us an authentication challange.
184
authenticate(userID, psswrd);
185
186         return this;
187     }
188     
189     
190     public void authenticate(String JavaDoc userID, String JavaDoc psswrd) throws javax.naming.AuthenticationException JavaDoc{
191         // TODO:1: Skip this if the identity hasn't been changed and
192
// the user already has been authenticated.
193
AuthenticationRequest req = new AuthenticationRequest(userID, psswrd);
194         AuthenticationResponse res = null;
195
196     try {
197         res = requestAuthorization(req);
198     } catch (java.rmi.RemoteException JavaDoc e) {
199         throw new javax.naming.AuthenticationException JavaDoc(e.getLocalizedMessage());
200     }
201         
202         switch (res.getResponseCode()) {
203             case AUTH_GRANTED:
204                 client = res.getIdentity();
205                 break;
206             case AUTH_REDIRECT:
207                 client = res.getIdentity();
208                 server = res.getServer();
209                 break;
210             case AUTH_DENIED:
211                 throw new javax.naming.AuthenticationException JavaDoc("This principle is not authorized.");
212         }
213     }
214     
215     // Construct a new handler and proxy.
216
public EJBHomeProxy createEJBHomeProxy(EJBMetaDataImpl ejbData){
217         
218         EJBHomeHandler handler = EJBHomeHandler.createEJBHomeHandler(ejbData, server, client);
219         EJBHomeProxy proxy = handler.createEJBHomeProxy();
220         handler.ejb.ejbHomeProxy = proxy;
221         
222         return proxy;
223     
224     }
225     
226     //-------------------------------------------------------------//
227
// Context implementation //
228
//-------------------------------------------------------------//
229

230     //----------------------------------------------------------------------//
231
// Supportted methods //
232
//----------------------------------------------------------------------//
233

234     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
235
236         if ( name == null ) throw new InvalidNameException JavaDoc("The name cannot be null");
237         else if ( name.equals("") ) return new JNDIContext(this);
238         else if ( !name.startsWith("/") ) name = tail+name;
239
240         JNDIRequest req = new JNDIRequest();
241         req.setRequestMethod( JNDIRequest.JNDI_LOOKUP );
242         req.setRequestString( name );
243
244         JNDIResponse res = null;
245         try{
246             res = request(req);
247         } catch (Exception JavaDoc e){
248             // TODO:1: Better exception handling
249
throw new javax.naming.NamingException JavaDoc("Cannot lookup "+name+": Received error: "+e.getMessage());
250         }
251
252         switch ( res.getResponseCode() ) {
253             case JNDI_EJBHOME:
254                 // Construct a new handler and proxy.
255
return createEJBHomeProxy( (EJBMetaDataImpl)res.getResult() );
256             
257             case JNDI_OK:
258                 return res.getResult();
259             
260             case JNDI_CONTEXT:
261                 JNDIContext subCtx = new JNDIContext(this);
262                 if (!name.endsWith("/")) name += '/';
263                 subCtx.tail = name;
264                 return subCtx;
265             
266             case JNDI_NOT_FOUND:
267                 throw new NameNotFoundException JavaDoc(name + " not found");
268             
269             case JNDI_NAMING_EXCEPTION:
270                 throw (NamingException JavaDoc) res.getResult();
271             
272             case JNDI_RUNTIME_EXCEPTION:
273                 throw (RuntimeException JavaDoc) res.getResult();
274             
275             case JNDI_ERROR:
276                 throw (Error JavaDoc) res.getResult();
277             default:
278                 throw new RuntimeException JavaDoc("Invalid response from server :"+res.getResponseCode());
279         }
280     }
281
282     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
283         return lookup(name.toString());
284     }
285
286     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
287         throw new javax.naming.NamingException JavaDoc("TODO: Needs to be implemented");
288     }
289
290     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
291         return list(name.toString());
292     }
293
294     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
295         throw new javax.naming.NamingException JavaDoc("TODO: Needs to be implemented");
296     }
297
298     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
299         return listBindings(name.toString());
300     }
301
302
303     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
304         return lookup(name);
305     }
306
307     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
308         return lookupLink(name.toString());
309     }
310
311     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
312         throw new javax.naming.NamingException JavaDoc("TODO: Needs to be implemented");
313     }
314
315     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
316         return getNameParser(name.toString());
317     }
318
319     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
320         throw new javax.naming.NamingException JavaDoc("TODO: Needs to be implemented");
321     }
322
323     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
324         throw new javax.naming.NamingException JavaDoc("TODO: Needs to be implemented");
325     }
326
327     public Object JavaDoc addToEnvironment(String JavaDoc key, Object JavaDoc value) throws NamingException JavaDoc {
328         return env.put(key, value);
329     }
330
331     public Object JavaDoc removeFromEnvironment(String JavaDoc key) throws NamingException JavaDoc {
332         return env.remove(key);
333     }
334
335     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
336         return (Hashtable JavaDoc)env.clone();
337     }
338
339     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
340         return "";
341     }
342
343     public void close() throws NamingException JavaDoc {
344     }
345
346     //==============================
347
// Unsupported Context methods
348
//
349

350     /**
351      * Throws a javax.naming.OperationNotSupportedException.
352      *
353      * Clients are not allowed to add beans to the name space.
354      *
355      * @param name
356      * @param obj
357      * @exception NamingException
358      */

359     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
360         throw new javax.naming.OperationNotSupportedException JavaDoc();
361     }
362
363     /**
364      * Throws a javax.naming.OperationNotSupportedException.
365      *
366      * Clients are not allowed to add beans to the name space.
367      *
368      * @param name
369      * @param obj
370      * @exception NamingException
371      */

372     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
373         bind(name.toString(), obj);
374     }
375
376     /**
377      * Throws a javax.naming.OperationNotSupportedException.
378      *
379      * Clients are not allowed to add beans to the name space.
380      *
381      * @param name
382      * @param obj
383      * @exception NamingException
384      */

385     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
386         throw new javax.naming.OperationNotSupportedException JavaDoc();
387
388     }
389
390     /**
391      * Throws a javax.naming.OperationNotSupportedException.
392      *
393      * Clients are not allowed to add beans to the name space.
394      *
395      * @param name
396      * @param obj
397      * @exception NamingException
398      */

399     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
400         rebind(name.toString(), obj);
401     }
402
403     /**
404      * Throws a javax.naming.OperationNotSupportedException.
405      *
406      * Clients are not allowed to remove beans from the name space.
407      *
408      * @param name
409      * @exception NamingException
410      */

411     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
412         throw new javax.naming.OperationNotSupportedException JavaDoc();
413
414     }
415
416     /**
417      * Throws a javax.naming.OperationNotSupportedException.
418      *
419      * Clients are not allowed to remove beans from the name space.
420      *
421      * @param name
422      * @exception NamingException
423      */

424     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
425         unbind(name.toString());
426     }
427
428     /**
429      * Throws a javax.naming.OperationNotSupportedException.
430      *
431      * Clients are not allowed to rename beans.
432      *
433      * @param oldname
434      * @param newname
435      * @exception NamingException
436      */

437     public void rename(String JavaDoc oldname, String JavaDoc newname)
438     throws NamingException JavaDoc {
439         throw new javax.naming.OperationNotSupportedException JavaDoc();
440     }
441
442     /**
443      * Throws a javax.naming.OperationNotSupportedException.
444      *
445      * Clients are not allowed to rename beans.
446      *
447      * @param oldname
448      * @param newname
449      * @exception NamingException
450      */

451     public void rename(Name JavaDoc oldname, Name JavaDoc newname)
452     throws NamingException JavaDoc {
453         rename(oldname.toString(), newname.toString());
454     }
455
456     /**
457      * Throws a javax.naming.OperationNotSupportedException.
458      *
459      * Clients are not allowed to remove a sub-context from the name space.
460      *
461      * @param name
462      * @exception NamingException
463      */

464     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
465         throw new javax.naming.OperationNotSupportedException JavaDoc();
466     }
467
468     /**
469      * Throws a javax.naming.OperationNotSupportedException.
470      *
471      * Clients are not allowed to remove a sub-context from the name space.
472      *
473      * @param name
474      * @exception NamingException
475      */

476     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
477         destroySubcontext(name.toString());
478     }
479
480     /**
481      * Throws a javax.naming.OperationNotSupportedException.
482      *
483      * Clients are not allowed to add a sub-context to the name space.
484      *
485      * @param name
486      * @return Context
487      * @exception NamingException
488      */

489     public Context JavaDoc createSubcontext(String JavaDoc name)
490     throws NamingException JavaDoc {
491         throw new javax.naming.OperationNotSupportedException JavaDoc();
492     }
493
494     /**
495      * Throws a javax.naming.OperationNotSupportedException.
496      *
497      * Clients are not allowed to add a sub-context to the name space.
498      *
499      * @param name
500      * @return Context
501      * @exception NamingException
502      */

503     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
504         return createSubcontext(name.toString());
505     }
506
507     //
508
// Unsupported Context methods
509
//==============================
510
}
511
512
513
Popular Tags