1 23 24 package com.sun.enterprise.deployment.backend; 25 26 import java.io.IOException ; 27 import java.io.*; 28 import java.util.*; 29 import java.util.jar.*; 30 import java.nio.*; 31 import java.nio.channels.*; 32 33 import com.sun.enterprise.config.ConfigContext; 34 import com.sun.enterprise.config.ConfigException; 35 import com.sun.enterprise.config.serverbeans.DasConfig; 36 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 37 import com.sun.enterprise.deployment.Application; 38 import com.sun.enterprise.deployment.archivist.ApplicationArchivist; 39 import com.sun.enterprise.deployment.archivist.Archivist; 40 import com.sun.enterprise.deployment.archivist.ArchivistFactory; 41 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 42 import com.sun.enterprise.deployment.deploy.shared.AbstractArchiveFactory; 43 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 44 import com.sun.enterprise.deployment.deploy.shared.FileArchiveFactory; 45 import com.sun.enterprise.deployment.deploy.shared.InputJarArchive; 46 import com.sun.enterprise.deployment.deploy.shared.JarArchiveFactory; 47 import com.sun.enterprise.deployment.io.DeploymentDescriptorFile; 48 import com.sun.enterprise.deployment.util.ModuleDescriptor; 49 import com.sun.enterprise.server.ApplicationServer; 50 import com.sun.enterprise.util.i18n.StringManager; 51 import com.sun.enterprise.util.io.FileUtils; 52 import com.sun.enterprise.util.OS; 53 import com.sun.enterprise.util.zip.ZipFile; 54 import com.sun.enterprise.util.zip.ZipFileException; 55 56 import java.io.File ; 57 import java.io.IOException ; 58 import java.util.*; 59 import java.util.logging.Level ; 60 import java.util.logging.Logger ; 61 import java.util.zip.ZipException ; 62 import javax.enterprise.deploy.shared.ModuleType ; 63 64 65 72 73 public class J2EEModuleExploder { 74 75 private static StringManager localStrings = 76 StringManager.getManager( J2EEModuleExploder.class ); 77 78 private static Logger logger = null; 79 80 private static final String PRESERVED_MANIFEST_NAME = java.util.jar.JarFile.MANIFEST_NAME + ".preserved"; 81 82 private static final String WEB_INF_PREFIX = "WEB-INF/"; 83 84 private static String validationLevel = null; 85 86 97 public static void explode(File archive, File directory, String moduleName) 98 throws IOException , IASDeploymentException { 99 explode(archive, directory, moduleName, false); 100 } 101 102 109 public static void explode(File archive, File directory, String moduleName, boolean preserveManifest) 110 throws IOException , IASDeploymentException { 111 assert archive != null; 112 113 114 AbstractArchiveFactory factory = null; 115 if (archive.isDirectory()) { 116 factory = new FileArchiveFactory(); 117 } else { 118 factory = new JarArchiveFactory(); 119 } 120 AbstractArchive source = factory.openArchive(archive.getAbsolutePath()); 121 122 Archivist archivist = null; 124 try { 125 archivist = ArchivistFactory.getArchivistForArchive(source); 126 if (archivist == null) { 127 String msg = localStrings.getString 128 ("enterprise.deployment.backend.no_archivist_recognized_arch", 129 archive.getAbsolutePath() 130 ); 131 throw new IASDeploymentException(msg); 132 } 133 } catch (IOException ioe) { 134 String msg = localStrings.getString 135 ("enterprise.deployment.backend.error_getting_archivist", 136 archive.getAbsolutePath() 137 ); 138 throw new IASDeploymentException(msg, ioe); 139 } 140 if (!archivist.getModuleType().equals(ModuleType.EAR)) { 141 explodeModule(archivist, source, directory, moduleName, preserveManifest); 142 } 143 } 144 145 private static void explodeModule(Archivist archivist, AbstractArchive source, File directory, String moduleName, boolean preserveManifest) 146 throws IOException , IASDeploymentException { 147 148 File explodedManifest = null; 149 File preservedManifestFromArchive = null; 150 151 FileArchive target = new FileArchive(); 152 target.create(directory.getAbsolutePath()); 153 154 explodeJar(new File(source.getArchiveUri()), directory); 155 157 if (preserveManifest) { 158 explodedManifest = new File(directory, java.util.jar.JarFile.MANIFEST_NAME); 159 if (explodedManifest.exists()) { 160 161 preservedManifestFromArchive = new File(directory, PRESERVED_MANIFEST_NAME); 162 try { 163 if (OS.isWindows()) { 164 FileUtils.validateWindowsFilePathLength(preservedManifestFromArchive); 165 } 166 } catch (IOException ioe) { 167 IOException newIOE = new IOException (localStrings.getString( 168 "enterprise.deployment.backend.error_saving_manifest", 169 new Object [] 170 { explodedManifest.getAbsolutePath(), 171 preservedManifestFromArchive.getAbsolutePath() 172 } 173 )); 174 newIOE.initCause(ioe); 175 throw newIOE; 176 } 177 if ( ! explodedManifest.renameTo(preservedManifestFromArchive)) { 178 throw new RuntimeException (localStrings.getString( 179 "enterprise.deployment.backend.error_saving_manifest", 180 new Object [] 181 { explodedManifest.getAbsolutePath(), 182 preservedManifestFromArchive.getAbsolutePath() 183 } ) ) ; 184 } 185 } 186 } 187 for (Enumeration itr = source.entries();itr.hasMoreElements();) { 191 String fileName = (String ) itr.nextElement(); 192 193 194 198 201 if (fileName.toLowerCase().endsWith(".jar") && ( ! fileName.replace('\\', '/').toUpperCase().startsWith(WEB_INF_PREFIX)) ) { 202 203 try { 204 File f = new File(directory, fileName); 205 206 File targetDirectory = directory; 207 208 ZipFile zip = new ZipFile(f, targetDirectory); 209 zip.explode(); 210 } catch(ZipFileException e) { 211 IOException ioe = new IOException (e.getMessage()); 212 ioe.initCause(e); 213 throw ioe; 214 } 215 } 216 } 217 221 if (preservedManifestFromArchive != null) { 222 if (explodedManifest.exists()) { 223 if ( ! explodedManifest.delete()) { 224 throw new RuntimeException (localStrings.getString( 225 "enterprise.deployment.backend.error_deleting_manifest", 226 new Object [] 227 { explodedManifest.getAbsolutePath(), 228 preservedManifestFromArchive.getAbsolutePath() 229 } 230 ) ); 231 } 232 } 233 234 if ( ! preservedManifestFromArchive.renameTo(explodedManifest)) { 235 throw new RuntimeException (localStrings.getString( 236 "enterprise.deployment.backend.error_restoring_manifest", 237 new Object [] 238 { preservedManifestFromArchive.getAbsolutePath(), 239 explodedManifest.getAbsolutePath() 240 } 241 ) ); 242 } 243 } 244 245 source.close(); 246 target.close(); 247 } 248 249 public static Application explodeEar(File source, File destination) throws Exception { 250 251 explodeJar(source, destination); 253 254 ApplicationArchivist archivist = new ApplicationArchivist(); 256 archivist.setXMLValidationLevel(getValidationLevel()); 257 FileArchive appArchive = new FileArchive(); 258 appArchive.open(destination.getAbsolutePath()); 259 260 archivist.setManifest(appArchive.getManifest()); 261 262 Application appDesc = null; 264 if (archivist.hasStandardDeploymentDescriptor(appArchive)) { 265 appDesc = (Application) 266 archivist.readStandardDeploymentDescriptor(appArchive); 267 } else { 268 appDesc = Application.createApplication(appArchive,true); 269 } 270 271 archivist.setDescriptor(appDesc); 272 273 Iterator<ModuleDescriptor> bundles = appDesc.getModules(); 275 while (bundles.hasNext()) { 276 277 ModuleDescriptor bundle = bundles.next(); 278 279 String moduleName = bundle.getArchiveUri(); 280 String massagedModuleName = FileUtils.makeFriendlyFilename(moduleName); 281 File archiveFile = new File(destination, moduleName); 282 File moduleDir = new File(destination, massagedModuleName); 283 explodeJar(archiveFile, moduleDir); 284 285 archiveFile.delete(); 287 } 288 289 return appDesc; 290 } 291 292 293 294 public static void explodeJar(File source, File destination) throws IOException { 295 JarFile jarFile = null; 296 String fileSystemName = null; try { 298 jarFile = new JarFile(source); 299 Enumeration<JarEntry> e = jarFile.entries(); 300 while (e.hasMoreElements()) { 301 JarEntry entry = e.nextElement(); 302 fileSystemName = entry.getName().replace('/', File.separatorChar); 303 File out = new File(destination, fileSystemName); 304 if (OS.isWindows() ) { 305 FileUtils.validateWindowsFilePathLength(out); 306 } 307 308 if (entry.isDirectory()) { 309 out.mkdirs(); 310 } else { 311 InputStream is = null; 312 FileOutputStream fos = null; 313 try { 314 if (!out.getParentFile().exists()) { 315 out.getParentFile().mkdirs(); 316 } 317 is = new BufferedInputStream(jarFile.getInputStream(entry)); 318 fos = new FileOutputStream(out); 319 ReadableByteChannel inChannel = Channels.newChannel(is); 320 FileChannel outChannel = fos.getChannel(); 321 outChannel.transferFrom(inChannel, 0, entry.getSize()); 322 } finally { 323 if (is!=null) 324 is.close(); 325 if (fos!=null) 326 fos.close(); 327 } 328 } 329 } 330 } catch(Throwable e) { 331 340 String msg0 = localStrings.getString( 341 "enterprise.deployment.backend.error_expanding", 342 new Object [] {source.getAbsolutePath()}); 343 String msg = localStrings.getString( 344 "enterprise.deployment.backend.could_not_expand", 345 new Object [] {fileSystemName, destination.getAbsolutePath() }); 346 IOException ioe = new IOException (msg0); 347 ioe.initCause(e); 348 getLogger().log(Level.SEVERE, msg, ioe); 349 throw ioe; 350 } finally { 351 if (jarFile != null) { 352 jarFile.close(); 353 } 354 } 355 } 356 357 361 private static Logger getLogger() { 362 if (logger == null) { 363 logger = DeploymentLogger.get(); 364 } 365 return logger; 366 } 367 368 371 private static String getValidationLevel() { 372 if (validationLevel == null) { 373 try { 374 if (ApplicationServer.getServerContext() == null) { validationLevel = DeploymentDescriptorFile.FULL_VALIDATION; 376 } else { 377 ConfigContext ctx = 378 ApplicationServer.getServerContext().getConfigContext(); 379 DasConfig dc = ServerBeansFactory.getDasConfigBean(ctx); 380 validationLevel = dc.getDeployXmlValidation(); 381 } 382 } catch(ConfigException ce) { 383 validationLevel = DeploymentDescriptorFile.FULL_VALIDATION; 385 } 386 } 387 return validationLevel; 388 } 389 } 390 | Popular Tags |