sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1614923 - in /sling/trunk/contrib/crankstart: api/src/main/java/org/apache/sling/crankstart/api/ core/src/main/java/org/apache/sling/crankstart/core/ core/src/main/java/org/apache/sling/crankstart/core/commands/ launcher/ launcher/src/test...
Date Thu, 31 Jul 2014 15:32:42 GMT
Author: bdelacretaz
Date: Thu Jul 31 15:32:42 2014
New Revision: 1614923

URL: http://svn.apache.org/r1614923
Log:
Add support for bundle start levels, and move default values to CrankstartContext

Modified:
    sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartContext.java
    sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java
    sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/Defaults.java
    sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java
    sling/trunk/contrib/crankstart/launcher/pom.xml
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
    sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt

Modified: sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartContext.java?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartContext.java
(original)
+++ sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartContext.java
Thu Jul 31 15:32:42 2014
@@ -31,6 +31,11 @@ public class CrankstartContext {
     private final Map<String, String> osgiFrameworkProperties = new HashMap<String,
String>();
     private final Map<String, Object> attributes = new HashMap<String, Object>();
     
+    private final Map<String, String> defaults = new HashMap<String, String>();
+    
+    /** Name of the default value used to set bundle start levels */
+    public static final String DEFAULT_BUNDLE_START_LEVEL = "crankstart.bundle.start.level";
+
     public void setOsgiFrameworkProperty(String key, String value) {
         osgiFrameworkProperties.put(key, value);
     }
@@ -75,4 +80,8 @@ public class CrankstartContext {
     public Object getAttribute(String key) {
         return attributes.get(key);
     }
+    
+    public Map<String, String> getDefaults() {
+        return defaults;
+    }
 }
\ No newline at end of file

Modified: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java
(original)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/CrankstartFileProcessor.java
Thu Jul 31 15:32:42 2014
@@ -50,7 +50,6 @@ import org.slf4j.LoggerFactory;
 public class CrankstartFileProcessor implements Callable<Object> {
     private final CrankstartContext crankstartContext;
     private final Logger log = LoggerFactory.getLogger(getClass());
-    private Map<String, String> defaults = new HashMap<String, String>();
     
     private List<CrankstartCommand> builtinCommands = new ArrayList<CrankstartCommand>();
     private List<CrankstartCommand> extensionCommands = new ArrayList<CrankstartCommand>();
@@ -67,7 +66,7 @@ public class CrankstartFileProcessor imp
         builtinCommands.add(new StartBundles());
         builtinCommands.add(new StartFramework());
         builtinCommands.add(new Configure());
-        builtinCommands.add(new Defaults(defaults));
+        builtinCommands.add(new Defaults());
         
         // Need a null "classpath" command as our launcher uses it
         // outside of the usual command mechanism - it shouldn't cause
@@ -108,7 +107,7 @@ public class CrankstartFileProcessor imp
             protected String getVariable(String name) {
                 String result = System.getProperty(name);
                 if(result == null) {
-                    result = defaults.get(name);
+                    result = crankstartContext.getDefaults().get(name);
                 }
                 if(result == null) {
                     result = super.getVariable(name); 

Modified: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/Defaults.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/Defaults.java?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/Defaults.java
(original)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/Defaults.java
Thu Jul 31 15:32:42 2014
@@ -27,13 +27,8 @@ import org.slf4j.LoggerFactory;
 /** CrankstartCommand that sets a default value for a variable */
 public class Defaults implements CrankstartCommand {
     public static final String I_DEFAULTS = "defaults";
-    private final Map<String, String> variables;
     private final Logger log = LoggerFactory.getLogger(getClass());
     
-    public Defaults(Map<String, String> variables) {
-        this.variables = variables;
-    }
-    
     public String getDescription() {
         return I_DEFAULTS + ": set the default value of a crankstart variable";
     }
@@ -55,7 +50,7 @@ public class Defaults implements Crankst
             sb.append(parts[i]);
         }
         final String value = sb.toString();
-        variables.put(key, value);
+        crankstartContext.getDefaults().put(key, value);
         log.info("[{}] has default value [{}]", key, value);
     }
 }

Modified: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java
(original)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/InstallBundle.java
Thu Jul 31 15:32:42 2014
@@ -22,7 +22,9 @@ import java.net.URL;
 import org.apache.sling.crankstart.api.CrankstartCommand;
 import org.apache.sling.crankstart.api.CrankstartCommandLine;
 import org.apache.sling.crankstart.api.CrankstartContext;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.startlevel.BundleStartLevel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,10 +49,35 @@ public class InstallBundle implements Cr
         final BundleContext ctx = crankstartContext.getOsgiFramework().getBundleContext();
         final InputStream bundleStream = url.openStream();
         try {
-            ctx.installBundle(bundleRef, url.openStream());
-            log.info("bundle installed: {}", bundleRef);
+            final Bundle b = ctx.installBundle(bundleRef, url.openStream());
+            
+            final int level = getStartLevel(crankstartContext);
+            if(level > 0) {
+                final BundleStartLevel bsl = (BundleStartLevel)b.adapt(BundleStartLevel.class);
+                if(bsl == null) {
+                    log.warn("Bundle does not adapt to BundleStartLevel, cannot set start
level", bundleRef);
+                }
+                bsl.setStartLevel(level);
+            }
+            
+            log.info("bundle installed at start level {}: {}", level, bundleRef);
         } finally {
             bundleStream.close();
         }
     }
+    
+    private int getStartLevel(CrankstartContext ctx) {
+        int result = 0;
+        final String str = ctx.getDefaults().get(CrankstartContext.DEFAULT_BUNDLE_START_LEVEL);
+        if(str == null) {
+            log.debug("{} default value is not set, using default bundle start level", CrankstartContext.DEFAULT_BUNDLE_START_LEVEL);
+        } else {
+            try {
+                result = Integer.valueOf(str);
+            } catch(NumberFormatException nfe) {
+                log.warn("Invalid {} value [{}], will use default bundle start level", CrankstartContext.DEFAULT_BUNDLE_START_LEVEL,
str);
+            }
+        }
+        return result;
+    }
 }

Modified: sling/trunk/contrib/crankstart/launcher/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/pom.xml?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/pom.xml (original)
+++ sling/trunk/contrib/crankstart/launcher/pom.xml Thu Jul 31 15:32:42 2014
@@ -152,6 +152,12 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.6</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.1</version>

Modified: sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
(original)
+++ sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
Thu Jul 31 15:32:42 2014
@@ -22,6 +22,8 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.util.EntityUtils;
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.commons.testing.junit.Retry;
 import org.apache.sling.commons.testing.junit.RetryRule;
 import org.junit.AfterClass;
@@ -242,6 +244,43 @@ public class CrankstartBootstrapTest {
         }
     }
     
