KickJava   Java API By Example, From Geeks To Geeks.

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


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>Base implementation of service and component archive servants.</p>
36  **/

37 abstract public class ArchiveServantBase
38 extends org.omg.CORBA.LocalObject JavaDoc
39 {
40     // archive
41
static private String JavaDoc _class_name = "ArchiveServantBase";
42     private String JavaDoc _uuid;
43     private String JavaDoc _archive_location;
44     private String JavaDoc _archive_cache;
45     private java.util.Hashtable JavaDoc _entry2loc;
46
47     protected
48     ArchiveServantBase(String JavaDoc uuid,
49                        String JavaDoc loc)
50     {
51         // archive
52
_uuid = uuid;
53         _archive_location = loc;
54         _archive_cache = null;
55         _entry2loc = new java.util.Hashtable JavaDoc();
56     }
57
58     //
59
// abstract operations
60
//
61

62     abstract protected ORBService
63     getORBService();
64
65     //
66
// internal operations (for inherited classes)
67
//
68

69     final protected java.util.zip.ZipFile JavaDoc
70     openZip()
71     {
72         if (_archive_cache==null) {
73             // compute archive cache directory (for file extraction)
74
int idx = _archive_location.lastIndexOf(".");
75             _archive_cache = _archive_location.substring(0, idx);
76
77             // create archive cache
78
new java.io.File JavaDoc(_archive_cache).mkdirs();
79         }
80
81         try {
82             return new java.util.zip.ZipFile JavaDoc(_archive_location);
83         } catch(java.lang.Exception JavaDoc ex) {
84             // NOTE: TODO: should be reported as an exception
85
final String JavaDoc opname = "openZip";
86             TheLogger.error(_class_name, opname, "FAILED", ex);
87             return null;
88         }
89     }
90
91     final protected String JavaDoc
92     extractFile(java.util.zip.ZipFile JavaDoc zip,
93                 java.util.zip.ZipEntry JavaDoc entry)
94     {
95         final String JavaDoc opname = "extractFile";
96         java.io.InputStream JavaDoc in = null;
97         java.io.FileOutputStream JavaDoc out = null;
98
99         try {
100             in = zip.getInputStream(entry);
101             java.io.File JavaDoc fout = new java.io.File JavaDoc(_archive_cache+"/"+entry.getName());
102             fout.getParentFile().mkdirs();
103             out = new java.io.FileOutputStream JavaDoc(fout);
104         } catch (java.io.IOException JavaDoc ex) {
105             // NOTE: TODO: should be reported as an exception
106
TheLogger.error(_class_name, opname, "FAILED", ex);
107             return null;
108         }
109
110         int nread = 0;
111         byte[] buffer = new byte[2048];
112         try {
113             while (nread!=-1) {
114                 nread = in.read(buffer, 0, 2048);
115                 if (nread!=-1) out.write(buffer, 0, nread);
116             }
117
118             in.close();
119             out.close();
120         } catch (java.io.IOException JavaDoc ex) {
121             // NOTE: TODO: should be reported as an exception
122
final String JavaDoc msg = "FAILED (entry: "+entry.getName()+")";
123             TheLogger.error(_class_name, opname, "FAILED", ex);
124             return null;
125         }
126
127         return _archive_cache+"/"+entry.getName();
128     }
129
130     //
131
// IDL:coach.org/ECM/Archive:1.0
132
//
133

134     final public String JavaDoc
135     get_fileinarchive_location(String JavaDoc name)
136     {
137         final String JavaDoc opname = "get_fileinarchive_location";
138
139         // check if already unzipped
140
String JavaDoc location = (String JavaDoc)_entry2loc.get(name);
141         if (location!=null) {
142             return location;
143         }
144
145         // open zip
146
java.util.zip.ZipFile JavaDoc zip = openZip();
147
148         // extract CSD location
149
java.util.Enumeration JavaDoc entries = zip.entries();
150         java.util.zip.ZipEntry JavaDoc entry = null;
151         java.util.zip.ZipEntry JavaDoc fentry = null;
152         while (entries.hasMoreElements()) {
153             entry = (java.util.zip.ZipEntry JavaDoc)entries.nextElement();
154             if (entry.getName().equals(name)) {
155                 fentry = entry;
156                 break;
157             }
158         }
159
160         // check if found
161
if (fentry==null) {
162             final String JavaDoc msg = "FAILED (entry not found: "+name+")";
163             TheLogger.error(_class_name, opname, msg);
164         }
165
166         // extract file
167
location = extractFile(zip, fentry);
168
169         // store
170
_entry2loc.put(name, location);
171
172         return location;
173     }
174
175     final public String JavaDoc
176     get_href_location(String JavaDoc href)
177     {
178         final String JavaDoc opname = "get_href_location";
179         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
180         TheLogger.debug(_class_name, opname, msg);
181
182         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
183     }
184
185     final public String JavaDoc
186     get_link_location(String JavaDoc link)
187     {
188         final String JavaDoc opname = "get_link_location";
189         final String JavaDoc msg = "FAILED (not implemented, rethrown)";
190         TheLogger.debug(_class_name, opname, msg);
191
192         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
193     }
194
195     final public String JavaDoc
196     get_ins_as_objref(String JavaDoc ins)
197     {
198         RegistrationService regs = org.objectweb.corba.runtime.Runtime.getRegistrationService();
199         INSRegistrationScheme scheme = (INSRegistrationScheme)regs.get_scheme(INSRegistrationScheme.SCHEME_ID);
200
201         org.omg.CORBA.Object JavaDoc ref = scheme.lookup(ins, getORBService());
202         return getORBService().object_to_string(ref);
203     }
204
205     final public String JavaDoc
206     uuid()
207     {
208         return _uuid;
209     }
210 }
211
Popular Tags