SLING-7765 Application creation missing variable declarations
Allow variables being overridden for applications (e.g. when launching
an application)
diff --git a/src/main/java/org/apache/sling/feature/io/json/ApplicationJSONReader.java b/src/main/java/org/apache/sling/feature/io/json/ApplicationJSONReader.java
index 91b4048..05382ca 100644
--- a/src/main/java/org/apache/sling/feature/io/json/ApplicationJSONReader.java
+++ b/src/main/java/org/apache/sling/feature/io/json/ApplicationJSONReader.java
@@ -19,10 +19,12 @@
import org.apache.felix.configurator.impl.json.JSONUtil;
import org.apache.sling.feature.Application;
import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.KeyValueMap;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
+import java.util.Collections;
import java.util.Map;
import javax.json.Json;
@@ -41,11 +43,26 @@
* @return The application
* @throws IOException If an IO errors occurs or the JSON is invalid.
*/
- public static Application read(final Reader reader)
+ public static Application read(final Reader reader) throws IOException {
+ return read(reader, Collections.emptyMap());
+ }
+
+ /**
+ * Read a new application from the reader
+ * The reader is not closed. It is up to the caller to close the reader.
+ *
+ * @param reader The reader for the feature
+ * @param overriddenVariables Map of variables that override the variable
+ * values as in the application JSON
+ * @return The application
+ * @throws IOException If an IO errors occurs or the JSON is invalid.
+ */
+ public static Application read(final Reader reader, Map<String, String> overriddenVariables)
throws IOException {
try {
final ApplicationJSONReader mr = new ApplicationJSONReader();
- mr.readApplication(reader);
+
+ mr.readApplication(reader, overriddenVariables);
return mr.app;
} catch (final IllegalStateException | IllegalArgumentException e) {
throw new IOException(e);
@@ -55,6 +72,11 @@
/** The read application. */
private final Application app;
+ /** This holds the application variables including any overrides provided,
+ * e.g. via the launcher commandline.
+ */
+ private volatile KeyValueMap variables;
+
/**
* Private constructor
*/
@@ -66,9 +88,10 @@
/**
* Read a full application
* @param reader The reader
+ * @param overriddenVariables
* @throws IOException If an IO error occurs or the JSON is not valid.
*/
- private void readApplication(final Reader reader)
+ private void readApplication(final Reader reader, Map<String, String> overriddenVariables)
throws IOException {
final JsonObject json = Json.createReader(new StringReader(minify(reader))).readObject();
@@ -80,6 +103,15 @@
app.setFramework(ArtifactId.parse(frameworkId));
}
this.readVariables(map, app.getVariables());
+
+ this.variables = new KeyValueMap();
+ this.variables.putAll(app.getVariables());
+
+ // Apply the overrides
+ for (Map.Entry<String, String> entry : overriddenVariables.entrySet()) {
+ variables.put(entry.getKey(), entry.getValue());
+ }
+
this.readBundles(map, app.getBundles(), app.getConfigurations());
this.readFrameworkProperties(map, app.getFrameworkProperties());
this.readConfigurations(map, app.getConfigurations());
@@ -91,12 +123,12 @@
@Override
protected Object handleResolveVars(Object val) {
- return handleVars(val, app.getVariables());
+ return handleVars(val, variables);
}
@Override
protected Object handleLaunchVars(Object val) {
- return handleVars(val, app.getVariables());
+ return handleVars(val, variables);
}
}
diff --git a/src/test/java/org/apache/sling/feature/io/json/ApplicationJSONReaderTest.java b/src/test/java/org/apache/sling/feature/io/json/ApplicationJSONReaderTest.java
new file mode 100644
index 0000000..55c9145
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/io/json/ApplicationJSONReaderTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.feature.io.json;
+
+import org.apache.sling.feature.Application;
+import org.apache.sling.feature.Configuration;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileReader;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+public class ApplicationJSONReaderTest {
+ @Test
+ public void testOverrideVariables() throws Exception {
+ File appFile = new File(getClass().getResource("/applications/app_with_vars.json").toURI());
+ FileReader fr = new FileReader(appFile);
+
+ Map<String, String> overridden = new HashMap<>();
+ overridden.put("web.port", "12345");
+ overridden.put("something", "else");
+
+ Application app = ApplicationJSONReader.read(fr, overridden);
+ Configuration cfg = app.getConfigurations().getConfiguration("org.apache.felix.http");
+ assertEquals("12345", cfg.getProperties().get("org.osgi.service.http.port"));
+ }
+
+ @Test
+ public void testDefaultVariables() throws Exception {
+ File appFile = new File(getClass().getResource("/applications/app_with_vars.json").toURI());
+ FileReader fr = new FileReader(appFile);
+
+ Application app = ApplicationJSONReader.read(fr);
+ Configuration cfg = app.getConfigurations().getConfiguration("org.apache.felix.http");
+ assertEquals("8888", cfg.getProperties().get("org.osgi.service.http.port"));
+ }
+}
diff --git a/src/test/resources/applications/app_with_vars.json b/src/test/resources/applications/app_with_vars.json
new file mode 100644
index 0000000..86ff7c8
--- /dev/null
+++ b/src/test/resources/applications/app_with_vars.json
@@ -0,0 +1,24 @@
+{
+ "frameworkId":"org.apache.felix:org.apache.felix.framework:6.0.0",
+ "variables":{
+ "web.port":"8888"
+ },
+ "features":[
+ "org.apache.sling:mytestfeature:slingfeature:test:0.0.1-SNAPSHOT"
+ ],
+ "bundles":[
+ {
+ "id":"org.apache.felix:org.apache.felix.log:1.0.1",
+ "start-level":"1",
+ "start-order":"1"
+ }
+ ],
+ "configurations":{
+ "org.apache.felix.http":{
+ "org.osgi.service.http.port":"${web.port}"
+ }
+ },
+ "framework-properties":{
+ "org.osgi.framework.bootdelegation":"sun.*,com.sun.*"
+ }
+}
\ No newline at end of file