You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2009/02/18 14:32:18 UTC

svn commit: r745510 - in /incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport: AbstractBundleInstallMojo.java BundleUninstallMojo.java

Author: cziegeler
Date: Wed Feb 18 13:32:18 2009
New Revision: 745510

URL: http://svn.apache.org/viewvc?rev=745510&view=rev
Log:
SLING-861 : Apply patch by Alexander Klimetschek to support PUT in the maven-sling-plugin

Modified:
    incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
    incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java

Modified: incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java?rev=745510&r1=745509&r2=745510&view=diff
==============================================================================
--- incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java (original)
+++ incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java Wed Feb 18 13:32:18 2009
@@ -34,8 +34,10 @@
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.FileRequestEntity;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.multipart.FilePart;
 import org.apache.commons.httpclient.methods.multipart.FilePartSource;
 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
@@ -71,6 +73,38 @@
     protected String slingUrl;
 
     /**
+     * An optional url suffix which will be appended to the <code>sling.url</code>
+     * for use as the real target url. This allows to configure different target URLs
+     * in each POM, while using the same common <code>sling.url</code> in a parent
+     * POM (eg. <code>sling.url=http://localhost:8080</code> and
+     * <code>sling.urlSuffix=/project/specific/path</code>). This is typically used
+     * in conjunction with a HTTP PUT (<code>sling.usePut=true</code>).
+     *
+     * @parameter expression="${sling.urlSuffix}"
+     */
+    protected String slingUrlSuffix;
+
+    /**
+     * If a simple HTTP PUT should be used instead of the standard POST to the
+     * felix console. In the <code>uninstall</code> goal, a HTTP DELETE will be
+     * used.
+     * 
+     * @parameter expression="${sling.usePut}" default-value="false"
+     * @required
+     */
+    protected boolean usePut;
+
+    /**
+     * The content type / mime type used for the HTTP PUT (if
+     * <code>sling.usePut=true</code>).
+     * 
+     * @parameter expression="${sling.mimeTypeForPut}"
+     *            default-value="application/java-archive"
+     * @required
+     */
+    protected String mimeTypeForPut;
+    
+    /**
      * The user name to authenticate at the running Sling instance.
      *
      * @parameter expression="${sling.user}" default-value="admin"
@@ -133,6 +167,26 @@
 
     protected abstract String getBundleFileName() throws MojoExecutionException;
 
+    /**
+     * Returns the combination of <code>sling.url</code> and
+     * <code>sling.urlSuffix</code>.
+     */
+    protected String getTargetURL() {
+        String targetURL = slingUrl;
+        if (slingUrlSuffix != null) {
+            targetURL += slingUrlSuffix;
+        }
+        return targetURL;
+    }
+
+    /**
+     * Returns the URL for PUT or DELETE by appending the filename to the
+     * targetURL.
+     */
+    protected String getPutURL(String targetURL, String fileName) {
+        return targetURL + (targetURL.endsWith("/") ? "" : "/") + fileName;
+    }
+
     public void execute() throws MojoExecutionException {
 
         // get the file to upload
@@ -145,13 +199,21 @@
             getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
             return;
         }
+        
+        String targetURL = getTargetURL();
 
         getLog().info(
             "Installing Bundle " + bundleName + "(" + bundleFile + ") to "
-                + slingUrl);
-        post(slingUrl, bundleFile);
+                + targetURL + " via " + (usePut ? "PUT" : "POST"));
+        
+        if (usePut) {
+            put(targetURL, bundleFile);
+        } else {
+            post(targetURL, bundleFile);
+        }
+        
         if ( mountByFS ) {
-            configure(slingUrl, bundleFile);
+            configure(targetURL, bundleFile);
         }
     }
 
@@ -228,6 +290,29 @@
         }
     }
 
+    protected void put(String targetURL, File file) throws MojoExecutionException {
+
+        PutMethod filePut = new PutMethod(getPutURL(targetURL, file.getName()));
+        
+        try {
+            filePut.setRequestEntity(new FileRequestEntity(file, mimeTypeForPut));
+        
+            int status = getHttpClient().executeMethod(filePut);
+            if (status >= 200 && status < 300) {
+                getLog().info("Bundle installed");
+            } else {
+                getLog().error(
+                    "Installation failed, cause: "
+                        + HttpStatus.getStatusText(status));
+            }
+        } catch (Exception ex) {
+            throw new MojoExecutionException("Installation on " + targetURL
+                + " failed, cause: " + ex.getMessage(), ex);
+        } finally {
+            filePut.releaseConnection();
+        }
+    }
+        
     /**
      * Add configurations to a running OSGi instance for initial content.
      * @param targetURL The web console base url

Modified: incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java?rev=745510&r1=745509&r2=745510&view=diff
==============================================================================
--- incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java (original)
+++ incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java Wed Feb 18 13:32:18 2009
@@ -24,6 +24,7 @@
 
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.maven.plugin.MojoExecutionException;
 
@@ -60,11 +61,42 @@
             return;
         }
 
+        String targetURL = getTargetURL();
+        
         getLog().info(
             "Unistalling Bundle " + bundleName + ") from "
-                + slingUrl);
-        configure(slingUrl, bundleFile);
-        post(slingUrl, bundleName);
+                + targetURL + " via " + (usePut ? "DELETE" : "POST"));
+        
+        configure(targetURL, bundleFile);
+        
+        if (usePut) {
+            delete(targetURL, bundleFile);
+        } else {
+            post(targetURL, bundleName);
+        }
+    }
+
+    protected void delete(String targetURL, File file)
+        throws MojoExecutionException {
+        
+        final DeleteMethod delete = new DeleteMethod(getPutURL(targetURL, file.getName()));
+
+        try {
+
+            int status = getHttpClient().executeMethod(delete);
+            if (status >= 200 && status < 300) {
+                getLog().info("Bundle uninstalled");
+            } else {
+                getLog().error(
+                    "Uninstall failed, cause: "
+                        + HttpStatus.getStatusText(status));
+            }
+        } catch (Exception ex) {
+            throw new MojoExecutionException("Uninstall from " + targetURL
+                + " failed, cause: " + ex.getMessage(), ex);
+        } finally {
+            delete.releaseConnection();
+        }
     }
 
     protected void post(String targetURL, String symbolicName)