You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2008/10/16 04:58:14 UTC

svn commit: r705123 - in /tuscany/branches/sca-equinox: modules/thirdparty-library/ tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/

Author: jsdelfino
Date: Wed Oct 15 19:58:13 2008
New Revision: 705123

URL: http://svn.apache.org/viewvc?rev=705123&view=rev
Log:
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.

Modified:
    tuscany/branches/sca-equinox/modules/thirdparty-library/pom.xml
    tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java
    tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java

Modified: tuscany/branches/sca-equinox/modules/thirdparty-library/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-equinox/modules/thirdparty-library/pom.xml?rev=705123&r1=705122&r2=705123&view=diff
==============================================================================
--- tuscany/branches/sca-equinox/modules/thirdparty-library/pom.xml (original)
+++ tuscany/branches/sca-equinox/modules/thirdparty-library/pom.xml Wed Oct 15 19:58:13 2008
@@ -44,45 +44,57 @@
         </dependency>        
     </dependencies>
 
-    <build>
-        <plugins>
-            <plugin>
-              <artifactId>maven-clean-plugin</artifactId>
-              <configuration>
-                <filesets>
-                  <fileset>
-                    <directory>${basedir}/lib</directory>
-                    <includes>
-                      <include>*</include>
-                    </includes>
-                    <followSymlinks>false</followSymlinks>
-                   </fileset>
-                 </filesets>
-               </configuration>
-             </plugin>
-            
-            <plugin>
-                <groupId>org.apache.tuscany.sca</groupId>
-                <artifactId>tuscany-maven-bundle-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <id>assemble-thirdparty-bundle</id>
-                        <goals>
-                            <goal>assemble-thirdparty-bundle</goal>
-                        </goals>
-                        <configuration>
-                            <symbolicName>org.apache.tuscany.sca.thirdparty.library</symbolicName>
-                        </configuration>
-                    </execution>
-                   <execution>
-                        <id>generate-pde-classpath</id>
-                        <goals>
-                            <goal>generate-pde-classpath</goal>
-                        </goals>
-                    </execution>                    
-                </executions>
-            </plugin>
-
-        </plugins>
-    </build>
+    <profiles>
+        <profile>
+            <id>eclipse</id>
+            <build>
+                <plugins>
+                    <plugin>
+                      <artifactId>maven-clean-plugin</artifactId>
+                      <configuration>
+                        <filesets>
+                          <fileset>
+                            <directory>${basedir}/lib</directory>
+                            <includes>
+                              <include>*</include>
+                            </includes>
+                            <followSymlinks>false</followSymlinks>
+                           </fileset>
+                         </filesets>
+                       </configuration>
+                     </plugin>
+                    
+                    <plugin>
+                        <groupId>org.apache.tuscany.sca</groupId>
+                        <artifactId>tuscany-maven-bundle-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>assemble-thirdparty-bundle</id>
+                                <goals>
+                                    <goal>assemble-thirdparty-bundle</goal>
+                                </goals>
+                                <configuration>
+                                    <symbolicName>org.apache.tuscany.sca.thirdparty.library</symbolicName>
+                                </configuration>
+                            </execution>
+                        </executions>
+                    </plugin>
+        
+                    <plugin>
+                        <groupId>org.apache.tuscany.sca</groupId>
+                        <artifactId>tuscany-maven-bundle-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>generate-pde-classpath</id>
+                                <goals>
+                                    <goal>generate-pde-classpath</goal>
+                                </goals>
+                            </execution>
+                        </executions>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+    </profiles>
+    
 </project>

Modified: tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java?rev=705123&r1=705122&r2=705123&view=diff
==============================================================================
--- tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java (original)
+++ tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java Wed Oct 15 19:58:13 2008
@@ -49,70 +49,81 @@
  * @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 @@
         }
     }
 
+    /**
+     * 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 @@
         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 @@
     }
 
     /**
-     * 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 @@
         }
         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 @@
         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 @@
             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 @@
             }
             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;
     }
 
 }

Modified: tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java?rev=705123&r1=705122&r2=705123&view=diff
==============================================================================
--- tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java (original)
+++ tuscany/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java Wed Oct 15 19:58:13 2008
@@ -49,7 +49,7 @@
 /**
  * @version $Rev$ $Date$
  * @goal assemble-thirdparty-bundle
- * @phase process-classes
+ * @phase process-resources
  * @requiresDependencyResolution test
  * @description Build an OSGi bundle for third party dependencies
  */