diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-16 02:58:13 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-16 02:58:13 +0000 |
commit | 6e1fe17f597e9fdfc7778e4f4c89612f145cb28c (patch) | |
tree | 0651e74b686155db6ff99042c90c0adf3fab9e28 /branches/sca-equinox/tools/maven/maven-bundle-plugin | |
parent | fdf80a2492bdb2ed237da8244b905b388bfbc1f9 (diff) |
Moved thirdparty library bundle to the 'eclipse' profile. Changed Maven Bundle plugin to execute within the process-resources phase. Minor cleanup of the BundleUtil class.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@705123 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-equinox/tools/maven/maven-bundle-plugin')
2 files changed, 152 insertions, 100 deletions
diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java index bd8d8bd470..de28a99446 100644 --- a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java @@ -49,70 +49,81 @@ import java.util.zip.ZipInputStream; * @version $Rev$ $Date$ */ final class BundleUtil { - - static private Pattern pattern = Pattern.compile("-([0-9.]+)"); - static private Pattern pattern2 = Pattern.compile("_([0-9.]+)"); - - private static String version(String jarFile) { - String version = "1.0.0"; - boolean found = false; - Matcher matcher = pattern2.matcher(jarFile); - if (matcher.find()) { - found = true; - } else { - matcher = pattern.matcher(jarFile); - found = matcher.find(); - } - if (found) { - version = matcher.group(); - if (version.endsWith(".")) { - version = version.substring(1, version.length() - 1); - } else { - version = version.substring(1); - } - } - return version; - } - private static void addPackages(File jarFile, Set<String> packages) throws IOException { - if (getBundleName(jarFile) == null) { - String version = ";version=" + version(jarFile.getPath()); - addAllPackages(jarFile, packages, version); + /** + * Returns the name of a bundle, or null if the given file is not a bundle. + * + * @param file + * @return + * @throws IOException + */ + static String getBundleName(File file) throws IOException { + if (!file.exists()) { + return null; + } + String bundleName = null; + if (file.isDirectory()) { + File mf = new File(file, "META-INF/MANIFEST.MF"); + if (mf.isFile()) { + Manifest manifest = new Manifest(new FileInputStream(mf)); + bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); + } } else { - addExportedPackages(jarFile, packages); + JarFile jar = new JarFile(file, false); + Manifest manifest = jar.getManifest(); + bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); + jar.close(); } + if (bundleName == null) { + return bundleName; + } + int sc = bundleName.indexOf(';'); + if (sc != -1) { + bundleName = bundleName.substring(0, sc); + } + return bundleName; } - + + /** + * Generate a Bundle manifest for a set of JAR files. + * + * @param jarFiles + * @param name + * @param symbolicName + * @param version + * @return + * @throws IllegalStateException + */ static Manifest libraryManifest(Set<File> jarFiles, String name, String symbolicName, String version) throws IllegalStateException { try { // List exported packages and bundle classpath entries StringBuffer classpath = new StringBuffer(); - StringBuffer exports = new StringBuffer(); - StringBuffer imports = new StringBuffer(); - Set<String> packages = new HashSet<String>(); + Set<String> exportedPackages = new HashSet<String>(); for (File jarFile : jarFiles) { - addPackages(jarFile, packages); + addPackages(jarFile, exportedPackages); classpath.append("lib/"); classpath.append(jarFile.getName()); classpath.append(","); } - Set<String> importPackages = new HashSet<String>(); - for (String pkg : packages) { - exports.append(pkg); + // Generate export-package and import-package declarations + StringBuffer exports = new StringBuffer(); + StringBuffer imports = new StringBuffer(); + Set<String> importedPackages = new HashSet<String>(); + for (String export : exportedPackages) { + + // Add export declaration + exports.append(export); exports.append(','); - String importPackage = pkg; - int index = pkg.indexOf(';'); - if (index != -1) { - importPackage = pkg.substring(0, index); - } - if (!importPackages.contains(importPackage)) { - imports.append(importPackage); + // Add corresponding import declaration + String packageName = packageName(export); + if (!importedPackages.contains(packageName)) { + importedPackages.add(packageName); + imports.append(packageName); imports.append(','); - importPackages.add(importPackage); } } @@ -135,6 +146,13 @@ final class BundleUtil { } } + /** + * Write a bundle manifest. + * + * @param manifest + * @param out + * @throws IOException + */ static void write(Manifest manifest, OutputStream out) throws IOException { DataOutputStream dos = new DataOutputStream(out); Attributes attributes = manifest.getMainAttributes(); @@ -149,7 +167,31 @@ final class BundleUtil { write(attributes, EXPORT_PACKAGE, dos); dos.flush(); } - + + /** + * Add packages to be exported out of a JAR file. + * + * @param jarFile + * @param packages + * @throws IOException + */ + private static void addPackages(File jarFile, Set<String> packages) throws IOException { + if (getBundleName(jarFile) == null) { + String version = ";version=" + version(jarFile.getPath()); + addAllPackages(jarFile, packages, version); + } else { + addExportedPackages(jarFile, packages); + } + } + + /** + * Write manifest attributes. + * + * @param attributes + * @param key + * @param dos + * @throws IOException + */ private static void write(Attributes attributes, String key, DataOutputStream dos) throws IOException { StringBuffer line = new StringBuffer(); line.append(key); @@ -169,41 +211,7 @@ final class BundleUtil { } /** - * Returns the name of a bundle, or null if the given file is not a bundle. - * - * @param file - * @return - * @throws IOException - */ - static String getBundleName(File file) throws IOException { - if (!file.exists()) { - return null; - } - String bundleName = null; - if (file.isDirectory()) { - File mf = new File(file, "META-INF/MANIFEST.MF"); - if (mf.isFile()) { - Manifest manifest = new Manifest(new FileInputStream(mf)); - bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); - } - } else { - JarFile jar = new JarFile(file, false); - Manifest manifest = jar.getManifest(); - bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); - jar.close(); - } - if (bundleName == null) { - return bundleName; - } - int sc = bundleName.indexOf(';'); - if (sc != -1) { - bundleName = bundleName.substring(0, sc); - } - return bundleName; - } - - /** - * Strip the uses:= statement out of an OSGi export. + * Strip an OSGi export, only retain the package name and version. * * @param export * @return @@ -254,18 +262,20 @@ final class BundleUtil { } is.close(); } - - private static boolean isPresent(String export, Set<String> present) { - if (present == null) { - return true; - } + + /** + * Returns the name of the exported package in the given export. + * @param export + * @return + */ + private static String packageName(String export) { int sc = export.indexOf(';'); if (sc != -1) { export = export.substring(0, sc); } - return present.contains(export); + return export; } - + /** * Add the packages exported by a bundle. * @@ -278,7 +288,9 @@ final class BundleUtil { if (!file.exists()) { return; } - Set<String> present = null; + + // Read the export-package declaration and get a list of the packages available in a JAR + Set<String> existingPackages = null; String exports = null; if (file.isDirectory()) { File mf = new File(file, "META-INF/MANIFEST.MF"); @@ -291,13 +303,15 @@ final class BundleUtil { Manifest manifest = jar.getManifest(); exports = manifest.getMainAttributes().getValue(EXPORT_PACKAGE); jar.close(); - present = new HashSet<String>(); - addAllPackages(file, present, ""); + existingPackages = new HashSet<String>(); + addAllPackages(file, existingPackages, ""); } if (exports == null) { return; } - StringBuffer export = new StringBuffer(); + + // Parse the export-package declaration, and extract the individual packages + StringBuffer buffer = new StringBuffer(); boolean q = false; for (int i =0, n = exports.length(); i <n; i++) { char c = exports.charAt(i); @@ -306,20 +320,58 @@ final class BundleUtil { } if (!q) { if (c == ',') { - if (isPresent(export.toString(), present)) { - packages.add(stripExport(export.toString())); + + // Add the exported package to the set, after making sure it really exists in + // the JAR + String export = buffer.toString(); + if (existingPackages == null || existingPackages.contains(packageName(export))) { + packages.add(stripExport(export)); } - export = new StringBuffer(); + buffer = new StringBuffer(); continue; } } - export.append(c); + buffer.append(c); } - if (export.length() != 0) { - if (isPresent(export.toString(), present)) { - packages.add(stripExport(export.toString())); + if (buffer.length() != 0) { + + // Add the exported package to the set, after making sure it really exists in + // the JAR + String export = buffer.toString(); + if (existingPackages == null || existingPackages.contains(packageName(export))) { + packages.add(stripExport(export)); + } + } + } + + static private Pattern pattern = Pattern.compile("-([0-9.]+)"); + static private Pattern pattern2 = Pattern.compile("_([0-9.]+)"); + + /** + * Derive a bundle version from the given JAR file name. + * + * @param jarFile + * @return + */ + private static String version(String jarFile) { + String version = "1.0.0"; + boolean found = false; + Matcher matcher = pattern2.matcher(jarFile); + if (matcher.find()) { + found = true; + } else { + matcher = pattern.matcher(jarFile); + found = matcher.find(); + } + if (found) { + version = matcher.group(); + if (version.endsWith(".")) { + version = version.substring(1, version.length() - 1); + } else { + version = version.substring(1); } } + return version; } } diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java index ccd29e4cd6..cf15c5602d 100644 --- a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java @@ -49,7 +49,7 @@ import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; /** * @version $Rev$ $Date$ * @goal assemble-thirdparty-bundle - * @phase process-classes + * @phase process-resources * @requiresDependencyResolution test * @description Build an OSGi bundle for third party dependencies */ |