001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.jarpackager;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.jar.Attributes;
015: import java.util.jar.Manifest;
016:
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.CoreException;
019:
020: import org.eclipse.jdt.core.IPackageFragment;
021:
022: import org.eclipse.jdt.ui.jarpackager.IManifestProvider;
023: import org.eclipse.jdt.ui.jarpackager.JarPackageData;
024:
025: /**
026: * A manifest provider creates manifest files.
027: */
028: public class ManifestProvider implements IManifestProvider {
029:
030: // Constants
031: private static final String SEALED_VALUE = "true"; //$NON-NLS-1$
032: private static final String UNSEALED_VALUE = "false"; //$NON-NLS-1$
033:
034: /**
035: * Creates a manifest as defined by the <code>JarPackage</code>.
036: *
037: * @param jarPackage the JAR package specification
038: */
039: public Manifest create(JarPackageData jarPackage)
040: throws CoreException {
041: Assert.isNotNull(jarPackage);
042: if (jarPackage.isManifestGenerated())
043: return createGeneratedManifest(jarPackage);
044:
045: try {
046: return createSuppliedManifest(jarPackage);
047: } catch (IOException ex) {
048: throw JarPackagerUtil.createCoreException(ex
049: .getLocalizedMessage(), ex);
050: }
051: }
052:
053: /**
054: * Creates a default manifest.
055: *
056: * @param manifestVersion the version of the manifest
057: */
058: public Manifest createDefault(String manifestVersion) {
059: Manifest manifest = new Manifest();
060: manifest.getMainAttributes().put(
061: Attributes.Name.MANIFEST_VERSION, manifestVersion);
062: return manifest;
063: }
064:
065: /**
066: * Hook for subclasses to add additional manifest entries.
067: *
068: * @param manifest the manifest to which the entries should be added
069: * @param jarPackage the JAR package specification
070: */
071: protected void putAdditionalEntries(Manifest manifest,
072: JarPackageData jarPackage) {
073: }
074:
075: private Manifest createGeneratedManifest(JarPackageData jarPackage) {
076: Manifest manifest = new Manifest();
077: putVersion(manifest, jarPackage);
078: putSealing(manifest, jarPackage);
079: putMainClass(manifest, jarPackage);
080: putAdditionalEntries(manifest, jarPackage);
081: return manifest;
082: }
083:
084: private void putVersion(Manifest manifest, JarPackageData jarPackage) {
085: manifest.getMainAttributes().put(
086: Attributes.Name.MANIFEST_VERSION,
087: jarPackage.getManifestVersion());
088: }
089:
090: private void putSealing(Manifest manifest, JarPackageData jarPackage) {
091: if (jarPackage.isJarSealed()) {
092: manifest.getMainAttributes().put(Attributes.Name.SEALED,
093: SEALED_VALUE);
094: IPackageFragment[] packages = jarPackage
095: .getPackagesToUnseal();
096: if (packages != null) {
097: for (int i = 0; i < packages.length; i++) {
098: Attributes attributes = new Attributes();
099: attributes.put(Attributes.Name.SEALED,
100: UNSEALED_VALUE);
101: manifest.getEntries().put(
102: getInManifestFormat(packages[i]),
103: attributes);
104: }
105: }
106: } else {
107: IPackageFragment[] packages = jarPackage
108: .getPackagesToSeal();
109: if (packages != null)
110: for (int i = 0; i < packages.length; i++) {
111: Attributes attributes = new Attributes();
112: attributes
113: .put(Attributes.Name.SEALED, SEALED_VALUE);
114: manifest.getEntries().put(
115: getInManifestFormat(packages[i]),
116: attributes);
117: }
118: }
119: }
120:
121: private void putMainClass(Manifest manifest,
122: JarPackageData jarPackage) {
123: if (jarPackage.getManifestMainClass() != null
124: && jarPackage.getManifestMainClass()
125: .getFullyQualifiedName().length() > 0)
126: manifest.getMainAttributes().put(
127: Attributes.Name.MAIN_CLASS,
128: jarPackage.getManifestMainClass()
129: .getFullyQualifiedName());
130: }
131:
132: private String getInManifestFormat(IPackageFragment packageFragment) {
133: String name = packageFragment.getElementName();
134: return name.replace('.', '/') + '/';
135: }
136:
137: private Manifest createSuppliedManifest(JarPackageData jarPackage)
138: throws CoreException, IOException {
139: Manifest manifest;
140: // No need to use buffer here because Manifest(...) does
141: InputStream stream = jarPackage.getManifestFile().getContents(
142: false);
143: try {
144: manifest = new Manifest(stream);
145: } finally {
146: if (stream != null)
147: stream.close();
148: }
149: return manifest;
150: }
151: }
|