KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > ClientJarMakerUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.backend;
25
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.jar.Attributes JavaDoc;
36 import java.util.jar.Attributes.Name;
37 import java.util.jar.JarFile JavaDoc;
38 import java.util.jar.Manifest JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.Properties JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.Vector JavaDoc;
44 import java.util.zip.ZipException JavaDoc;
45 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
46
47 import com.sun.enterprise.deployment.Application;
48 import com.sun.enterprise.deployment.archivist.AppClientArchivist;
49 import com.sun.enterprise.deployment.archivist.Archivist;
50 import com.sun.enterprise.deployment.archivist.ArchivistFactory;
51 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
52 import com.sun.enterprise.deployment.interfaces.ClientJarMaker;
53 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
54 import com.sun.enterprise.deployment.util.ModuleDescriptor;
55 import com.sun.enterprise.util.io.FileUtils;
56 import com.sun.enterprise.util.shared.ArchivistUtils;
57 import com.sun.enterprise.util.zip.ZipItem;
58
59 /**
60  * This class is responsible for creating an appclient jar file that
61  * will be used by the appclient container to run the appclients for
62  * the deployed application.
63  *
64  * @author deployment dev team
65  */

66 class ClientJarMakerUtils {
67
68     static void populateStubs(AbstractArchive target, ZipItem[] stubs)
69         throws IOException JavaDoc {
70         Set JavaDoc elements = new HashSet JavaDoc();
71         for (ZipItem item : stubs) {
72             if (elements.contains(item.getName())) {
73                 continue;
74             }
75             elements.add(item.getName());
76             OutputStream JavaDoc os = null;
77             InputStream JavaDoc is = null;
78             try {
79                 os = target.putNextEntry(item.getName());
80                 is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(item.getFile()));
81                 ArchivistUtils.copyWithoutClose(is, os);
82             } finally {
83                 if (is != null) {
84                     is.close();
85                 }
86                 if (os != null) {
87                     target.closeEntry();
88                 }
89             }
90         }
91     }
92
93     static void populateModuleJar(AbstractArchive original,
94             AbstractArchive generated, AbstractArchive target)
95             throws IOException JavaDoc {
96
97         // Exclude the client jar file we are trying to generate
98
String JavaDoc excludeFileName = target.getArchiveUri().substring(
99             target.getArchiveUri().lastIndexOf(File.separatorChar)+1);
100         Set JavaDoc excludeList = new HashSet JavaDoc();
101         excludeList.add(excludeFileName);
102
103         // Copy all the generated content first. Note that this
104
// needs to be done before copying the original content
105
// as any duplicates are otherwise ignored.
106
// Also the ClientJarMaker does not have special knowledge
107
// on what's modified. that burden is on the deployment process to
108
// make sure all generated content are available in the generated/xml
109
if (generated != null) {
110             copyArchive(generated, target, excludeList);
111         }
112
113         // preserve all entries in the original appclient jar
114
copyArchive(original, target, excludeList);
115
116         // copy manifest file since it does not appear in the list of files
117
copy(original, target, JarFile.MANIFEST_NAME);
118     }
119
120     static void copyDeploymentDescriptors(
121         Archivist archivist, AbstractArchive original,
122         AbstractArchive generated, AbstractArchive target)
123         throws IOException JavaDoc {
124
125         AbstractArchive source = (generated == null) ? original : generated;
126         //standard dd
127
copy(source, target,
128              archivist.getStandardDDFile().getDeploymentDescriptorPath());
129         //runtime dd
130
copy(source, target,
131              archivist.getConfigurationDDFile().getDeploymentDescriptorPath());
132     }
133
134     /**
135      * This method finds all the library jars needed by the appclient.
136      * Note that instead of inspecting the relevant manifest files and retrieve
137      * the jars recursively, we will include all jar files found in the module
138      * sans anything that is included in non-ear Java EE module. This is done
139      * to avoid slowing down deployment performance.
140      */