+    @Test
+    @Retry(timeoutMsec=10000, intervalMsec=250)
+    public void testSpecificStartLevel() throws Exception {
+        // Verify that this bundle is only installed, as it's set to start level 99
+        setAdminCredentials();
+        final String path = "/system/console/bundles/org.apache.commons.collections.json";
+        final HttpUriRequest get = new HttpGet(baseUrl + path);
+        HttpResponse response = null;
+        try {
+            response = client.execute(get);
+            assertEquals("Expecting bundle status to be available at " + get.getURI(), 200,
response.getStatusLine().getStatusCode());
+            assertNotNull("Expecting response entity", response.getEntity());
+            String encoding = "UTF-8";
+            if(response.getEntity().getContentEncoding() != null) {
+                encoding = response.getEntity().getContentEncoding().getValue();
+            }
+            final String content = IOUtils.toString(response.getEntity().getContent(), encoding);
+            
+            // Start level is in the props array, with key="Start Level"
+            final JSONObject status = new JSONObject(content);
+            final JSONArray props = status.getJSONArray("data").getJSONObject(0).getJSONArray("props");
+            final String KEY = "key";
+            final String SL = "Start Level";
+            boolean found = false;
+            for(int i=0; i < props.length(); i++) {
+                final JSONObject o = props.getJSONObject(i);
+                if(o.has(KEY) && SL.equals(o.getString(KEY))) {
+                    found = true;
+                    assertEquals("Expecting the start level that we set", "99", o.getString("value"));
+                }
+            }
+            assertTrue("Expecting start level to be found in JSON output", found);
+        } finally {
+            closeConnection(response);
+        }
+    }
+    
     private static String getOsgiStoragePath() {
         final File tmpRoot = new File(System.getProperty("java.io.tmpdir"));
         final Random random = new Random();

Modified: sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt?rev=1614923&r1=1614922&r2=1614923&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt (original)
+++ sling/trunk/contrib/crankstart/launcher/src/test/resources/launcher-test.crank.txt Thu
Jul 31 15:32:42 2014
@@ -42,6 +42,11 @@ bundle mvn:org.apache.sling/org.apache.s
 bundle mvn:org.apache.sling/org.apache.sling.crankstart.sling.extensions/0.0.1-SNAPSHOT
 bundle mvn:commons-io/commons-io/2.4
 
+# Install a bundle at a start level higher than the current one
+# to be able to check that it's not active
+defaults crankstart.bundle.start.level 99
+bundle mvn:commons-collections/commons-collections/3.2.1
+
 # Now start our bundles
 start.all.bundles
 



Mime
View raw message