Author: bdelacretaz
Date: Tue Jul 1 10:56:26 2014
New Revision: 1607039
URL: http://svn.apache.org/r1607039
Log:
Fix registration of multiple resources via the Sling installer
Added:
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerRegisterCommand.java
Modified:
sling/trunk/contrib/crankstart/api/src/main/java/org/apache/sling/crankstart/api/CrankstartContext.java
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
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerResourceCommand.java
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=1607039&r1=1607038&r2=1607039&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
Tue Jul 1 10:56:26 2014
@@ -25,6 +25,7 @@ import org.osgi.framework.launch.Framewo
public class CrankstartContext {
private Framework osgiFramework;
private final Map<String, String> osgiFrameworkProperties = new
HashMap<String, String>();
+ private final Map<String, Object> attributes = new HashMap<String,
Object>();
public void setOsgiFrameworkProperty(String key, String value) {
osgiFrameworkProperties.put(key, value);
@@ -44,4 +45,12 @@ public class CrankstartContext {
public Framework getOsgiFramework() {
return osgiFramework;
}
+
+ public void setAttribute(String key, Object value) {
+ attributes.put(key, value);
+ }
+
+ public Object getAttribute(String key) {
+ return attributes.get(key);
+ }
}
\ No newline at end of file
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=1607039&r1=1607038&r2=1607039&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
Tue Jul 1 10:56:26 2014
@@ -193,6 +193,29 @@ public class CrankstartBootstrapTest {
@Test
@Retry(timeoutMsec=10000, intervalMsec=250)
+ public void testAdditionalBundles() throws Exception {
+ setAdminCredentials();
+ final String basePath = "/system/console/bundles/";
+ final String [] addBundles = {
+ "org.apache.sling.commons.mime",
+ "org.apache.sling.settings"
+ };
+
+ for(String name : addBundles) {
+ final String path = basePath + name;
+ final HttpUriRequest get = new HttpGet(baseUrl + path);
+ HttpResponse response = null;
+ try {
+ response = client.execute(get);
+ assertEquals("Expecting additional bundle to be present at " +
get.getURI(), 200, response.getStatusLine().getStatusCode());
+ } finally {
+ closeConnection(response);
+ }
+ }
+ }
+
+ @Test
+ @Retry(timeoutMsec=10000, intervalMsec=250)
public void testFelixFormatConfig() throws Exception {
setAdminCredentials();
final String path =
"/system/console/config/configuration-status-20140606-1347+0200.txt";
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=1607039&r1=1607038&r2=1607039&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
Tue Jul 1 10:56:26 2014
@@ -69,8 +69,13 @@ config empty.config.should.work FORMAT:f
# Test an extension command provided by our test-services bundle
test.system.property the.test.system.property was set by test-services bundle
-# Add the JUnit core via the Sling installer and our sling.extensions
+# Prepare additional resources for the Sling installer
sling.installer.resource mvn:org.apache.sling/org.apache.sling.junit.core/1.0.8
+sling.installer.resource
mvn:org.apache.sling/org.apache.sling.commons.mime/2.1.4
+sling.installer.resource mvn:org.apache.sling/org.apache.sling.settings/1.3.0
+
+# And register the installer resources
+sling.installer.register crankstart
# Informative log
log felix http service should come up at http://localhost:${http.port}
Added:
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerRegisterCommand.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerRegisterCommand.java?rev=1607039&view=auto
==============================================================================
---
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerRegisterCommand.java
(added)
+++
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerRegisterCommand.java
Tue Jul 1 10:56:26 2014
@@ -0,0 +1,91 @@
+/*
+ * 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.extensions.sling;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.io.input.AutoCloseInputStream;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.crankstart.api.CrankstartCommand;
+import org.apache.sling.crankstart.api.CrankstartCommandLine;
+import org.apache.sling.crankstart.api.CrankstartContext;
+import org.apache.sling.crankstart.api.CrankstartException;
+import org.apache.sling.installer.api.InstallableResource;
+import org.apache.sling.installer.api.OsgiInstaller;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** CrankstartCommand that registers prepared resources with the Sling
installer */
+@Component
+@Service
+public class InstallerRegisterCommand implements CrankstartCommand {
+
+ public static final String CONTEXT_ATTRIBUTE_NAME =
InstallerRegisterCommand.class.getName();
+ public static final String I_INSTALLER_REGISTER =
"sling.installer.register";
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ public boolean appliesTo(CrankstartCommandLine commandLine) {
+ return I_INSTALLER_REGISTER.equals(commandLine.getVerb());
+ }
+
+ public String getDescription() {
+ return I_INSTALLER_REGISTER + ": register prepared resources with the
Sling installer";
+ }
+
+ public void execute(CrankstartContext crankstartContext,
CrankstartCommandLine commandLine) throws Exception {
+ @SuppressWarnings("unchecked")
+ final List<String> resources =
(List<String>)crankstartContext.getAttribute(CONTEXT_ATTRIBUTE_NAME);
+
+ final List<InstallableResource> toRegister = new
LinkedList<InstallableResource>();
+ for(String resourceRef : resources) {
+ final URL url = new URL(resourceRef);
+ final InputStream stream = new
AutoCloseInputStream(url.openStream());
+ final String digest = resourceRef;
+ toRegister.add(new InstallableResource(resourceRef, stream, null,
digest, "file", 100));
+ }
+
+ if(toRegister.isEmpty()) {
+ log.warn("No prepared resources found to register, use {} first",
InstallerRegisterCommand.I_INSTALLER_REGISTER);
+ } else {
+ final String prefix = commandLine.getQualifier();
+ if(prefix == null || prefix.length() == 0) {
+ throw new CrankstartException("Missing command qualifier,
required to specify installer resources prefix");
+ }
+ final BundleContext ctx =
crankstartContext.getOsgiFramework().getBundleContext();
+ final String serviceClass = OsgiInstaller.class.getName();
+ final ServiceReference ref = ctx.getServiceReference(serviceClass);
+ if(ref == null) {
+ throw new CrankstartException("Installer service not
available, cannot register resource (" + serviceClass + ")");
+ }
+ final OsgiInstaller installer = (OsgiInstaller)ctx.getService(ref);
+ try {
+ installer.registerResources(prefix, toRegister.toArray(new
InstallableResource[] {}));
+ log.info("Registered {} resources with installer, using prefix
'{}'", toRegister.size(), prefix);
+ resources.clear();
+ } finally {
+ ctx.ungetService(ref);
+ }
+ }
+
+ }
+}
Modified:
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerResourceCommand.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerResourceCommand.java?rev=1607039&r1=1607038&r2=1607039&view=diff
==============================================================================
---
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerResourceCommand.java
(original)
+++
sling/trunk/contrib/crankstart/sling-extensions/src/main/java/org/apache/sling/crankstart/extensions/sling/InstallerResourceCommand.java
Tue Jul 1 10:56:26 2014
@@ -16,24 +16,19 @@
*/
package org.apache.sling.crankstart.extensions.sling;
-import java.io.InputStream;
-import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
-import org.apache.commons.io.input.AutoCloseInputStream;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.crankstart.api.CrankstartCommand;
import org.apache.sling.crankstart.api.CrankstartCommandLine;
import org.apache.sling.crankstart.api.CrankstartContext;
import org.apache.sling.crankstart.api.CrankstartException;
-import org.apache.sling.installer.api.InstallableResource;
-import org.apache.sling.installer.api.OsgiInstaller;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/** CrankstartCommand that registers a resource with the Sling installer */
+/** CrankstartCommand that prepares resources for registration with the Sling
installer */
@Component
@Service
public class InstallerResourceCommand implements CrankstartCommand {
@@ -46,27 +41,23 @@ public class InstallerResourceCommand im
}
public String getDescription() {
- return I_INSTALLER_RESOURCE + ": register a resource with the Sling
installer";
+ return I_INSTALLER_RESOURCE + ": prepares a resource for registration
with the Sling installer";
}
public void execute(CrankstartContext crankstartContext,
CrankstartCommandLine commandLine) throws Exception {
final String resourceRef = commandLine.getQualifier();
- final URL url = new URL(resourceRef);
- final BundleContext ctx =
crankstartContext.getOsgiFramework().getBundleContext();
- final String serviceClass = OsgiInstaller.class.getName();
- final ServiceReference ref = ctx.getServiceReference(serviceClass);
- if(ref == null) {
- throw new CrankstartException("Installer service not available,
cannot register resource (" + serviceClass + ")");
+ if(resourceRef == null || resourceRef.length() == 0) {
+ throw new CrankstartException("Missing command qualifier, required
to specifiy the resource to register");
}
- final OsgiInstaller installer = (OsgiInstaller)ctx.getService(ref);
- try {
- final InputStream stream = new
AutoCloseInputStream(url.openStream());
- final String digest = resourceRef;
- final InstallableResource r = new InstallableResource(resourceRef,
stream, null, digest, "file", 100);
- installer.registerResources("crankstart", new
InstallableResource[] { r });
- log.info("Resource registered with Sling installer: {}",
resourceRef);
- } finally {
- ctx.ungetService(ref);
+
+ @SuppressWarnings("unchecked")
+ List<String> resources =
(List<String>)crankstartContext.getAttribute(InstallerRegisterCommand.CONTEXT_ATTRIBUTE_NAME);
+ if(resources == null) {
+ resources = new LinkedList<String>();
+
crankstartContext.setAttribute(InstallerRegisterCommand.CONTEXT_ATTRIBUTE_NAME,
resources);
}
+
+ resources.add(resourceRef);
+ log.info("Installer resource prepared: {}", resourceRef);
}
}