*/
package org.opendaylight.mdsal.binding.dom.adapter.osgi;
-import static java.util.Objects.requireNonNull;
-
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Iterators;
+import com.google.common.collect.Maps;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
@NonNullByDefault
final class Dict extends Dictionary<String, Object> {
+ private static final Logger LOG = LoggerFactory.getLogger(Dict.class);
private static final Dict EMPTY = new Dict(ImmutableMap.of());
private final Map<String, Object> map;
private Dict(final Map<String, Object> map) {
- this.map = requireNonNull(map);
+ this.map = ImmutableMap.copyOf(map);
}
static Dict fromReference(final ServiceReference<?> ref) {
return EMPTY;
}
- final Builder<String, Object> b = ImmutableMap.builderWithExpectedSize(keys.length);
+ final Map<String, Object> props = Maps.newHashMapWithExpectedSize(keys.length);
+ for (String key : keys) {
+ // Ignore properties with our prefix: we are not exporting those
+ if (!key.startsWith(ServiceProperties.PREFIX)) {
+ final Object value = ref.getProperty(key);
+ if (value != null) {
+ props.put(key, value);
+ }
+ }
+ }
+
+ // Second phase: apply any our properties
for (String key : keys) {
- final Object value = ref.getProperty(key);
- if (value != null) {
- b.put(key, value);
+ if (key.startsWith(ServiceProperties.OVERRIDE_PREFIX)) {
+ final Object value = ref.getProperty(key);
+ if (value != null) {
+ final String newKey = key.substring(ServiceProperties.OVERRIDE_PREFIX.length());
+ if (!newKey.isEmpty()) {
+ LOG.debug("Overriding property {}", newKey);
+ props.put(newKey, value);
+ }
+ }
}
}
- return new Dict(b.build());
+ return new Dict(props);
}
@Override
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.adapter.osgi;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Properties recognized and manipulated by {@link DynamicBindingAdapter}. All properties starting with
+ * {@code odl.mdsal.binding.adapter.} are stripped from the re-exported service.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public final class ServiceProperties {
+ static final String PREFIX = "odl.mdsal.binding.adapter.";
+
+ /**
+ * Instruction to ignore the service. When a service with this property is found, {@link DynamicBindingAdapter}
+ * will completely ignore it.
+ */
+ public static final String IGNORE_PROP = "odl.mdsal.binding.adapter.ignore";
+
+ /**
+ * Prefix for properties which should be replaced. For any property with a name which starts with this prefix,
+ * {@link DynamicBindingAdapter} will strip this prefix and advertise the resulting property in the re-exported
+ * service.
+ */
+ public static final String OVERRIDE_PREFIX = "odl.mdsal.binding.adapter.override.";
+
+ private ServiceProperties() {
+
+ }
+}