KickJava   Java API By Example, From Geeks To Geeks.

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


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  ** <p><i>NOTE: the <tt>archives.autoload</tt> system property may be set with a list of comma
38  ** separated pattern "uuid=location". Each pattern shall correspond to an archive which
39  ** will be installed using the UUID "uuid" and from the location "location".</i></p>
40  **/

41 public class ExtComponentInstallationImpl
42 extends org.omg.CORBA.LocalObject JavaDoc
43 implements Servant, org.coach.ECM.ExtComponentInstallationOperations
44 {
45     // system component
46
static final private String JavaDoc _class_name = "ExtComponentInstallationImpl";
47
48     // servant
49
static final private String JavaDoc _type_id = org.coach.ECM.ExtComponentInstallationHelper.id();
50
51     // component installation
52
private java.util.Hashtable JavaDoc _uuid2location; // (::Components::Deployment::UUID, URL)
53
private String JavaDoc _zip_cache;
54     private int _archive_number;
55     private String JavaDoc _dtd_location;
56
57     // extended component installation
58
private java.util.Hashtable JavaDoc _uuid2sarch; // (::Components::Deployment::UUID, ServiceArchiveServantImpl)
59
private java.util.Hashtable JavaDoc _uuid2carch; // (::Components::Deployment::UUID, ComponentArchiveServantImpl)
60
private POAService _poa_service;
61     private ORBService _orb_service;
62
63     // default constructor
64
protected
65     ExtComponentInstallationImpl()
66     {
67         // component installation
68
_uuid2location = new java.util.Hashtable JavaDoc();
69         _zip_cache = ".";
70         _archive_number = 0;
71         _dtd_location = ".";
72
73         // extended component installation
74
_uuid2sarch = new java.util.Hashtable JavaDoc();
75         _uuid2carch = new java.util.Hashtable JavaDoc();
76         _poa_service = null;
77         _orb_service = null;
78     }
79
80     //
81
// Entry point
82
//
83

84     static public Servant
85     create_servant()
86     {
87         return new ExtComponentInstallationImpl();
88     }
89
90     //
91
// IDL:org/objectweb/corba/runtime/SystemComponent:1.0
92
//
93

94     final public void
95     system_configuration_complete(SystemConfiguration cfg)
96     {
97         final String JavaDoc opname = "system_configuration_complete";
98         // obtain zip cache directory
99
String JavaDoc zc = System.getProperty("zip.cache");
100         if (zc!=null) {
101             _zip_cache = zc;
102         }
103
104         // obtain DTDs location
105
String JavaDoc loc = System.getProperty("dtd.location");
106         if (loc!=null) {
107             _dtd_location = loc;
108         }
109
110         // obtain services
111
ServantConfiguration scfg = (ServantConfiguration)cfg;
112         _poa_service = scfg.poa_service();
113         _orb_service = scfg.orb_service();
114
115         // check if some auto-loaded archives are set
116
String JavaDoc auto = System.getProperty("archives.autoload");
117         if (auto==null) {
118             return ;
119         }
120
121         // break the string
122
java.util.Vector JavaDoc varchives = new java.util.Vector JavaDoc();
123         int idx = auto.indexOf(',');
124         String JavaDoc pattern = "";
125         while (idx!=-1) {
126             pattern = auto.substring(0, idx);
127             if (!pattern.equals("")) {
128                 varchives.add(pattern);
129             }
130
131             auto = auto.substring(idx+1);
132             idx = auto.indexOf(',');
133         }
134
135         varchives.add(auto);
136
137         // install the archives
138
String JavaDoc[] archives = (String JavaDoc[])varchives.toArray(new String JavaDoc[0]);
139         for (int i=0;i<archives.length;i++) {
140             // look for the pattern "<uuid>=<location>"
141
// look for the "="
142
idx = archives[i].indexOf("=");
143
144             try {
145                 String JavaDoc msg = "autoload: "+archives[i].substring(0, idx);
146                 TheLogger.debug(_class_name, opname, msg);
147                 msg = "location: "+archives[i].substring(idx+1);
148                 TheLogger.debug(_class_name, opname, msg);
149
150                 install(archives[i].substring(0, idx), archives[i].substring(idx+1));
151             } catch(Exception JavaDoc ex) {
152                 final String JavaDoc msg = "FAILED (UUID: "+archives[i].substring(0, idx)+")";
153                 TheLogger.error(_class_name, opname, msg, ex);
154             }
155         }
156     }
157
158     final public void
159     destroy()
160     {
161         // TODO
162
}
163
164     //
165
// IDL:org/objectweb/corba/runtime/Servant:1.0
166
//
167

168     final public String JavaDoc
169     type_id()
170     {
171         return _type_id;
172     }
173
174     final public org.omg.PortableServer.Servant JavaDoc
175     as_native_servant()
176     {
177         return new org.coach.ECM.ExtComponentInstallationPOATie(this);
178     }
179
180     //
181
// IDL:coach.org/runtime/deployment/ExtComponentInstallation:1.0
182
//
183

184     final public org.coach.ECM.ServiceArchive
185     get_service_archive(String JavaDoc uuid)
186     {
187         final String JavaDoc opname = "get_service_archive";
188         // NOTE: to avoid complex implementation of the ServiceArchive interface,
189
// the same object instance is returned for a given uuid
190
// That way, we don't have to take care about local file management problem
191
// (in particular, if many instances share the same archive, then files may be
192
// extracted twice or more, ...)
193

194         // NOTE2: currently, the service archive is a servant, but it is not really necessary
195
// Maybe, it could be a simple object
196

197         // check if an instance is already ready for this uuid
198
ServiceArchiveImpl sarch = (ServiceArchiveImpl)_uuid2sarch.get(uuid);
199
200         if (sarch==null) {
201             String JavaDoc archloc = (String JavaDoc)_uuid2location.get(uuid);
202
203             // check if uuid is known
204
if (archloc==null) {
205                 // NOTE: TODO: should throw an exception instead
206
final String JavaDoc msg = "FAILED (unknown uuid: "+uuid+")";
207                 TheLogger.error(_class_name, opname, msg);
208             }
209
210             sarch = new ServiceArchiveImpl(_orb_service, _dtd_location, uuid, archloc);
211             sarch.system_configuration_complete(null);
212
213             // store
214
_uuid2sarch.put(uuid, sarch);
215
216             // activate
217
// use UUID as oid
218
_poa_service.activate_servant_with_id(sarch, uuid.getBytes());
219         }
220
221         //
222
org.omg.CORBA.Object JavaDoc obj = _poa_service.create_ref_with_id(uuid.getBytes(), sarch.type_id());
223         return org.coach.ECM.ServiceArchiveHelper.narrow(obj);
224     }
225
226     final public org.coach.ECM.ComponentArchive
227     get_component_archive(String JavaDoc uuid)
228     {
229         final String JavaDoc opname = "get_component_archive";
230         // NOTE: to avoid complex implementation of the ComponentArchive interface,
231
// the same object instance is returned for a given uuid
232
// That way, we don't have to take care about local file management problem
233
// (in particular, if many instances share the same archive, then files may be
234
// extracted twice or more, ...)
235

236         // NOTE2: currently, the component archie is a servant, but it is not really necessary
237
// Maybe, it could be a simple object
238

239         // check if an instance is already ready for this uuid
240
ComponentArchiveImpl carch = (ComponentArchiveImpl)_uuid2carch.get(uuid);
241
242         if (carch==null) {
243             String JavaDoc archloc = (String JavaDoc)_uuid2location.get(uuid);
244
245             // check if uuid is known
246
if (archloc==null) {
247                 // NOTE: TODO: should throw an exception instead
248
final String JavaDoc msg = "FAILED (unknown uuid: "+uuid+")";
249                 TheLogger.error(_class_name, opname, msg);
250             }
251
252             carch = new ComponentArchiveImpl(this, _orb_service, _dtd_location, uuid, archloc);
253             carch.system_configuration_complete(null);
254
255             // store
256
_uuid2carch.put(uuid, carch);
257
258             // activate
259
// use UUID as oid
260
_poa_service.activate_servant_with_id(carch, uuid.getBytes());
261         }
262
263         //
264
org.omg.CORBA.Object JavaDoc obj = _poa_service.create_ref_with_id(uuid.getBytes(), carch.type_id());
265         return org.coach.ECM.ComponentArchiveHelper.narrow(obj);
266     }
267
268     //
269
// IDL:omg.org/Components/Deployment/ComponentInstallation:1.0
270
//
271

272     final public void
273     install(String JavaDoc uuid, String JavaDoc location)
274     throws org.omg.Components.Deployment.InvalidLocation,
275            org.omg.Components.Deployment.InstallationFailure
276     {
277         final String JavaDoc opname = "install";
278         // try to open as local file
279
java.io.InputStream JavaDoc in = null;
280         try {
281             in = new java.io.FileInputStream JavaDoc(location);
282         } catch (java.io.FileNotFoundException JavaDoc ex) {
283             // ignore
284
}
285
286         if (in==null) {
287             // try to open as URL
288
java.net.URL JavaDoc url = null;
289             try {
290                 url = new java.net.URL JavaDoc(location);
291                 in = url.openStream();
292             } catch (Exception JavaDoc ex) {
293                 // ignore
294
}
295         }
296
297         if (in==null) {
298             final String JavaDoc msg = "FAILED (invalid format, rethrown)";
299             TheLogger.debug(_class_name, opname, msg);
300             throw new org.omg.Components.Deployment.InvalidLocation();
301         }
302
303         // create local file
304
java.io.FileOutputStream JavaDoc out = null;
305         String JavaDoc fname = _zip_cache
306             + "/archive_"
307             + java.lang.Integer.toString(_archive_number)
308             + ".zip";
309
310         try {
311             out = new java.io.FileOutputStream JavaDoc(fname);
312         } catch (java.io.FileNotFoundException JavaDoc ex) {
313             final String JavaDoc msg = "FAILED (file not found, rethrown)";
314             TheLogger.debug(_class_name, opname, msg, ex);
315             throw new org.omg.Components.Deployment.InvalidLocation();
316         }
317
318         int nread = 0;
319         byte[] buffer = new byte[2048];
320         try {
321             while (nread!=-1) {
322                 nread = in.read(buffer, 0, 2048);
323                 if (nread!=-1) out.write(buffer, 0, nread);
324             }
325
326             in.close();
327             out.close();
328         } catch (java.io.IOException JavaDoc ex) {
329             final String JavaDoc msg = "FAILED (download aborted, rethrown)";
330             TheLogger.debug(_class_name, opname, msg, ex);
331             throw new org.omg.Components.Deployment.InstallationFailure();
332         }
333
334         // store location with uuid and increase the archive number
335
_uuid2location.put(uuid, fname);
336         _archive_number++;
337     }
338
339     final public void
340     replace(String JavaDoc uuid, String JavaDoc location)
341     throws org.omg.Components.Deployment.InvalidLocation,
342            org.omg.Components.Deployment.InstallationFailure
343     {
344         final String JavaDoc opname = "replace";
345         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
346         TheLogger.debug(_class_name, opname, msg);
347
348         // TODO: implement
349
throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
350     }
351
352     final public void
353     remove(String JavaDoc uuid)
354     throws org.omg.Components.Deployment.UnknownImplId,
355            org.omg.Components.RemoveFailure
356     {
357         final String JavaDoc opname = "remove";
358         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
359         TheLogger.debug(_class_name, opname, msg);
360
361         // TODO: implement
362
throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
363     }
364
365     final public String JavaDoc
366     get_implementation(String JavaDoc uuid)
367     throws org.omg.Components.Deployment.UnknownImplId,
368            org.omg.Components.Deployment.InstallationFailure
369     {
370         final String JavaDoc opname = "get_implementation";
371         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
372         TheLogger.debug(_class_name, opname, msg);
373
374         // TODO: implement
375
throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
376     }
377 }
378
Popular Tags