Author: rfeng
Date: Thu Oct  8 19:04:44 2009
New Revision: 823280

URL: http://svn.apache.org/viewvc?rev=823280&view=rev
Log:
Add compile dependencies to the .classpath file if it doesn't supply any 
packages to the OSGi import. This maks this plugin more closer to the mvn 
eclipse:eclipse behavior. 

Modified:
    
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java
    
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java

Modified: 
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java
URL: 
http://svn.apache.org/viewvc/tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java?rev=823280&r1=823279&r2=823280&view=diff
==============================================================================
--- 
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java
 (original)
+++ 
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java
 Thu Oct  8 19:04:44 2009
@@ -37,6 +37,7 @@
 import java.io.OutputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Properties;
 import java.util.Set;
@@ -59,7 +60,7 @@
  *
  * @version $Rev$ $Date$
  */
-final class BundleUtil {
+public final class BundleUtil {
     static final String META_INF_SERVICES = 
"META-INF.services;partial=true;mandatory:=partial";
     private final static Logger logger = 
Logger.getLogger(BundleUtil.class.getName());
     /**
@@ -69,7 +70,7 @@
      * @return
      * @throws IOException
      */
-    static String getBundleSymbolicName(File file) throws IOException {
+    public static String getBundleSymbolicName(File file) throws IOException {
         Manifest manifest = getManifest(file);
         return getBundleSymbolicName(manifest);
     }
@@ -376,37 +377,68 @@
      * @return
      * @throws IOException
      */
-    private static void addExportedPackages(File file, Set<String> packages) 
throws IOException {
+    public static Set<String> getExportedPackages(File file) throws 
IOException {
         if (!file.exists()) {
-            return;
+            return Collections.emptySet();
         }
 
+        Set<String> packages = new HashSet<String>();
+        Manifest manifest = getManifest(file);
+
         // Read the export-package declaration and get a list of the packages 
available in a JAR
-        Set<String> existingPackages = null;
+        String bundleName = null;
         String exports = null;
-        if (file.isDirectory()) {
-            File mf = new File(file, "META-INF/MANIFEST.MF");
-            if (mf.isFile()) {
-                Manifest manifest = new Manifest(new FileInputStream(mf));
-                exports = 
manifest.getMainAttributes().getValue(EXPORT_PACKAGE);
-            }
-        } else {
-            JarFile jar = new JarFile(file, false);
-            Manifest manifest = jar.getManifest();
+
+        if (manifest != null) {
             exports = manifest.getMainAttributes().getValue(EXPORT_PACKAGE);
-            jar.close();
-            existingPackages = new HashSet<String>();
-            addAllPackages(file, existingPackages, "");
+            bundleName = 
manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME);
         }
-        if (exports == null) {
-            return;
+
+        if (bundleName == null) {
+            Set<String> allPackages = new HashSet<String>();
+            addAllPackages(file, allPackages, "");
+            for (String p : allPackages) {
+                packages.add(packageName(p));
+            }
+            return packages;
+        }
+
+        packages.addAll(parsePackages(exports));
+
+        return packages;
+    }
+    
+    public static Set<String> getImportedPackages(File file) throws 
IOException {
+        if (!file.exists()) {
+            return Collections.emptySet();
+        }
+
+        Manifest manifest = getManifest(file);
+
+        // Read the export-package declaration and get a list of the packages 
available in a JAR
+        String bundleName = null;
+        String imports = null;
+
+        if (manifest != null) {
+            imports = manifest.getMainAttributes().getValue(IMPORT_PACKAGE);
+            bundleName = 
manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME);
+            if (imports != null && bundleName != null) {
+                return parsePackages(imports);
+            }
         }
+        return Collections.emptySet();
+    }
 
+    private static Set<String> parsePackages(String header) {
+        if (header == null) {
+            return Collections.emptySet();
+        }
+        Set<String> packages = new HashSet<String>();
         // 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);
+        for (int i = 0, n = header.length(); i < n; i++) {
+            char c = header.charAt(i);
             if (c == '\"') {
                 q = !q;
             }
@@ -416,9 +448,7 @@
                     // 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));
-                    }
+                    packages.add(packageName(export));
                     buffer = new StringBuffer();
                     continue;
                 }
@@ -426,14 +456,10 @@
             buffer.append(c);
         }
         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));
-            }
+            packages.add(packageName(export));
         }
