Author: cziegeler Date: Fri Jan 2 03:21:19 2009 New Revision: 730701 URL: http://svn.apache.org/viewvc?rev=730701&view=rev Log: SLING-798 : Initial support to create fs provider configs. New configs are currently posted on each sling:install
Modified: incubator/sling/trunk/maven/maven-sling-plugin/pom.xml incubator/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java Modified: incubator/sling/trunk/maven/maven-sling-plugin/pom.xml URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven/maven-sling-plugin/pom.xml?rev=730701&r1=730700&r2=730701&view=diff ============================================================================== --- incubator/sling/trunk/maven/maven-sling-plugin/pom.xml (original) +++ incubator/sling/trunk/maven/maven-sling-plugin/pom.xml Fri Jan 2 03:21:19 2009 @@ -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> 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=730701&r1=730700&r2=730701&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 Fri Jan 2 03:21:19 2009 @@ -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.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 @@ /** * The password to authenticate at the running Sling instance. - * + * * @parameter expression="${sling.password}" default-value="admin" * @required */ @@ -64,7 +76,7 @@ /** * The startlevel for the uploaded bundle - * + * * @parameter expression="${sling.bundle.startlevel}" default-value="20" * @required */ @@ -72,7 +84,7 @@ /** * Whether to start the uploaded bundle or not - * + * * @parameter expression="${sling.bundle.start}" default-value="true" * @required */ @@ -86,6 +98,23 @@ */ 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 @@ 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 @@ if (bundleStart) { partList.add(new StringPart("bundlestart", "start")); } - + if (refreshPackages) { partList.add(new StringPart("refreshPackages", "true")); } @@ -163,4 +195,120 @@ 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