FELIX-475: support deploy-file goal

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@618608 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/Config.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/Config.java
index 54952be..c2d12c7 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/Config.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/Config.java
@@ -19,6 +19,9 @@
 package org.apache.felix.obr.plugin;
 
 
+import java.net.URI;
+
+
 /**
  * this class is used to store some user information about configuration of the plugin.
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -26,33 +29,25 @@
  */
 public class Config
 {
-
-    /**
-     * use relative path or not.
-     */
     private boolean m_pathRelative; // use relative or absolute path in repository.xml
-
-    /**
-     * deploy file or not.
-     */
-    private boolean m_fileRemote; // deploy file on remote server
+    private boolean m_remoteFile; // deploy file on remote server
+    private URI m_remoteBundle; // public address of deployed bundle
 
 
     /**
      * constructor: set default configuration: use relative path and don't upload file.
-     *
      */
     public Config()
     {
         // default configuration
         m_pathRelative = true;
-        m_fileRemote = false;
+        m_remoteFile = false;
+        m_remoteBundle = null;
     }
 
 
     /**
-     * set relativePath attribute.
-     * @param value new value of attribute
+     * @param value enable to use relative path
      */
     public void setPathRelative( boolean value )
     {
@@ -61,18 +56,25 @@
 
 
     /**
-     * set fileRemote attribute.
-     * @param value new value of attribute
+     * @param value enable when uploading
      */
-    public void setRemotely( boolean value )
+    public void setRemoteFile( boolean value )
     {
-        m_fileRemote = value;
+        m_remoteFile = value;
     }
 
 
     /**
-     * get use path relative.
-     * @return true if plugin use relative path, else false
+     * @param value public address of deployed bundle
+     */
+    public void setRemoteBundle( URI value )
+    {
+        m_remoteBundle = value;
+    }
+
+
+    /**
+     * @return true if plugin uses relative path, else false
      */
     public boolean isPathRelative()
     {
@@ -81,11 +83,19 @@
 
 
     /**
-     * get if use upload file.
      * @return true if the file will be uploaded, else false
      */
-    public boolean isRemotely()
+    public boolean isRemoteFile()
     {
-        return m_fileRemote;
+        return m_remoteFile;
+    }
+
+
+    /**
+     * @return public address of deployed bundle
+     */
+    public URI getRemoteBundle()
+    {
+        return m_remoteBundle;
     }
 }
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeploy.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeploy.java
index 6e91405..ab24490 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeploy.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeploy.java
@@ -71,6 +71,13 @@
     private String altDeploymentRepository;
 
     /**
+     * Optional public URL where the bundle has been deployed.
+     *
+     * @parameter expression="${bundleUrl}"
+     */
+    private String bundleUrl;
+
+    /**
      * Local Repository.
      * 
      * @parameter expression="${localRepository}"
@@ -109,11 +116,18 @@
     {
         if ( "NONE".equalsIgnoreCase( obrRepository ) )
         {
+            getLog().info( "OBR update disabled (enable with -DobrRepository)" );
             return;
         }
 
-        URI repositoryURI = ObrUtils.findRepositoryXml( "", obrRepository );
-        String repositoryName = new File( repositoryURI.getPath() ).getName();
+        URI remoteBundleURI = null;
+        if ( null != bundleUrl )
+        {
+            remoteBundleURI = URI.create( bundleUrl );
+        }
+
+        URI tempURI = ObrUtils.findRepositoryXml( "", obrRepository );
+        String repositoryName = new File( tempURI.getPath() ).getName();
 
         Log log = getLog();
         ObrUpdate update;
@@ -139,8 +153,9 @@
             URI bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
 
             Config userConfig = new Config();
+            userConfig.setRemoteBundle( remoteBundleURI );
             userConfig.setPathRelative( true );
-            userConfig.setRemotely( true );
+            userConfig.setRemoteFile( true );
 
             update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
 
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeployFile.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeployFile.java
index 0596556..a8ccdde 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeployFile.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrDeployFile.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -19,7 +19,15 @@
 package org.apache.felix.obr.plugin;
 
 
-import org.apache.maven.plugin.AbstractMojo;
+import java.io.File;
+import java.net.URI;
+
+import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.settings.Settings;
 
 
 /**
@@ -31,10 +39,153 @@
  * 
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class ObrDeployFile extends AbstractMojo
+public final class ObrDeployFile extends AbstractFileMojo
 {
-    public void execute()
+    /**
+     * When true, ignore remote locking.
+     * 
+     * @parameter expression="${ignoreLock}"
+     */
+    private boolean ignoreLock;
+
+    /**
+     * OBR Repository.
+     * 
+     * @parameter expression="${obrRepository}"
+     */
+    private String obrRepository;
+
+    /**
+     * Remote repository id, used to lookup authentication settings.
+     *
+     * @parameter expression="${repositoryId}" default-value="remote-repository"
+     * @required
+     */
+    private String repositoryId;
+
+    /**
+     * Remote OBR repository URL, where the bundle details are to be uploaded.
+     *
+     * @parameter expression="${url}"
+     * @required
+     */
+    private String url;
+
+    /**
+     * Optional public URL where the bundle has been deployed.
+     *
+     * @parameter expression="${bundleUrl}"
+     */
+    private String bundleUrl;
+
+    /**
+     * Local Repository.
+     * 
+     * @parameter expression="${localRepository}"
+     * @required
+     * @readonly
+     */
+    private ArtifactRepository localRepository;
+
+    /**
+     * Local Maven settings.
+     * 
+     * @parameter expression="${settings}"
+     * @required
+     * @readonly
+     */
+    private Settings settings;
+
+    /**
+     * The Wagon manager.
+     * 
+     * @component
+     */
+    private WagonManager m_wagonManager;
+
+
+    public void execute() throws MojoExecutionException
     {
-        // TODO Auto-generated method stub       
+        MavenProject project = getProject();
+
+        if ( "NONE".equalsIgnoreCase( obrRepository ) )
+        {
+            getLog().info( "OBR update disabled (enable with -DobrRepository)" );
+            return;
+        }
+
+        URI remoteBundleURI = null;
+        if ( null != bundleUrl )
+        {
+            remoteBundleURI = URI.create( bundleUrl );
+        }
+
+        URI tempURI = ObrUtils.findRepositoryXml( "", obrRepository );
+        String repositoryName = new File( tempURI.getPath() ).getName();
+
+        Log log = getLog();
+        ObrUpdate update;
+
+        RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
+        remoteFile.connect( repositoryId, url );
+
+        // ======== LOCK REMOTE OBR ========
+        log.info( "LOCK " + remoteFile + '/' + repositoryName );
+        remoteFile.lockFile( repositoryName, ignoreLock );
+        File downloadedRepositoryXml = null;
+
+        try
+        {
+            // ======== DOWNLOAD REMOTE OBR ========
+            log.info( "Downloading " + repositoryName );
+            downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
+
+            String mavenRepository = localRepository.getBasedir();
+
+            URI repositoryXml = downloadedRepositoryXml.toURI();
+            URI obrXmlFile = ObrUtils.toFileURI( obrXml );
+            URI bundleJar;
+
+            if ( null == file )
+            {
+                bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
+            }
+            else
+            {
+                bundleJar = file.toURI();
+            }
+
+            Config userConfig = new Config();
+            userConfig.setRemoteBundle( remoteBundleURI );
+            userConfig.setPathRelative( true );
+            userConfig.setRemoteFile( true );
+
+            update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
+
+            update.updateRepository();
+
+            if ( downloadedRepositoryXml.exists() )
+            {
+                // ======== UPLOAD MODIFIED OBR ========
+                log.info( "Uploading " + repositoryName );
+                remoteFile.put( downloadedRepositoryXml, repositoryName );
+            }
+        }
+        catch ( Exception e )
+        {
+            log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
+        }
+        finally
+        {
+            // ======== UNLOCK REMOTE OBR ========
+            log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
+            remoteFile.unlockFile( repositoryName );
+            remoteFile.disconnect();
+
+            if ( null != downloadedRepositoryXml )
+            {
+                downloadedRepositoryXml.delete();
+            }
+        }
     }
 }
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java
index fd1a80d..e026d91 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstall.java
@@ -70,6 +70,7 @@
     {
         if ( "NONE".equalsIgnoreCase( obrRepository ) )
         {
+            getLog().info( "OBR update disabled (enable with -DobrRepository)" );
             return;
         }
 
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java
index c833fd5..757c37a 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrInstallFile.java
@@ -61,6 +61,7 @@
 
         if ( "NONE".equalsIgnoreCase( obrRepository ) )
         {
+            getLog().info( "OBR update disabled (enable with -DobrRepository)" );
             return;
         }
 
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java
index 8891032..27dfd7e 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java
@@ -135,7 +135,7 @@
 
         m_resourceBundle = new ResourcesBundle( logger );
 
-        if ( userConfig.isRemotely() )
+        if ( userConfig.isRemoteFile() )
         {
             m_baseURI = ObrUtils.toFileURI( mavenRepositoryPath );
         }
@@ -167,10 +167,14 @@
         File bundleFile = new File( m_bundleJar );
         if ( bundleFile.exists() )
         {
-            URI resourceURI = m_bundleJar;
-            if ( m_userConfig.isPathRelative() )
+            URI resourceURI = m_userConfig.getRemoteBundle();
+            if ( null == resourceURI )
             {
-                resourceURI = ObrUtils.getRelativeURI( m_baseURI, resourceURI );
+                resourceURI = m_bundleJar;
+                if ( m_userConfig.isPathRelative() )
+                {
+                    resourceURI = ObrUtils.getRelativeURI( m_baseURI, resourceURI );
+                }
             }
 
             m_resourceBundle.setSize( String.valueOf( bundleFile.length() ) );
@@ -219,7 +223,7 @@
             m_logger.error( "unable to build Bindex informations" );
             e.printStackTrace();
 
-            throw new MojoExecutionException( "MojoFailureException" );
+            throw new MojoExecutionException( "BindexException" );
         }
 
         m_resourceBundle.construct( m_project, bindexExtractor );
@@ -288,7 +292,7 @@
             catch ( MojoExecutionException e )
             {
                 e.printStackTrace();
-                throw new MojoExecutionException( "MojoExecutionException" );
+                throw new MojoExecutionException( "IOException" );
             }
         }
 
@@ -542,7 +546,7 @@
             return;
         }
 
-        System.out.println( "Second branch..." );
+        m_logger.info( "Second branch..." );
         NodeList list = node.getChildNodes();
         if ( list.getLength() > 0 )
         {