KickJava   Java API By Example, From Geeks To Geeks.

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


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 ComponentArchiveImpl
38 extends ArchiveServantBase
39 implements Servant, org.coach.ECM.ComponentArchiveOperations
40 {
41     // system component
42
static final private String JavaDoc _class_name = "ComponentArchiveImpl";
43
44     // servant
45
static final private String JavaDoc _type_id = org.coach.ECM.ComponentArchiveHelper.id();
46
47     // archive
48
private ORBService _orb_service;
49     private String JavaDoc _uuid;
50     private String JavaDoc _archive_location;
51     private String JavaDoc _dtd_location;
52
53     // component archive
54
private ExtComponentInstallationImpl _extci;
55     private String JavaDoc _code_location;
56     private String JavaDoc _code_entrypoint;
57     private String JavaDoc _csd_location;
58     private String JavaDoc _ccd_location;
59     private String JavaDoc _home_repid;
60     private String JavaDoc _component_repid;
61     private org.coach.ECM.ValuetypeFactoryDependency[] _valuefactories;
62     private java.util.HashMap JavaDoc _sname2policies;
63
64     public
65     ComponentArchiveImpl(ExtComponentInstallationImpl extci,
66                          ORBService orbs,
67                          String JavaDoc dtdloc,
68                          String JavaDoc uuid,
69                          String JavaDoc loc)
70     {
71         super(uuid, loc);
72
73         // archive
74
_orb_service = orbs;
75         _uuid = uuid;
76         _archive_location = loc;
77         _dtd_location = dtdloc;
78
79         // component archive
80
_extci = extci;
81         _code_location = null;
82         _code_entrypoint = null;
83         _csd_location = null;
84         _ccd_location = null;
85         _home_repid = null;
86         _component_repid = null;
87         _valuefactories = new org.coach.ECM.ValuetypeFactoryDependency[0];
88         _sname2policies = new java.util.HashMap JavaDoc();
89     }
90
91     //
92
// abstract operations implementation
93
//
94

95     final protected ORBService
96     getORBService()
97     {
98         return _orb_service;
99     }
100
101     //
102
// internal operations
103
//
104

105     private void
106     initializeInternalFields()
107     {
108         final String JavaDoc opname = "initializeInternalFields";
109         // open zip
110
java.util.zip.ZipFile JavaDoc zip = openZip();
111
112         // extract CSD location
113
java.util.Enumeration JavaDoc entries = zip.entries();
114         java.util.zip.ZipEntry JavaDoc entry = null;
115         java.util.zip.ZipEntry JavaDoc csdentry = null;
116         while (entries.hasMoreElements()) {
117             entry = (java.util.zip.ZipEntry JavaDoc)entries.nextElement();
118             if (entry.getName().endsWith(".csd")) {
119                 csdentry = entry;
120             }
121         }
122
123         // check if CSD found
124
if (csdentry==null) {
125             // NOTE: TODO: should be reported as an exception
126
final String JavaDoc msg = "FAILED (no CSD found in "+_uuid+")";
127             TheLogger.error(_class_name, opname, msg);
128             return ;
129         }
130
131         // copy CSD file locally
132
String JavaDoc csdfname = extractFile(zip, csdentry);
133         _csd_location = csdfname;
134
135         // parse CSD file
136
CSDParser csdparser = CSDParser.parse(_csd_location, _dtd_location, _uuid);
137
138         // obtain code entry point
139
_code_entrypoint = csdparser.code_entrypoint;
140
141         // obtain valuetype factories
142
_valuefactories = (org.coach.ECM.ValuetypeFactoryDependency[])csdparser.valuetype_factories.toArray(new org.coach.ECM.ValuetypeFactoryDependency[0]);
143
144         // extract zip entry using result as entry name
145
entries = zip.entries();
146         java.util.zip.ZipEntry JavaDoc ccdentry = null;
147         java.util.zip.ZipEntry JavaDoc codelocentry = null;
148         String JavaDoc ccdname = csdparser.descriptor_location;
149         String JavaDoc codeloc = csdparser.code_location;
150         while (entries.hasMoreElements()) {
151             entry = (java.util.zip.ZipEntry JavaDoc)entries.nextElement();
152             if (entry.getName().equals(ccdname)) {
153                 ccdentry = entry;
154             }
155             else if (entry.getName().equals(codeloc)) {
156                 codelocentry = entry;
157             }
158         }
159
160         // check if code archive found
161
if (codelocentry==null) {
162             // NOTE: TODO: should be reported as an exception
163
final String JavaDoc msg = "FAILED (no code library found in "+_uuid+")";
164             TheLogger.error(_class_name, opname, msg);
165             return ;
166         }
167
168         // copy code archive locally
169
String JavaDoc codelocfname = extractFile(zip, codelocentry);
170         _code_location = codelocfname;
171
172         // check if CCD found
173
if (ccdentry==null) {
174             // NOTE: TODO: should be reported as an exception
175
final String JavaDoc msg = "FAILED (CCD file not found in "+_uuid+")";
176             TheLogger.error(_class_name, opname, msg);
177             return ;
178         }
179
180         // copy CCD file locally
181
String JavaDoc ccdfname = extractFile(zip, ccdentry);
182         _ccd_location = ccdfname;
183
184         // parse CCD
185
CCDParser ccdparser = CCDParser.parse(_ccd_location, _dtd_location, _extci);
186         _home_repid = ccdparser.home_repid;
187         _component_repid = ccdparser.component_repid;
188         _sname2policies = ccdparser.sname2policies;
189
190         // close CSD
191
// NOTE: how ?
192
// close CCSD
193
// NOTE: how ?
194

195         // close zip
196
try {
197             zip.close();
198         } catch(java.io.IOException JavaDoc ex) {
199             // NOTE: TODO: should be reported as an exception
200
// ignore for the moment
201
TheLogger.error(_class_name, opname, "IGNORE", ex);
202         }
203     }
204
205     private void
206     updatePolicyList(java.util.ArrayList JavaDoc list, org.coach.ECM.CCDPolicyRef[] pols)
207     {
208         // NOTE: doesn't add already existing policies
209
for (int i=0;i<pols.length;i++) {
210             // check if a policy of the same service is already defined
211
org.coach.ECM.CCDPolicyRef pol = null;
212             String JavaDoc sid = pols[i].service_id;
213             boolean found = false;
214             for (int j=0;j<list.size();j++) {
215                 pol = (org.coach.ECM.CCDPolicyRef)list.get(j);
216                 if (sid.equals(pol.service_id)) {
217                     found = true;
218                     break;
219                 }
220
221             }
222
223             if (!found) {
224                 // add policy to the list
225
list.add(pols[i]);
226             }
227         }
228     }
229
230     //
231
// IDL:org/objectweb/corba/runtime/SystemComponent:1.0
232
//
233

234     final public void
235     system_configuration_complete(SystemConfiguration cfg)
236     {
237         // initiliaze some internal fields
238
initializeInternalFields();
239     }
240
241     final public void
242     destroy()
243     {
244         // TODO
245
}
246
247     //
248
// IDL:org/objectweb/corba/runtime/Servant:1.0
249
//
250

251     final public String JavaDoc
252     type_id()
253     {
254         return _type_id;
255     }
256
257     final public org.omg.PortableServer.Servant JavaDoc
258     as_native_servant()
259     {
260         return new org.coach.ECM.ComponentArchivePOATie(this);
261     }
262
263     //
264
// IDL:org/objectweb/ccm/runtime/Archive:1.0
265
//
266

267     // public String get_fileinarchive_location(String name, String link)
268
// public String get_href_location(String href)
269
// public String get_link_location(String link)
270
// public String get_ins_as_objref(String ins)
271
// public String uuid()
272

273     //
274
// IDL:coach.org/ECM/ComponentArchive:1.0
275
//
276

277     final public String JavaDoc
278     csd_location()
279     {
280         return _csd_location;
281     }
282
283     final public String JavaDoc
284     ccd_location()
285     {
286         return _ccd_location;
287     }
288
289     final public String JavaDoc
290     code_location()
291     {
292         return _code_location;
293     }
294
295     final public String JavaDoc
296     code_entrypoint()
297     {
298         return _code_entrypoint;
299     }
300
301     final public String JavaDoc
302     home_repid()
303     {
304         return _home_repid;
305     }
306
307     final public String JavaDoc
308     component_repid()
309     {
310         return _component_repid;
311     }
312
313     final public org.coach.ECM.ValuetypeFactoryDependency[]
314     get_valuetype_factories()
315     {
316         return _valuefactories;
317     }
318
319     final public org.coach.ECM.CCDPolicyRef[]
320     get_policies_for_target(String JavaDoc sname)
321     {
322         final String JavaDoc opname = "get_policies_for_target";
323         String JavaDoc msg = "scoped name: "+sname;
324         TheLogger.debug(_class_name, opname, msg);
325
326         // NOTE: wildcard management: for a scoped name ::a::b, we look for:
327
// - ::a::b
328
// - ::a::*
329
// NOTE: attribute management: for a getter (or setter) attribute ::I::_get_attr, we look for:
330
// - ::I::_get_attr
331
// - ::I::_get_*
332
// - ::I::attr
333
// - ::I::*
334

335         // NOTE2: duplicated policies are removed (ie policies of the same service)
336
// the precedence order is: ::a::b > ::a::*
337
// ::I::_get_attr > ::I::attr > ::I::_get_* > ::I::*
338

339         java.util.ArrayList JavaDoc res = new java.util.ArrayList JavaDoc();
340         java.util.ArrayList JavaDoc starres = new java.util.ArrayList JavaDoc();
341
342         org.coach.ECM.CCDPolicyRef[] prefs = null;
343         int idx = sname.lastIndexOf(':');
344         // name==b or name==_get_attr
345
String JavaDoc name = sname.substring(idx+1, sname.length());
346         // pname==::a or name==::I
347
String JavaDoc pname = sname.substring(0, idx-1);
348
349         // normal case
350
//msg = "normal case: "+sname;
351
//TheLogger.debug(_class_name, "get_policies_for_target", msg);
352
prefs = (org.coach.ECM.CCDPolicyRef[])_sname2policies.get(sname);
353         if (prefs!=null) {
354             res.addAll(java.util.Arrays.asList(prefs));
355         }
356
357         // starname==::a::* or starname==::I::*
358
String JavaDoc starname = pname+"::*";
359
360         //msg = "star case: "+starname;
361
//TheLogger.debug(_class_name, "get_policies_for_target", msg);
362

363         prefs = (org.coach.ECM.CCDPolicyRef[])_sname2policies.get(starname);
364         if (prefs!=null) {
365             starres.addAll(java.util.Arrays.asList(prefs));
366         }
367
368         // check if attribute
369
// quick check to avoid string comparison in most cases
370
if (name.charAt(0)=='_') {
371             if (name.startsWith("_get_")) {
372                 // attrname==::I::attr
373
String JavaDoc attrname = pname+"::"+name.substring(5, name.length());
374
375                 //msg = "attribute case: "+attrname;
376
//TheLogger.debug(_class_name, "get_policies_for_target", msg);
377

378                 prefs = (org.coach.ECM.CCDPolicyRef[])_sname2policies.get(attrname);
379                 if (prefs!=null) {
380                     updatePolicyList(res, prefs);
381                 }
382
383                 // attrstarname==::I::_get_*
384
String JavaDoc attrstarname = pname+"::_get_*";
385
386                 //msg = "attribute star case: "+attrstarname;
387
//TheLogger.debug(_class_name, "get_policies_for_target", msg);
388

389                 prefs = (org.coach.ECM.CCDPolicyRef[])_sname2policies.get(attrstarname);
390                 if (prefs!=null) {
391                     updatePolicyList(starres, prefs);
392                 }
393             }
394             else if (name.startsWith("_set_")) {
395                 // attrname==::I::attr
396
String JavaDoc attrname = pname+"::"+name.substring(5, name.length());
397
398                 //msg = "attribute case: "+attrname;
399
//TheLogger.debug(_class_name, "get_policies_for_target", msg);
400

401                 prefs = (org.coach.ECM.CCDPolicyRef[])_sname2policies.get(attrname);
402                 if (prefs!=null) {
403                     updatePolicyList(res, prefs);
404                 }
405
406                 // attrstarname==::I::_set_*
407
String JavaDoc attrstarname = pname+"::_set_*";
408
409                 //msg = "attribute star case: "+attrstarname;
410
//TheLogger.debug(_class_name, "get_policies_for_target", msg);
411

412                 prefs = (org.coach.ECM.CCDPolicyRef[])_sname2policies.get(attrstarname);
413                 if (prefs!=null) {
414                     updatePolicyList(starres, prefs);
415                 }
416             }
417         }
418
419         // update normal list with star list
420
org.coach.ECM.CCDPolicyRef[] starprefs = (org.coach.ECM.CCDPolicyRef[])starres.toArray(new org.coach.ECM.CCDPolicyRef[0]);
421         updatePolicyList(res, starprefs);
422
423         prefs = (org.coach.ECM.CCDPolicyRef[])res.toArray(new org.coach.ECM.CCDPolicyRef[0]);
424
425         if (prefs.length==0) {
426             TheLogger.debug(_class_name, opname, "no policy found");
427         } else {
428             msg = "#"+prefs.length+" policies found";
429             TheLogger.debug(_class_name, opname, msg);
430             for (int i=0;i<prefs.length;i++) {
431                 msg = "#"+i+": "+prefs[i].service_id+" / "+prefs[i].policyref;
432                 TheLogger.debug(_class_name, opname, msg);
433             }
434         }
435
436         return prefs;
437     }
438
439     final public String JavaDoc
440     exec_xpath_request_in_csd(String JavaDoc req)
441     {
442         final String JavaDoc opname = "exec_xpath_request_in_csd";
443         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
444         TheLogger.debug(_class_name, opname, msg);
445
446         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
447     }
448
449     final public String JavaDoc
450     exec_xpath_request_in_ccd(String JavaDoc req)
451     {
452         final String JavaDoc opname = "exec_xpath_request_in_ccd";
453         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
454         TheLogger.debug(_class_name, opname, msg);
455
456         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
457     }
458 }
459
Popular Tags