Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / CORBA / NamingService / TransientContext.java
blob4b7c1938fde3821258b8e1a61aa2fc2f4a2c46cd
1 /* nContext.java -- implementation of NamingContext
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.CORBA.NamingService;
41 import org.omg.CORBA.Object;
42 import org.omg.CosNaming.Binding;
43 import org.omg.CosNaming.BindingIteratorHolder;
44 import org.omg.CosNaming.BindingListHolder;
45 import org.omg.CosNaming.BindingType;
46 import org.omg.CosNaming.NameComponent;
47 import org.omg.CosNaming.NamingContext;
48 import org.omg.CosNaming.NamingContextOperations;
49 import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
50 import org.omg.CosNaming.NamingContextPackage.CannotProceed;
51 import org.omg.CosNaming.NamingContextPackage.InvalidName;
52 import org.omg.CosNaming.NamingContextPackage.NotEmpty;
53 import org.omg.CosNaming.NamingContextPackage.NotFound;
54 import org.omg.CosNaming.NamingContextPackage.NotFoundReason;
55 import org.omg.CosNaming._NamingContextImplBase;
57 import java.util.Iterator;
58 import java.util.Map;
60 /**
61 * This class implements the transient naming service, defined by
62 * {@link NamingContex}. The 'transient' means that the service does
63 * not store its state into the persistent memory. If the service is
64 * restarted, the named objects must be re-registered again.
66 * TODO Write the persistent naming service.
68 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
70 public class TransientContext
71 extends _NamingContextImplBase
72 implements NamingContext, NamingContextOperations
74 /**
75 * The already named contexts.
77 protected final NamingMap named_contexts = new NamingMap();
79 /**
80 * The already named objects.
82 protected final NamingMap named_objects = new NamingMap();
84 /**
85 * Gives the object a name, valid in this context.
87 * @param a_name the name, being given to the object.
88 * @param an_object the object, being named.
90 * @throws AlreadyBound if the object is already named in this context.
91 * @throws InvalidName if the name has zero length or otherwise invalid.
93 public void bind(NameComponent[] a_name, Object an_object)
94 throws NotFound, CannotProceed, InvalidName, AlreadyBound
96 if (a_name.length == 1)
97 named_objects.bind(a_name [ 0 ], an_object);
98 else
100 NamingContext context =
101 (NamingContext) named_contexts.get(a_name [ 0 ]);
102 context.bind(getSuffix(a_name), an_object);
107 * Gives a child context name, valid in this context.
109 * @param a_name the name, being given to the child context.
110 * @param a_context the child context being named.
112 * @throws AlreadyBound if the child context is already named in
113 * the current context.
115 public void bind_context(NameComponent[] a_name, NamingContext a_context)
116 throws NotFound, CannotProceed, InvalidName, AlreadyBound
118 if (a_name.length == 1)
119 named_contexts.bind(a_name [ 0 ], a_context);
120 else
122 NamingContext context =
123 (NamingContext) named_contexts.get(a_name [ 0 ]);
124 context.bind_context(getSuffix(a_name), a_context);
129 * Create a new context and give it a given name (bound it)
130 * in the current context.
132 * The context being created is returned by calling
133 * {@link #new_context()}.
135 * @param a_name the name being given to the new context.
137 * @return the newly created context.
139 * @throws AlreadyBound if the name is already in use.
140 * @throws InvalidName if the name has zero length or otherwise invalid.
142 public NamingContext bind_new_context(NameComponent[] a_name)
143 throws NotFound, AlreadyBound, CannotProceed,
144 InvalidName
146 if (named_contexts.containsKey(a_name [ 0 ]) ||
147 named_objects.containsKey(a_name [ 0 ])
149 throw new AlreadyBound();
151 NamingContext child = new_context();
152 bind_context(a_name, child);
153 return child;
157 * Destroy this context (must be empty).
158 * @throws NotEmpty if the context being destroyed is not empty.
160 public void destroy()
161 throws NotEmpty
163 if (named_contexts.size() > 0 || named_objects.size() > 0)
164 throw new NotEmpty();
168 * Iterate over all bindings, defined in this namind context.
170 * @param amount the maximal number of context to return in the
171 * holder a_list. The remaining bindings are accessible via iterator
172 * an_iter. If the parameter amount is zero, all bindings are accessed only
173 * via this iterator.
175 * This implementation list contexts first, then objects.
177 * @param a_list the holder, where the returned bindigs are stored.
178 * @param an_iter the iterator that can be used to access the remaining
179 * bindings.
181 public void list(int amount, BindingListHolder a_list,
182 BindingIteratorHolder an_iter
185 int nb = named_contexts.size() + named_objects.size();
186 int nl = nb;
187 if (nl > amount)
188 nl = amount;
190 a_list.value = new Binding[ nl ];
192 Iterator contexts = named_contexts.entries().iterator();
193 Iterator objects = named_objects.entries().iterator();
195 // Create a binding list.
196 for (int i = 0; i < nl; i++)
198 if (contexts.hasNext())
199 a_list.value [ i ] = mkBinding(contexts.next(), BindingType.ncontext);
200 else if (objects.hasNext())
201 a_list.value [ i ] = mkBinding(objects.next(), BindingType.nobject);
202 else
203 throw new InternalError();
206 // Create an iterator.
207 Binding[] remainder = new Binding[ nb - nl ];
208 int p = 0;
210 while (contexts.hasNext())
211 remainder [ p++ ] = mkBinding(contexts.next(), BindingType.ncontext);
213 while (objects.hasNext())
214 remainder [ p++ ] = mkBinding(objects.next(), BindingType.nobject);
216 Binding_iterator_impl bit = new Binding_iterator_impl(remainder);
217 _orb().connect(bit);
218 an_iter.value = bit;
222 * Creates a new naming context, not bound to any name.
224 public NamingContext new_context()
226 Ext context = new Ext(new TransientContext());
228 // Connect the context to the current ORB:
229 _orb().connect(context);
230 return context;
234 * Names or renames the object.
236 * @param a_name the new name, being given to the object
237 * in the scope of the current context. If the object is already
238 * named in this context, it is renamed.
240 * @param an_object the object, being named.
242 * @throws InvalidName if the name has zero length or otherwise invalid.
244 public void rebind(NameComponent[] a_name, Object an_object)
245 throws NotFound, CannotProceed, InvalidName
247 if (a_name.length == 1)
248 named_objects.rebind(a_name [ 0 ], an_object);
249 else
251 NamingContext context =
252 (NamingContext) named_contexts.get(a_name [ 0 ]);
253 context.rebind(getSuffix(a_name), an_object);
258 * Names or renames the child context.
259 * If the child context is already named in
260 * the current context, it is renamed. The the name being given is in
261 * use, the old meaning of the name is discarded.
263 * @param a_name the name, being given to the child context in the scope
264 * of the current context.
266 * @param a_context the child context being named.
268 * @throws InvalidName if the name has zero length or otherwise invalid.
270 public void rebind_context(NameComponent[] a_name, NamingContext a_context)
271 throws NotFound, CannotProceed, InvalidName
273 if (a_name.length == 1)
274 named_contexts.rebind(a_name [ 0 ], a_context);
275 else
277 NamingContext context =
278 (NamingContext) named_contexts.get(a_name [ 0 ]);
279 context.rebind_context(getSuffix(a_name), a_context);
284 * Get the object, bound to the specified name in this
285 * context. The given object must match the bound
286 * name.
288 * This implementation resolves the names as defined in specification
289 * of the CORBA naming service. This means, if the beginning of the
290 * name can be resolved to some naming context, the request is
291 * forwarded to this context, passing the unresolved name part as a
292 * parameter. In this way, it is possible to have a hierarchy of the
293 * naming services. The central services resolve the the beginning
294 * of the name. The local services resolve the remaining nodes of the
295 * name that may be relevant to some local details. It can be three or
296 * more ranks of the naming services.
298 * @param a_name the object name.
300 * @return the object, matching this name. The client
301 * usually casts or narrows (using the helper) the returned value
302 * to the more specific type.
304 * @throws NotFound if the name cannot be resolved.
305 * @throws InvalidName if the name has zero length or otherwise invalid.
307 public Object resolve(NameComponent[] a_name)
308 throws NotFound, CannotProceed, InvalidName
310 NameValidator.check(a_name);
312 if (a_name.length > 1)
313 return resolveSubContext(a_name);
314 else
316 // A single node name.
317 org.omg.CORBA.Object object;
319 object = named_objects.get(a_name [ 0 ]);
320 if (object != null)
321 return object;
323 object = named_contexts.get(a_name [ 0 ]);
324 if (object != null)
325 return object;
328 throw new NotFound(NotFoundReason.missing_node, a_name);
332 * Removes the name from the binding context.
334 * @param a_name a name to remove.
336 * @throws InvalidName if the name has zero length or otherwise invalid.
338 public void unbind(NameComponent[] a_name)
339 throws NotFound, CannotProceed, InvalidName
341 NameValidator.check(a_name);
343 // Single node name - handle it.
344 if (a_name.length == 1)
346 if (named_objects.containsKey(a_name [ 0 ]))
347 named_objects.remove(a_name [ 0 ]);
348 else if (named_contexts.containsKey(a_name [ 0 ]))
349 named_contexts.remove(a_name [ 0 ]);
350 else
351 throw new NotFound(NotFoundReason.missing_node, a_name);
353 else
355 // Handle the first node and forward the command.
356 NamingContext subcontext =
357 (NamingContext) named_contexts.get(a_name [ 0 ]);
359 if (subcontext == null)
360 throw new NotFound(NotFoundReason.missing_node, a_name);
362 subcontext.unbind(getSuffix(a_name));
367 * Get the name suffix, discarding the first member.
369 private NameComponent[] getSuffix(NameComponent[] a_name)
371 NameComponent[] suffix = new NameComponent[ a_name.length - 1 ];
372 System.arraycopy(a_name, 1, suffix, 0, suffix.length);
373 return suffix;
377 * Create a binding.
379 * @param entry the entry, defining the bound object.
380 * @param type the binding type.
381 * @return the created binding.
383 private Binding mkBinding(java.lang.Object an_entry, BindingType type)
385 Map.Entry entry = (Map.Entry) an_entry;
386 Binding b = new Binding();
388 // The name component has always only one node (the current context)
389 b.binding_name = new NameComponent[] { (NameComponent) entry.getKey() };
390 b.binding_type = type;
391 return b;
395 * Find the context, bound to the first name of the given
396 * name, and pass the remainder (without the first node)
397 * of the name for that context to resolve.
399 * @param name the name to resolve.
401 * @return the resolved context
403 private Object resolveSubContext(NameComponent[] a_name)
404 throws NotFound, CannotProceed, InvalidName
406 // A multiple node name.
407 // This context resolves the first node only.
408 NamingContext context = (NamingContext) named_contexts.get(a_name [ 0 ]);
409 if (context == null)
410 throw new NotFound(NotFoundReason.missing_node, a_name);
412 NameComponent[] suffix = getSuffix(a_name);
414 return context.resolve(suffix);