You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/09/24 10:34:30 UTC

svn commit: r578707 - in /incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport: AbstractBundleInstallMojo.java AbstractBundlePostMojo.java BundleInstallFileMojo.java BundleInstallMojo.java

Author: fmeschbe
Date: Mon Sep 24 01:34:29 2007
New Revision: 578707

URL: http://svn.apache.org/viewvc?rev=578707&view=rev
Log:
Add new plugin comparable to install named install-file which allows installation of a bundle without a POM

Added:
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java
Modified:
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java

Added: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java?rev=578707&view=auto
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java (added)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java Mon Sep 24 01:34:29 2007
@@ -0,0 +1,149 @@
+/*
+ * $Url: $
+ * $Id: $
+ *
+ * Copyright 1997-2005 Day Management AG
+ * Barfuesserplatz 6, 4001 Basel, Switzerland
+ * All Rights Reserved.
+ *
+ * This software is the confidential and proprietary information of
+ * Day Management AG, ("Confidential Information"). You shall not
+ * disclose such Confidential Information and shall use it only in
+ * accordance with the terms of the license agreement you entered into
+ * with Day.
+ */
+package org.apache.sling.maven.bundlesupport;
+
+import java.io.File;
+import java.net.ConnectException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpClient;
+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.PostMethod;
+import org.apache.commons.httpclient.methods.multipart.FilePart;
+import org.apache.commons.httpclient.methods.multipart.FilePartSource;
+import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.Part;
+import org.apache.commons.httpclient.methods.multipart.StringPart;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+public abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
+
+    /**
+     * The URL of the running Sling instance.
+     *
+     * @parameter expression="${sling.url}" default-value="http://localhost:8080/sling"
+     * @required
+     */
+    private String slingUrl;
+    
+    /**
+     * The user name to authenticate at the running Sling instance.
+     *
+     * @parameter expression="${sling.user}" default-value="admin"
+     * @required
+     */
+    private String user;
+    
+    /**
+     * The password to authenticate at the running Sling instance.
+     *
+     * @parameter expression="${sling.password}" default-value="admin"
+     * @required
+     */
+    private String password;
+    
+    /**
+     * The startlevel for the uploaded bundle
+     *
+     * @parameter expression="${sling.bundle.startlevel}" default-value="20"
+     * @required
+     */
+    private String bundleStartLevel;
+    
+    /**
+     * Whether to start the uploaded bundle or not
+     *
+     * @parameter expression="${sling.bundle.start}" default-value="true"
+     * @required
+     */
+    private boolean bundleStart;
+
+    public AbstractBundleInstallMojo() {
+        super();
+    }
+
+    protected abstract String getBundleFileName() throws MojoExecutionException;
+    
+    public void execute() throws MojoExecutionException {
+
+        // get the file to upload
+        String bundleFileName = getBundleFileName();
+
+        // only upload if packaging as an osgi-bundle
+        File bundleFile = new File(bundleFileName);
+        String bundleName = getBundleSymbolicName(bundleFile);
+        if (bundleName == null) {
+            getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
+            return;
+        }
+
+        getLog().info("Installing Bundle " + bundleName + "(" + bundleFile + ") to " + slingUrl);
+        post(slingUrl, bundleFile);
+    }
+
+    protected void post(String targetURL, File file) {
+    
+        // append pseudo path after root URL to not get redirected for nothing
+        PostMethod filePost = new PostMethod(targetURL + "/install");
+    
+        try {
+    
+            List<Part> partList = new ArrayList<Part>();
+            partList.add(new StringPart("action", "install"));
+            partList.add(new StringPart("_noredir_", "_noredir_"));
+            partList.add(new FilePart("bundlefile", new FilePartSource(file.getName(), file)));
+            partList.add(new StringPart("bundlestartlevel", bundleStartLevel));
+            
+            if (bundleStart) {
+                partList.add(new StringPart("bundlestart", "start"));
+            }
+    
+            Part[] parts = partList.toArray(new Part[partList.size()]);
+    
+            filePost.setRequestEntity(new MultipartRequestEntity(parts,
+                filePost.getParams()));
+            HttpClient client = new HttpClient();
+            client.getHttpConnectionManager().getParams().setConnectionTimeout(
+                5000);
+            
+            // authentication stuff
+            client.getParams().setAuthenticationPreemptive(true);
+            Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
+            client.getState().setCredentials(AuthScope.ANY, defaultcreds);
+            
+            int status = client.executeMethod(filePost);
+            if (status == HttpStatus.SC_OK) {
+                getLog().info("Bundle installed");
+            } else {
+                getLog().error(
+                    "Installation failed, cause: " + HttpStatus.getStatusText(status));
+            }
+        } catch (ConnectException ce) {
+            getLog().info("Installation on " + targetURL + " failed, cause: " + ce.getMessage());
+            getLog().debug(ce); // dump on debug
+        } catch (Exception ex) {
+            getLog().error(ex.getClass().getName() + " " + ex.getMessage());
+            ex.printStackTrace();
+        } finally {
+            filePost.releaseConnection();
+        }
+    }
+
+}
\ No newline at end of file

