1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.vaadin.buildhelpers;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.jar.Attributes.Name;
/**
* Generates Export-Packages attribute for OSGi compatible manifest.
*
* Reads the included Java packages in Vaadin JAR, generates a corresponding
* MANIFEST.MF file, and replaces the dummy one in the JAR with the
* generated one.
*
* See #3521 for details.
*
* @author magi
*/
public class GeneratePackageExports {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Invalid number of parameters\n"+
"Usage: java -cp .. GenerateManifest <package.jar>");
System.exit(1);
}
// Open tje JAR
String jarFilename = args[0];
JarFile jar = null;
try {
jar = new JarFile(jarFilename);
} catch (IOException e) {
System.err.println("Unable to open JAR '"+jarFilename+"'");
System.exit(1);
}
// List the included Java packages
HashSet<String> packages = new HashSet<String>();
for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();) {
JarEntry entry = it.nextElement();
if (entry.getName().startsWith("com") && entry.getName().endsWith(".class")) {
int lastSlash = entry.getName().lastIndexOf('/');
String pkg = entry.getName().substring(0, lastSlash).replace('/', '.');
packages.add(pkg);
}
}
// List theme packages
for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();) {
JarEntry entry = it.nextElement();
if (entry.isDirectory() && entry.getName().startsWith("VAADIN/themes")) {
// Strip ending slash
int lastSlash = entry.getName().lastIndexOf('/');
String pkg = entry.getName().substring(0, lastSlash).replace('/', '.');
packages.add(pkg);
}
}
// Replacement for the "Export-Package" attribute in the manifest
String exportPackage = "";
// Produce an ordered listing of the package names
String packageArray[] = new String[packages.size()];
packages.toArray(packageArray);
Arrays.sort(packageArray);
for (int i=0; i<packageArray.length; i++) {
if (i == 0)
exportPackage = packageArray[i];
else
exportPackage += ", " + packageArray[i];
}
// Read old manifest
Manifest oldMF = null;
try {
oldMF = jar.getManifest();
} catch (IOException e) {
e.printStackTrace();
}
// Read main attributes
Attributes mainAtts = oldMF.getMainAttributes();
Vector<String> keys = new Vector<String>(mainAtts.size());
for (Iterator<Object> attrit = mainAtts.keySet().iterator(); attrit.hasNext();) {
Name name = (Name) attrit.next();
keys.add(name.toString());
}
// Put the manifest version as the first line
String orderedKeys[] = new String[keys.size()];
keys.toArray(orderedKeys);
Arrays.sort(orderedKeys); // Must sort to be able to search
int mvPos = Arrays.binarySearch(orderedKeys, "Manifest-Version");
orderedKeys[mvPos] = orderedKeys[0]; // Swap
orderedKeys[0] = "Manifest-Version";
// This final ordering is just for esthetic reasons and
// in practice unnecessary and will actually be messed up
// when the 'jar' command reads the manifest
Arrays.sort(orderedKeys, 1, orderedKeys.length-1);
// Create the modified manifest
ManifestWriter manifest = new ManifestWriter();
for (int i=0; i<orderedKeys.length; i++) {
// Skip an existing Export-Package attribute
if (orderedKeys[i].equals("Export-Package")) {
// Copy the attribute to the modified manifest
manifest.writeAttribute(orderedKeys[i], mainAtts.getValue(orderedKeys[i]));
}
}
// Add the Export-Package attribute at the end of the manifest.
// The alternative would be replacing an existing attribute in
// the loop above, but it's not guaranteed that it exists.
manifest.writeAttribute("Export-Package", exportPackage);
// Update the manifest in the Jar
int status = manifest.updateJar(jarFilename);
if (status != 0)
System.exit(status);
}
}
|