+        return packages;
     }
 
     /**

Modified: 
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java
URL: 
http://svn.apache.org/viewvc/tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java?rev=823280&r1=823279&r2=823280&view=diff
==============================================================================
--- 
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java
 (original)
+++ 
tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java
 Thu Oct  8 19:04:44 2009
@@ -31,10 +31,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.jar.Attributes;
-import java.util.jar.JarFile;
 import java.util.jar.Manifest;
-import java.util.zip.ZipFile;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -65,6 +62,7 @@
 import org.apache.maven.plugin.ide.IdeDependency;
 import org.apache.maven.plugin.ide.IdeUtils;
 import org.apache.maven.project.MavenProject;
+import org.apache.tuscany.maven.bundle.plugin.BundleUtil;
 import org.codehaus.plexus.logging.LogEnabled;
 import org.codehaus.plexus.logging.Logger;
 
@@ -541,6 +539,12 @@
             if ( resolveDependencies )
             {
                 MavenProject project = getProject();
+                Set<String> imported = Collections.emptySet();
+                try {
+                    imported = 
BundleUtil.getImportedPackages(project.getBasedir());
+                } catch (IOException e1) {
+                    throw new MojoExecutionException(e1.getMessage(), e1);
+                }
                 ArtifactRepository localRepo = getLocalRepository();
 
                 List deps = getProject().getDependencies();
@@ -651,44 +655,21 @@
                             // we need to check the manifest, if 
"Bundle-SymbolicName" is there the artifact can be
                             // considered
                             // an osgi bundle
+                            if ("pom".equals(art.getType())) {
+                                continue;
+                            }
+                            File artifactFile = art.getFile();
+                            MavenProject reactorProject = 
getReactorProject(art);
+                            if (reactorProject != null) {
+                                artifactFile = reactorProject.getBasedir();
+                            }
                             boolean isOsgiBundle = false;
                             String osgiSymbolicName = null;
-                            if ( art.getFile() != null )
-                            {
-                                JarFile jarFile = null;
-                                try
-                                {
-                                    jarFile = new JarFile( art.getFile(), 
false, ZipFile.OPEN_READ );
-
-                                    Manifest manifest = jarFile.getManifest();
-                                    if ( manifest != null )
-                                    {
-                                        osgiSymbolicName =
-                                            
manifest.getMainAttributes().getValue(
-                                                                               
    new Attributes.Name(
-                                                                               
                         "Bundle-SymbolicName" ) );
-                                    }
-                                }
-                                catch ( IOException e )
-                                {
-                                    getLog().info( "Unable to read jar 
manifest from " + art.getFile() );
-                                }
-                                finally
-                                {
-                                    if ( jarFile != null )
-                                    {
-                                        try
-                                        {
-                                            jarFile.close();
-                                        }
-                                        catch ( IOException e )
-                                        {
-                                            // ignore
-                                        }
-                                    }
-                                }
+                            try {
+                                osgiSymbolicName = 
BundleUtil.getBundleSymbolicName(artifactFile);
+                            } catch (IOException e) {
+                                getLog().error("Unable to read jar manifest 
from " + artifactFile, e);
                             }
-
                             isOsgiBundle = osgiSymbolicName != null;
 
                             IdeDependency dep =
@@ -707,6 +688,31 @@
                                 if (!(pde && 
(Artifact.SCOPE_COMPILE.equals(art.getScope()) || Artifact.SCOPE_PROVIDED
                                     .equals(art.getScope())))) {
                                     dependencies.add( dep );
+                                } else {
+                                    // Check this compile dependency is an 
OSGi package supplier
+                                    if (!imported.isEmpty()) {
+                                        Set<String> exported = 
Collections.emptySet();
+                                        try {
+                                            exported = 
BundleUtil.getExportedPackages(artifactFile);
+                                        } catch (IOException e) {
+                                            getLog().error("Unable to read jar 
manifest from " + art.getFile(), e);
+                                        }
+                                        boolean matched = false;
+                                        for (String p : imported) {
+                                            if (exported.contains(p)) {
+                                                matched = true;
+                                                break;
+                                            }
+                                        }
+                                        if (!matched) {
+                                            dependencies.add(dep);
+                                        } else {
+                                            getLog()
+                                                .debug("Compile dependency is 
skipped as it is added through OSGi dependency: " + art);
+                                        }
+                                    } else {
+                                        dependencies.add(dep);
+                                    }
                                 }
                             }
                         }


Reply via email to