KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > services > DefaultNamingContext


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.kilim.model.services;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.objectweb.kilim.KilimException;
25 import org.objectweb.kilim.description.KILIM;
26 import org.objectweb.kilim.model.Component;
27 import org.objectweb.kilim.model.ComponentElement;
28 import org.objectweb.kilim.model.RtExternalValue;
29
30 /**
31  * @author horn
32  */

33 public class DefaultNamingContext implements NamingContext {
34     private String JavaDoc localName;
35     private NamingContext parentContext;
36     private HashMap JavaDoc childContexts;
37     private HashMap JavaDoc boundNames;
38     private ExternalValueReferences externalNames;
39             
40     /**
41      * a public constructor for a naming context.
42      * @param aName : the local name of the naming context.
43      * @param aParent : the parent naming context.
44      * @param extNames : a table of external references (instanciateds objects not associated to a kilim representation).
45      * @throws KilimException : generated if aName is null.
46      */

47     public DefaultNamingContext(String JavaDoc aName, NamingContext aParent, ExternalValueReferences extNames) throws KilimException {
48         if (aName == null) {
49             throw new KilimException("attempt to create a naming context with a null name");
50         }
51         localName = aName;
52         parentContext = aParent;
53         externalNames = extNames;
54     }
55     
56     /** a public constructor for a naming context. It is strictly equivalent to DefaultNamingContext(aName, aParent, null).
57      * @param aName : the local name of the naming context.
58      * @param aParent : the parent naming context.
59      * @throws KilimException :: generated if aName is null.
60      */

61     public DefaultNamingContext(String JavaDoc aName, NamingContext aParent) throws KilimException {
62         this(aName, aParent, null);
63     }
64     
65     /**
66      * @see org.objectweb.kilim.model.services.NamingContext#getParentNamingContext()
67      */

68     public NamingContext getParentNamingContext() {
69         return parentContext;
70     }
71     
72     /**
73      * @see org.objectweb.kilim.model.services.NamingContext#addChildNamingContext(String, NamingContext)
74      */

75     public void addChildNamingContext(String JavaDoc aName, NamingContext aContext) throws KilimException {
76         if (aName == null) {
77             throw new KilimException("attempt to add a child context with a null name in naming context " + getQualifiedName());
78         }
79         if (aContext == null) {
80             throw new KilimException("attempt to add a null child context " + aName + " in naming context " + getQualifiedName());
81         }
82         if (childContexts == null) {
83             childContexts = new HashMap JavaDoc();
84         }
85         if (childContexts.containsKey(aName)) {
86             throw new KilimException("attempt to add a child context with an already existing name " + aName + " in naming context " + getQualifiedName());
87         }
88         childContexts.put(aName, aContext);
89     }
90     
91     /**
92      * @see org.objectweb.kilim.model.services.NamingContext#removeChildNamingContext(String)
93      */

94     public void removeChildNamingContext(String JavaDoc aName) throws KilimException {
95         if (aName == null) {
96             throw new KilimException("attempt to remove a child context through a null name in naming context " + getQualifiedName());
97         }
98         if (childContexts == null) {
99             throw new KilimException("attempt to remove a child context from an empty naming context " + getQualifiedName());
100         }
101         Object JavaDoc result = childContexts.remove(aName);
102         if (result == null) {
103                 throw new KilimException("attempt to remove an unknown child context " + aName + " in naming context " + getQualifiedName());
104         }
105     }
106     
107     /**
108      * @see org.objectweb.kilim.model.services.NamingContext#getChildNamingContexts()
109      */

110     public Iterator JavaDoc getChildNamingContexts() {
111         if (childContexts == null) {
112             return KILIM.EMPTY_ITERATOR;
113         }
114         return childContexts.keySet().iterator();
115     }
116     
117     /**
118      * @see org.objectweb.kilim.model.services.NamingContext#setParentNamingContext(NamingContext)
119      */

120     public void setParentNamingContext(NamingContext aContext) {
121         parentContext = aContext;
122     }
123
124     /**
125      * @see org.objectweb.kilim.model.services.NamingContext#setExternalReferenceMap(HashMap)
126      */

127     public void setExternalReferences(ExternalValueReferences xReferences) {
128         externalNames = xReferences;
129     }
130
131     /**
132      * @see org.objectweb.kilim.model.services.NamingContext#getExternalReferenceMap()
133      */

134     public ExternalValueReferences getExternalReferenceMap() {
135         return externalNames;
136     }
137         
138     /**
139      * @see org.objectweb.kilim.model.services.NamingContext#getQualifiedName()
140      */

141     public String JavaDoc getQualifiedName() {
142         if (parentContext == null) {
143             return localName;
144         }
145         
146         String JavaDoc contName = parentContext.getQualifiedName();
147
148         if ("".equals(contName)) {
149             return localName;
150         }
151         return contName + "/" + localName;
152     }
153     
154     /**
155      * @see org.objectweb.kilim.model.services.NamingContext#getLocalName()
156      */

157     public String JavaDoc getLocalName() {
158         return localName;
159     }
160
161     /**
162      * @see java.lang.Object#toString()
163      */

164     public String JavaDoc toString() {
165             return "[" + getQualifiedName() + "]";
166     }
167     
168     /**
169      * @see org.objectweb.kilim.model.services.NamingContext#addBoundName(String, Object)
170      */

171     public void addBoundName(String JavaDoc aName, ComponentElement aElement) throws KilimException {
172         if (aName == null) {
173             throw new KilimException("null name in addBoundName of naming context " + getQualifiedName());
174         }
175         
176         if (aElement == null) {
177             throw new KilimException("null element in addBoundName of naming context " + getQualifiedName());
178         }
179         
180         if (boundNames == null) {
181             boundNames = new HashMap JavaDoc();
182         }
183         
184         if (boundNames.containsKey(aName)) {
185             throw new KilimException("Name clash in addBoundName of naming context " + getQualifiedName() + " : \"" + aName + "\"");
186         }
187     
188         boundNames.put(aName, aElement);
189     }
190     
191     /**
192      * @see org.objectweb.kilim.model.services.NamingContext#removeBoundName(String)
193      */

194     public void removeBoundName(String JavaDoc aName) throws KilimException {
195         if (aName == null) {
196             throw new KilimException("null name in removeBoundName of naming context " + getQualifiedName());
197         }
198         
199         if (boundNames == null) {
200             throw new KilimException("no bound names in current naming context " + getQualifiedName());
201         }
202         
203         Object JavaDoc previous = boundNames.remove(aName);
204         if (previous == null) {
205             throw new KilimException("name " + aName + " unknown in current naming context " + getQualifiedName());
206         }
207     }
208         
209     /**
210      * @see org.objectweb.kilim.model.services.NamingContext#getBoundNames()
211      */

212     public Iterator JavaDoc getBoundNames() {
213         if (boundNames == null) {
214             return KILIM.EMPTY_ITERATOR;
215         }
216         
217         return boundNames.keySet().iterator();
218     }
219         
220     /**
221      * @see org.objectweb.kilim.model.services.NamingContext#resolveReference(String, Component)
222      */

223     public ComponentElement resolveReference(String JavaDoc aName, Component origin) throws KilimException {
224         // A / prefix in a name is assumed to be a convention for designating external references
225

226         if (aName.startsWith("/")) {
227             if (externalNames != null) {
228                 Object JavaDoc eResult = externalNames.getExternalValueReference(aName);
229                 if (eResult != null) {
230                     RtExternalValue result = new RtExternalValue(aName, eResult);
231                     boundNames.put(aName, result);
232                     return result;
233                 }
234             }
235             throw new KilimException("unknown external reference " + aName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName());
236         }
237         
238         // A ../ prefix is a convention for performing the lookup in the super component (here the super name service).
239
//When the super name service does not exist, the name is assumed to reference an external value.
240

241         if (aName.startsWith("../")) {
242             NamingContext parent = getParentNamingContext();
243             if (parent == null) {
244                 if (externalNames != null) {
245                     Object JavaDoc result = externalNames.getExternalValueReference(aName);
246                     if (result != null) {
247                         //return new RtExternalValue(aName, result, fMapper, fMappingContext);
248
return new RtExternalValue(aName, result);
249                     }
250                 }
251                 throw new KilimException("unresolvable reference (1) " + aName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName());
252             }
253             return parent.resolveReference(aName.substring(3), origin);
254         }
255         
256         // A ./ prefix is a convention for doing the lookup in the current component (here the current factory).It is just stripped.
257
if (aName.startsWith("./")) {
258             aName = aName.substring(2);
259         }
260         
261         // The lookup is performed here. This allows to deal with ad hoc local naming spaces : the corresponding names should
262
// have the form "name1/name2". Any kind of convention may however be used, since no check is performed.
263
// The look up is done through the "boundNames" table.
264
if (boundNames != null) {
265             ComponentElement result = (ComponentElement) boundNames.get(aName);
266             if (result != null) {
267                 return result;
268             }
269         }
270         
271         // the existence of a / in the name means that the look up should be done in a subcomponent. If the subcomponent
272
// does not exist, it is assumed the name references an external value.This allows the use of a predefinite "context"
273
// in which all external references are defined (such as _EXTERNAL/extern_edit = ....., _EXTERNAL/extern_debug = ....
274
int pos = aName.indexOf("/");
275         if (pos != -1) {
276             String JavaDoc lName = aName.substring(0, pos);
277             Object JavaDoc child = null;
278             if (childContexts != null) {
279                 child = childContexts.get(lName);
280             }
281             if (child == null) {
282                 if (externalNames != null) {
283                     Object JavaDoc result = externalNames.getExternalValueReference(aName);
284                     if (result != null) {
285                         //return new RtExternalValue(aName, result, fMapper, fMappingContext);
286
return new RtExternalValue(aName, result);
287                     }
288                 }
289                 throw new KilimException("unresolvable reference (2) " + aName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName());
290             }
291             
292             if (!(child instanceof NamingContext)) {
293                 throw new KilimException("attempt to use as a naming context an illegal element " + lName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName());
294             }
295             return ((NamingContext) child).resolveReference(aName.substring(pos + 1), origin);
296         }
297                 
298         // the previous lookup did not find the name in the "boundNames" table. The name is assumed to be an external reference
299
if (externalNames != null) {
300             Object JavaDoc result = externalNames.getExternalValueReference(aName);
301             if (result != null) {
302                 //return new RtExternalValue(aName, result, fMapper, fMappingContext);
303
return new RtExternalValue(aName, result);
304             }
305         }
306         return null;
307     }
308 }
Popular Tags