sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1616687 - 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/ core/src/test/java/org/apac...
Date Fri, 08 Aug 2014 08:26:45 GMT
Author: bdelacretaz
Date: Fri Aug  8 08:26:45 2014
New Revision: 1616687

URL: http://svn.apache.org/r1616687
Log:
By default, stop intepreting the crankstart file once framework is started, except on first
startup

Added:
    sling/trunk/contrib/crankstart/core/src/test/java/org/apache/sling/crankstart/core/commands/StartFrameworkTest.java
Modified:
    sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartCommandLine.java
    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/StartFramework.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/CrankstartCommandLine.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartCommandLine.java?rev=1616687&r1=1616686&r2=1616687&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartCommandLine.java
(original)
+++ sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartCommandLine.java
Fri Aug  8 08:26:45 2014
@@ -17,6 +17,7 @@
 package org.apache.sling.crankstart.api;
 
 import java.util.Dictionary;
+import java.util.Hashtable;
 
 /** A command line read from a crankstart txt file */
 public class CrankstartCommandLine {
@@ -24,10 +25,12 @@ public class CrankstartCommandLine {
     private final String qualifier;
     private final Dictionary<String, Object> properties;
     
+    private static final Dictionary<String, Object> EMPTY_PROPERTIES = new Hashtable<String,
Object>();
+    
     public CrankstartCommandLine(String verb, String qualifier, Dictionary<String, Object>
properties) {
         this.verb = verb;
         this.qualifier = qualifier;
-        this.properties = properties;
+        this.properties = properties == null ? EMPTY_PROPERTIES : properties;
     }
     
     @Override

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=1616687&r1=1616686&r2=1616687&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
Fri Aug  8 08:26:45 2014
@@ -36,6 +36,9 @@ public class CrankstartContext {
     /** Name of the default value used to set bundle start levels */
     public static final String DEFAULT_BUNDLE_START_LEVEL = "crankstart.bundle.start.level";
 
+    /** Name of the attribute that causes crankstart processing to stop */
+    public static final String ATTR_STOP_CRANKSTART_PROCESSING = "crankstart.stop.processing";
+
     public void setOsgiFrameworkProperty(String key, String value) {
         osgiFrameworkProperties.put(key, value);
     }

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=1616687&r1=1616686&r2=1616687&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
Fri Aug  8 08:26:45 2014
@@ -21,10 +21,8 @@ import java.io.FileReader;
 import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.Callable;
 
 import org.apache.sling.crankstart.api.CrankstartCommand;
@@ -139,6 +137,10 @@ public class CrankstartFileProcessor imp
                 }
             }
             
+            if(crankstartContext.getAttribute(CrankstartContext.ATTR_STOP_CRANKSTART_PROCESSING)
!= null) {
+                log.info("{} attribute is set, ignoring the remaining crankstart commands",
CrankstartContext.ATTR_STOP_CRANKSTART_PROCESSING);
+                break;
+            }
         }
     }
     

Modified: sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/StartFramework.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/StartFramework.java?rev=1616687&r1=1616686&r2=1616687&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/StartFramework.java
(original)
+++ sling/trunk/contrib/crankstart/core/src/main/java/org/apache/sling/crankstart/core/commands/StartFramework.java
Fri Aug  8 08:26:45 2014
@@ -28,6 +28,8 @@ public class StartFramework implements C
     public static final String I_START_FRAMEWORK = "start.framework";
     private final Logger log = LoggerFactory.getLogger(getClass());
     
