FELIX-497: use attached sources instead of SCM url

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@629001 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeploy.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeploy.java
index 2c48052..c556f17 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeploy.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeploy.java
@@ -130,6 +130,11 @@
      */
     private WagonManager m_wagonManager;
 
+    /**
+     * Attached source artifact
+     */
+    private Artifact m_sourceArtifact;
+
 
     public void execute() throws MojoExecutionException
     {
@@ -144,6 +149,17 @@
             return;
         }
 
+        // check for any attached sources
+        for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
+        {
+            Artifact artifact = ( Artifact ) i.next();
+            if ( "sources".equals( artifact.getClassifier() ) )
+            {
+                m_sourceArtifact = artifact;
+                break;
+            }
+        }
+
         // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead
         if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
         {
@@ -154,6 +170,7 @@
         String repositoryName = new File( tempURI.getPath() ).getName();
 
         Log log = getLog();
+        ObrUpdate update;
 
         RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
         openRepositoryConnection( remoteFile );
@@ -174,12 +191,20 @@
             URI repositoryXml = downloadedRepositoryXml.toURI();
             URI obrXmlFile = ObrUtils.findObrXml( project.getResources() );
 
-            updateRemoteBundleMetadata( project.getArtifact(), repositoryXml, obrXmlFile, mavenRepository );
+            Config userConfig = new Config();
+            userConfig.setRemoteFile( true );
+
+            update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
+            update.parseRepositoryXml();
+
+            updateRemoteBundleMetadata( project.getArtifact(), update );
             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
             {
-                updateRemoteBundleMetadata( ( Artifact ) i.next(), repositoryXml, obrXmlFile, mavenRepository );
+                updateRemoteBundleMetadata( ( Artifact ) i.next(), update );
             }
 
+            update.writeRepositoryXml();
+
             if ( downloadedRepositoryXml.exists() )
             {
                 // ======== UPLOAD MODIFIED OBR ========
@@ -238,20 +263,21 @@
     }
 
 
-    private void updateRemoteBundleMetadata( Artifact artifact, URI repoXml, URI obrXml, String mavenRepo )
-        throws MojoExecutionException
+    private void updateRemoteBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
     {
         if ( !"bundle".equals( artifact.getType() ) || null == artifact.getFile() || artifact.getFile().isDirectory() )
         {
             return;
         }
 
-        URI bundleJar = ObrUtils.findBundleJar( localRepository, artifact );
+        URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
+        URI sourceJar = null;
 
-        Config userConfig = new Config();
-        userConfig.setRemoteFile( true );
+        if ( null != m_sourceArtifact )
+        {
+            sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
+        }
 
-        ObrUpdate update = new ObrUpdate( repoXml, obrXml, project, bundleJar, mavenRepo, userConfig, getLog() );
-        update.updateRepository();
+        update.updateRepository( bundleJar, sourceJar );
     }
 }
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeployFile.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeployFile.java
index d09bbda..7ec28ec 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeployFile.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrDeployFile.java
@@ -170,7 +170,7 @@
 
             if ( null == file )
             {
-                bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
+                bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
             }
             else
             {
@@ -191,9 +191,12 @@
                 userConfig.setRemoteBundle( URI.create( localRepository.pathOf( project.getArtifact() ) ) );
             }
 
-            update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
+            update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
+            update.parseRepositoryXml();
 
-            update.updateRepository();
+            update.updateRepository( bundleJar, null );
+
+            update.writeRepositoryXml();
 
             if ( downloadedRepositoryXml.exists() )
             {
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstall.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstall.java
index 4948852..1cf04b2 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstall.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstall.java
@@ -28,6 +28,7 @@
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
 
 
@@ -81,6 +82,11 @@
      */
     private List attachedArtifacts;
 
+    /**
+     * Attached source artifact
+     */
+    private Artifact m_sourceArtifact;
+
 
     public void execute()
     {
@@ -95,6 +101,20 @@
             return;
         }
 
+        // check for any attached sources
+        for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
+        {
+            Artifact artifact = ( Artifact ) i.next();
+            if ( "sources".equals( artifact.getClassifier() ) )
+            {
+                m_sourceArtifact = artifact;
+                break;
+            }
+        }
+
+        Log log = getLog();
+        ObrUpdate update;
+
         try
         {
             String mavenRepository = localRepository.getBasedir();
@@ -102,32 +122,41 @@
             URI repositoryXml = ObrUtils.findRepositoryXml( mavenRepository, obrRepository );
             URI obrXmlFile = ObrUtils.findObrXml( project.getResources() );
 
-            updateLocalBundleMetadata( project.getArtifact(), repositoryXml, obrXmlFile, mavenRepository );
+            Config userConfig = new Config();
+
+            update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
+            update.parseRepositoryXml();
+
+            updateLocalBundleMetadata( project.getArtifact(), update );
             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
             {
-                updateLocalBundleMetadata( ( Artifact ) i.next(), repositoryXml, obrXmlFile, mavenRepository );
+                updateLocalBundleMetadata( ( Artifact ) i.next(), update );
             }
+
+            update.writeRepositoryXml();
         }
         catch ( Exception e )
         {
-            getLog().warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
+            log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
         }
     }
 
 
