KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > runtime > POAFactoryImpl


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.corba.runtime;
31
32 // import of some PortableServer decls to avoid to long lines
33
import org.omg.PortableServer.THREAD_POLICY_ID JavaDoc;
34 import org.omg.PortableServer.ThreadPolicy JavaDoc;
35 import org.omg.PortableServer.ThreadPolicyValue JavaDoc;
36 import org.omg.PortableServer.ThreadPolicyValueHelper;
37 import org.omg.PortableServer.LIFESPAN_POLICY_ID JavaDoc;
38 import org.omg.PortableServer.LifespanPolicy JavaDoc;
39 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
40 import org.omg.PortableServer.LifespanPolicyValueHelper;
41 import org.omg.PortableServer.ID_UNIQUENESS_POLICY_ID JavaDoc;
42 import org.omg.PortableServer.IdUniquenessPolicy JavaDoc;
43 import org.omg.PortableServer.IdUniquenessPolicyValue JavaDoc;
44 import org.omg.PortableServer.IdUniquenessPolicyValueHelper;
45 import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID JavaDoc;
46 import org.omg.PortableServer.IdAssignmentPolicy JavaDoc;
47 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
48 import org.omg.PortableServer.IdAssignmentPolicyValueHelper;
49 import org.omg.PortableServer.IMPLICIT_ACTIVATION_POLICY_ID JavaDoc;
50 import org.omg.PortableServer.ImplicitActivationPolicy JavaDoc;
51 import org.omg.PortableServer.ImplicitActivationPolicyValue JavaDoc;
52 import org.omg.PortableServer.ImplicitActivationPolicyValueHelper;
53 import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID JavaDoc;
54 import org.omg.PortableServer.ServantRetentionPolicy JavaDoc;
55 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc;
56 import org.omg.PortableServer.ServantRetentionPolicyValueHelper;
57 import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID JavaDoc;
58 import org.omg.PortableServer.RequestProcessingPolicy JavaDoc;
59 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
60 import org.omg.PortableServer.RequestProcessingPolicyValueHelper;
61
62 /**
63  ** <p>Default factory implementation for the <tt>POAService</tt> system service.</p>
64  **/

