Browse Source

Generate package exports for the JAR only for com.vaadin and com.google

Other (unrebased) packages are not included in the exported packages
list of the manifest.
tags/7.0.0.beta1
Henri Sara 12 years ago
parent
commit
93e9eaf7ab

+ 40
- 28
build/buildhelpers/com/vaadin/buildhelpers/GeneratePackageExports.java View File

import java.util.Iterator; import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import java.util.jar.Attributes.Name;


/** /**
* Generates Export-Packages attribute for OSGi compatible manifest. * Generates Export-Packages attribute for OSGi compatible manifest.
* *
* Reads the included Java packages in Vaadin JAR, generates a corresponding * 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.
* MANIFEST.MF file, and replaces the dummy one in the JAR with the generated
* one.
* *
* See #3521 for details. * See #3521 for details.
* *
* @author magi * @author magi
*/ */
public class GeneratePackageExports { public class GeneratePackageExports {
public static final String VAADIN_PACKAGE_PATH_PREFIX = "com/vaadin/";
public static final String GOOGLE_PACKAGE_PATH_PREFIX = "com/google/";

public static void main(String[] args) { public static void main(String[] args) {
if (args.length < 1) { if (args.length < 1) {
System.err.println("Invalid number of parameters\n"+
"Usage: java -cp .. GenerateManifest <package.jar>");
System.err.println("Invalid number of parameters\n"
+ "Usage: java -cp .. GenerateManifest <package.jar>");
System.exit(1); System.exit(1);
} }


// Open tje JAR
// Open the JAR
String jarFilename = args[0]; String jarFilename = args[0];
JarFile jar = null; JarFile jar = null;
try { try {
jar = new JarFile(jarFilename); jar = new JarFile(jarFilename);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Unable to open JAR '"+jarFilename+"'");
System.err.println("Unable to open JAR '" + jarFilename + "'");
System.exit(1); System.exit(1);
} }


HashSet<String> packages = new HashSet<String>(); HashSet<String> packages = new HashSet<String>();
for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();) { for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();) {
JarEntry entry = it.nextElement(); JarEntry entry = it.nextElement();
if (entry.getName().startsWith("com") && entry.getName().endsWith(".class")) {
if ((entry.getName().startsWith(VAADIN_PACKAGE_PATH_PREFIX) || entry
.getName().startsWith(GOOGLE_PACKAGE_PATH_PREFIX))
&& entry.getName().endsWith(".class")) {
int lastSlash = entry.getName().lastIndexOf('/'); int lastSlash = entry.getName().lastIndexOf('/');
String pkg = entry.getName().substring(0, lastSlash).replace('/', '.');
String pkg = entry.getName().substring(0, lastSlash)
.replace('/', '.');
packages.add(pkg); packages.add(pkg);
} }
} }
// List theme packages // List theme packages
for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();) { for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();) {
JarEntry entry = it.nextElement(); JarEntry entry = it.nextElement();
if (entry.isDirectory() && entry.getName().startsWith("VAADIN/themes")) {
if (entry.isDirectory()
&& entry.getName().startsWith("VAADIN/themes")) {
// Strip ending slash // Strip ending slash
int lastSlash = entry.getName().lastIndexOf('/'); int lastSlash = entry.getName().lastIndexOf('/');
String pkg = entry.getName().substring(0, lastSlash).replace('/', '.');
String pkg = entry.getName().substring(0, lastSlash)
.replace('/', '.');
packages.add(pkg); packages.add(pkg);
} }
} }
// Replacement for the "Export-Package" attribute in the manifest // Replacement for the "Export-Package" attribute in the manifest
String exportPackage = ""; String exportPackage = "";


String packageArray[] = new String[packages.size()]; String packageArray[] = new String[packages.size()];
packages.toArray(packageArray); packages.toArray(packageArray);
Arrays.sort(packageArray); Arrays.sort(packageArray);
for (int i=0; i<packageArray.length; i++) {
if (i == 0)
for (int i = 0; i < packageArray.length; i++) {
if (i == 0) {
exportPackage = packageArray[i]; exportPackage = packageArray[i];
else
} else {
exportPackage += ", " + packageArray[i]; exportPackage += ", " + packageArray[i];
}
} }
// Read old manifest // Read old manifest
Manifest oldMF = null; Manifest oldMF = null;
try { try {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Read main attributes // Read main attributes
Attributes mainAtts = oldMF.getMainAttributes(); Attributes mainAtts = oldMF.getMainAttributes();
Vector<String> keys = new Vector<String>(mainAtts.size()); Vector<String> keys = new Vector<String>(mainAtts.size());
for (Iterator<Object> attrit = mainAtts.keySet().iterator(); attrit.hasNext();) {
for (Iterator<Object> attrit = mainAtts.keySet().iterator(); attrit
.hasNext();) {
Name name = (Name) attrit.next(); Name name = (Name) attrit.next();
keys.add(name.toString()); keys.add(name.toString());
} }
try { try {
jar.close(); jar.close();
} catch (IOException e) { } catch (IOException e) {
System.err.println("Unable to close JAR '"+jarFilename+"'");
System.err.println("Unable to close JAR '" + jarFilename + "'");
} }
// Put the manifest version as the first line // Put the manifest version as the first line
String orderedKeys[] = new String[keys.size()];
String orderedKeys[] = new String[keys.size()];
keys.toArray(orderedKeys); keys.toArray(orderedKeys);
Arrays.sort(orderedKeys); // Must sort to be able to search Arrays.sort(orderedKeys); // Must sort to be able to search
int mvPos = Arrays.binarySearch(orderedKeys, "Manifest-Version"); int mvPos = Arrays.binarySearch(orderedKeys, "Manifest-Version");
orderedKeys[0] = "Manifest-Version"; orderedKeys[0] = "Manifest-Version";


// This final ordering is just for esthetic reasons and // This final ordering is just for esthetic reasons and
// in practice unnecessary and will actually be messed up
// in practice unnecessary and will actually be messed up
// when the 'jar' command reads the manifest // when the 'jar' command reads the manifest
Arrays.sort(orderedKeys, 1, orderedKeys.length-1);
Arrays.sort(orderedKeys, 1, orderedKeys.length - 1);


// Create the modified manifest // Create the modified manifest
ManifestWriter manifest = new ManifestWriter(); ManifestWriter manifest = new ManifestWriter();
for (int i=0; i<orderedKeys.length; i++) {
for (int i = 0; i < orderedKeys.length; i++) {
// Skip an existing Export-Package attribute // Skip an existing Export-Package attribute
if (orderedKeys[i].equals("Export-Package")) { if (orderedKeys[i].equals("Export-Package")) {
// Copy the attribute to the modified manifest // Copy the attribute to the modified manifest
manifest.writeAttribute(orderedKeys[i], mainAtts.getValue(orderedKeys[i]));
manifest.writeAttribute(orderedKeys[i],
mainAtts.getValue(orderedKeys[i]));
} }
} }
// Add the Export-Package attribute at the end of the manifest. // Add the Export-Package attribute at the end of the manifest.
// The alternative would be replacing an existing attribute in // The alternative would be replacing an existing attribute in
// the loop above, but it's not guaranteed that it exists. // the loop above, but it's not guaranteed that it exists.
// before this is done. // before this is done.
int status = manifest.updateJar(jarFilename); int status = manifest.updateJar(jarFilename);


if (status != 0)
if (status != 0) {
System.exit(status); System.exit(status);
}
} }
} }

Loading…
Cancel
Save