-    private void updateLocalBundleMetadata( Artifact artifact, URI repoXml, URI obrXml, String mavenRepo )
-        throws MojoExecutionException
+    private void updateLocalBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
     {
         if ( !"bundle".equals( artifact.getType() ) || null == artifact.getFile() || artifact.getFile().isDirectory() )
         {
             return;
         }
 
-        URI bundleJar = ObrUtils.findBundleJar( localRepository, artifact );
+        URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
+        URI sourceJar = null;
 
-        Config userConfig = new Config();
+        if ( null != m_sourceArtifact )
+        {
+            sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
+        }
 
-        ObrUpdate update = new ObrUpdate( repoXml, obrXml, project, bundleJar, mavenRepo, userConfig, getLog() );
-        update.updateRepository();
+        update.updateRepository( bundleJar, sourceJar );
     }
 }
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstallFile.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstallFile.java
index 7e0c3cf..435b9c8 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstallFile.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrInstallFile.java
@@ -91,7 +91,7 @@
 
         if ( null == file )
         {
-            bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
+            bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
         }
         else
         {
@@ -100,8 +100,11 @@
 
         Config userConfig = new Config();
 
-        update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
+        update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
+        update.parseRepositoryXml();
 
-        update.updateRepository();
+        update.updateRepository( bundleJar, null );
+
+        update.writeRepositoryXml();
     }
 }
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUpdate.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUpdate.java
index fb754b0..d3b45b7 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUpdate.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUpdate.java
@@ -77,11 +77,6 @@
     private URI m_obrXml;
 
     /**
-     * name and path to the bundle jar file.
-     */
-    private URI m_bundleJar;
-
-    /**
      * maven project description.
      */
     private MavenProject m_project;
@@ -117,15 +112,13 @@
      * @param repositoryXml path to the repository descriptor file
      * @param obrXml path and filename to the obr.xml file
      * @param project maven project description
-     * @param bundleJar path to the bundle jar file
      * @param mavenRepositoryPath path to the local maven repository
      * @param userConfig user information
      * @param logger plugin logger
      */
