You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by st...@apache.org on 2014/07/15 09:53:43 UTC
svn commit: r1610606 - in
/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools:
osgi/WebconsoleClient.java sling/BundlesInstaller.java
Author: stefanegli
Date: Tue Jul 15 07:53:42 2014
New Revision: 1610606
URL: http://svn.apache.org/r1610606
Log:
SLING-3760 : improved installBundles with checks as to whether the bundle already exists on the server. If it exists in the very same version (excluding SNAPSHOT), then the bundle is left untouched. If it exists but in a different version or a SNAPSHOT version, the bundle is first uninstalled, then installed. If it doesn't exist yet, then it is just installed (as was done before)
Modified:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java
Modified: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java?rev=1610606&r1=1610605&r2=1610606&view=diff
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java (original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java Tue Jul 15 07:53:42 2014
@@ -40,6 +40,8 @@ public class WebconsoleClient {
private final String username;
private final String password;
+ public static final String JSON_KEY_ID = "id";
+ public static final String JSON_KEY_VERSION = "version";
public static final String JSON_KEY_DATA = "data";
public static final String JSON_KEY_STATE = "state";
public static final String CONSOLE_BUNDLES_PATH = "/system/console/bundles";
@@ -51,6 +53,18 @@ public class WebconsoleClient {
this.password = password;
}
+ public void uninstallBundle(String symbolicName, File f) throws Exception {
+ final long bundleId = getBundleId(symbolicName);
+
+ final MultipartEntity entity = new MultipartEntity();
+ entity.addPart("action",new StringBody("uninstall"));
+ executor.execute(
+ builder.buildPostRequest(CONSOLE_BUNDLES_PATH+"/"+bundleId)
+ .withCredentials(username, password)
+ .withEntity(entity)
+ ).assertStatus(200);
+ }
+
/** Install a bundle using the Felix webconsole HTTP interface, with a specific start level */
public void installBundle(File f, boolean startBundle) throws Exception {
installBundle(f, startBundle, 0);
@@ -92,8 +106,7 @@ public class WebconsoleClient {
new RetryingContentChecker(executor, builder).check(path, 200, timeoutSeconds, 500);
}
- /** Get specified bundle state */
- public String getBundleState(String symbolicName) throws Exception {
+ private JSONObject getBundleData(String symbolicName) throws Exception {
// This returns a data structure like
// {"status":"Bundle information: 173 bundles in total - all 173 bundles active.","s":[173,171,2,0,0],"data":
// [
@@ -118,6 +131,24 @@ public class WebconsoleClient {
if(!bundle.has(JSON_KEY_STATE)) {
fail(path + ".data[0].state missing, JSON content=" + content);
}
+ return bundle;
+ }
+
+ /** Get bundle id */
+ public long getBundleId(String symbolicName) throws Exception {
+ final JSONObject bundle = getBundleData(symbolicName);
+ return bundle.getLong(JSON_KEY_ID);
+ }
+
+ /** Get bundle version **/
+ public String getBundleVersion(String symbolicName) throws Exception {
+ final JSONObject bundle = getBundleData(symbolicName);
+ return bundle.getString(JSON_KEY_VERSION);
+ }
+
+ /** Get specified bundle state */
+ public String getBundleState(String symbolicName) throws Exception {
+ final JSONObject bundle = getBundleData(symbolicName);
return bundle.getString(JSON_KEY_STATE);
}
Modified: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java?rev=1610606&r1=1610605&r2=1610606&view=diff
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java (original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java Tue Jul 15 07:53:42 2014
@@ -40,10 +40,52 @@ public class BundlesInstaller {
public BundlesInstaller(WebconsoleClient wcc) {
webconsoleClient = wcc;
}
+
+ public boolean isInstalled(File bundleFile) throws Exception {
+ final String bundleSymbolicName = getBundleSymbolicName(bundleFile);
+ try{
+ log.debug("Checking if installed: "+bundleSymbolicName);
+ webconsoleClient.checkBundleInstalled(bundleSymbolicName, 1);
+ // if this succeeds, then there's no need to install again
+ log.debug("Already installed: "+bundleSymbolicName);
+ return true;
+ } catch(AssertionError e) {
+ log.debug("Not yet installed: "+bundleSymbolicName);
+ return false;
+ }
+
+ }
+
+ /** Check if the installed version matches the one of the bundle (file) **/
+ public boolean isInstalledWithSameVersion(File bundleFile) throws Exception {
+ final String bundleSymbolicName = getBundleSymbolicName(bundleFile);
+ final String versionOnServer = webconsoleClient.getBundleVersion(bundleSymbolicName);
+ final String versionInBundle = getBundleVersion(bundleFile);
+ if (versionOnServer.equals(versionInBundle)) {
+ return true;
+ } else {
+ log.info("Bundle installed doesn't match: "+bundleSymbolicName+
+ ", versionOnServer="+versionOnServer+", versionInBundle="+versionInBundle);
+ return false;
+ }
+ }
/** Install a list of bundles supplied as Files */
public void installBundles(List<File> toInstall, boolean startBundles) throws Exception {
for(File f : toInstall) {
+ final String bundleSymbolicName = getBundleSymbolicName(f);
+ if (isInstalled(f)) {
+ if (f.getName().contains("SNAPSHOT")) {
+ log.info("Reinstalling (due to SNAPSHOT version): "+bundleSymbolicName);
+ webconsoleClient.uninstallBundle(bundleSymbolicName, f);
+ } else if (!isInstalledWithSameVersion(f)) {
+ log.info("Reinstalling (due to version mismatch): "+bundleSymbolicName);
+ webconsoleClient.uninstallBundle(bundleSymbolicName, f);
+ } else {
+ log.info("Not reinstalling: "+bundleSymbolicName);
+ continue;
+ }
+ }
webconsoleClient.installBundle(f, startBundles);
}
log.info("{} additional bundles installed from {}", toInstall.size(), toInstall.get(0).getAbsolutePath());
@@ -105,4 +147,19 @@ public class BundlesInstaller {
}
return name;
}
+
+ public String getBundleVersion(File bundleFile) throws IOException {
+ String version = null;
+ final JarInputStream jis = new JarInputStream(new FileInputStream(bundleFile));
+ try {
+ final Manifest m = jis.getManifest();
+ if(m == null) {
+ fail("Manifest is null in " + bundleFile.getAbsolutePath());
+ }
+ version = m.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
+ } finally {
+ jis.close();
+ }
+ return version;
+ }
}
\ No newline at end of file