You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:16:20 UTC

[sling-maven-sling-plugin] 07/28: SLING-798 : Initial support to create fs provider configs. New configs are currently posted on each sling:install

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag maven-sling-plugin-2.0.4-incubator
in repository https://gitbox.apache.org/repos/asf/sling-maven-sling-plugin.git

commit 91a1d2a3a0c183b7f98d0ba444f078fee3f3a32e
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Jan 2 11:21:19 2009 +0000

    SLING-798 : Initial support to create fs provider configs. New configs are currently posted on each sling:install
    
    git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/maven/maven-sling-plugin@730701 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  10 ++
 .../bundlesupport/AbstractBundleInstallMojo.java   | 164 ++++++++++++++++++++-
 2 files changed, 166 insertions(+), 8 deletions(-)

diff --git a/pom.xml b/pom.xml
index c7cac3a..ed3e7b9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -86,6 +86,16 @@
         </dependency>
 
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.2-incubator</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.0.2-incubator</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.maven</groupId>
             <artifactId>maven-plugin-api</artifactId>
             <version>2.0</version>
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
index 2ac5385..5da5bfe 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
@@ -19,11 +19,16 @@
 package org.apache.sling.maven.bundlesupport;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
 
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
@@ -33,22 +38,29 @@ 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.model.Resource;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.sling.commons.osgi.ManifestHeader;
+import org.apache.sling.commons.osgi.ManifestHeader.Entry;
 
 abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
 
+    private static final String HEADER_INITIAL_CONTENT = "Sling-Initial-Content";
+    private static final String FS_FACTORY = "org.apache.sling.fsprovider.FsResourceProvider";
+
     /**
      * The URL of the running Sling instance.
-     * 
+     *
      * @parameter expression="${sling.url}"
-     *            default-value="http://localhost:8080/sling"
+     *            default-value="http://localhost:8080/system/console"
      * @required
      */
     private String slingUrl;
 
     /**
      * The user name to authenticate at the running Sling instance.
-     * 
+     *
      * @parameter expression="${sling.user}" default-value="admin"
      * @required
      */
@@ -56,7 +68,7 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
 
     /**
      * The password to authenticate at the running Sling instance.
-     * 
+     *
      * @parameter expression="${sling.password}" default-value="admin"
      * @required
      */
@@ -64,7 +76,7 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
 
     /**
      * The startlevel for the uploaded bundle
-     * 
+     *
      * @parameter expression="${sling.bundle.startlevel}" default-value="20"
      * @required
      */
@@ -72,7 +84,7 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
 
     /**
      * Whether to start the uploaded bundle or not
-     * 
+     *
      * @parameter expression="${sling.bundle.start}" default-value="true"
      * @required
      */
@@ -86,6 +98,23 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
      */
     private boolean refreshPackages;
 
+    /**
+     * Whether to add the mapping for the fs provider
+     *
+     * @parameter expression="${sling.mountByFS}" default-value="true"
+     * @required
+     */
+    private boolean mountByFS;
+
+    /**
+     * The Maven project.
+     *
+     * @parameter expression="${project}"
+     * @required
+     * @readonly
+     */
+    protected MavenProject project;
+
     public AbstractBundleInstallMojo() {
         super();
     }
@@ -108,7 +137,10 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
         getLog().info(
             "Installing Bundle " + bundleName + "(" + bundleFile + ") to "
                 + slingUrl);
-        post(slingUrl, bundleFile);
+        configure(slingUrl, bundleFile);
+        if ( mountByFS ) {
+            post(slingUrl, bundleFile);
+        }
     }
 
     protected void post(String targetURL, File file)
@@ -129,7 +161,7 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
             if (bundleStart) {
                 partList.add(new StringPart("bundlestart", "start"));
             }
-            
+
             if (refreshPackages) {
                 partList.add(new StringPart("refreshPackages", "true"));
             }
@@ -163,4 +195,120 @@ abstract class AbstractBundleInstallMojo extends AbstractBundlePostMojo {
             filePost.releaseConnection();
         }
     }
