KickJava   Java API By Example, From Geeks To Geeks.

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


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>Home specific implementation for the <tt>Components::Session2Context</tt> interface. This
36  ** class implements the features which are common to all component instance in a particular
37  ** home instance and is therefore provided to the home instances.</p>
38  **/

39 public class HomeSession2ContextImpl
40 extends org.omg.CORBA.LocalObject JavaDoc
41 implements org.omg.Components.Session2Context, org.coach.ECM.ECMContext
42 {
43     // session context
44
static private String JavaDoc _class_name = "Session2ContextImpl";
45     private org.omg.Components.CCMHome _home_ref;
46     private ORBService _orb_service;
47     private ComponentPOAService _poa_service;
48     private ServicesSet _services_set;
49     private HomeLocalEventsImpl _events;
50     private HomeLocalReceptaclesImpl _receptacles;
51     private MonolithicIdCodec _idcodec;
52
53     // empty constructor
54
public
55     HomeSession2ContextImpl()
56     {
57         // session context
58
_home_ref = null;
59         _orb_service = null;
60         _poa_service = null;
61         _services_set = null;
62         _events = null;
63         _receptacles = null;
64         _idcodec = null;
65     }
66
67     // default constructor
68
protected
69     HomeSession2ContextImpl(org.omg.Components.CCMHome href,
70                             ORBService orbs,
71                             ComponentPOAService poas,
72                             ServicesSet sset,
73                             HomeLocalEventsImpl levts,
74                             HomeLocalReceptaclesImpl lrecs)
75     {
76         // session context
77
_home_ref = href;
78         _orb_service = orbs;
79         _poa_service = poas;
80         _services_set = sset;
81         _events = levts;
82         _receptacles = lrecs;
83
84         // create ObjectId codec
85
_idcodec = new MonolithicIdCodec(_orb_service);
86     }
87
88     //
89
// internal operations
90
//
91

92     private org.omg.PortableServer.Current JavaDoc
93     getPOACurrent()
94     {
95         // get poa current
96
org.omg.CORBA.Object JavaDoc curr = _orb_service.resolve_initial_references("POACurrent");
97         return org.omg.PortableServer.CurrentHelper.narrow(curr);
98     }
99
100     //
101
// public operations (for component context)
102
//
103

104     final public HomeLocalEventsImpl
105     getLocalEvents()
106     {
107         return _events;
108     }
109
110     final public HomeLocalReceptaclesImpl
111     getLocalReceptacles()
112     {
113         return _receptacles;
114     }
115
116     //
117
// IDL:omg.org/Components/CCMContext:1.0
118
//
119

120     final public org.omg.Components.CCMHome
121     get_CCM_home()
122     {
123         return _home_ref;
124     }
125
126     //
127
// IDL:omg.org/Components/CCM2Context:1.0
128
//
129

130     final public org.omg.Components.HomeRegistrationLocal
131     get_home_registration()
132     {
133         // TODO
134
return null;
135     }
136
137     final public void
138     req_passivate()
139     throws org.omg.Components.IllegalState,
140            org.omg.Components.PolicyMismatch
141     {
142         final String JavaDoc opname = "req_passivate";
143         try {
144             org.omg.PortableServer.Current JavaDoc curr = getPOACurrent();
145             byte[] id = curr.get_object_id();
146             _poa_service.passivate_component(id);
147         } catch (org.omg.PortableServer.CurrentPackage.NoContext JavaDoc ex) {
148             TheLogger.debug(_class_name, opname, "FAILED (no context, rethrown)", ex);
149             throw new org.omg.Components.IllegalState();
150         } catch (org.objectweb.ccm.runtime.WrongPolicy ex) {
151             TheLogger.debug(_class_name, opname, "FAILED (wrong policy, rethrown)", ex);
152             throw new org.omg.Components.PolicyMismatch();
153         }
154     }
155
156     final public org.omg.Components.LocalEvents
157     get_events()
158     {
159         // NOTE: not supported in home context implementation
160
return null;
161     }
162
163     final public org.omg.Components.LocalReceptacles
164     get_receptacles()
165     {
166         // NOTE: not supported in home context implementation
167
return null;
168     }
169
170     final public void
171     remove_component(org.omg.Components.CCMObject comp)
172     {
173         _poa_service.remove_component(comp);
174     }
175
176     final public void
177     register_valuefactory(String JavaDoc repid, org.omg.CORBA.portable.ValueFactory JavaDoc vfact)
178     {
179         ValueFactory fact = new ValueFactoryImpl(repid, vfact);
180         _orb_service.register_valuefactory(fact);
181     }
182
183     final public org.omg.CORBA.Any JavaDoc
184     create_any()
185     {
186         return _orb_service.create_any();
187     }
188
189     //
190
// IDL:omg.org/Components/SessionContext:1.0
191
//
192

193     final public org.omg.CORBA.Object JavaDoc
194     get_CCM_object()
195     throws org.omg.Components.IllegalState
196     {
197         try {
198             org.omg.PortableServer.Current JavaDoc curr = getPOACurrent();
199             byte[] id = curr.get_object_id();
200             org.omg.PortableServer.POA JavaDoc poa = curr.get_POA();
201
202             return poa.id_to_reference(id);
203         } catch (org.omg.PortableServer.CurrentPackage.NoContext JavaDoc exc) {
204             throw new org.omg.Components.IllegalState();
205         } catch (Exception JavaDoc ex) {
206             // NOTE: should not happen
207
final String JavaDoc opname = "get_CCM_object";
208             TheLogger.error(_class_name, opname, "FAILED", ex);
209             return null;
210         }
211     }
212
213     //
214
// IDL:omg.org/Components/Session2Context:1.0
215
//
216

217     final public byte[]
218     get_object_id()
219     throws org.omg.Components.IllegalState
220     {
221         try {
222             return get_id_from_ref(get_CCM_object());
223         } catch (org.omg.Components.BadComponentReference ex) {
224             // should not happen
225
final String JavaDoc opname = "get_object_id";
226             TheLogger.error(_class_name, opname, "FAILED", ex);
227             return null;
228         }
229     }
230
231     final public org.omg.CORBA.Object JavaDoc
232     create_ref(String JavaDoc repid)
233     {
234         // create a dummy id
235
byte[] id = Double.toString(Math.random()).getBytes();
236
237         return create_ref_with_id(id, repid);
238     }
239
240     final public org.omg.CORBA.Object JavaDoc
241     create_ref_with_id(byte[] id, String JavaDoc repid)
242     {
243         final String JavaDoc opname = "create_ref_with_id";
244
245         // NOTE: the caller MUST be the home
246
// NOTE2: obtain the object id of the home
247
byte[] home_id = null;
248         try {
249             home_id = getPOACurrent().get_object_id();
250         } catch (org.omg.PortableServer.CurrentPackage.NoContext JavaDoc exc) {
251             // should not happen
252
TheLogger.error(_class_name, opname, "FAILED (no context)", exc);
253         }
254
255         // create and encode oid
256
MonolithicId mid = new MonolithicId();
257         mid.home_id = home_id;
258         mid.component_id = id;
259         byte[] rid = _idcodec.encode(mid);
260
261         // create a ref
262
return _poa_service.create_ref_with_id(rid, repid);
263     }
264
265     final public byte[]
266     get_id_from_ref(org.omg.CORBA.Object JavaDoc ref)
267     throws org.omg.Components.BadComponentReference
268     {
269         final String JavaDoc opname = "get_id_from_ref";
270         byte[] id = null;
271         try {
272             id = _poa_service.get_id_from_ref(ref);
273         } catch (org.objectweb.ccm.runtime.InvalidReference ex) {
274             // NOTE: to be completed with a reason
275
TheLogger.debug(_class_name, opname, "FAILED (invalid ref, rethrown)", ex);
276             throw new org.omg.Components.BadComponentReference();
277         }
278
279         // decode
280
MonolithicId mid = _idcodec.decode(id);
281         if (mid==null) {
282             // NOTE: to be completed with a reason
283
TheLogger.debug(_class_name, opname, "FAILED (wrong format)");
284             throw new org.omg.Components.BadComponentReference();
285         }
286
287         return mid.component_id;
288     }
289
290     final public org.omg.CORBA.Object JavaDoc
291     create_with_new_target(String JavaDoc repid)
292     throws org.omg.Components.IllegalState
293     {
294         // to know if we're in a callback operation
295
byte[] id = null;
296         try {
297             id = getPOACurrent().get_object_id();
298         } catch (org.omg.PortableServer.CurrentPackage.NoContext JavaDoc exc) {
299             throw new org.omg.Components.IllegalState();
300         }
301
302         // create a ref
303
return _poa_service.create_ref_with_id(id, repid);
304     }
305
306     //
307
// IDL:coach.org/ECM/ECMContext:1.0
308
//
309

310     final public org.omg.CORBA.Object JavaDoc
311     get_service_internal(String JavaDoc sid, String JavaDoc siid)
312     throws org.coach.ECM.UnknownService,
313            org.coach.ECM.UnknownServiceInternal
314     {
315         // find service
316
Service service = _services_set.find_service(sid);
317         if (service==null) {
318             throw new org.coach.ECM.UnknownService();
319         }
320
321         // find internal
322
org.omg.CORBA.Object JavaDoc internal = service.get_service_internal(siid);
323         if (internal==null) {
324             throw new org.coach.ECM.UnknownServiceInternal();
325         }
326
327         return internal;
328     }
329 }
330
Popular Tags