KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > ManifestProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.jarpackager;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.jar.Attributes JavaDoc;
16 import java.util.jar.Manifest JavaDoc;
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 /**
28  * A manifest provider creates manifest files.
29  */

30 public class ManifestProvider implements IManifestProvider {
31
32     // Constants
33
private static final String JavaDoc SEALED_VALUE= "true"; //$NON-NLS-1$
34
private static final String JavaDoc UNSEALED_VALUE= "false"; //$NON-NLS-1$
35

36     /**
37      * Creates a manifest as defined by the <code>JarPackage</code>.
38      *
39      * @param jarPackage the JAR package specification
40      */

41     public Manifest JavaDoc 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 JavaDoc ex) {
49             throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex);
50         }
51     }
52
53     /**
54      * Creates a default manifest.
55      *
56      * @param manifestVersion the version of the manifest
57      */

58     public Manifest JavaDoc createDefault(String JavaDoc manifestVersion) {
59         Manifest JavaDoc manifest= new Manifest JavaDoc();
60         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, manifestVersion);
61         return manifest;
62     }
63
64     /**
65      * Hook for subclasses to add additional manifest entries.
66      *
67      * @param manifest the manifest to which the entries should be added
68      * @param jarPackage the JAR package specification
69      */

70     protected void putAdditionalEntries(Manifest JavaDoc manifest, JarPackageData jarPackage) {
71     }
72
73     private Manifest JavaDoc createGeneratedManifest(JarPackageData jarPackage) {
74         Manifest JavaDoc manifest= new Manifest JavaDoc();
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 JavaDoc manifest, JarPackageData jarPackage) {
83         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, jarPackage.getManifestVersion());
84     }
85         
86     private void putSealing(Manifest JavaDoc 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 JavaDoc attributes= new Attributes JavaDoc();
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 JavaDoc attributes= new Attributes JavaDoc();
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 JavaDoc 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 JavaDoc getInManifestFormat(IPackageFragment packageFragment) {
115         String JavaDoc name= packageFragment.getElementName();
116         return name.replace('.', '/') + '/';
117     }
118
119     private Manifest JavaDoc createSuppliedManifest(JarPackageData jarPackage) throws CoreException, IOException JavaDoc {
120         Manifest JavaDoc manifest;
121         // No need to use buffer here because Manifest(...) does
122
InputStream JavaDoc stream= jarPackage.getManifestFile().getContents(false);
123         try {
124             manifest= new Manifest JavaDoc(stream);
125         } finally {
126             if (stream != null)
127                 stream.close();
128         }
129         return manifest;
130     }
131 }
132
Popular Tags