65 public class POAFactoryImpl
66 extends org.omg.CORBA.LocalObject JavaDoc
67 implements SystemFactory
68 {
69     // system factory
70
static private String JavaDoc _class_name = "POAFactoryImpl";
71
72     // default constructor
73
protected
74     POAFactoryImpl()
75     {
76     }
77
78     //
79
// entry point
80
//
81

82     static public SystemFactory
83     create_factory()
84     {
85         return new POAFactoryImpl();
86     }
87
88     //
89
// internal operations
90
//
91

92     static private org.omg.CORBA.Policy JavaDoc[]
93     checkPolicies(org.omg.CORBA.Policy JavaDoc[] pols,
94                   ORBService orbs)
95     {
96         final String JavaDoc opname = "checkPolicies";
97
98         // check that the policy set is ok
99
// and add missing policies
100
// the following policy set must be specified:
101
// - Theading: ORB, SINGLE OR MAIN (if not, then use ORB)
102
// - Lifespan: TRANSIENT or PERSISTENT (if not, then use TRANSIENT)
103
// - IdUniqueness: MULTIPLE_ID or UNIQUE_ID (if not, then use UNIQUE_ID)
104
// - IdAssignment: USER_ID
105
// - ServantRetention: RETAIN
106
// - RequestProcessing: USE_ACTIVE_OBJECT_MAP_ONLY
107
// - ImplicitActivation: NO_IMPLICIT_ACTIVATION
108
java.util.Vector JavaDoc rpols = new java.util.Vector JavaDoc();
109
110         boolean threadingpol_set = false;
111         boolean lifespanpol_set = false;
112         boolean iduniqpol_set = false;
113         boolean idassignpol_set = false;
114         boolean servantretpol_set = false;
115         boolean reqprocpol_set = false;
116         boolean implactpol_set = false;
117         for (int i=0;i<pols.length;i++) {
118             if (pols[i].policy_type()==THREAD_POLICY_ID.value) {
119                 // no check
120
rpols.add(pols[i]);
121                 threadingpol_set = true;
122             }
123             else if (pols[i].policy_type()==LIFESPAN_POLICY_ID.value) {
124                 // no check
125
rpols.add(pols[i]);
126                 lifespanpol_set = true;
127             }
128             else if (pols[i].policy_type()==ID_UNIQUENESS_POLICY_ID.value) {
129                 // no check
130
rpols.add(pols[i]);
131                 iduniqpol_set = true;
132             }
133             else if (pols[i].policy_type()==ID_ASSIGNMENT_POLICY_ID.value) {
134                 // check USER_ID
135
IdAssignmentPolicy JavaDoc idassignpol = (IdAssignmentPolicy JavaDoc)pols[i];
136                 if (idassignpol.value()!=IdAssignmentPolicyValue.USER_ID) {
137                     // NOTE: ignore the policy setting
138
final String JavaDoc msg = "IGNORE (was expecting USER_ID)";
139                     TheLogger.debug(_class_name, opname, msg);
140                 } else {
141                     rpols.add(pols[i]);
142                     idassignpol_set = true;
143                 }
144             }
145             else if (pols[i].policy_type()==IMPLICIT_ACTIVATION_POLICY_ID.value) {
146                 // check NO_IMPLICIT_ACTIVATION
147
ImplicitActivationPolicy JavaDoc implactpol = (ImplicitActivationPolicy JavaDoc)pols[i];
148                 if (implactpol.value()!=ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION) {
149                     // NOTE: ignore the policy setting
150
final String JavaDoc msg = "IGNORE (was expecting NO_IMPLICIT_ACTIVATION)";
151                     TheLogger.debug(_class_name, opname, msg);
152                 } else {
153                     rpols.add(pols[i]);
154                     implactpol_set = true;
155                 }
156             }
157             else if (pols[i].policy_type()==SERVANT_RETENTION_POLICY_ID.value) {
158                 // check RETAIN
159
ServantRetentionPolicy JavaDoc servantretpol = (ServantRetentionPolicy JavaDoc)pols[i];
160                 if (servantretpol.value()!=ServantRetentionPolicyValue.RETAIN) {
161                     // NOTE: ignore the policy setting
162
final String JavaDoc msg = "IGNORE (was expecting RETAIN)";
163                     TheLogger.debug(_class_name, opname, msg);
164                 } else {
165                     rpols.add(pols[i]);
166                     servantretpol_set = true;
167                 }
168             }
169             else if (pols[i].policy_type()==REQUEST_PROCESSING_POLICY_ID.value) {
170                 // check USE_ACTIVE_OBJECT_MAP_ONLY
171
RequestProcessingPolicy JavaDoc reqprocpol = (RequestProcessingPolicy JavaDoc)pols[i];
172                 if (reqprocpol.value()!=RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY) {
173                     // NOTE: ignore the policy setting
174
final String JavaDoc msg = "IGNORE (was expecting USE_ACTIVE_OBJECT_MAP_ONLY)";
175                     TheLogger.debug(_class_name, opname, msg);
176                 } else {
177                     rpols.add(pols[i]);
178                     reqprocpol_set = true;
179                 }
180             }
181             else {
182                 rpols.add(pols[i]);
183             }
184         }
185
186         // complete missing policies
187
org.omg.CORBA.Policy JavaDoc pol = null;
188         org.omg.CORBA.Any JavaDoc value = null;
189         if (!threadingpol_set) {
190             value = orbs.create_any();
191             ThreadPolicyValueHelper.insert(value, ThreadPolicyValue.ORB_CTRL_MODEL);
192             pol = orbs.create_policy(THREAD_POLICY_ID.value, value);
193             rpols.add(pol);
194         }
195
196         if (!lifespanpol_set) {
197             value = orbs.create_any();
198             LifespanPolicyValueHelper.insert(value, LifespanPolicyValue.TRANSIENT);
199             pol = orbs.create_policy(LIFESPAN_POLICY_ID.value, value);
200             rpols.add(pol);
201         }
202
203         if (!iduniqpol_set) {
204             value = orbs.create_any();
205             IdUniquenessPolicyValueHelper.insert(value, IdUniquenessPolicyValue.UNIQUE_ID);
206             pol = orbs.create_policy(ID_UNIQUENESS_POLICY_ID.value, value);
207             rpols.add(pol);
208         }
209
210         if (!idassignpol_set) {
211             value = orbs.create_any();
212             IdAssignmentPolicyValueHelper.insert(value, IdAssignmentPolicyValue.USER_ID);
213             pol = orbs.create_policy(ID_ASSIGNMENT_POLICY_ID.value, value);
214             rpols.add(pol);
215         }
216
217         if (!servantretpol_set) {
218             value = orbs.create_any();
219             ServantRetentionPolicyValueHelper.insert(value, ServantRetentionPolicyValue.RETAIN);
220             pol = orbs.create_policy(SERVANT_RETENTION_POLICY_ID.value, value);
221             rpols.add(pol);
222         }
223
224         if (!reqprocpol_set) {
225             value = orbs.create_any();
226             RequestProcessingPolicyValueHelper.insert(value, RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY);
227             pol = orbs.create_policy(REQUEST_PROCESSING_POLICY_ID.value, value);
228             rpols.add(pol);
229         }
230
231         if (!implactpol_set) {
232             value = orbs.create_any();
233             ImplicitActivationPolicyValueHelper.insert(value, ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION);
234             pol = orbs.create_policy(IMPLICIT_ACTIVATION_POLICY_ID.value, value);
235             rpols.add(pol);
236         }
237
238         return (org.omg.CORBA.Policy JavaDoc[])rpols.toArray(new org.omg.CORBA.Policy JavaDoc[0]);
239     }
240
241     static private org.omg.PortableServer.POA JavaDoc
242     createPOA(POAConfiguration cfg)
243     {
244         //
245
ORBService orbs = cfg.orb_service();
246
247         // obtain root POA
248
org.omg.PortableServer.POA JavaDoc root_poa = null;
249         root_poa = org.omg.PortableServer.POAHelper.narrow(orbs.resolve_initial_references("RootPOA"));
250
251         // create new POA, with servant locator
252
org.omg.PortableServer.POA JavaDoc poa = null;
253         org.omg.CORBA.Policy JavaDoc[] rpols = checkPolicies(cfg.policies(), orbs);
254         String JavaDoc name = "";
255         try {
256             // check name
257
if ((cfg.name()==null) || (cfg.name().equals(""))) {
258                 // use a random number
259
name = Double.toString(Math.random());
260             }
261             else {
262                 name = cfg.name();
263             }
264
265             poa = root_poa.create_POA(name, null, rpols);
266             poa.the_POAManager().activate();
267         } catch (Exception JavaDoc ex) {
268             final String JavaDoc opname = "createPOA";
269             TheLogger.error(_class_name, opname, "FAILED", ex);
270             return null;
271         }
272
273         return poa;
274     }
275
276     //
277
// IDL:objectweb.org/corba/runtime/SystemFactory:1.0
278
//
279

280     final public SystemComponent
281     create_system_component(SystemConfiguration cfg)
282     {
283         POAConfiguration poacfg = (POAConfiguration)cfg;
284
285         // obtain the right POA
286
org.omg.PortableServer.POA JavaDoc poa = createPOA(poacfg);
287
288         // create a new POA service instance
289
POAServiceImpl poas = new POAServiceImpl(poa);
290
291         // complete configuration
292
poas.system_configuration_complete(poacfg);
293
294         return poas;
295     }
296 }
297
Popular Tags