support variables in crankstart files, with default values

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1595123 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 1384639..e120a5b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -113,7 +113,7 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-simple</artifactId>
             <version>1.7.6</version>
-            <scope>provided</scope>
+            <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.ops4j.pax.url</groupId>
diff --git a/sling.crank.txt b/sling.crank.txt
index c698b2a..51aa387 100644
--- a/sling.crank.txt
+++ b/sling.crank.txt
@@ -2,6 +2,9 @@
 # TODO: not all integration tests pass, we might be missing some
 # bundles or properties that the Sling launchpad provides
 
+# Set default values for our variables
+defaults http.port 12345
+
 # Set bootstrap classpath, mvn: protocol can be used
 classpath mvn:org.apache.felix/org.apache.felix.framework/4.4.0
 classpath mvn:org.osgi/org.osgi.compendium/4.2.0
@@ -15,7 +18,7 @@
 classpath mvn:org.apache.sling/org.apache.sling.crankstart.api/0.0.1-SNAPSHOT
 
 # Set OSGi framework properties
-osgi.property org.osgi.service.http.port 1234
+osgi.property org.osgi.service.http.port ${http.port}
 osgi.property sling.home sling-crankstart
 osgi.property org.osgi.framework.storage sling-crankstart/osgi.framework.storage
 osgi.property org.apache.sling.commons.log.level INFO
@@ -148,4 +151,4 @@
 
 # Now start bundles
 start.all.bundles
-log Sling will be available at http://localhost:1234/system/console
+log Sling will be available at http://localhost:${http.port}/system/console
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
index 389f606..d716d45 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
@@ -6,6 +6,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
+import java.net.ServerSocket;
 import java.util.Random;
 
 import org.apache.commons.httpclient.HttpClient;
@@ -23,7 +24,7 @@
  */
 public class CrankstartBootstrapTest {
     
-    private static final int port = Integer.valueOf(System.getProperty("test.http.port", "12345"));
+    private static final int port = getAvailablePort();
     private static final HttpClient client = new HttpClient();
     private static Thread crankstartThread;
     private static String baseUrl = "http://localhost:" + port;
@@ -31,6 +32,24 @@
     @Rule
     public final RetryRule retryRule = new RetryRule();
     
+    private static int getAvailablePort() {
+        int result = -1;
+        ServerSocket s = null;
+        try {
+            try {
+                s = new ServerSocket(0);
+                result = s.getLocalPort();
+            } finally {
+                if(s != null) {
+                    s.close();
+                }
+            }
+        } catch(Exception e) {
+            throw new RuntimeException("getAvailablePort failed", e);
+        }
+        return result;
+    }
+    
     private final static String CRANKSTART = 
         "classpath mvn:org.apache.felix/org.apache.felix.framework/4.4.0\n"
         + "classpath mvn:org.slf4j/slf4j-api/1.6.2\n"
@@ -38,7 +57,7 @@
         + "classpath mvn:org.ops4j.pax.url/pax-url-commons/1.6.0\n"
         + "classpath mvn:org.apache.sling/org.apache.sling.crankstart.core/0.0.1-SNAPSHOT\n"
         + "classpath mvn:org.apache.sling/org.apache.sling.crankstart.api/0.0.1-SNAPSHOT\n"
-        + "osgi.property org.osgi.service.http.port " + port + "\n"
+        + "osgi.property org.osgi.service.http.port ${http.port}\n"
         + "osgi.property org.osgi.framework.storage " + getOsgiStoragePath() + "\n"
         + "start.framework\n"
         + "bundle mvn:org.apache.felix/org.apache.felix.http.jetty/2.2.0\n"
@@ -52,12 +71,13 @@
         + "config org.apache.sling.crankstart.testservices.SingleConfigServlet\n"
         + "  path=/single\n"
         + "  message=doesn't matter\n"
-        + "log felix http service should come up at http://localhost:" + port + "\n"
+        + "log felix http service should come up at http://localhost:${http.port}\n"
     ;
     
     @BeforeClass
     public static void setup() {
         final GetMethod get = new GetMethod(baseUrl);
+        System.setProperty("http.port", String.valueOf(port));
         
         try {
             client.executeMethod(get);