Author: cziegeler
Date: Wed Jan 14 13:09:21 2009
New Revision: 734513
URL: http://svn.apache.org/viewvc?rev=734513&view=rev
Log:
SLING-798 : Get available configurations from web console before creating
configs
Modified:
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/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=734513&r1=734512&r2=734513&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 Jan 14 13:09:21 2009
@@ -21,8 +21,10 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
@@ -32,6 +34,7 @@
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.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.FilePartSource;
@@ -41,6 +44,9 @@
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.osgi.ManifestHeader;
import org.apache.sling.commons.osgi.ManifestHeader.Entry;
@@ -137,9 +143,9 @@
getLog().info(
"Installing Bundle " + bundleName + "(" + bundleFile + ") to "
+ slingUrl);
- configure(slingUrl, bundleFile);
+ post(slingUrl, bundleFile);
if ( mountByFS ) {
- post(slingUrl, bundleFile);
+ configure(slingUrl, bundleFile);
}
}
@@ -221,7 +227,7 @@
// authentication stuff
client.getParams().setAuthenticationPreemptive(true);
- Credentials defaultcreds = new UsernamePasswordCredentials(user,
+ final Credentials defaultcreds = new UsernamePasswordCredentials(user,
password);
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
@@ -231,6 +237,13 @@
if ( resources == null || resources.size() == 0 ) {
throw new MojoExecutionException("No resources configured for this
project.");
}
+ // now get current configurations
+ final Map configs = this.getCurrentFileProviderConfigs(targetURL,
client);
+ final Iterator configIter = configs.keySet().iterator();
+ while ( configIter.hasNext() ) {
+ final String key = configIter.next().toString();
+ getLog().info("Found " + key + " : " + configs.get(key));
+ }
final Entry[] entries = header.getEntries();
for(final Entry entry : entries) {
final String path = entry.getValue();
@@ -292,6 +305,51 @@
}
/**
+ * Return all file provider configs for this project
+ * @param targetURL The targetURL of the webconsole
+ * @param client The http client
+ * @return A map (may be empty) with the pids as keys and the path as value
+ * @throws MojoExecutionException
+ */
+ protected Map getCurrentFileProviderConfigs(final String targetURL, final
HttpClient client)
+ throws MojoExecutionException {
+ getLog().debug("Getting current file provider configurations.");
+ final Map result = new HashMap();
+ final String getUrl = targetURL + "/configMgr/(service.factoryPid=" +
FS_FACTORY + ").json";
+ final GetMethod get = new GetMethod(getUrl);
+
+ try {
+ final int status = client.executeMethod(get);
+ if ( status == 200 )
+ {
+ final String jsonText = get.getResponseBodyAsString();
+ try {
+ JSONArray array = new JSONArray(jsonText);
+ for(int i=0; i<array.length(); i++) {
+ final JSONObject obj = array.getJSONObject(i);
+ final String pid = obj.getString("pid");
+ final String path =
obj.getJSONObject("provider.file").getString("value");
+ if ( path != null &&
path.startsWith(this.project.getBasedir().getAbsolutePath()) ) {
+ getLog().debug("Found configuration with pid: " +
pid + ", path: " + path);
+ result.put(pid, path);
+ }
+ }
+ } catch (JSONException ex) {
+ throw new MojoExecutionException("Reading configuration
from " + getUrl
+ + " failed, cause: " + ex.getMessage(), ex);
+ }
+ }
+ } catch (HttpException ex) {
+ throw new MojoExecutionException("Reading configuration from " +
getUrl
+ + " failed, cause: " + ex.getMessage(), ex);
+ } catch (IOException ex) {
+ throw new MojoExecutionException("Reading configuration from " +
getUrl
+ + " failed, cause: " + ex.getMessage(), ex);
+ }
+ return result;
+ }
+
+ /**
* Get the manifest from the File.
* @param bundleFile The bundle jar
* @return The manifest.