1 4 package org.oddjob.arooa.registry; 5 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import org.apache.commons.beanutils.PropertyUtils; 11 import org.oddjob.arooa.ArooaException; 12 import org.oddjob.values.types.MapType; 13 14 21 public class ComponentRegistry { 22 23 24 private final ServerId serverId; 25 26 27 private Path path; 28 29 30 private final Map ids = new HashMap (); 31 32 33 private final Map components = new HashMap (); 34 35 38 private final Map children = new HashMap (); 39 40 44 public ComponentRegistry() { 45 this(ServerId.local()); 46 } 47 48 54 public ComponentRegistry(ServerId serverId) { 55 if (serverId == null) { 56 throw new NullPointerException ("serverId must not be null!"); 57 } 58 this.serverId = serverId; 59 this.path = new Path(); 60 } 61 62 67 public ServerId getServerId() { 68 return serverId; 69 } 70 71 78 public void addChild(ComponentRegistry child, Object owner) { 79 if (serverId.equals(child.serverId)) { 80 String ownerId = getIdForComponent(owner); 81 Path childPath = path.addId(ownerId); 82 child.path = childPath; 83 } 84 else { 85 child.path = new Path(); 86 } 87 children.put(owner, child); 88 } 89 90 96 public void removeChild(Object owner) { 97 children.remove(owner); 98 } 99 100 108 public ComponentRegistry registryOwnedBy(Object owner) { 109 return (ComponentRegistry) children.get(owner); 110 } 111 112 118 public boolean isOwner(Object component) { 119 return !(children.get(component) == null); 120 } 121 122 128 public void register(String id, Object component) { 129 if (id == null) { 130 return; 131 } 132 if (id.indexOf(PropertyUtils.NESTED_DELIM) > -1 133 || id.indexOf(PropertyUtils.INDEXED_DELIM) > -1 134 || id.indexOf(PropertyUtils.INDEXED_DELIM2) > -1 135 || id.indexOf(PropertyUtils.MAPPED_DELIM) > -1 136 || id.indexOf(PropertyUtils.MAPPED_DELIM2) > -1 137 || id.indexOf(Path.PATH_SEPARATOR) > -1) { 138 throw new ArooaException("Id contains a reserverd character."); 139 } 140 if (ids.containsKey(id)) { 141 throw new ArooaException("A component with id [" + id + "] is already registered"); 142 } 143 ids.put(id, component); 144 components.put(component, id); 145 } 146 147 153 public String getIdForComponent(Object component) { 154 return (String ) components.get(component); 155 } 156 157 163 public Object objectForPath(Path path) { 164 if (path == null) { 165 return null; 166 } 167 Path childPath = path.getChildPath(); 168 if (childPath == null) { 169 return null; 170 } 171 String id = path.getId(); 172 Object component = ids.get(id); 173 if (childPath.size() == 0) { 174 return component; 175 } 176 ComponentRegistry child = 177 (ComponentRegistry) children.get(component); 178 if (child == null) { 179 return null; 180 } 181 return child.objectForPath(childPath); 182 } 183 184 193 public Object objectForAddress(Address address) { 194 if (address == null) { 195 return null; 196 } 197 ComponentRegistry registry = registryForServer(address.getServerId()); 198 if (registry == null) { 199 return null; 200 } 201 Path relativePath = registry.path.relativeTo(address.getPath()); 202 return registry.objectForPath(relativePath); 203 } 204 205 212 public Path pathForObject(Object component) { 213 if (component == null) { 214 throw new NullPointerException ("Component must not be null!"); 215 } 216 return pathToComponent(new Path(), component); 217 } 218 219 228 Path pathToComponent(Path existing, Object component) { 229 String id = getIdForComponent(component); 230 if (id != null) { 231 return existing.addId(id); 232 } 233 for (Iterator it = children.entrySet().iterator(); it.hasNext();) { 236 Map.Entry entry = (Map.Entry ) it.next(); 237 Object childComponent = entry.getKey(); 238 String childId = (String ) components.get(childComponent); 239 if (childId == null) { 240 return null; 243 } 244 ComponentRegistry child = (ComponentRegistry) entry.getValue(); 245 Path result = child.pathToComponent( 246 existing.addId(childId), component); 247 if (result != null) { 248 return result; 249 } 250 } 251 return null; 252 } 253 254 264 ComponentRegistry registryForServer(ServerId serverId) { 265 if (this.serverId.equals(serverId)) { 266 return this; 267 } 268 for (Iterator it = children.entrySet().iterator(); it.hasNext();) { 271 Map.Entry entry = (Map.Entry ) it.next(); 272 ComponentRegistry child = (ComponentRegistry) entry.getValue(); 273 ComponentRegistry result = child.registryForServer(serverId); 274 if (result != null) { 275 return result; 276 } 277 } 278 return null; 279 } 280 281 288 public Address addressForObject(Object object) { 289 String id = (String ) components.get(object); 290 if (id == null) { 291 return null; 292 } 293 return new Address(serverId, 294 path.addId(id)); 295 } 296 297 306 ComponentRegistry registryForComponent(Object component) { 307 String id = getIdForComponent(component); 308 if (id != null) { 309 return this; 310 } 311 for (Iterator it = children.entrySet().iterator(); it.hasNext();) { 314 Map.Entry entry = (Map.Entry ) it.next(); 315 Object childComponent = entry.getKey(); 316 String childId = (String ) components.get(childComponent); 317 if (childId == null) { 318 return null; 321 } 322 ComponentRegistry child = (ComponentRegistry) entry.getValue(); 323 ComponentRegistry result = child.registryForComponent(component); 324 if (result != null) { 325 return result; 326 } 327 } 328 return null; 329 } 330 331 332 337 public void remove(Object component) { 338 String id = (String ) components.remove(component); 339 ids.remove(id); 340 children.remove(component); 341 } 342 343 348 public int childCount() { 349 return children.size(); 350 } 351 352 356 public String toString() { 357 StringBuffer buf = new StringBuffer (); 358 buf.append("ComponentRegistry, ids [" + ids.size() + "], children [" + children.size() + "]"); 359 buf.append('\n'); 360 buf.append(MapType.propertiesFrom(ids)); 361 return buf.toString(); 362 } 363 } 364 | Popular Tags |