KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jnp > server > NamingServer


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jnp.server;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Vector JavaDoc;
29 import javax.naming.Binding JavaDoc;
30 import javax.naming.CannotProceedException JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InvalidNameException JavaDoc;
33 import javax.naming.Name JavaDoc;
34 import javax.naming.NameAlreadyBoundException JavaDoc;
35 import javax.naming.NameClassPair JavaDoc;
36 import javax.naming.NameNotFoundException JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.naming.NotContextException JavaDoc;
39 import javax.naming.Reference JavaDoc;
40 import javax.naming.spi.ResolveResult JavaDoc;
41
42 import org.jnp.interfaces.Naming;
43 import org.jnp.interfaces.NamingContext;
44 import org.jnp.interfaces.NamingParser;
45 import org.jboss.logging.Logger;
46
47 /**
48  * The JNDI naming server implementation class.
49  *
50  * @author Rickard Oberg
51  * @author patriot1burke
52  * @author Scott.Stark@jboss.org
53  * @version $Revision: 38033 $
54  */

55 public class NamingServer
56    implements Naming, java.io.Serializable JavaDoc
57 {
58    private static Logger log = Logger.getLogger(NamingServer.class);
59
60    /** @since 1.12 at least */
61    private static final long serialVersionUID = 4183855539507934373L;
62    // Constants -----------------------------------------------------
63

64    // Attributes ----------------------------------------------------
65

66    protected Hashtable JavaDoc table = new Hashtable JavaDoc();
67    protected Name JavaDoc prefix;
68    protected NamingParser parser = new NamingParser();
69    protected NamingServer parent;
70
71    // Static --------------------------------------------------------
72

73    // Constructors --------------------------------------------------
74
public NamingServer()
75       throws NamingException JavaDoc
76    {
77       this(null, null);
78    }
79    
80    public NamingServer(Name JavaDoc prefix, NamingServer parent)
81       throws NamingException JavaDoc
82    {
83       if (prefix == null) prefix = parser.parse("");
84       this.prefix = prefix;
85       
86       this.parent = parent;
87    }
88    
89    // Public --------------------------------------------------------
90

91    // Naming implementation -----------------------------------------
92
public synchronized void bind(Name JavaDoc name, Object JavaDoc obj, String JavaDoc className)
93       throws NamingException JavaDoc
94    {
95       if (name.isEmpty())
96       {
97          // Empty names are not allowed
98
throw new InvalidNameException JavaDoc();
99       } else if (name.size() > 1)
100       {
101          // Recurse to find correct context
102
// System.out.println("bind#"+name+"#");
103

104          Object JavaDoc ctx = getObject(name);
105          if (ctx != null)
106          {
107             if (ctx instanceof NamingServer)
108             {
109                ((NamingServer)ctx).bind(name.getSuffix(1),obj, className);
110             } else if (ctx instanceof Reference JavaDoc)
111             {
112                // Federation
113
if (((Reference JavaDoc)ctx).get("nns") != null)
114                {
115                   CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
116                   cpe.setResolvedObj(ctx);
117                   cpe.setRemainingName(name.getSuffix(1));
118                   throw cpe;
119                } else
120                {
121                   throw new NotContextException JavaDoc();
122                }
123             } else
124             {
125                throw new NotContextException JavaDoc();
126             }
127          } else
128          {
129             throw new NameNotFoundException JavaDoc();
130          }
131       } else
132       {
133          // Bind object
134
if (name.get(0).equals(""))
135          {
136             throw new InvalidNameException JavaDoc();
137          } else
138          {
139 // System.out.println("bind "+name+"="+obj);
140
try
141             {
142                getBinding(name);
143                // Already bound
144
throw new NameAlreadyBoundException JavaDoc();
145             } catch (NameNotFoundException JavaDoc e)
146             {
147                setBinding(name,obj,className);
148             }
149          }
150       }
151    }
152
153    public synchronized void rebind(Name JavaDoc name, Object JavaDoc obj, String JavaDoc className)
154       throws NamingException JavaDoc
155    {
156       if (name.isEmpty())
157       {
158          // Empty names are not allowed
159
throw new InvalidNameException JavaDoc();
160       } else if (name.size() > 1)
161       {
162          // Recurse to find correct context
163
// System.out.println("rebind#"+name+"#");
164

165          Object JavaDoc ctx = getObject(name);
166          if (ctx instanceof NamingServer)
167          {
168             ((NamingServer)ctx).rebind(name.getSuffix(1),obj, className);
169          } else if (ctx instanceof Reference JavaDoc)
170          {
171             // Federation
172
if (((Reference JavaDoc)ctx).get("nns") != null)
173             {
174                CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
175                cpe.setResolvedObj(ctx);
176                cpe.setRemainingName(name.getSuffix(1));
177                throw cpe;
178             } else
179             {
180                throw new NotContextException JavaDoc();
181             }
182          } else
183          {
184             throw new NotContextException JavaDoc();
185          }
186       } else
187       {
188          // Bind object
189
if (name.get(0).equals(""))
190          {
191             throw new InvalidNameException JavaDoc();
192          } else
193          {
194 // System.out.println("rebind "+name+"="+obj+"("+this+")");
195
setBinding(name,obj,className);
196          }
197       }
198    }
199    
200    public synchronized void unbind(Name JavaDoc name)
201       throws NamingException JavaDoc
202    {
203       if (name.isEmpty())
204       {
205          // Empty names are not allowed
206
throw new InvalidNameException JavaDoc();
207       } else if (name.size() > 1)
208       {
209          // Recurse to find correct context
210
// System.out.println("unbind#"+name+"#");
211

212          Object JavaDoc ctx = getObject(name);
213          if (ctx instanceof NamingServer)
214          {
215             ((NamingServer)ctx).unbind(name.getSuffix(1));
216          } else if (ctx instanceof Reference JavaDoc)
217          {
218             // Federation
219
if (((Reference JavaDoc)ctx).get("nns") != null)
220             {
221                CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
222                cpe.setResolvedObj(ctx);
223                cpe.setRemainingName(name.getSuffix(1));
224                throw cpe;
225             } else
226             {
227                throw new NotContextException JavaDoc();
228             }
229          } else
230          {
231             throw new NotContextException JavaDoc();
232          }
233       } else
234       {
235          // Unbind object
236
if (name.get(0).equals(""))
237          {
238             throw new InvalidNameException JavaDoc();
239          } else
240          {
241 // System.out.println("unbind "+name+"="+getBinding(name));
242
if (getBinding(name) != null)
243             {
244                removeBinding(name);
245             } else
246             {
247                throw new NameNotFoundException JavaDoc();
248             }
249          }
250       }
251    }
252
253 // public synchronized Object lookup(Name name)
254
public Object JavaDoc lookup(Name JavaDoc name)
255       throws NamingException JavaDoc
256    {
257         Object JavaDoc result;
258       if (name.isEmpty())
259       {
260          // Return this
261
result = new NamingContext(null, (Name JavaDoc)(prefix.clone()), getRoot());
262       } else if (name.size() > 1)
263       {
264          // Recurse to find correct context
265
// System.out.println("lookup#"+name+"#");
266

267          Object JavaDoc ctx = getObject(name);
268          if (ctx instanceof NamingServer)
269          {
270             result = ((NamingServer)ctx).lookup(name.getSuffix(1));
271          } else if (ctx instanceof Reference JavaDoc)
272          {
273             // Federation
274
if (((Reference JavaDoc)ctx).get("nns") != null)
275             {
276                CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
277                cpe.setResolvedObj(ctx);
278                cpe.setRemainingName(name.getSuffix(1));
279                throw cpe;
280             }
281             
282             result = new ResolveResult JavaDoc(ctx, name.getSuffix(1));
283          } else
284          {
285             throw new NotContextException JavaDoc();
286          }
287       } else
288       {
289          // Get object to return
290
if (name.get(0).equals(""))
291          {
292             result = new NamingContext(null, prefix, getRoot());
293          } else
294          {
295 // System.out.println("lookup "+name);
296
Object JavaDoc res = getObject(name);
297             
298             if (res instanceof NamingServer)
299             {
300                Name JavaDoc fullName = (Name JavaDoc)(prefix.clone());
301                fullName.addAll(name);
302                result = new NamingContext(null, fullName, getRoot());
303             }
304             else
305                result = res;
306          }
307       }
308         
309         return result;
310    }
311    
312    public Collection JavaDoc list(Name JavaDoc name)
313       throws NamingException JavaDoc
314    {
315 // System.out.println("list of #"+name+"#"+name.size());
316
if (name.isEmpty())
317       {
318 // System.out.println("list "+name);
319

320          Vector JavaDoc list = new Vector JavaDoc();
321          Enumeration JavaDoc keys = table.keys();
322          while (keys.hasMoreElements())
323          {
324             String JavaDoc key = (String JavaDoc)keys.nextElement();
325             Binding JavaDoc b = getBinding(key);
326             
327             list.addElement(new NameClassPair JavaDoc(b.getName(),b.getClassName(),true));
328          }
329          return list;
330       } else
331       {
332 // System.out.println("list#"+name+"#");
333

334          Object JavaDoc ctx = getObject(name);
335          if (ctx instanceof NamingServer)
336          {
337             return ((NamingServer)ctx).list(name.getSuffix(1));
338          } else if (ctx instanceof Reference JavaDoc)
339          {
340             // Federation
341
if (((Reference JavaDoc)ctx).get("nns") != null)
342             {
343                CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
344                cpe.setResolvedObj(ctx);
345                cpe.setRemainingName(name.getSuffix(1));
346                throw cpe;
347             } else
348             {
349                throw new NotContextException JavaDoc();
350             }
351          } else
352          {
353             throw new NotContextException JavaDoc();
354          }
355       }
356    }
357     
358    public Collection JavaDoc listBindings(Name JavaDoc name)
359       throws NamingException JavaDoc
360    {
361       if (name.isEmpty())
362       {
363          Collection JavaDoc bindings = table.values();
364          Collection JavaDoc newBindings = new Vector JavaDoc(bindings.size());
365          Iterator JavaDoc iter = bindings.iterator();
366          while (iter.hasNext())
367          {
368             Binding JavaDoc b = (Binding JavaDoc)iter.next();
369             if (b.getObject() instanceof NamingServer)
370             {
371                Name JavaDoc n = (Name JavaDoc)prefix.clone();
372                n.add(b.getName());
373                newBindings.add(new Binding JavaDoc(b.getName(),
374                                            b.getClassName(),
375                                            new NamingContext(null, n, getRoot())));
376             } else
377             {
378                newBindings.add(b);
379             }
380          }
381          
382          return newBindings;
383       } else
384       {
385          Object JavaDoc ctx = getObject(name);
386          if (ctx instanceof NamingServer)
387          {
388             return ((NamingServer)ctx).listBindings(name.getSuffix(1));
389          } else if (ctx instanceof Reference JavaDoc)
390          {
391             // Federation
392
if (((Reference JavaDoc)ctx).get("nns") != null)
393             {
394                CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
395                cpe.setResolvedObj(ctx);
396                cpe.setRemainingName(name.getSuffix(1));
397                throw cpe;
398             } else
399             {
400                throw new NotContextException JavaDoc();
401             }
402          } else
403          {
404             throw new NotContextException JavaDoc();
405          }
406       }
407    }
408    
409    public Context JavaDoc createSubcontext(Name JavaDoc name)
410       throws NamingException JavaDoc
411    {
412        if( name.size() == 0 )
413           throw new InvalidNameException JavaDoc("Cannot pass an empty name to createSubcontext");
414
415       NamingException JavaDoc ex = null;
416       Context JavaDoc subCtx = null;
417       if (name.size() > 1)
418       {
419          Object JavaDoc ctx = getObject(name);
420          if (ctx != null)
421          {
422             Name JavaDoc subCtxName = name.getSuffix(1);
423             if (ctx instanceof NamingServer)
424             {
425                subCtx = ((NamingServer)ctx).createSubcontext(subCtxName);
426             }
427             else if (ctx instanceof Reference JavaDoc)
428             {
429                // Federation
430
if (((Reference JavaDoc)ctx).get("nns") != null)
431                {
432                   CannotProceedException JavaDoc cpe = new CannotProceedException JavaDoc();
433                   cpe.setResolvedObj(ctx);
434                   cpe.setRemainingName(subCtxName);
435                   throw cpe;
436                }
437                else
438                {
439                   ex = new NotContextException JavaDoc();
440                   ex.setResolvedName(name.getPrefix(0));
441                   ex.setRemainingName(subCtxName);
442                   throw ex;
443                }
444             }
445             else
446             {
447                ex = new NotContextException JavaDoc();
448                ex.setResolvedName(name.getPrefix(0));
449                ex.setRemainingName(subCtxName);
450                throw ex;
451             }
452          }
453          else
454          {
455             ex = new NameNotFoundException JavaDoc();
456             ex.setRemainingName(name);
457             throw ex;
458          }
459       }
460       else
461       {
462          Object JavaDoc binding = table.get(name.get(0));
463          if( binding != null )
464          {
465             ex = new NameAlreadyBoundException JavaDoc();
466             ex.setResolvedName(prefix);
467             ex.setRemainingName(name);
468             throw ex;
469          }
470          else
471          {
472             Name JavaDoc fullName = (Name JavaDoc) prefix.clone();
473             fullName.addAll(name);
474             NamingServer subContext = new NamingServer(fullName, this);
475             setBinding(name, subContext, NamingContext.class.getName());
476             subCtx = new NamingContext(null, fullName, getRoot());
477          }
478       }
479       return subCtx;
480    }
481       
482    public Naming getRoot()
483    {
484       if (parent == null)
485          return this;
486       else
487          return parent.getRoot();
488    }
489
490    // Y overrides ---------------------------------------------------
491

492    // Package protected ---------------------------------------------
493

494    // Protected -----------------------------------------------------
495

496    // Private -------------------------------------------------------
497
private void setBinding(Name JavaDoc name, Object JavaDoc obj, String JavaDoc className)
498    {
499       String JavaDoc n = name.toString();
500       table.put(n, new Binding JavaDoc(n, className, obj, true));
501    }
502
503    private Binding JavaDoc getBinding(String JavaDoc key)
504       throws NameNotFoundException JavaDoc
505    {
506       Binding JavaDoc b = (Binding JavaDoc)table.get(key);
507       if (b == null)
508       {
509          if( log.isTraceEnabled() )
510          {
511             StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("No binding for: "+key);
512             tmp.append(" in context ");
513             tmp.append(this.prefix);
514             tmp.append(", bindings:\n");
515             Iterator JavaDoc bindings = table.values().iterator();
516             while( bindings.hasNext() )
517             {
518                Binding JavaDoc value = (Binding JavaDoc) bindings.next();
519                tmp.append(value.getName());
520                tmp.append('=');
521                if( value.getObject() != null )
522                   tmp.append(value.getObject().toString());
523                else
524                   tmp.append("null");
525                tmp.append('\n');
526             }
527             log.trace(tmp.toString());
528          }
529          throw new NameNotFoundException JavaDoc(key + " not bound");
530       }
531       return b;
532    }
533
534    private Binding JavaDoc getBinding(Name JavaDoc key)
535       throws NameNotFoundException JavaDoc
536    {
537       return getBinding(key.get(0));
538    }
539    
540    private Object JavaDoc getObject(Name JavaDoc key)
541       throws NameNotFoundException JavaDoc
542    {
543       return getBinding(key).getObject();
544    }
545
546    private void removeBinding(Name JavaDoc name)
547    {
548       table.remove(name.get(0));
549    }
550    
551 }
552
Popular Tags