Frames | No Frames |
1: /* nContext.java -- implementation of NamingContext 2: Copyright (C) 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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. 20: 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. 25: 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. */ 37: 38: 39: package gnu.CORBA.NamingService; 40: 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; 56: 57: import java.util.Iterator; 58: import java.util.Map; 59: 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. 65: * 66: * TODO Write the persistent naming service. 67: * 68: * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) 69: */ 70: public class TransientContext 71: extends _NamingContextImplBase 72: implements NamingContext, NamingContextOperations 73: { 74: /** 75: * The already named contexts. 76: */ 77: protected final NamingMap named_contexts = new NamingMap(); 78: 79: /** 80: * The already named objects. 81: */ 82: protected final NamingMap named_objects = new NamingMap(); 83: 84: /** 85: * Gives the object a name, valid in this context. 86: * 87: * @param a_name the name, being given to the object. 88: * @param an_object the object, being named. 89: * 90: * @throws AlreadyBound if the object is already named in this context. 91: * @throws InvalidName if the name has zero length or otherwise invalid. 92: */ 93: public void bind(NameComponent[] a_name, Object an_object) 94: throws NotFound, CannotProceed, InvalidName, AlreadyBound 95: { 96: if (a_name.length == 1) 97: named_objects.bind(a_name [ 0 ], an_object); 98: else 99: { 100: NamingContext context = 101: (NamingContext) named_contexts.get(a_name [ 0 ]); 102: context.bind(getSuffix(a_name), an_object); 103: } 104: } 105: 106: /** 107: * Gives a child context name, valid in this context. 108: * 109: * @param a_name the name, being given to the child context. 110: * @param a_context the child context being named. 111: * 112: * @throws AlreadyBound if the child context is already named in 113: * the current context. 114: */ 115: public void bind_context(NameComponent[] a_name, NamingContext a_context) 116: throws NotFound, CannotProceed, InvalidName, AlreadyBound 117: { 118: if (a_name.length == 1) 119: named_contexts.bind(a_name [ 0 ], a_context); 120: else 121: { 122: NamingContext context = 123: (NamingContext) named_contexts.get(a_name [ 0 ]); 124: context.bind_context(getSuffix(a_name), a_context); 125: } 126: } 127: 128: /** 129: * Create a new context and give it a given name (bound it) 130: * in the current context. 131: * 132: * The context being created is returned by calling 133: * {@link #new_context()}. 134: * 135: * @param a_name the name being given to the new context. 136: * 137: * @return the newly created context. 138: * 139: * @throws AlreadyBound if the name is already in use. 140: * @throws InvalidName if the name has zero length or otherwise invalid. 141: */ 142: public NamingContext bind_new_context(NameComponent[] a_name) 143: throws NotFound, AlreadyBound, CannotProceed, 144: InvalidName 145: { 146: if (named_contexts.containsKey(a_name [ 0 ]) || 147: named_objects.containsKey(a_name [ 0 ]) 148: ) 149: throw new AlreadyBound(); 150: 151: NamingContext child = new_context(); 152: bind_context(a_name, child); 153: return child; 154: } 155: 156: /** 157: * Destroy this context (must be empty). 158: * @throws NotEmpty if the context being destroyed is not empty. 159: */ 160: public void destroy() 161: throws NotEmpty 162: { 163: if (named_contexts.size() > 0 || named_objects.size() > 0) 164: throw new NotEmpty(); 165: } 166: 167: /** 168: * Iterate over all bindings, defined in this namind context. 169: * 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. 174: * 175: * This implementation list contexts first, then objects. 176: * 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. 180: */ 181: public void list(int amount, BindingListHolder a_list, 182: BindingIteratorHolder an_iter 183: ) 184: { 185: int nb = named_contexts.size() + named_objects.size(); 186: int nl = nb; 187: if (nl > amount) 188: nl = amount; 189: 190: a_list.value = new Binding[ nl ]; 191: 192: Iterator contexts = named_contexts.entries().iterator(); 193: Iterator objects = named_objects.entries().iterator(); 194: 195: // Create a binding list. 196: for (int i = 0; i < nl; i++) 197: { 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(); 204: } 205: 206: // Create an iterator. 207: Binding[] remainder = new Binding[ nb - nl ]; 208: int p = 0; 209: 210: while (contexts.hasNext()) 211: remainder [ p++ ] = mkBinding(contexts.next(), BindingType.ncontext); 212: 213: while (objects.hasNext()) 214: remainder [ p++ ] = mkBinding(objects.next(), BindingType.nobject); 215: 216: Binding_iterator_impl bit = new Binding_iterator_impl(remainder); 217: _orb().connect(bit); 218: an_iter.value = bit; 219: } 220: 221: /** 222: * Creates a new naming context, not bound to any name. 223: */ 224: public NamingContext new_context() 225: { 226: Ext context = new Ext(new TransientContext()); 227: 228: // Connect the context to the current ORB: 229: _orb().connect(context); 230: return context; 231: } 232: 233: /** 234: * Names or renames the object. 235: * 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. 239: * 240: * @param an_object the object, being named. 241: * 242: * @throws InvalidName if the name has zero length or otherwise invalid. 243: */ 244: public void rebind(NameComponent[] a_name, Object an_object) 245: throws NotFound, CannotProceed, InvalidName 246: { 247: if (a_name.length == 1) 248: named_objects.rebind(a_name [ 0 ], an_object); 249: else 250: { 251: NamingContext context = 252: (NamingContext) named_contexts.get(a_name [ 0 ]); 253: context.rebind(getSuffix(a_name), an_object); 254: } 255: } 256: 257: /** 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. 262: * 263: * @param a_name the name, being given to the child context in the scope 264: * of the current context. 265: * 266: * @param a_context the child context being named. 267: * 268: * @throws InvalidName if the name has zero length or otherwise invalid. 269: */ 270: public void rebind_context(NameComponent[] a_name, NamingContext a_context) 271: throws NotFound, CannotProceed, InvalidName 272: { 273: if (a_name.length == 1) 274: named_contexts.rebind(a_name [ 0 ], a_context); 275: else 276: { 277: NamingContext context = 278: (NamingContext) named_contexts.get(a_name [ 0 ]); 279: context.rebind_context(getSuffix(a_name), a_context); 280: } 281: } 282: 283: /** 284: * Get the object, bound to the specified name in this 285: * context. The given object must match the bound 286: * name. 287: * 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. 297: * 298: * @param a_name the object name. 299: * 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. 303: * 304: * @throws NotFound if the name cannot be resolved. 305: * @throws InvalidName if the name has zero length or otherwise invalid. 306: */ 307: public Object resolve(NameComponent[] a_name) 308: throws NotFound, CannotProceed, InvalidName 309: { 310: NameValidator.check(a_name); 311: 312: if (a_name.length > 1) 313: return resolveSubContext(a_name); 314: else 315: { 316: // A single node name. 317: org.omg.CORBA.Object object; 318: 319: object = named_objects.get(a_name [ 0 ]); 320: if (object != null) 321: return object; 322: 323: object = named_contexts.get(a_name [ 0 ]); 324: if (object != null) 325: return object; 326: } 327: 328: throw new NotFound(NotFoundReason.missing_node, a_name); 329: } 330: 331: /** 332: * Removes the name from the binding context. 333: * 334: * @param a_name a name to remove. 335: * 336: * @throws InvalidName if the name has zero length or otherwise invalid. 337: */ 338: public void unbind(NameComponent[] a_name) 339: throws NotFound, CannotProceed, InvalidName 340: { 341: NameValidator.check(a_name); 342: 343: // Single node name - handle it. 344: if (a_name.length == 1) 345: { 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); 352: } 353: else 354: { 355: // Handle the first node and forward the command. 356: NamingContext subcontext = 357: (NamingContext) named_contexts.get(a_name [ 0 ]); 358: 359: if (subcontext == null) 360: throw new NotFound(NotFoundReason.missing_node, a_name); 361: 362: subcontext.unbind(getSuffix(a_name)); 363: } 364: } 365: 366: /** 367: * Get the name suffix, discarding the first member. 368: */ 369: private NameComponent[] getSuffix(NameComponent[] a_name) 370: { 371: NameComponent[] suffix = new NameComponent[ a_name.length - 1 ]; 372: System.arraycopy(a_name, 1, suffix, 0, suffix.length); 373: return suffix; 374: } 375: 376: /** 377: * Create a binding. 378: * 379: * @param entry the entry, defining the bound object. 380: * @param type the binding type. 381: * @return the created binding. 382: */ 383: private Binding mkBinding(java.lang.Object an_entry, BindingType type) 384: { 385: Map.Entry entry = (Map.Entry) an_entry; 386: Binding b = new Binding(); 387: 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; 392: } 393: 394: /** 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. 398: * 399: * @param name the name to resolve. 400: * 401: * @return the resolved context 402: */ 403: private Object resolveSubContext(NameComponent[] a_name) 404: throws NotFound, CannotProceed, InvalidName 405: { 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); 411: 412: NameComponent[] suffix = getSuffix(a_name); 413: 414: return context.resolve(suffix); 415: } 416: }