KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > EventConsumerBaseStubI


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>.</p>
36  **/

37 public class EventConsumerBaseStubI
38 extends org.omg.Components._EventConsumerBaseStub
39 {
40     // consumer base stub
41
static private String JavaDoc _class_name = "EventConsumerBaseStubI";
42     private ORBService _orb_service;
43     private ServicesSet _services_set;
44     private String JavaDoc _uuid;
45     private ClientCallContextImpl _call_ctx;
46     private java.util.Hashtable JavaDoc _informations;
47     private org.omg.Components.EventConsumerBase _delegate;
48     private String JavaDoc _scoped_name;
49
50     // default constructor
51
public
52     EventConsumerBaseStubI(ORBService orbs, ServicesSet sset,
53                            String JavaDoc uuid, org.omg.CORBA.Object JavaDoc del,
54                            String JavaDoc sname)
55     {
56         // consumer base stub
57
_orb_service = orbs;
58         _services_set = sset;
59         _uuid = uuid;
60         _delegate = org.omg.Components.EventConsumerBaseHelper.narrow(del);
61         _informations = new java.util.Hashtable JavaDoc();
62         _scoped_name = sname;
63
64         // create call context
65
_call_ctx = new ClientCallContextImpl(_orb_service, _services_set, del);
66     }
67
68     //
69
// internal operations
70
//
71

72     private CallInformation
73     _getCallInformation(String JavaDoc name)
74     {
75         // check if information is already computed
76
CallInformation info = (CallInformation)_informations.get(name);
77         
78         if (info==null) {
79             // create information
80
info = new CallInformation();
81             // store
82
_informations.put(name, info);
83         }
84         
85         // obtain policies in effect
86
// check if policies are already computed
87
if (info.policies==null) {
88             info.policies = _services_set.get_policies(_uuid, name);
89         }
90         
91         // NOTE: controllers are created on demand in the CallInformation class
92

93         return info;
94     }
95     
96     private short
97     _preinvoke(org.coach.ECA.ClientCallController[] ctrls)
98     {
99         final String JavaDoc opname = "_preinvoke";
100         org.omg.CORBA.Any JavaDoc curr_exc = null;
101         org.omg.CORBA.Any JavaDoc curr_res = null;
102         
103         // initialization
104
short fstate = FlowState.SEND_REQUEST;
105         int ctrl_idx = 0;
106         boolean finished = false;
107         org.coach.ECA.ClientCallController ctrl = null;
108         
109         while ((!finished) && (ctrl_idx<ctrls.length)){
110             // check if a system exception was set
111
curr_exc = _call_ctx.system_exception();
112             if (curr_exc!=null) {
113                 // change flow state
114
fstate = FlowState.RECEIVE_SYSTEM_EXCEPTION;
115             }
116         
117             // check if a user exception was set
118
curr_exc = _call_ctx.user_exception();
119             if (curr_exc!=null) {
120                 // change flow state
121
fstate = FlowState.RECEIVE_USER_EXCEPTION;
122             }
123         
124             // check if a result was set
125
curr_res = _call_ctx.result();
126             if (curr_res!=null) {
127                 // not possible in preinvoke as result is not modifiable by controllers
128
final String JavaDoc msg = "FAILED (unexpected state in preinvoke: RECEIVE_REPLY)";
129                 TheLogger.error(_class_name, opname, msg);
130             }
131         
132             // we are on the send_request IP
133
if (fstate==FlowState.SEND_REQUEST) {
134                 // take next controller
135
ctrl = ctrls[ctrl_idx++];
136                 ctrl.send_request(_call_ctx);
137         
138                 // push completed controller on the flow stack (starting IP)
139
_call_ctx.pushController(ctrl);
140             }
141             // check if there's anymore controllers on the flow stack
142
else if (_call_ctx.isEmpty()) {
143                 finished = true;
144             }
145             // we are on the receive_system_exception IP
146
else if (fstate==FlowState.RECEIVE_SYSTEM_EXCEPTION) {
147                 // get controller from flow stack (ending IP)
148
ctrl = _call_ctx.popController();
149                 ctrl.receive_system_exception(_call_ctx);
150             }
151             // we are on the receive_user_exception IP
152
else if (fstate==FlowState.RECEIVE_SYSTEM_EXCEPTION) {
153                 // get controller from flow stack (ending IP)
154
ctrl = _call_ctx.popController();
155                 ctrl.receive_user_exception(_call_ctx);
156             }
157             // we are on the receive_reply IP
158
else {
159                 // not possible in preinvoke as result is not modifiable by controllers
160
final String JavaDoc msg = "FAILED (unexpected state in preinvoke: RECEIVE_REPLY)";
161                 TheLogger.error(_class_name, opname, msg);
162             }
163         }
164         
165         // return flow state
166
return fstate;
167     }
168     
169     /**
170      * .
171      */

172     private short
173     _postinvoke(short fstate)
174     {
175         org.omg.CORBA.Any JavaDoc curr_exc = null;
176         org.omg.CORBA.Any JavaDoc curr_res = null;
177         boolean finished = false;
178         org.coach.ECA.ClientCallController ctrl = null;
179         
180         // intialization of the flow state to take into account
181
// business code actions
182

183         // check if a system exception was set
184
curr_exc = _call_ctx.system_exception();
185         curr_exc = _call_ctx.user_exception();
186         if (curr_exc!=null) {
187             // change flow state
188
fstate = FlowState.RECEIVE_SYSTEM_EXCEPTION;
189         }
190         // check if a user exception was set
191
else if (curr_exc!=null) {
192             // change flow state
193
fstate = FlowState.RECEIVE_USER_EXCEPTION;
194         }
195         else {
196             // change flow state
197
fstate = FlowState.RECEIVE_REPLY;
198         }
199         
200         while (!finished) {
201             // check if there's anymore controllers on the flow stack
202
if (_call_ctx.isEmpty()) {
203                 finished = true;
204             }
205             // we are on the receive_system_exception IP
206
else if (fstate==FlowState.RECEIVE_SYSTEM_EXCEPTION) {
207                 // get controller from flow stack (ending IP)
208
ctrl = _call_ctx.popController();
209                 ctrl.receive_system_exception(_call_ctx);
210             }
211             // we are on the receive_user_exception IP
212
else if (fstate==FlowState.RECEIVE_SYSTEM_EXCEPTION) {
213                 // get controller from flow stack (ending IP)
214
ctrl = _call_ctx.popController();
215                 ctrl.receive_user_exception(_call_ctx);
216             }
217             // we are on the receive_reply IP
218
else {
219                 // get controller from flow stack (ending IP)
220
ctrl = _call_ctx.popController();
221                 ctrl.receive_reply(_call_ctx);
222             }
223         
224             // check if a system exception was set
225
curr_exc = _call_ctx.system_exception();
226             if (curr_exc!=null) {
227                 // change flow state
228
fstate = FlowState.RECEIVE_SYSTEM_EXCEPTION;
229             }
230         
231             // check if a user exception was set
232
curr_exc = _call_ctx.user_exception();
233             if (curr_exc!=null) {
234                 // change flow state
235
fstate = FlowState.RECEIVE_USER_EXCEPTION;
236             }
237         
238             // check if a result was set
239
curr_res = _call_ctx.result();
240             if (curr_res!=null) {
241                 // change flow state
242
fstate = FlowState.RECEIVE_REPLY;
243             }
244         }
245         
246         // return flow state
247
return fstate;
248     }
249
250     //
251
// IDL:omg.org/Components/EventConsumerBase:1.0
252
//
253

254     final public void
255     push_event(org.omg.Components.EventBase evt)
256     throws org.omg.Components.BadEventType
257     {
258         final String JavaDoc opname = "push_event";
259         org.omg.CORBA.Any JavaDoc anyexc = null;
260         
261         // check if controllers are created
262
CallInformation callinfo = _getCallInformation(_scoped_name);
263         
264         // create a new entry in the ClientCallContext
265
_call_ctx.addEntry(callinfo, opname);
266         
267         // call preinvoke routine
268
short fstate = _preinvoke(callinfo.getClientCallControllers());
269         
270         // if flow state is not SEND_REQUEST, then skip business call
271
if (fstate==FlowState.RECEIVE_SYSTEM_EXCEPTION) {
272             anyexc = _call_ctx.system_exception();
273             _call_ctx.removeEntry();
274             throw org.omg.CORBA.SystemExceptionHelper.extract(anyexc);
275         }
276         else if (fstate==FlowState.RECEIVE_USER_EXCEPTION) {
277             anyexc = _call_ctx.user_exception();
278             _call_ctx.removeEntry();
279             // check which exception was raised
280
if (anyexc.type()==org.omg.Components.BadEventTypeHelper.type()) {
281                 throw org.omg.Components.BadEventTypeHelper.extract(anyexc);
282             }
283             else {
284                 TheLogger.error(_class_name, opname, "unknown user exception was raised");
285             }
286         }
287         else if (fstate==FlowState.RECEIVE_REPLY) {
288             // not possible in preinvoke as result is not modifiable by controllers
289
TheLogger.error(_class_name, opname, "unexpected state after preinvoke: RECEIVE_REPLY");
290         }
291         
292         try {
293             // call business op
294
_delegate.push_event(evt);
295         }
296         catch (org.omg.CORBA.SystemException JavaDoc ex) {
297             anyexc = _call_ctx.create_any();
298             org.omg.CORBA.SystemExceptionHelper.insert(anyexc, ex);
299             _call_ctx.send_system_exception(anyexc);
300         }
301         catch (org.omg.Components.BadEventType ex) {
302             // set info in context
303
anyexc = _orb_service.create_any();
304             org.omg.Components.BadEventTypeHelper.insert(anyexc, ex);
305             _call_ctx.send_user_exception(anyexc);
306         }
307         // call postinvoke routine
308
fstate = _postinvoke(fstate);
309         
310         // check final flow state
311
if (fstate==FlowState.RECEIVE_SYSTEM_EXCEPTION) {
312             anyexc = _call_ctx.system_exception();
313             _call_ctx.removeEntry();
314             throw org.omg.CORBA.SystemExceptionHelper.extract(anyexc);
315         }
316         else if (fstate==FlowState.RECEIVE_USER_EXCEPTION) {
317             anyexc = _call_ctx.user_exception();
318             _call_ctx.removeEntry();
319             // check which exception was raised
320
if (anyexc.type()==org.omg.Components.BadEventTypeHelper.type()) {
321                 throw org.omg.Components.BadEventTypeHelper.extract(anyexc);
322             }
323             else {
324                 TheLogger.error(_class_name, opname, "unknown user exception was raised");
325             }
326         }
327         else if (fstate==FlowState.RECEIVE_REPLY) {
328             _call_ctx.removeEntry();
329         }
330     }
331 }
332
Popular Tags