1 29 package jegg.impl; 30 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Vector ; 36 37 import jegg.EggBase; 38 import jegg.Port; 39 import jegg.PortException; 40 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 49 public class PortRegistry extends EggBase 50 { 51 private static final Log LOG = LogFactory.getLog(PortRegistry.class); 52 53 public static final String SEPARATOR = "/"; 54 55 private static final Map _contexts_ = new HashMap (); 56 57 private String _name; 58 private PortRegistry _parent; 59 60 private Map _ports = new HashMap (); 61 62 private Map _children = new HashMap (); 63 64 private List _outstanding = new Vector (); 65 66 private List _doneList = new Vector (); 67 68 72 public static final PortRegistry getInstance() 73 { 74 return getInstance("."); 75 } 76 77 83 public static final PortRegistry getInstance(final String name) 84 { 85 PortRegistry registry = null; 86 synchronized (_contexts_) 87 { 88 registry = (PortRegistry) _contexts_.get(name); 89 if (null == registry) 90 { 91 try 92 { 93 EggInfo info = HenHouse.getHenHouse().hatch(name,PortRegistry.class.getName(),null,true, true); 94 registry = (PortRegistry) info.getEgg().getHandler(); 95 registry.setName(name); 96 _contexts_.put(name, registry); 97 } 98 catch (HatchException e) 99 { 100 LOG.error("Failed to hatch port registry "+name); 101 } 102 } 103 } 104 return registry; 105 } 106 107 PortRegistry() 108 { 109 super(); 110 } 111 112 PortRegistry(final String name) 113 { 114 this(name,null); 115 } 116 117 PortRegistry(final String name, final PortRegistry parent) 118 { 119 super(); 120 _name = name; 121 _parent = parent; 122 } 123 124 void setName(final String n) {_name = n;} 125 126 public Port getPort() 127 { 128 return getContext().getPort(); 129 } 130 131 134 public void handle(Object message) 135 { 136 LOG.warn("PortRegistry: got unexpected 'Object' message: "+message); 137 } 138 139 143 public void handle(LocatePortMessage message) 144 { 145 LOG.debug("PortRegistry: got LocateMessage"); 146 147 String name = message.getName(); 148 Port p = find(name); 149 if (null != p) 150 { 151 getContext().respond(p); 152 } 153 else 154 { 155 _outstanding.add(new QueuedRequest(name,getContext().getFromPort())); 156 } 157 } 158 159 160 164 private class QueuedRequest 165 { 166 public String name; 167 public Port requestor; 168 public QueuedRequest(String n,Port r) 169 { 170 name = n; 171 requestor = r; 172 } 173 } 174 175 179 public void handle(PublishPortMessage message) 180 { 181 183 String name = message.getName(); 184 Port p = message.getPort(); 185 try 186 { 187 register(name, p); 188 _doneList.clear(); 189 if (!_outstanding.isEmpty()) 190 { 191 MessageImpl msg = new MessageImpl(p,getContext().getPort(),Priority.MEDIUM); 192 for (Iterator it=_outstanding.iterator(); it.hasNext(); ) 193 { 194 QueuedRequest qr = (QueuedRequest) it.next(); 195 if (qr.name.equals(name)) 196 { 197 try 198 { 199 qr.requestor.send(msg); 200 } 201 catch (PortException e1) 202 { 203 LOG.error("Failed to send locate port response "+msg+": ", e1); 204 } 205 _doneList.add(qr); 206 } 207 } 208 for (Iterator it=_doneList.iterator(); it.hasNext(); ) 209 { 210 QueuedRequest qr = (QueuedRequest) it.next(); 211 _outstanding.remove(qr); 212 } 213 _doneList.clear(); 214 } 215 } 216 catch (DuplicatePortException e) 217 { 218 try 219 { 220 getContext().getFromPort().send(getContext().createMessage(e)); 222 } 223 catch (PortException e1) 224 { 225 LOG.error("Failed to send duplicate-name-exception "+e.getMessage(), e1); 226 } 227 } 228 } 229 230 237 public final PortRegistry createRegistry(final String name) 238 throws DuplicatePortException 239 { 240 if (_children.containsKey(name)) 241 { 242 throw new DuplicatePortException(name); 243 } 244 PortRegistry registry = new PortRegistry(name, this); 245 _children.put(name, registry); 246 return registry; 247 } 248 249 256 Port find(final String name) 257 { 258 List stack = new Vector (); 259 stack.add(this); 260 while (!stack.isEmpty()) 261 { 262 PortRegistry registry = (PortRegistry) stack.remove(0); 263 if (registry.isRegistered(name)) 264 { 265 return registry.getPort(name); 266 } 267 268 PortRegistry[] children = registry.getChildren(); 269 for (int i = 0; i < children.length; ++i) 270 { 271 stack.add(children[i]); 272 } 273 } 274 275 return null; 276 } 277 278 283 PortRegistry[] getChildren() 284 { 285 return (PortRegistry[]) _children.values().toArray( 286 new PortRegistry[_children.size()]); 287 } 288 289 296 private void register(final String name, final Port port) 297 throws DuplicatePortException 298 { 299 301 if (_ports.containsKey(name)) 302 { 303 throw new DuplicatePortException("already registered: " + name); 305 } 306 307 _ports.put(name, port); 308 } 309 310 316 boolean isRegistered(final String name) 317 { 318 return _ports.containsKey(name); 319 } 320 321 329 Port getPort(final String name) 330 { 331 if (isRegistered(name)) 332 { 333 return (Port) _ports.get(name); 334 } 335 else if (null != _parent) 336 { 337 return _parent.getPort(name); 338 } 339 else 340 { 341 return null; 342 } 343 } 344 345 349 PortRegistry getParent() 350 { 351 return _parent; 352 } 353 354 358 String getName() 359 { 360 return _name; 361 } 362 363 372 String getFullName() 373 { 374 if (null == _parent) 375 { 376 return getName(); 377 } 378 else 379 { 380 return getParent().getFullName() + PortRegistry.SEPARATOR + getName(); 381 } 382 } 383 384 } 385 | Popular Tags |