You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2020/10/11 05:27:25 UTC
[karaf] branch master updated: Alternatively read features.core
configuration from ConfigAdmin (#1215)
This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/master by this push:
new d618e43 Alternatively read features.core configuration from ConfigAdmin (#1215)
d618e43 is described below
commit d618e4325f416454cc442603e8b50ea01785209f
Author: Henning Treu <he...@instana.com>
AuthorDate: Sun Oct 11 07:27:19 2020 +0200
Alternatively read features.core configuration from ConfigAdmin (#1215)
---
.../karaf/features/internal/osgi/Activator.java | 26 +++++-
.../features/internal/osgi/ActivatorTest.java | 101 +++++++++++++++++++++
2 files changed, 123 insertions(+), 4 deletions(-)
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
index 28226e6..9a69301 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
@@ -68,6 +68,7 @@ import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.hooks.bundle.CollisionHook;
import org.osgi.framework.hooks.resolver.ResolverHookFactory;
+import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.repository.Repository;
@@ -90,7 +91,8 @@ import org.slf4j.LoggerFactory;
)
public class Activator extends BaseActivator {
- public static final String FEATURES_SERVICE_CONFIG_FILE = "org.apache.karaf.features.cfg";
+ static final String FEATURES_SERVICE_CONFIG = "org.apache.karaf.features";
+ public static final String FEATURES_SERVICE_CONFIG_FILE = FEATURES_SERVICE_CONFIG + ".cfg";
public static final String FEATURES_SERVICE_PROCESSING_FILE = "org.apache.karaf.features.xml";
public static final String FEATURES_SERVICE_PROCESSING_VERSIONS_FILE = "versions.properties";
@@ -121,10 +123,26 @@ public class Activator extends BaseActivator {
logger.warn("Error reading configuration file " + configFile.toString(), e);
}
}
- Dictionary<String, String> props = new Hashtable<>();
- for (Map.Entry<String, String> entry : configuration.entrySet()) {
- props.put(entry.getKey(), entry.getValue());
+
+ Dictionary<String, Object> props = new Hashtable<>();
+
+ if (!configuration.isEmpty()) {
+ for (Map.Entry<String, String> entry : configuration.entrySet()) {
+ props.put(entry.getKey(), entry.getValue());
+ }
+ } else {
+ // work around https://issues.apache.org/jira/browse/KARAF-6866
+ // org.apache.karaf.features.cfg might have been read empty
+ // but ConfigurationAdmin also has all values available:
+ ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class);
+ if (configurationAdmin != null) {
+ Configuration featuresServiceConfig = configurationAdmin.getConfiguration(FEATURES_SERVICE_CONFIG);
+ if (featuresServiceConfig != null) {
+ props = featuresServiceConfig.getProperties();
+ }
+ }
}
+
updated(props);
}
diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/osgi/ActivatorTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/osgi/ActivatorTest.java
new file mode 100644
index 0000000..fbc559f
--- /dev/null
+++ b/features/core/src/test/java/org/apache/karaf/features/internal/osgi/ActivatorTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.karaf.features.internal.osgi;
+
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.niceMock;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+public class ActivatorTest {
+
+ private Activator activator;
+ private ConfigurationAdmin configurationAdmin;
+ private BundleContext bundleContext;
+
+ @Before
+ public void setup() throws Exception {
+ activator = new TestActivator();
+
+ configurationAdmin = niceMock(ConfigurationAdmin.class);
+ Configuration featuresConfig = niceMock(Configuration.class);
+ Dictionary<String, Object> properties = buildTestProperties();
+ expect(featuresConfig.getProperties()).andReturn(properties);
+ expect(configurationAdmin.getConfiguration(Activator.FEATURES_SERVICE_CONFIG)).andReturn(featuresConfig);
+
+ bundleContext = niceMock(BundleContext.class);
+ Bundle bundle = niceMock(Bundle.class);
+ expect(bundle.getResource(anyString())).andReturn(null);
+ expect(bundleContext.getBundle()).andReturn(bundle);
+
+ replay(bundleContext, bundle, configurationAdmin, featuresConfig);
+ }
+
+ private Dictionary<String, Object> buildTestProperties() {
+ Dictionary<String, Object> properties = new Hashtable<>();
+ properties.put("key1", "value1");
+ properties.put("key2", "value2");
+
+ return properties;
+ }
+
+ @Test
+ public void testStart() throws Exception {
+ activator.start(bundleContext);
+
+ Dictionary<String, ?> configuration = ((TestActivator) activator).getConfiguration();
+ assertThat(configuration.get("key1"), is("value1"));
+ assertThat(configuration.get("key2"), is("value2"));
+ }
+
+ /**
+ * Overwrite #getTrackedService which could otherwise not be mocked.
+ * Overwrite #doStart to avoid full start up of the Activator.
+ * Overwrite #getConfiguration to allow test validation.
+ */
+ public class TestActivator extends Activator {
+ @Override
+ protected <T> T getTrackedService(Class<T> clazz) {
+ if (clazz.isAssignableFrom(ConfigurationAdmin.class)) {
+ return (T) configurationAdmin;
+ }
+ return null;
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ // nop
+ }
+
+ @Override
+ public Dictionary<String, ?> getConfiguration() {
+ return super.getConfiguration();
+ }
+ }
+}