KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > ivm > naming > NameNode


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 "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES 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  * EXOFFICE TECHNOLOGIES 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 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: NameNode.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.core.ivm.naming;
46
47
48 /**
49  * This class is a combination linked list and binary tree for resolving name-object lookups
50  * and binding objects to names. The linked list (subTree) hold subcontexts while the binary
51  * tree (lessTree/grtrTree) allows for quick navigation of the a hierarchical path used in the \
52  * JNDI ENC. Resolving paths requires the use of the ParsedName class.
53  *
54  * Navigation through the binary tree is determined by the hashCode of the ParsedName components
55  * as compared to the atomicName of the NameNode. When the hashcodes and String values are equal
56  * navigation proceeds down the subtree or the name is immediately resolved if its the last component
57  * in the ParsedName. When a hashcode is greater or when the hashcodes are equal but the objects are
58  * not equal navigation proceeds down the grtrTree. When the hashcode of the component is less then
59  * the atomicName navigation proceeds down the lessTree.
60  */

61 public class NameNode implements java.io.Serializable JavaDoc {
62     public String JavaDoc atomicName;
63     public int atomicHash;
64     public NameNode lessTree;
65     public NameNode grtrTree;
66     public NameNode subTree;
67     public NameNode parent;
68     public Object JavaDoc myObject;
69     public transient IvmContext myContext;
70     
71     public NameNode(NameNode parent, ParsedName name, Object JavaDoc obj){
72         atomicName = name.getComponent();
73         atomicHash = name.getComponentHashCode();
74         this.parent = parent;
75         if(name.next())
76             subTree = new NameNode(this, name, obj);
77         else
78             myObject = obj;
79     }
80     public Object JavaDoc getBinding(){
81         if(myObject != null)
82             return myObject;// if NameNode has an object it must be a binding
83
else{
84             if(myContext == null)
85                 myContext = new IvmContext(this);
86             return myContext;
87         }
88     }
89     public Object JavaDoc resolve(ParsedName name)throws javax.naming.NameNotFoundException JavaDoc{
90         int compareResult = name.compareTo(atomicHash);
91         
92         if(compareResult == ParsedName.IS_EQUAL && name.getComponent().equals(atomicName)){// hashcodes and String valuse are equal
93
if(name.next()){
94                 if(subTree == null) throw new javax.naming.NameNotFoundException JavaDoc("Can not resolve "+name);
95                 return subTree.resolve(name);
96             }else
97                 return getBinding();
98         }else if(compareResult == ParsedName.IS_LESS){// parsed hash is less than
99
if(lessTree == null) throw new javax.naming.NameNotFoundException JavaDoc("Can not resolve "+name);
100             return lessTree.resolve(name);
101         
102         }else{//ParsedName.IS_GREATER
103
//...or ParsedName.IS_EQUAL but components are not the same string (hash code collision)
104
if(grtrTree == null) throw new javax.naming.NameNotFoundException JavaDoc("Can not resolve "+name);
105             return grtrTree.resolve(name);
106         }
107     }
108     
109     public void bind(ParsedName name, Object JavaDoc obj) throws javax.naming.NameAlreadyBoundException JavaDoc {
110         int compareResult = name.compareTo(atomicHash);
111         if(compareResult == ParsedName.IS_EQUAL && name.getComponent().equals(atomicName)){
112             if(name.next()){
113         if( myObject != null) {
114             throw new javax.naming.NameAlreadyBoundException JavaDoc();
115         }
116                 if(subTree==null)
117                     subTree = new NameNode(this, name,obj);
118                 else
119                     subTree.bind(name,obj);
120             }
121         else {
122         if( subTree != null) {
123             throw new javax.naming.NameAlreadyBoundException JavaDoc();
124         }
125                 myObject = obj;// bind the object to this node
126
}
127         }else if(compareResult == ParsedName.IS_LESS){
128             if(lessTree == null)
129                 lessTree = new NameNode(this.parent, name, obj);
130             else
131                 lessTree.bind(name,obj);
132         }else{//ParsedName.IS_GREATER ...
133
//...or ParsedName.IS_EQUAL but components are not the same string (hash code collision)
134
if(grtrTree == null)
135                 grtrTree = new NameNode(this.parent, name, obj);
136             else
137                 grtrTree.bind(name,obj);
138         }
139     }
140
141     public IvmContext createSubcontext( ParsedName name) throws javax.naming.NameAlreadyBoundException JavaDoc {
142     try {
143         bind( name, null);
144         name.reset();
145         return (IvmContext)resolve( name);
146     }
147     catch( javax.naming.NameNotFoundException JavaDoc exception) {
148         exception.printStackTrace();
149         throw new RuntimeException JavaDoc();
150     }
151     }
152 }
153
Popular Tags