SLING-4728 - move new Crankstart (that uses provisioning model) under contrib

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1684874 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..5842bb6
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>22</version>
+        <relativePath />
+    </parent>
+    
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.crankstart3.test.services</artifactId>
+    <version>1.0.3-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+    <name>Apache Sling Crankstart Test Services</name>
+    <description>Bundle used to test the Crankstart launcher</description>
+
+    <scm>
+      <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/contrib/crankstart/test-services</connection>
+      <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/contrib/crankstart/test-services</developerConnection>
+      <url>http://sling.apache.org</url>
+    </scm>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Private-Package>org.apache.sling.crankstart.testservices.*</Private-Package>
+                        <Export-Package></Export-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/src/main/java/org/apache/sling/crankstart/testservices/ConfigDumpServlet.java b/src/main/java/org/apache/sling/crankstart/testservices/ConfigDumpServlet.java
new file mode 100644
index 0000000..a24ce3d
--- /dev/null
+++ b/src/main/java/org/apache/sling/crankstart/testservices/ConfigDumpServlet.java
@@ -0,0 +1,102 @@
+/*
+ * 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.testservices;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+/** Dump configs, for testing
+ */
+@Component(immediate=true,metatype=true)
+@Service(value=Servlet.class)
+@Reference(name="httpService",referenceInterface=HttpService.class)
+public class ConfigDumpServlet extends TestServlet {
+    private static final long serialVersionUID = -6918378772515948581L;
+    
+    @Reference
+    private ConfigurationAdmin configAdmin;
+
+    @Activate
+    protected void activate(Map<String, Object> config) throws ServletException, NamespaceException {
+        message = "no message yet";
+        path = "/test/config";
+        register();
+    }
+    
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        final String configPid = req.getPathInfo().substring(1);
+        final Configuration cfg = configAdmin.getConfiguration(configPid);
+
+        if(cfg == null || cfg.getProperties() == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND, 
+                    "No config found with PID=" + configPid
+                    + " (the PID is extracted from the path information that follows this servlet's path");
+            return;
+        }
+        
+        final SortedSet<String> keys = new TreeSet<String>();
+        final Enumeration<?> e = cfg.getProperties().keys();
+        while(e.hasMoreElements()) {
+            keys.add(e.nextElement().toString());
+        }
+        final StringBuilder b = new StringBuilder();
+        b.append(configPid).append("#");
+        for(String key : keys) {
+            final Object value = cfg.getProperties().get(key);
+            b.append(key)
+            .append("=(")
+            .append(value.getClass().getSimpleName())
+            .append(")")
+            .append(prettyprint(value))
+            .append("#")
+            ;
+        }
+        b.append("#EOC#");
+        
+        resp.setContentType("text/plain");
+        resp.setCharacterEncoding("UTF-8");
+        resp.getWriter().write(b.toString());
+        resp.getWriter().flush();
+    }
+    
+    private static String prettyprint(Object value) {
+        if(value instanceof String []) {
+            return Arrays.asList((String[])value).toString();
+        } else {
+            return value.toString();
+        }
+    }
+}
diff --git a/src/main/java/org/apache/sling/crankstart/testservices/ConfigFactoryServlet.java b/src/main/java/org/apache/sling/crankstart/testservices/ConfigFactoryServlet.java
new file mode 100644
index 0000000..105bc7f
--- /dev/null
+++ b/src/main/java/org/apache/sling/crankstart/testservices/ConfigFactoryServlet.java
@@ -0,0 +1,61 @@
+/*
+ * 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.testservices;
+
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+/** Servlet that requires a configuration, used to test the
+ *  Crankstart initial config feature with factory configs
+ */
+@Component(immediate=true,configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true)
+@Service(value=Servlet.class)
+@Reference(name="httpService",referenceInterface=HttpService.class)
+public class ConfigFactoryServlet extends TestServlet {
+    private static final long serialVersionUID = -6918378772515948579L;
+    
+    @Property(value="default message")
+    protected static final String PROP_MESSAGE = "message";
+    
+    @Property(value="/default_path")
+    protected static final String PROP_PATH = "path";
+    
+    @Activate
+    protected void activate(Map<String, Object> config) throws ServletException, NamespaceException {
+        message = PropertiesUtil.toString(config.get(PROP_MESSAGE), "no message");
+        path = PropertiesUtil.toString(config.get(PROP_PATH), "no path");
+        register();
+    }
+    
+    @Deactivate
+    protected void deactivate(Map<String, Object> config) throws ServletException, NamespaceException {
+        unregister();
+    }
+}
diff --git a/src/main/java/org/apache/sling/crankstart/testservices/SingleConfigServlet.java b/src/main/java/org/apache/sling/crankstart/testservices/SingleConfigServlet.java
new file mode 100644
index 0000000..c01eeb1
--- /dev/null
+++ b/src/main/java/org/apache/sling/crankstart/testservices/SingleConfigServlet.java
@@ -0,0 +1,60 @@
+/*
+ * 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.testservices;
+
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+/** Servlet with a non-factory configuration, used to test the
+ *  Crankstart initial config feature
+ */
+@Component(immediate=true,metatype=true)
+@Service(value=Servlet.class)
+@Reference(name="httpService",referenceInterface=HttpService.class)
+public class SingleConfigServlet extends TestServlet {
+    private static final long serialVersionUID = -6918378772515948581L;
+    
+    @Property(value="default message")
+    protected static final String PROP_MESSAGE = "message";
+    
+    @Property(value="/default_path")
+    protected static final String PROP_PATH = "path";
+    
+    @Activate
+    protected void activate(Map<String, Object> config) throws ServletException, NamespaceException {
+        message = PropertiesUtil.toString(config.get(PROP_MESSAGE), "no message");
+        path = PropertiesUtil.toString(config.get(PROP_PATH), "/no_path");
+        register();
+    }
+    
+    @Deactivate
+    protected void deactivate(Map<String, Object> config) throws ServletException, NamespaceException {
+        unregister();
+    }
+}
diff --git a/src/main/java/org/apache/sling/crankstart/testservices/TestServlet.java b/src/main/java/org/apache/sling/crankstart/testservices/TestServlet.java
new file mode 100644
index 0000000..3d03789
--- /dev/null
+++ b/src/main/java/org/apache/sling/crankstart/testservices/TestServlet.java
@@ -0,0 +1,65 @@
+/*
+ * 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.testservices;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Base class for our test servlets */
+abstract class TestServlet extends HttpServlet {
+    private static final long serialVersionUID = -6918378772515948578L;
+    protected String message;
+    protected String path;
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+    
+    protected HttpService httpService;
+    
+    protected void register() throws ServletException, NamespaceException {
+        httpService.registerServlet(path, this, null, null);
+        log.info("Registered {} on path {}", this, path);
+    }
+    
+    protected void unregister() throws ServletException, NamespaceException {
+        httpService.unregister(path);
+        log.info("Unregistered path {}", path);
+    }
+    
+    protected void bindHttpService(HttpService s) {
+        httpService = s;
+    }
+    
+    protected void unbindHttpService(HttpService s) {
+        httpService = null;
+    }
+    
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        resp.setContentType("text/plain");
+        resp.setCharacterEncoding("UTF-8");
+        resp.getWriter().write(getClass().getSimpleName() + ":" + message);
+        resp.getWriter().flush();
+    }
+}