+
+    protected void configure(String targetURL, File file)
+    throws MojoExecutionException {
+        // first, let's get the manifest and see if initial content is configured
+        ManifestHeader header = null;
+        try {
+            final Manifest mf = this.getManifest(file);
+            final String value = mf.getMainAttributes().getValue(HEADER_INITIAL_CONTENT);
+            if ( value == null ) {
+                getLog().debug("Bundle has no initial content - no file system provider config created.");
+                return;
+            }
+            header = ManifestHeader.parse(value);
+            if ( header == null || header.getEntries().length == 0 ) {
+                getLog().warn("Unable to parse header or header is empty: " + value);
+                return;
+            }
+        } catch (IOException ioe) {
+            throw new MojoExecutionException("Unable to read manifest from file " + file, ioe);
+        }
+        // setup http client
+        final 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);
+
+        getLog().info("Trying to configure file system provider...");
+        // quick check if resources are configured
+        final List resources = project.getResources();
+        if ( resources == null || resources.size() == 0 ) {
+            throw new MojoExecutionException("No resources configured for this project.");
+        }
+        final Entry[] entries = header.getEntries();
+        for(final Entry entry : entries) {
+            final String path = entry.getValue();
+            // check if we should ignore this
+            final String ignoreValue = entry.getDirectiveValue("maven:mount");
+            if ( ignoreValue != null && ignoreValue.equalsIgnoreCase("false") ) {
+                getLog().debug("Ignoring " + path);
+                continue;
+            }
+            String installPath = entry.getDirectiveValue("path");
+            if ( installPath == null ) {
+                installPath = "/";
+            }
+            // search the path in the resources (usually this should be the first resource
+            // entry but this might be reconfigured
+            File dir = null;
+            final Iterator i = resources.iterator();
+            while ( dir == null && i.hasNext() ) {
+                final Resource rsrc = (Resource)i.next();
+                dir = new File(rsrc.getDirectory(), path);
+                if ( !dir.exists() ) {
+                    dir = null;
+                }
+            }
+            if ( dir == null ) {
+                throw new MojoExecutionException("No resource entry found containing " + path);
+            }
+            // check for root mapping - which we don't support atm
+            if ( "/".equals(installPath) ) {
+                throw new MojoExecutionException("Mapping to root path not supported by fs provider at the moment. Please adapt your initial content configuration.");
+            }
+            getLog().info("Mapping " + dir + " to " + installPath);
+            final String postUrl = targetURL  + "/configMgr/" + FS_FACTORY;
+            final PostMethod post = new PostMethod(postUrl);
+            post.addParameter("apply", "true");
+            post.addParameter("factoryPid", FS_FACTORY);
+            post.addParameter("pid", "new");
+            post.addParameter("provider.file", dir.toString());
+            post.addParameter("provider.roots", installPath);
+            post.addParameter("propertylist", "provider.roots,provider.file");
+            try {
+                final int status = client.executeMethod(post);
+                // we get a moved temporarily back from the configMgr plugin
+                if (status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_OK) {
+                    getLog().info("Configuration created.");
+                } else {
+                    getLog().error(
+                        "Configuration failed, cause: "
+                            + HttpStatus.getStatusText(status));
+                }
+            } catch (HttpException ex) {
+                throw new MojoExecutionException("Configuration on " + postUrl
+                        + " failed, cause: " + ex.getMessage(), ex);
+            } catch (IOException ex) {
+                throw new MojoExecutionException("Configuration on " + postUrl
+                        + " failed, cause: " + ex.getMessage(), ex);
+            }
+        }
+    }
+
+    /**
+     * Get the manifest from the File.
+     * @param bundleFile The bundle jar
+     * @return The manifest.
+     * @throws IOException
+     */
+    protected Manifest getManifest(final File bundleFile) throws IOException {
+        JarFile file = null;
+        try {
+            file = new JarFile(bundleFile);
+            return file.getManifest();
+        } finally {
+            if (file != null) {
+                try {
+                    file.close();
+                } catch (IOException ignore) {
+                }
+            }
+        }
+    }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.