KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > impl > EggContextImpl


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of GOSSIP nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

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 /**
41  *
42  *
43  * @author Bruce Lowery
44  */

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 JavaDoc getProperty(String JavaDoc key)
57     {
58         return _egg.getProperty(key);
59     }
60     
61     public String JavaDoc getProperty(String JavaDoc key, String JavaDoc 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 JavaDoc m)
77     {
78         return createMessage(m,Priority.MEDIUM);
79     }
80
81     public final Message createMessage(final Object JavaDoc 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 JavaDoc getId()
112     {
113         return _egg.getId();
114     }
115     
116     public final Class JavaDoc[] 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 JavaDoc n)
127     {
128         _egg.publishPort(n);
129     }
130     
131     
132     public final void requestPort(final String JavaDoc n)
133     {
134         _egg.requestPort(n);
135     }
136
137     
138     /**
139      * Send a message to the egg that sent the current message.
140      * The message will be sent with a <i>medium</i> priority
141      * level assigned to it.
142      *
143      * @param message the message to send to the sending egg.
144      */

145     public final void respond(final Object JavaDoc message)
146     {
147         respond(message,Priority.MEDIUM);
148     }
149     
150     /**
151      * Send a message to the egg that sent the current message.
152      * The message will be sent with the specified priority
153      * level assigned to it.
154      *
155      * @param message the message to send to the sending egg.
156      * @param priority the priority to assign to the message.
157      */

158     public final void respond(final Object JavaDoc message, final Priority priority)
159     {
160         respond(_egg.getFromPort(),message,priority);
161     }
162     
163     /**
164      * Send a message to the egg that the specified port
165      * belongs to. This method is useful when a message
166      * response has to be delayed until later (so the port
167      * is saved for use later). The message will be sent
168      * with a medium priority level assigned to it.
169      *
170      * @param port the port to send the message on.
171      * @param message the message to send to the egg that
172      * the specified port belongs to.
173      */

174     public final void respond(final Port port, final Object JavaDoc message)
175     {
176         respond(port,message,Priority.MEDIUM);
177     }
178     
179     /**
180      * Send a message to the egg that the specified port
181      * belongs to. This method is useful when a message
182      * response has to be delayed until later (so the port
183      * is saved for use later). The message will be sent
184      * with the specified priority level assigned to it.
185      *
186      * @param port the port to send the message on.
187      * @param message the message to send to the egg that
188      * the specified port belongs to.
189      * @param priority the message's priority assignment.
190      */

191     public final void respond(final Port port, final Object JavaDoc 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     /**
204      * Broadcast a message to all eggs that have bound to this egg's port
205      * (see {@link #bindToPort(Port) bindToPort}).
206      *
207      * @param message the message to send.
208      */

209     public final void send(final Object JavaDoc msg)
210     {
211         ((PortImpl)_egg.getPort()).broadcast(msg);
212     }
213
214 }
215
Popular Tags