1 23 24 package com.sun.enterprise.deployment.backend; 25 26 import java.io.BufferedInputStream ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.util.Enumeration ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.jar.Attributes ; 36 import java.util.jar.Attributes.Name; 37 import java.util.jar.JarFile ; 38 import java.util.jar.Manifest ; 39 import java.util.List ; 40 import java.util.logging.Level ; 41 import java.util.Properties ; 42 import java.util.Set ; 43 import java.util.Vector ; 44 import java.util.zip.ZipException ; 45 import javax.enterprise.deploy.shared.ModuleType ; 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 66 class ClientJarMakerUtils { 67 68 static void populateStubs(AbstractArchive target, ZipItem[] stubs) 69 throws IOException { 70 Set elements = new HashSet (); 71 for (ZipItem item : stubs) { 72 if (elements.contains(item.getName())) { 73 continue; 74 } 75 elements.add(item.getName()); 76 OutputStream os = null; 77 InputStream is = null; 78 try { 79 os = target.putNextEntry(item.getName()); 80 is = new BufferedInputStream (new FileInputStream (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 { 96 97 String excludeFileName = target.getArchiveUri().substring( 99 target.getArchiveUri().lastIndexOf(File.separatorChar)+1); 100 Set excludeList = new HashSet (); 101 excludeList.add(excludeFileName); 102 103 if (generated != null) { 110 copyArchive(generated, target, excludeList); 111 } 112 113 copyArchive(original, target, excludeList); 115 116 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 { 124 125 AbstractArchive source = (generated == null) ? original : generated; 126 copy(source, target, 128 archivist.getStandardDDFile().getDeploymentDescriptorPath()); 129 copy(source, target, 131 archivist.getConfigurationDDFile().getDeploymentDescriptorPath()); 132 } 133 134 141 static List <String > getLibraryEntries( 142 Application app, AbstractArchive appSource) 143 throws IOException { 144 145 File appArchive = new File (appSource.getArchiveUri()); 146 Vector <String > excludeList = new Vector (); 147 for (Iterator modules = app.getModules(); modules.hasNext();) { 148 ModuleDescriptor md = (ModuleDescriptor) modules.next(); 149 String moduleRoot = appArchive.getAbsolutePath() + File.separator + 150 FileUtils.makeFriendlyFilename(md.getArchiveUri()); 151 excludeList.add(moduleRoot); 152 } 153 154 Vector <String > libraries = new Vector (); 155 File [] files = appArchive.listFiles(); 157 for (File file : files) { 158 if (!file.isDirectory() && file.getName().endsWith(".jar")) { 159 libraries.add(file.getName()); 161 } else if (file.isDirectory()) { 162 if (!isExcluded(file.getAbsolutePath(), excludeList)) { 165 getLibraryEntries( 166 appArchive.getAbsolutePath(),file, libraries); 167 } 168 } else { 169 } 171 } 172 173 if (DeploymentLogger.get().isLoggable(Level.FINEST)) { 174 for (String 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 path, List excludeList) { 183 for (Iterator it = excludeList.iterator(); it.hasNext();) { 184 String exclude = (String ) it.next(); 185 if (path.startsWith(exclude)) { 186 return true; 187 } 188 } 189 return false; 190 } 191 192 private static void getLibraryEntries( 193 String topDir, File directory, List libraries) { 194 File [] files = directory.listFiles(); 195 for (File file : files) { 196 if (!file.isDirectory() && file.getName().endsWith(".jar")) { 197 String 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 } 205 } 206 } 207 208 212 static void copy( 213 AbstractArchive source, AbstractArchive target, String entryName) 214 throws IOException { 215 216 InputStream is=null; 217 OutputStream os=null; 218 try { 219 is = source.getEntry(entryName); 220 if (is != null) { 221 try { 222 os = target.putNextEntry(entryName); 223 } catch(ZipException ze) { 224 return; 226 } 227 ArchivistUtils.copyWithoutClose(is, os); 228 } 229 } catch (IOException ioe) { 230 throw ioe; 231 } finally { 232 IOException closeEntryIOException = null; 233 if (os!=null) { 234 try { 235 target.closeEntry(); 236 } catch (IOException 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 excludeList) { 252 for (Enumeration e = source.entries();e.hasMoreElements();) { 253 String entryName = String .class.cast(e.nextElement()); 254 if (excludeList.contains(entryName)) { 255 continue; 256 } 257 try { 258 copy(source, target, entryName); 259 } catch(IOException ioe) { 260 } 262 } 263 } 264 } 265 | Popular Tags |