KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > WSDeployableObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.websphere6;
20
21 import java.io.*;
22 import java.util.*;
23 import java.lang.reflect.*;
24 import java.util.jar.JarEntry JavaDoc;
25 import java.util.jar.JarFile JavaDoc;
26 import java.util.jar.JarInputStream JavaDoc;
27
28 import javax.enterprise.deploy.model.*;
29 import javax.enterprise.deploy.model.exceptions.*;
30 import javax.enterprise.deploy.shared.*;
31
32 import org.openide.*;
33 import org.netbeans.modules.j2ee.deployment.devmodules.spi.*;
34
35 import org.netbeans.modules.j2ee.websphere6.util.WSDebug;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38
39 /**
40  * Wrapper for j2eeserver's <code>DeployableObject</code> implementation.
41  * This class serves as a wrapper for j2eeserver's <code>DeployableObject</code>
42  * implementation that fixes its incompartibility with the JSR-88 specification.
43  * The j2eeserver's implementation does not implement the
44  * <code>getEntry()</code> and <code>entries()</code> methods, while these are
45  * used intesively by the servers' classes.
46  *
47  * @author Kirill Sorokin
48  */

49 public class WSDeployableObject implements DeployableObject {
50     
51     /**
52      * The original j2eeserver's <code>DeployableObject</code> implementation,
53      * all operations except <code>getEntry()</code> and <code>entries</code>
54      * are delegated to it
55      */

56     DeployableObject deployableObject;
57     
58     /**
59      * The module provider that we fetch from the deployable object in order to
60      * implement the <code>getEntry()</code> and <code>entries</code> methods
61      */

62     J2eeModuleProvider provider;
63     
64     /**
65      * Map of <code>DDBeanRoot</code>s associated with this deployable object
66      */

67     Map ddBeanRoots = new HashMap();
68     
69     /**
70      * Creates a new instance of <code>WSDeployableObject</code>.
71      *
72      * @param deployableObject the original j2eeserver's
73      * <code>DeployableObject</code> implementation
74      */

75     public WSDeployableObject(DeployableObject deployableObject) {
76         if (WSDebug.isEnabled()) // debug output
77
WSDebug.notify(getClass(), "WSDeployableObject(" + // NOI18N
78
deployableObject + ")"); // NOI18N
79

80         // save the supplied deployable object
81
this.deployableObject = deployableObject;
82         
83         // fetch the J2eeModuleProvider handle
84
try {
85             Method method = deployableObject.getClass().
86                     getMethod("getProvider", new Class JavaDoc[0]); // NOI18N
87
this.provider = (J2eeModuleProvider) method.
88                     invoke(deployableObject, new Object JavaDoc[0]);
89         } catch (IllegalAccessException JavaDoc e) {
90             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
91         } catch (NoSuchMethodException JavaDoc e) {
92             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
93         } catch (InvocationTargetException e) {
94             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
95         }
96     }
97     
98     /**
99      * Finds a plugin's wrapper for the supplied j2eeserver's
100      * <code>DDBEanRoot</code> implemetation.
101      *
102      * @param bean j2eeserver's implementation that is used as a key during
103      * search
104      */

105     public DDBeanRoot findDDBeanRoot(DDBeanRoot bean) {
106         // get all registered wrappers
107
Collection values = ddBeanRoots.values();
108         
109         // iterate over the collection and check whether any of them is the
110
// wrapper for the supplied key
111
for (Iterator iterator = values.iterator(); iterator.hasNext();) {
112             WSDDBeanRoot wsBean = (WSDDBeanRoot) iterator.next();
113             
114             // if the wrapper's origin is the key - return it
115
if (wsBean.getOrigin().equals(bean)) {
116                 return wsBean;
117             }
118         }
119         
120         // if no wrappers found return null
121
return null;
122     }
123     
124     /**
125      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
126      * implementation object. Returns a wrapper for the result value.
127      */

128     public DDBeanRoot getDDBeanRoot(String JavaDoc str) throws FileNotFoundException,
129             DDBeanCreateException {
130         if (WSDebug.isEnabled()) // debug output
131
WSDebug.notify(getClass(), "getDDBeanRoot(" + str + ")"); // NOI18N
132

133         // if there is no such wrapper registered already - create a new one
134
// basing on the return value from the j2eeserver's deployable object
135
if (ddBeanRoots.get(str) == null) {
136             ddBeanRoots.put(str, new WSDDBeanRoot(deployableObject.
137                     getDDBeanRoot(str), this));
138         }
139         
140         // return the registered wrapper for this string
141
return (DDBeanRoot) ddBeanRoots.get(str);
142     }
143     
144     /**
145      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
146      * implementation object. Returns a wrapper for the result value.
147      */

148     public DDBeanRoot getDDBeanRoot() {
149         if (WSDebug.isEnabled()) // debug output
150
WSDebug.notify(getClass(), "getDDBeanRoot()"); // NOI18N
151

152         // if there is no wrapper for the blank string registered already -
153
// create a new one basing on the return value from the j2eeserver's
154
// deployable object
155
if (ddBeanRoots.get("") == null) { // NOI18N
156
ddBeanRoots.put("", new WSDDBeanRoot(deployableObject. // NOI18N
157
getDDBeanRoot(), this));
158         }
159         
160         // return the registered wrapper for the blank string
161
return (DDBeanRoot) ddBeanRoots.get(""); // NOI18N
162
}
163
164     /**
165      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
166      * implementation object.
167      */

168     public String JavaDoc[] getText(String JavaDoc str) {
169         if (WSDebug.isEnabled()) // debug output
170
WSDebug.notify(getClass(), "getText(" + str + ")"); // NOI18N
171

172         return deployableObject.getText(str);
173     }
174     
175     /**
176      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
177      * implementation object.
178      */

179     public Class JavaDoc getClassFromScope(String JavaDoc str) {
180         if (WSDebug.isEnabled()) // debug output
181
WSDebug.notify(getClass(), "getClassFromScope(" + str + // NOI18N
182
")"); // NOI18N
183

184         return deployableObject.getClassFromScope(str);
185     }
186     
187     /**
188      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
189      * implementation object.
190      */

191     public DDBean[] getChildBean(String JavaDoc str) {
192         if (WSDebug.isEnabled()) // debug output
193
WSDebug.notify(getClass(), "getChildBean(" + str + ")"); // NOI18N
194

195         return deployableObject.getChildBean(str);
196     }
197     
198     /**
199      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
200      * implementation object.
201      */

202     public ModuleType getType() {
203         if (WSDebug.isEnabled()) // debug output
204
WSDebug.notify(getClass(), "getType()"); // NOI18N
205

206         return deployableObject.getType();
207     }
208     
209     /**
210      * Delegates the call to the j2eeserver's <code>DeployableObject</code>
211      * implementation object.
212      */

213     public String JavaDoc getModuleDTDVersion() {
214         if (WSDebug.isEnabled()) // debug output
215
WSDebug.notify(getClass(), "getModuleDTDVersion()"); // NOI18N
216

217         return deployableObject.getDDBeanRoot().getDDBeanRootVersion();
218     }
219
220     /**
221      * Returns an <code>Enumeration</code> that contains all the source files
222      * that are be included in the deployable object. While not being
223      * completely to the spec which requires the compiled files to be included
224      * here, it solves the issue of the method not being implemented, as the
225      * servers are most interested in configuration files which are not affected
226      * by compilation.
227      *
228      * @return an <code>Enumeartion</code> with all the source files of the
229      * project
230      */

231     public Enumeration entries() {
232         if (WSDebug.isEnabled()) // debug output
233
WSDebug.notify(getClass(), "entries()"); // NOI18N
234

235         // return a new Enumeration built by WSEntries object
236
return new WSEntries(provider).getEntries();
237     }
238     
239     /**
240      * Returns an open <code>InputStream</code> to a named deployable object
241      * entry.
242      *
243      * @param str the name of the entry
244      * @return an <code>InputStream</code> to the named entry, null if the
245      * entry cannot be found
246      */

247     public InputStream JavaDoc getEntry(String JavaDoc str) {
248         if (WSDebug.isEnabled()) // debug output
249
WSDebug.notify(getClass(), "getEntry(" + str + ")"); // NOI18N
250

251         // construct a new WSEntries object and request the entry
252
InputStream JavaDoc in = new WSEntries(provider).getEntry(str);
253         
254         // return the input stream
255
return in;
256     }
257     
258     /**
259      * Returns a File handle for the named deployment object entry.
260      *
261      * @param str the name of the entry
262      * @return a File handle for the named entry, null if the entry cannot be
263      * found
264      */

265     public File JavaDoc getEntryFile(String JavaDoc str) {
266         if (WSDebug.isEnabled()) // debug output
267
WSDebug.notify(getClass(), "getEntryFile(" + str + ")"); // NOI18N
268

269         // construct a new WSEntries object and request the entry
270
for (FileObject fo: provider.getSourceRoots()) {
271             File JavaDoc file = new File JavaDoc(FileUtil.toFile(fo), str);
272             
273             if (file.exists()) {
274                 if (WSDebug.isEnabled()) // debug output
275
WSDebug.notify(getClass(), " -- " + file); // NOI18N
276

277                 return file;
278             }
279         }
280         
281         if (WSDebug.isEnabled()) // debug output
282
WSDebug.notify(getClass(), " -- null"); // NOI18N
283
return null;
284     }
285     
286     /**
287      * A class that is responsible for performing all operation on deployable
288      * object entries, i.e. returning an <code>Enumeration</code> with all the
289      * files of the project and returning a named entry.
290      *
291      * @author Kirill Sorokin
292      */

293     private static class WSEntries {
294         
295         /**
296          * Handle for the <code>J2eeModuleProvider</code> of the parent
297          * deployable object
298          */

299         private J2eeModuleProvider provider;
300         
301         /**
302          * Creates a new instance of <code>WSEntries</code>.
303          *
304          * @param provider the J2eeModuleProvider of the parent deployable
305          * object
306          */

307         public WSEntries (J2eeModuleProvider provider) {
308             this.provider = provider;
309         }
310         
311         /**
312          * Returns an input stream to the named entry
313          *
314          * @param str entry the entry name
315          * @return an InputStream to the entry's file
316          */

317         public InputStream JavaDoc getEntry(String JavaDoc entry) {
318             JarFile JavaDoc jar = null;
319             try {
320                 File JavaDoc archive = FileUtil.toFile(provider.getJ2eeModule().getArchive());
321                 if (WSDebug.isEnabled())
322                     WSDebug.notify(WSEntries.class, archive.getPath());
323                 
324                 jar = new JarFile JavaDoc(archive);
325                 
326                 return jar.getInputStream(jar.getEntry(entry));
327             } catch (IOException e) {
328                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
329             }
330 // finally {
331
// if (jar != null) {
332
// try {
333
// jar.close();
334
// } catch (IOException e) {
335
// ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
336
// }
337
// }
338
// }
339

340             return null;
341         }
342         
343         /**
344          * Returns an Enumeration with all the entries of the parent deployable
345          * object
346          *
347          * @return Enumeration with the entries
348          */

349         public Enumeration getEntries() {
350             Vector entries = new Vector();
351             
352             JarFile JavaDoc jar = null;
353             try {
354                 File JavaDoc archive = FileUtil.toFile(provider.getJ2eeModule().getArchive());
355                 
356                 if (WSDebug.isEnabled())
357                     WSDebug.notify(WSEntries.class, archive.getPath());
358                 
359                 jar = new JarFile JavaDoc(archive);
360                 Enumeration<JarEntry JavaDoc> jarEntries = jar.entries();
361                 
362                 while (jarEntries.hasMoreElements()) {
363                     String JavaDoc name = jarEntries.nextElement().getName();
364                     
365                     if (!name.endsWith("/")) {
366                         if (WSDebug.isEnabled())
367                             WSDebug.notify(WSEntries.class, " -- " + name);
368                         entries.add(name);
369                     }
370                 }
371                 
372                 return entries.elements();
373             } catch (Exception JavaDoc ex) {
374                 WSDebug.notify(ex);
375             } finally {
376                 if (jar != null) {
377                     try {
378                         jar.close();
379                     } catch (IOException e) {
380                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
381                     }
382                 }
383             }
384             
385             return null;
386         }
387     }
388 }
Popular Tags