Frames | No Frames |
1: /* NamingMap.java -- 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.CosNaming.NameComponent; 42: import org.omg.CosNaming.NamingContextPackage.AlreadyBound; 43: import org.omg.CosNaming.NamingContextPackage.InvalidName; 44: 45: import java.util.Iterator; 46: import java.util.Map; 47: import java.util.Set; 48: import java.util.TreeMap; 49: 50: /** 51: * The Naming Map maps the single names components into associated objects or 52: * naming contexts. 53: * 54: * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) 55: */ 56: public class NamingMap 57: { 58: /** 59: * The actual map. 60: */ 61: protected final TreeMap map; 62: 63: /** 64: * Creates an instance of the naming map, intialising the comparator 65: * to the {@link NameComponentComparator}. 66: */ 67: public NamingMap() 68: { 69: map = new TreeMap(NameComponentComparator.singleton); 70: } 71: 72: /** 73: * Put the given GIOP object, specifying the given name as a key. 74: * If the entry with the given name already exists, or if the given 75: * object is already mapped under another name, the 76: * {@link AlreadyBound} exception will be thrown. 77: * 78: * @param name the name 79: * @param object the object 80: */ 81: public void bind(NameComponent name, org.omg.CORBA.Object object) 82: throws AlreadyBound, InvalidName 83: { 84: if (containsKey(name)) 85: { 86: Object x = get(name); 87: 88: // Do not throw an exception if the same object is named by 89: // the same name. 90: if (x.equals(object)) 91: throw new AlreadyBound("The name is in use for another object"); 92: } 93: else 94: { 95: if (containsValue(object)) 96: throw new AlreadyBound("The object has another name"); 97: } 98: 99: // There are no restrictions in binding the object. 100: rebind(name, object); 101: } 102: 103: /** 104: * Checks if this map contains the definition of the given name. 105: * 106: * @param key the name to check. 107: */ 108: public boolean containsKey(NameComponent key) 109: { 110: return map.containsKey(key); 111: } 112: 113: /** 114: * Checks if this map contains the definition of the given object. 115: * 116: * @param object the object to check. 117: */ 118: public boolean containsValue(org.omg.CORBA.Object object) 119: { 120: return map.containsValue(object); 121: } 122: 123: /** 124: * Returns the map entry set. 125: * 126: * @return the map entry set, containing the instances of the 127: * Map.Entry. 128: */ 129: public Set entries() 130: { 131: return map.entrySet(); 132: } 133: 134: /** 135: * Get the CORBA object, associated with the given name. 136: * 137: * @param name the name. 138: * 139: * @return the associated object, null if none. 140: */ 141: public org.omg.CORBA.Object get(NameComponent name) 142: { 143: return (org.omg.CORBA.Object) map.get(name); 144: } 145: 146: /** 147: * Put the given GIOP object, specifying the given name as a key. 148: * Remove all pre - existing mappings for the given name and object. 149: * 150: * @param name the name. 151: * @param object 152: */ 153: public void rebind(NameComponent name, org.omg.CORBA.Object object) 154: throws InvalidName 155: { 156: // Remove the existing mapping for the given name, if present. 157: remove(name); 158: 159: Iterator iter = entries().iterator(); 160: Map.Entry item; 161: 162: // Remove the existing mapping for the given object, if present. 163: while (iter.hasNext()) 164: { 165: item = (Map.Entry) iter.next(); 166: if (item.getValue().equals(object)) 167: iter.remove(); 168: } 169: 170: map.put(name, object); 171: } 172: 173: /** 174: * Removes the given name, if present. 175: * 176: * @param name a name to remove. 177: */ 178: public void remove(NameComponent name) 179: { 180: map.remove(name); 181: } 182: 183: /** 184: * Get the size of the map. 185: */ 186: public int size() 187: { 188: return map.size(); 189: } 190: }