+    public static final String EXIT_IF_NOT_FIRST = "exitIfNotFirstStartup";
+    
     @Override
     public boolean appliesTo(CrankstartCommandLine commandLine) {
         return I_START_FRAMEWORK.equals(commandLine.getVerb());
@@ -44,6 +46,40 @@ public class StartFramework implements C
         final FrameworkFactory frameworkFactory = (FrameworkFactory)getClass().getClassLoader().loadClass("org.apache.felix.framework.FrameworkFactory").newInstance();
         crankstartContext.setOsgiFramework(frameworkFactory.newFramework(crankstartContext.getOsgiFrameworkProperties()));
         crankstartContext.getOsgiFramework().start();
-        log.info("OSGi framework started");
+        final int nBundles = crankstartContext.getOsgiFramework().getBundleContext().getBundles().length;
+        log.info("OSGi framework started, {} bundles installed", nBundles);
+        
+        // Unless specified otherwise, stop processing the crankstart file if this is not
the first
+        // startup. Crankstart is meant for immutable instances.
+        if(stopProcessing(commandLine, nBundles)) {
+            crankstartContext.setAttribute(CrankstartContext.ATTR_STOP_CRANKSTART_PROCESSING,
true);
+        }
+    }
+    
+    /** True if we should stop processing the crankstart file according to
+     *  how many bundles are present after framework startup, combined
+     *  with cmd options.
+     */
+    boolean stopProcessing(CrankstartCommandLine cmd, int nBundles) {
+        boolean result = false;
+        final Object exitSetting = cmd.getProperties().get(EXIT_IF_NOT_FIRST);
+        final boolean doNotExit = exitSetting != null && !"true".equals(exitSetting);
+        
+        if(nBundles <= 0) {
+            throw new IllegalArgumentException("Expecting at least one installed bundle after
startup");
+        } else if(doNotExit) {
+            log.info("{}={}, will continue processing the crankstart file although this is
not the first startup", EXIT_IF_NOT_FIRST, exitSetting);
+        } else if (nBundles == 1) {
+            log.info(
+                    "Only {} bundle installed after framework startup, processing the full
crankstart file",
+                    nBundles);
+        } else {
+            result = true;
+            log.info(
+                    "{} bundles installed, ignoring the rest of the crankstart file (set
{}=false in this command's properties to disable this feature)", 
+                    nBundles,
+                    EXIT_IF_NOT_FIRST);
+        }
+        return result;
     }
 }

Added: sling/trunk/contrib/crankstart/core/src/test/java/org/apache/sling/crankstart/core/commands/StartFrameworkTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/core/src/test/java/org/apache/sling/crankstart/core/commands/StartFrameworkTest.java?rev=1616687&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/core/src/test/java/org/apache/sling/crankstart/core/commands/StartFrameworkTest.java
(added)
+++ sling/trunk/contrib/crankstart/core/src/test/java/org/apache/sling/crankstart/core/commands/StartFrameworkTest.java
Fri Aug  8 08:26:45 2014
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.crankstart.core.commands;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.sling.crankstart.api.CrankstartCommandLine;
+import org.junit.Test;
+
+/** Verify that factory configs are handled properly
+ *  when starting the OSGi framework multiple times
+ *  with Crankstart.
+ */
+public class StartFrameworkTest {
+
+    private static final Dictionary<String, Object> EXIT_OPT= new Hashtable<String,
Object>();
+    
+    {
+        EXIT_OPT.put(StartFramework.EXIT_IF_NOT_FIRST, "false");
+        EXIT_OPT.put("something else", "should not matter");
+    }
+    
+    private static void assertStopProcessing(CrankstartCommandLine cmd, int nBundles, boolean
expected) {
+        final boolean actual = new StartFramework().stopProcessing(cmd, nBundles); 
+        assertEquals("Expecting stop processing attribute to match", expected, actual);
+    }
+    
+    @Test(expected=IllegalArgumentException.class)
+    public void testZeroStartupNoOption() throws Exception {
+        assertStopProcessing(new CrankstartCommandLine(StartFramework.I_START_FRAMEWORK,
"", null), 0,  false);
+    }
+    
+    @Test
+    public void testFirstStartupNoOption() throws Exception {
+        assertStopProcessing(new CrankstartCommandLine(StartFramework.I_START_FRAMEWORK,
"", null), 1,  false);
+    }
+    
+    @Test
+    public void testFirstStartupExitOption() throws Exception {
+        assertStopProcessing(new CrankstartCommandLine(StartFramework.I_START_FRAMEWORK,
"", EXIT_OPT), 1,  false);
+    }
+    
+    @Test
+    public void testSecondStartupNoOption() throws Exception {
+        assertStopProcessing(new CrankstartCommandLine(StartFramework.I_START_FRAMEWORK,
"", null), 12,  true);
+    }
+    
+    @Test
+    public void testSecondStartupExitOption() throws Exception {
+        assertStopProcessing(new CrankstartCommandLine(StartFramework.I_START_FRAMEWORK,
"", EXIT_OPT), 42,  false);
+    }
+    
+}

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=1616687&r1=1616686&r2=1616687&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 Fri
Aug  8 08:26:45 2014
@@ -8,6 +8,7 @@ defaults felix.http.jetty.version 2.2.0
 # Bootstrap classpath (variables are not supported here)
 classpath mvn:org.apache.felix/org.apache.felix.framework/4.4.0
 classpath mvn:org.slf4j/slf4j-api/1.6.2
+classpath mvn:org.slf4j/slf4j-simple/1.6.2
 classpath mvn:org.apache.sling/org.apache.sling.crankstart.core/0.0.1-SNAPSHOT
 classpath mvn:org.apache.sling/org.apache.sling.crankstart.api/0.0.1-SNAPSHOT
 
@@ -15,7 +16,8 @@ classpath mvn:org.apache.sling/org.apach
 osgi.property org.osgi.service.http.port ${http.port}
 osgi.property org.osgi.framework.storage ${osgi.storage.path}
 
-# Start the framework
+# Start the framework. Crankstart file processing will stop here
+# if this is not the first startup.
 start.framework
 
 # Start ConfigAdmin, HTTP service and SCR



Mime
View raw message