Modified: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java?rev=578707&r1=578706&r2=578707&view=diff
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java (original)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java Mon Sep 24 01:34:29 2007
@@ -24,13 +24,14 @@
 import java.util.jar.Manifest;
 
 import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
 
 public abstract class AbstractBundlePostMojo extends AbstractMojo {
 
     /**
      * Returns the symbolic name of the given bundle. If the
-     * <code>jarFile</code> does not exist or does not contain a manifest with
-     * a <code>Bundle-SymbolicName</code> header <code>null</code> is
+     * <code>jarFile</code> does not contain a manifest with a
+     * <code>Bundle-SymbolicName</code> header <code>null</code> is
      * returned. Otherwise the value of the <code>Bundle-SymbolicName</code>
      * header is returned.
      * <p>
@@ -42,15 +43,17 @@
      *            requested.
      * @return The bundle's symbolic name from the
      *         <code>Bundle-SymbolicName</code> manifest header or
-     *         <code>null</code> if the file does not exists or no manifest
-     *         exists in the file or the header is not contained in the
-     *         manifest. However, if <code>null</code> is returned, the file
-     *         may be assumed to not contain an OSGi bundle.
+     *         <code>null</code> if no manifest exists in the file or the
+     *         header is not contained in the manifest. However, if
+     *         <code>null</code> is returned, the file may be assumed to not
+     *         contain an OSGi bundle.
+     * @throws MojoExecutionException if the file does not exist
      */
-    protected String getBundleSymbolicName(File jarFile) {
+    protected String getBundleSymbolicName(File jarFile)
+            throws MojoExecutionException {
+
         if (!jarFile.exists()) {
-            getLog().debug("getBundleSymbolicName: " + jarFile + " does not exist");
-            return null;
+            throw new MojoExecutionException("Missing file " + jarFile);
         }
 
         JarFile jaf = null;
@@ -58,20 +61,24 @@
             jaf = new JarFile(jarFile);
             Manifest manif = jaf.getManifest();
             if (manif == null) {
-                getLog().debug("getBundleSymbolicName: Missing manifest in " + jarFile);
+                getLog().debug(
+                    "getBundleSymbolicName: Missing manifest in " + jarFile);
                 return null;
             }
 
             String symbName = manif.getMainAttributes().getValue(
                 "Bundle-SymbolicName");
             if (symbName == null) {
-                getLog().debug("getBundleSymbolicName: No Bundle-SymbolicName in " + jarFile);
+                getLog().debug(
+                    "getBundleSymbolicName: No Bundle-SymbolicName in "
+                        + jarFile);
                 return null;
             }
 
             return symbName;
         } catch (IOException ioe) {
-            getLog().warn("getBundleSymbolicName: Problem checking " + jarFile, ioe);
+            getLog().warn("getBundleSymbolicName: Problem checking " + jarFile,
+                ioe);
         } finally {
             if (jaf != null) {
                 try {

Added: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java?rev=578707&view=auto
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java (added)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java Mon Sep 24 01:34:29 2007
@@ -0,0 +1,48 @@
+/*
+ * 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 regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sling.maven.bundlesupport;
+
+import java.io.File;
+
+import org.apache.maven.plugin.MojoExecutionException;
+
+/**
+ * Install an OSGi bundle to a running Sling instance.
+ *
+ * @goal install-file
+ * @description install an OSGi bundle jar to a running Sling instance
+ * @requiresProject false
+ */
+public class BundleInstallFileMojo extends AbstractBundleInstallMojo {
+
+    /**
+     * The name of the generated JAR file.
+     *
+     * @parameter expression="${sling.file}"
+     */
+    private String bundleFileName;
+    
+    @Override
+    protected String getBundleFileName() throws MojoExecutionException {
+        if (bundleFileName == null) {
+            throw new MojoExecutionException("Missing sling.file parameter");
+        }
+        
+        return bundleFileName;
+    }
+}
\ No newline at end of file

Modified: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java?rev=578707&r1=578706&r2=578707&view=diff
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java (original)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java Mon Sep 24 01:34:29 2007
@@ -33,6 +33,9 @@
 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
 import org.apache.commons.httpclient.methods.multipart.Part;
 import org.apache.commons.httpclient.methods.multipart.StringPart;
+import org.apache.maven.model.Build;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
 
 /**
  * Install an OSGi bundle to a running Sling instance.
@@ -41,7 +44,7 @@
  * @phase install
  * @description install an OSGi bundle jar to a running Sling instance
  */
-public class BundleInstallMojo extends AbstractBundlePostMojo {
+public class BundleInstallMojo extends AbstractBundleInstallMojo {
 
     /**
      * Whether to skip this step even though it has been configured in the
@@ -62,113 +65,19 @@
      */
     private String bundleFileName;
 
-    /**
-     * The URL of the running Sling instance.
-     *
-     * @parameter expression="${sling.url}" default-value="http://localhost:8080/sling"
-     * @required
-     */
-    private String slingUrl;
-    
-    /**
-     * The user name to authenticate at the running Sling instance.
-     *
-     * @parameter expression="${sling.user}" default-value="admin"
-     * @required
-     */
-    private String user;
-    
-    /**
-     * The password to authenticate at the running Sling instance.
-     *
-     * @parameter expression="${sling.password}" default-value="admin"
-     * @required
-     */
-    private String password;
-    
-    /**
-     * The startlevel for the uploaded bundle
-     *
-     * @parameter expression="${sling.bundle.startlevel}" default-value="20"
-     * @required
-     */
-    private String bundleStartLevel;
-    
-    /**
-     * Whether to start the uploaded bundle or not
-     *
-     * @parameter expression="${sling.bundle.start}" default-value="true"
-     * @required
-     */
-    private boolean bundleStart;
-
-	/**
-	 * Execute this Mojo
-	 */
-	public void execute() {
-	    // don't do anything, if this step is to be skipped
-	    if (skip) {
-	        getLog().debug("Skipping bundle installation as instructed");
-	        return;
-	    }
-
-        // only upload if packaging as an osgi-bundle
-        File bundleFile = new File(bundleFileName);
-        String bundleName = getBundleSymbolicName(bundleFile);
-        if (bundleName == null) {
-            getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
+    @Override
+    public void execute() throws MojoExecutionException {
+        // don't do anything, if this step is to be skipped
+        if (skip) {
+            getLog().debug("Skipping bundle installation as instructed");
             return;
         }
 
-        getLog().info("Installing Bundle " + bundleName + "(" + bundleFile + ") to " + slingUrl);
-        post(slingUrl, bundleFile);
-	}
-
-	private void post(String targetURL, File file) {
-
-        // append pseudo path after root URL to not get redirected for nothing
-        PostMethod filePost = new PostMethod(targetURL + "/install");
-
-        try {
-
-            List<Part> partList = new ArrayList<Part>();
-            partList.add(new StringPart("action", "install"));
-            partList.add(new StringPart("_noredir_", "_noredir_"));
-            partList.add(new FilePart("bundlefile", new FilePartSource(file.getName(), file)));
-            partList.add(new StringPart("bundlestartlevel", bundleStartLevel));
-            
-            if (bundleStart) {
-                partList.add(new StringPart("bundlestart", "start"));
-            }
-
-            Part[] parts = partList.toArray(new Part[partList.size()]);
-
-            filePost.setRequestEntity(new MultipartRequestEntity(parts,
-                filePost.getParams()));
-            HttpClient client = new HttpClient();
-            client.getHttpConnectionManager().getParams().setConnectionTimeout(
-                5000);
-            
-            // authentication stuff
-            client.getParams().setAuthenticationPreemptive(true);
-            Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
-            client.getState().setCredentials(AuthScope.ANY, defaultcreds);
-            
-            int status = client.executeMethod(filePost);
-            if (status == HttpStatus.SC_OK) {
-                getLog().info("Bundle installed");
-            } else {
-                getLog().error(
-                    "Installation failed, cause: " + HttpStatus.getStatusText(status));
-            }
-        } catch (ConnectException ce) {
-            getLog().info("Installation on " + targetURL + " failed, cause: " + ce.getMessage());
-            getLog().debug(ce); // dump on debug
-        } catch (Exception ex) {
-            getLog().error(ex.getClass().getName() + " " + ex.getMessage());
-            ex.printStackTrace();
-        } finally {
-            filePost.releaseConnection();
-        }
+        super.execute();
+    }
+    
+    @Override
+    protected String getBundleFileName() {
+        return bundleFileName;
     }
 }