1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.jar.Attributes ; 16 import java.util.jar.Manifest ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 21 22 import org.eclipse.jdt.core.IPackageFragment; 23 24 import org.eclipse.jdt.ui.jarpackager.IManifestProvider; 25 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 26 27 30 public class ManifestProvider implements IManifestProvider { 31 32 private static final String SEALED_VALUE= "true"; private static final String UNSEALED_VALUE= "false"; 36 41 public Manifest create(JarPackageData jarPackage) throws CoreException { 42 Assert.isNotNull(jarPackage); 43 if (jarPackage.isManifestGenerated()) 44 return createGeneratedManifest(jarPackage); 45 46 try { 47 return createSuppliedManifest(jarPackage); 48 } catch (IOException ex) { 49 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 50 } 51 } 52 53 58 public Manifest createDefault(String manifestVersion) { 59 Manifest manifest= new Manifest (); 60 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, manifestVersion); 61 return manifest; 62 } 63 64 70 protected void putAdditionalEntries(Manifest manifest, JarPackageData jarPackage) { 71 } 72 73 private Manifest createGeneratedManifest(JarPackageData jarPackage) { 74 Manifest manifest= new Manifest (); 75 putVersion(manifest, jarPackage); 76 putSealing(manifest, jarPackage); 77 putMainClass(manifest, jarPackage); 78 putAdditionalEntries(manifest, jarPackage); 79 return manifest; 80 } 81 82 private void putVersion(Manifest manifest, JarPackageData jarPackage) { 83 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, jarPackage.getManifestVersion()); 84 } 85 86 private void putSealing(Manifest manifest, JarPackageData jarPackage) { 87 if (jarPackage.isJarSealed()) { 88 manifest.getMainAttributes().put(Attributes.Name.SEALED, SEALED_VALUE); 89 IPackageFragment[] packages= jarPackage.getPackagesToUnseal(); 90 if (packages != null) { 91 for (int i= 0; i < packages.length; i++) { 92 Attributes attributes= new Attributes (); 93 attributes.put(Attributes.Name.SEALED, UNSEALED_VALUE); 94 manifest.getEntries().put(getInManifestFormat(packages[i]), attributes); 95 } 96 } 97 } 98 else { 99 IPackageFragment[] packages= jarPackage.getPackagesToSeal(); 100 if (packages != null) 101 for (int i= 0; i < packages.length; i++) { 102 Attributes attributes= new Attributes (); 103 attributes.put(Attributes.Name.SEALED, SEALED_VALUE); 104 manifest.getEntries().put(getInManifestFormat(packages[i]), attributes); 105 } 106 } 107 } 108 109 private void putMainClass(Manifest manifest, JarPackageData jarPackage) { 110 if (jarPackage.getManifestMainClass() != null && jarPackage.getManifestMainClass().getFullyQualifiedName().length() > 0) 111 manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, jarPackage.getManifestMainClass().getFullyQualifiedName()); 112 } 113 114 private String getInManifestFormat(IPackageFragment packageFragment) { 115 String name= packageFragment.getElementName(); 116 return name.replace('.', '/') + '/'; 117 } 118 119 private Manifest createSuppliedManifest(JarPackageData jarPackage) throws CoreException, IOException { 120 Manifest manifest; 121 InputStream stream= jarPackage.getManifestFile().getContents(false); 123 try { 124 manifest= new Manifest (stream); 125 } finally { 126 if (stream != null) 127 stream.close(); 128 } 129 return manifest; 130 } 131 } 132 | Popular Tags |