]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/jarpackagerfat/FatJarManifestProvider.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / jarpackagerfat / FatJarManifestProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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  *     Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 83258 [jar exporter] Deploy java application as executable jar
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.jarpackagerfat;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.jar.Attributes;
20 import java.util.jar.Manifest;
21
22 import org.eclipse.core.runtime.CoreException;
23
24 import org.eclipse.jdt.core.IPackageFragment;
25
26 import org.eclipse.jdt.ui.jarpackager.IManifestProvider;
27 import org.eclipse.jdt.ui.jarpackager.JarPackageData;
28
29 /**
30  * A manifest provider creates manifest files for a fat jar.
31  *
32  * @since 3.4
33  */
34 public class FatJarManifestProvider implements IManifestProvider {
35
36         public static final String SEALED_VALUE= "true"; //$NON-NLS-1$
37         public static final String UNSEALED_VALUE= "false"; //$NON-NLS-1$
38
39         FatJarBuilder fBuilder;
40
41         public FatJarManifestProvider(FatJarBuilder builder) {
42                 fBuilder= builder;
43         }
44
45         /**
46          * {@inheritDoc}
47          */
48         public Manifest create(JarPackageData jarPackage) throws CoreException {
49                 Manifest result;
50                 Manifest ownManifest= createOwn(jarPackage);
51                 return fBuilder.generated_4550296889019119873(this, jarPackage, ownManifest);
52         }
53
54         void setManifestClasspath(Manifest ownManifest, String manifestClasspath) {
55                 if ((manifestClasspath != null) && !manifestClasspath.trim().equals("")) { //$NON-NLS-1$
56                         Attributes mainAttr= ownManifest.getMainAttributes();
57                         mainAttr.putValue("Class-Path", manifestClasspath); //$NON-NLS-1$
58                 }
59         }
60
61         Manifest merge(Manifest ownManifest, List<Manifest> otherManifests) {
62                 Manifest mergedManifest= new Manifest(ownManifest);
63                 Map<String, Attributes> mergedEntries= mergedManifest.getEntries();
64                 for (Iterator<Manifest> iter= otherManifests.iterator(); iter.hasNext();) {
65                         Manifest otherManifest= iter.next();
66                         Map<String, Attributes> otherEntries= otherManifest.getEntries();
67                         for (Iterator<String> iterator= otherEntries.keySet().iterator(); iterator.hasNext();) {
68                                 String attributeName= iterator.next();
69                                 if (mergedEntries.containsKey(attributeName)) {
70                                         // TODO: WARNING
71                                 } else {
72                                         mergedEntries.put(attributeName, otherEntries.get(attributeName));
73                                 }
74                         }
75                 }
76                 return mergedManifest;
77         }
78
79         private Manifest createOwn(JarPackageData jarPackage) throws CoreException {
80                 return jarPackage.generated_6092720410613538293(this);
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         public Manifest createDefault(String manifestVersion) {
87                 Manifest manifest= new Manifest();
88                 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, manifestVersion);
89                 return manifest;
90         }
91
92         /**
93          * Hook for subclasses to add additional manifest entries.
94          *
95          * @param       manifest        the manifest to which the entries should be added
96          * @param       jarPackage      the JAR package specification
97          */
98         protected void putAdditionalEntries(Manifest manifest, JarPackageData jarPackage) {
99         }
100
101         public Manifest createGeneratedManifest(JarPackageData jarPackage) {
102                 Manifest manifest= jarPackage.generated_4521662842632988270(this);
103                 putAdditionalEntries(manifest, jarPackage);
104                 return manifest;
105         }
106
107         public void putVersion(Manifest manifest, JarPackageData jarPackage) {
108                 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, jarPackage.getManifestVersion());
109         }
110
111         public void putSealing(Manifest manifest, JarPackageData jarPackage) {
112                 jarPackage.generated_5948248238074113241(manifest, this);
113         }
114
115         public void putMainClass(Manifest manifest, JarPackageData jarPackage) {
116                 jarPackage.generated_4199067722412062797(manifest);
117         }
118
119         public String getInManifestFormat(IPackageFragment packageFragment) {
120                 String name= packageFragment.getElementName();
121                 return name.replace('.', '/') + '/';
122         }
123
124         public Manifest createSuppliedManifest(JarPackageData jarPackage) throws CoreException, IOException {
125                 Manifest manifest;
126                 // No need to use buffer here because Manifest(...) does
127                 InputStream stream= jarPackage.getManifestFile().getContents(false);
128                 try {
129                         manifest= new Manifest(stream);
130                 } finally {
131                         if (stream != null)
132                                 stream.close();
133                 }
134                 return manifest;
135         }
136
137 }