141     static List JavaDoc<String JavaDoc> getLibraryEntries(
142         Application app, AbstractArchive appSource)
143             throws IOException JavaDoc {
144
145         File JavaDoc appArchive = new File JavaDoc(appSource.getArchiveUri());
146         Vector JavaDoc<String JavaDoc> excludeList = new Vector JavaDoc();
147         for (Iterator JavaDoc modules = app.getModules(); modules.hasNext();) {
148             ModuleDescriptor md = (ModuleDescriptor) modules.next();
149             String JavaDoc moduleRoot = appArchive.getAbsolutePath() + File.separator +
150                             FileUtils.makeFriendlyFilename(md.getArchiveUri());
151             excludeList.add(moduleRoot);
152         }
153     
154         Vector JavaDoc<String JavaDoc> libraries = new Vector JavaDoc();
155         //start with the top level directory.
156
File JavaDoc[] files = appArchive.listFiles();
157         for (File JavaDoc file : files) {
158             if (!file.isDirectory() && file.getName().endsWith(".jar")) {
159                 //for backward compatibility, keep all top level jars
160
libraries.add(file.getName());
161             } else if (file.isDirectory()) {
162                 //exlude any sub-module directory (so we are not including
163
//any jar files from the web module)
164
if (!isExcluded(file.getAbsolutePath(), excludeList)) {
165                     getLibraryEntries(
166                         appArchive.getAbsolutePath(),file, libraries);
167                 }
168             } else {
169                 //ignore the rest
170
}
171         }
172         
173         if (DeploymentLogger.get().isLoggable(Level.FINEST)) {
174             for (String JavaDoc lib : libraries) {
175                 DeploymentLogger.get().fine(
176                     "Adding to the appclient jar, library [" + lib + "]");
177             }
178         }
179         return libraries;
180     }
181
182     private static boolean isExcluded(String JavaDoc path, List JavaDoc excludeList) {
183         for (Iterator JavaDoc it = excludeList.iterator(); it.hasNext();) {
184             String JavaDoc exclude = (String JavaDoc) it.next();
185             if (path.startsWith(exclude)) {
186                 return true;
187             }
188         }
189         return false;
190     }
191
192     private static void getLibraryEntries(
193         String JavaDoc topDir, File JavaDoc directory, List JavaDoc libraries) {
194         File JavaDoc[] files = directory.listFiles();
195         for (File JavaDoc file : files) {
196             if (!file.isDirectory() && file.getName().endsWith(".jar")) {
197                 String JavaDoc entryName =
198                     file.getAbsolutePath().substring(topDir.length()+1);
199                 libraries.add(entryName);
200             } else if (file.isDirectory()) {
201                 getLibraryEntries(topDir, file, libraries);
202             } else {
203                 //ignore the test
204
}
205         }
206     }
207
208     /**
209      * copy the entryName element from the source abstract archive into
210      * the target abstract archive
211      */

212     static void copy(
213         AbstractArchive source, AbstractArchive target, String JavaDoc entryName)
214         throws IOException JavaDoc {
215             
216         InputStream JavaDoc is=null;
217         OutputStream JavaDoc os=null;
218         try {
219             is = source.getEntry(entryName);
220             if (is != null) {
221                 try {
222                     os = target.putNextEntry(entryName);
223                 } catch(ZipException JavaDoc ze) {
224                     // this is a duplicate...
225
return;
226                 }
227                 ArchivistUtils.copyWithoutClose(is, os);
228             }
229         } catch (IOException JavaDoc ioe) {
230             throw ioe;
231         } finally {
232             IOException JavaDoc closeEntryIOException = null;
233             if (os!=null) {
234                 try {
235                     target.closeEntry();
236                 } catch (IOException JavaDoc ioe) {
237                     closeEntryIOException = ioe;
238                 }
239             }
240             if (is!=null) {
241                 is.close();
242             }
243  
244             if (closeEntryIOException != null) {
245                 throw closeEntryIOException;
246             }
247         }
248     }
249
250     private static void copyArchive (
251         AbstractArchive source, AbstractArchive target, Set JavaDoc excludeList) {
252         for (Enumeration JavaDoc e = source.entries();e.hasMoreElements();) {
253             String JavaDoc entryName = String JavaDoc.class.cast(e.nextElement());
254             if (excludeList.contains(entryName)) {
255                 continue;
256             }
257             try {
258                 copy(source, target, entryName);
259             } catch(IOException JavaDoc ioe) {
260                 // duplicate, we ignore
261
}
262         }
263     }
264 }
265
Popular Tags