1 29 package jegg.impl; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import jegg.EggContext; 35 import jegg.Message; 36 import jegg.Port; 37 import jegg.PortException; 38 import jegg.timer.Timer; 39 40 45 public class EggContextImpl implements EggContext 46 { 47 private static final Log LOG = LogFactory.getLog(EggContextImpl.class); 48 49 private final EggShell _egg; 50 51 public EggContextImpl(final EggShell e) 52 { 53 _egg = e; 54 } 55 56 public String getProperty(String key) 57 { 58 return _egg.getProperty(key); 59 } 60 61 public String getProperty(String key, String defValue) 62 { 63 return _egg.getProperty(key,defValue); 64 } 65 66 public Property[] getProperties() 67 { 68 return _egg.getProperties(); 69 } 70 71 public final void bindToPort(final Port p) 72 { 73 ((PortImpl)p).connect(_egg.getPort()); 74 } 75 76 public final Message createMessage(final Object m) 77 { 78 return createMessage(m,Priority.MEDIUM); 79 } 80 81 public final Message createMessage(final Object m, final Priority p) 82 { 83 return new MessageImpl(m,_egg.getPort(),p); 84 } 85 86 public final Timer createRepeatingTimer(final long interval_msec, final long delay_msec) 87 { 88 return _egg.createRepeatingTimer(interval_msec, delay_msec); 89 } 90 91 public final Timer createSingleShotTimer(final long delay_msec) 92 { 93 return _egg.createSingleShotTimer(delay_msec); 94 } 95 96 public final Message getCurrentMessage() 97 { 98 return _egg.getCurrentMessage(); 99 } 100 101 public final Dispatcher getDispatcher() 102 { 103 return _egg.getDispatcher(); 104 } 105 106 public final Port getFromPort() 107 { 108 return _egg.getFromPort(); 109 } 110 111 public final Object getId() 112 { 113 return _egg.getId(); 114 } 115 116 public final Class [] getInterface() 117 { 118 return _egg.getInterface(); 119 } 120 121 public final Port getPort() 122 { 123 return _egg.getPort(); 124 } 125 126 public final void publishPort(final String n) 127 { 128 _egg.publishPort(n); 129 } 130 131 132 public final void requestPort(final String n) 133 { 134 _egg.requestPort(n); 135 } 136 137 138 145 public final void respond(final Object message) 146 { 147 respond(message,Priority.MEDIUM); 148 } 149 150 158 public final void respond(final Object message, final Priority priority) 159 { 160 respond(_egg.getFromPort(),message,priority); 161 } 162 163 174 public final void respond(final Port port, final Object message) 175 { 176 respond(port,message,Priority.MEDIUM); 177 } 178 179 191 public final void respond(final Port port, final Object message, final Priority priority) 192 { 193 try 194 { 195 port.send(_egg.createMessage(message,priority)); 196 } 197 catch (PortException e) 198 { 199 LOG.error("Failed to send response "+message+": ", e); 200 } 201 } 202 203 209 public final void send(final Object msg) 210 { 211 ((PortImpl)_egg.getPort()).broadcast(msg); 212 } 213 214 } 215 | Popular Tags |