-    public ObrUpdate( URI repositoryXml, URI obrXml, MavenProject project, URI bundleJar, String mavenRepositoryPath,
+    public ObrUpdate( URI repositoryXml, URI obrXml, MavenProject project, String mavenRepositoryPath,
         Config userConfig, Log logger )
     {
-        m_bundleJar = bundleJar;
         m_repositoryXml = repositoryXml;
         m_obrXml = obrXml;
         m_project = project;
@@ -133,8 +126,6 @@
 
         m_userConfig = userConfig;
 
-        m_resourceBundle = new ResourcesBundle( logger );
-
         if ( userConfig.isRemoteFile() )
         {
             m_baseURI = ObrUtils.toFileURI( mavenRepositoryPath );
@@ -143,34 +134,43 @@
         {
             m_baseURI = m_repositoryXml;
         }
+
+        m_documentBuilder = initDocumentBuilder();
     }
 
 
     /**
-     * update the repository descriptor file. parse the old repository descriptor file, get the old reference of the bundle or determine the id for a new bundle, extract information from bindex set the new information in descriptor file and save it.
+     * update the repository descriptor file. parse the old repository descriptor file,
+     * get the old reference of the bundle or determine the id for a new bundle, extract
+     * information from bindex set the new information in descriptor file and save it.
+     * 
+     * @param bundleJar path to the bundle jar file
+     * @param sourceJar path to the source jar file
+     * 
      * @throws MojoExecutionException if the plugin failed
      */
-    public void updateRepository() throws MojoExecutionException
+    public void updateRepository( URI bundleJar, URI sourceJar ) throws MojoExecutionException
     {
         m_logger.debug( " (f) repositoryXml = " + m_repositoryXml );
-        m_logger.debug( " (f) bundleJar = " + m_bundleJar );
+        m_logger.debug( " (f) bundleJar = " + bundleJar );
+        m_logger.debug( " (f) sourceJar = " + sourceJar );
         m_logger.debug( " (f) obrXml = " + m_obrXml );
 
-        m_documentBuilder = initDocumentBuilder();
-
-        if ( m_documentBuilder == null )
+        if ( m_repositoryDoc == null )
         {
             return;
         }
 
+        m_resourceBundle = new ResourcesBundle( m_logger );
+
         // get the file size
-        File bundleFile = new File( m_bundleJar );
+        File bundleFile = new File( bundleJar );
         if ( bundleFile.exists() )
         {
             URI resourceURI = m_userConfig.getRemoteBundle();
             if ( null == resourceURI )
             {
-                resourceURI = m_bundleJar;
+                resourceURI = bundleJar;
                 if ( m_userConfig.isPathRelative() )
                 {
                     resourceURI = ObrUtils.getRelativeURI( m_baseURI, resourceURI );
@@ -191,13 +191,7 @@
         }
         else
         {
-            m_logger.error( "file doesn't exist: " + m_bundleJar );
-            return;
-        }
-
-        // parse repository
-        if ( parseRepositoryXml() == -1 )
-        {
+            m_logger.error( "file doesn't exist: " + bundleJar );
             return;
         }
 
@@ -225,7 +219,7 @@
         try
         {
             // use bindex to extract bundle information
-            bindexExtractor = new ExtractBindexInfo( m_repositoryXml, m_bundleJar.getPath() );
+            bindexExtractor = new ExtractBindexInfo( m_repositoryXml, bundleJar.getPath() );
         }
         catch ( MojoExecutionException e )
         {
@@ -235,20 +229,36 @@
             throw new MojoExecutionException( "BindexException" );
         }
 
-        m_resourceBundle.construct( m_project, bindexExtractor );
+        String sourcePath = null;
+        if ( null != sourceJar )
+        {
+            if ( m_userConfig.isPathRelative() )
+            {
+                sourcePath = ObrUtils.getRelativeURI( m_baseURI, sourceJar ).toASCIIString();
+            }
+            else
+            {
+                sourcePath = sourceJar.toASCIIString();
+            }
+        }
+
+        m_resourceBundle.construct( m_project, bindexExtractor, sourcePath );
 
         Element rootElement = m_repositoryDoc.getDocumentElement();
         if ( !walkOnTree( rootElement ) )
         {
             // the correct resource node was not found, we must create it
-            // we compute the new id
             String id = m_resourceBundle.getId();
             searchRepository( rootElement, id );
         }
+    }
 
-        // the repository.xml file have been modified, so we save it
+
+    public void writeRepositoryXml() throws MojoExecutionException
+    {
         m_logger.info( "Writing OBR metadata" );
-        writeToFile( m_repositoryXml, rootElement );
+
+        writeToFile( m_repositoryXml, m_repositoryDoc.getDocumentElement() );
     }
 
 
@@ -277,12 +287,11 @@
     /**
      * Parse the repository descriptor file.
      * 
-     * @return 0 if the bundle is already in the descriptor, else -1
+     * @return true if the repository file was parsed, otherwise false
      * @throws MojoExecutionException if the plugin failed
      */
-    private int parseRepositoryXml() throws MojoExecutionException
+    public boolean parseRepositoryXml() throws MojoExecutionException
     {
-
         File fout = new File( m_repositoryXml );
         if ( !fout.exists() )
         {
@@ -305,14 +314,9 @@
             }
         }
 
-        // now we parse the repository.xml file
         m_repositoryDoc = parseFile( m_repositoryXml, m_documentBuilder );
-        if ( m_repositoryDoc == null )
-        {
-            return -1;
-        }
 
-        return 0;
+        return ( null != m_repositoryDoc );
     }
 
 
@@ -454,6 +458,7 @@
             e.printStackTrace();
             throw new MojoExecutionException( "TransformerConfigurationException" );
         }
+
         Properties proprietes = new Properties();
         proprietes.put( "method", "xml" );
         proprietes.put( "version", "1.0" );
@@ -510,7 +515,6 @@
      */
     private boolean walkOnTree( Node node )
     {
-
         if ( node.getNodeName().compareTo( "resource" ) == 0 )
         {
             return resource( node );
@@ -574,7 +578,6 @@
      */
     private boolean resource( Node node )
     {
-
         // this part save all the id free if we need to add resource
         String id = node.getAttributes().getNamedItem( "id" ).getNodeValue();
         NamedNodeMap map = node.getAttributes();
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUtils.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUtils.java
index 01bc86f..081de24 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUtils.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ObrUtils.java
@@ -108,7 +108,7 @@
      * @param artifact maven artifact
      * @return file URI pointing to artifact in repository
      */
-    public static URI findBundleJar( ArtifactRepository repository, Artifact artifact )
+    public static URI getArtifactURI( ArtifactRepository repository, Artifact artifact )
     {
         String baseDir = repository.getBasedir();
         String artifactPath = repository.pathOf( artifact );
diff --git a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ResourcesBundle.java b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ResourcesBundle.java
index b214b73..95c6ace 100644
--- a/bundleplugin/src/main/java/org/apache/felix/obrplugin/ResourcesBundle.java
+++ b/bundleplugin/src/main/java/org/apache/felix/obrplugin/ResourcesBundle.java
@@ -381,9 +381,10 @@
      * this method gets information form pom.xml to complete missing data from those given by user.
      * @param project project information given by maven
      * @param ebi bundle information extracted from bindex
+     * @param sourcePath path to local sources
      * @return true
      */
-    public boolean construct( MavenProject project, ExtractBindexInfo ebi )
+    public boolean construct( MavenProject project, ExtractBindexInfo ebi, String sourcePath )
     {
 
         if ( ebi.getPresentationName() != null )
@@ -466,12 +467,7 @@
         }
         else
         {
-            String src = null;
-            if ( project.getScm() != null )
-            {
-                src = project.getScm().getUrl();
-            }
-            setSource( src );
+            setSource( sourcePath );
         }
 
         if ( ebi.getLicense() != null )