1 29 package jegg.impl; 30 31 import java.util.Collection ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 35 import jegg.Message; 36 import jegg.PortException; 37 import jegg.Port; 38 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 42 52 public final class PortImpl implements Port 53 { 54 55 private static final Log LOG = LogFactory.getLog(PortImpl.class); 56 57 private EggInfo _info = null; 58 59 60 private EggShell _owner; 61 62 63 private final Collection _otherPorts = new HashSet (); 64 65 PortImpl (final EggInfo info) 66 { 67 super(); 68 _info = info; 69 } 70 74 PortImpl (final EggShell egg) 75 { 76 super(); 77 _owner = egg; 78 _owner.setPort(this); 79 } 80 81 public Class [] getInterface() 82 { 83 if (null == _owner) 84 { 85 try 86 { 87 instantiate(); 88 } 89 catch (HatchException e) 90 { 91 RuntimeException t = new IllegalStateException ("Can't create egg "+_info.getEggName()); 92 t.initCause(e); 93 throw t; 94 } 95 } 96 return _owner.getInterface(); 97 } 98 99 public Object getId() 100 { 101 if (null == _owner) 102 { 103 try 104 { 105 instantiate(); 106 } 107 catch (HatchException e) 108 { 109 RuntimeException t = new IllegalStateException ("Can't create egg "+_info.getEggName()); 110 t.initCause(e); 111 throw t; 112 } 113 } 114 return _owner.getId(); 115 } 116 117 123 public final void send(final Object o) throws PortException 124 { 125 if (null == _owner) 126 { 127 try 128 { 129 instantiate(); 130 } 131 catch (HatchException e) 132 { 133 throw new PortException("Can't create egg "+_info.getEggName(), e); 134 } 135 } 136 137 send(_owner.createMessage(o)); 138 } 139 140 145 public final void send(final Message m) throws PortException 146 { 147 if (null == _owner) 148 { 149 try 150 { 151 instantiate(); 152 } 153 catch (HatchException e) 154 { 155 throw new PortException("Can't create egg "+_info.getEggName(), e); 156 } 157 } 158 _owner.enqueue(m); 159 } 160 161 167 final void broadcast(final Object msg) 168 { 169 broadcast(msg,Priority.MEDIUM); 170 } 171 172 178 final void broadcast(final Object msg, final Priority p) 179 { 180 MessageImpl message = new MessageImpl(msg,this,p); 181 for (Iterator it = _otherPorts.iterator(); it.hasNext(); ) 182 { 183 PortImpl po = (PortImpl) it.next(); 184 try 185 { 186 po.send(message); 187 } 188 catch (PortException e) 189 { 190 LOG.error("Failed to broadcast message to port "+po+": ", e); 191 } 192 } 193 } 194 195 200 public final void connect (final Port port) 201 { 202 _otherPorts.add(port); 203 } 204 205 209 public final void disconnect(final Port port) 210 { 211 _otherPorts.remove(port); 212 } 213 214 217 final Port[] getConnectedPorts() 218 { 219 return (Port[]) _otherPorts.toArray(new Port[_otherPorts.size()]); 220 } 221 222 void setOwner(EggShell owner) 223 { 224 if (LOG.isDebugEnabled()) 225 LOG.debug("setOwner("+owner+")"); 226 227 _owner = owner; 228 } 229 230 private void instantiate() throws HatchException 231 { 232 if (null != _owner) {return;} 233 234 HenHouse.getHenHouse().hatch(this._info, true, true); 235 _owner = _info.getEgg(); 236 } 237 238 } | Popular Tags |