This is an automated email from the ASF dual-hosted git repository. davidb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push: new 9801b5f Change configuration of Features Service from configadmin to factory 9801b5f is described below commit 9801b5fdf539c439a8a36635e6fc52a651ec672c Author: David Bosschaert <david.bosscha...@gmail.com> AuthorDate: Thu Jul 12 22:17:51 2018 +0200 Change configuration of Features Service from configadmin to factory --- .../{impl/Activator.java => FeaturesFactory.java} | 24 +----- .../sling/feature/service/impl/Activator.java | 12 +-- .../service/impl/FeaturesServiceFactoryImpl.java | 58 +++++++++++++ .../service/impl/FeaturesServiceManager.java | 96 ---------------------- 4 files changed, 65 insertions(+), 125 deletions(-) diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java similarity index 52% copy from featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java copy to featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java index db65574..f1fc474 100644 --- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java +++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java @@ -16,26 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.sling.feature.service.impl; +package org.apache.sling.feature.service; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.service.cm.ManagedService; +import java.util.Map; -import java.util.Dictionary; -import java.util.Hashtable; - -public class Activator implements BundleActivator { - @Override - public void start(BundleContext context) throws Exception { - Dictionary<String, Object> props = new Hashtable<>(); - props.put(Constants.SERVICE_PID, FeaturesServiceImpl.class.getName()); - context.registerService(ManagedService.class, - new FeaturesServiceManager(context), props); - } - - @Override - public void stop(BundleContext context) throws Exception { - } +public interface FeaturesFactory { + void initialize(Map<String, String> bundleFeatureMapping); } diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java index db65574..46bc3ed 100644 --- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java +++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java @@ -18,21 +18,15 @@ */ package org.apache.sling.feature.service.impl; +import org.apache.sling.feature.service.FeaturesFactory; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.service.cm.ManagedService; - -import java.util.Dictionary; -import java.util.Hashtable; public class Activator implements BundleActivator { @Override public void start(BundleContext context) throws Exception { - Dictionary<String, Object> props = new Hashtable<>(); - props.put(Constants.SERVICE_PID, FeaturesServiceImpl.class.getName()); - context.registerService(ManagedService.class, - new FeaturesServiceManager(context), props); + FeaturesServiceFactoryImpl ff = new FeaturesServiceFactoryImpl(context); + context.registerService(FeaturesFactory.class, ff, null); } @Override diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java new file mode 100644 index 0000000..330ac67 --- /dev/null +++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java @@ -0,0 +1,58 @@ +/* + * 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.service.impl; + +import org.apache.sling.feature.service.Features; +import org.apache.sling.feature.service.FeaturesFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Version; + +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; +import java.util.Map.Entry; + +class FeaturesServiceFactoryImpl implements FeaturesFactory { + private final BundleContext bundleContext; + + public FeaturesServiceFactoryImpl(BundleContext context) { + bundleContext = context; + } + + @Override + public void initialize(Map<String, String> bfm) { + Map<Entry<String, Version>, String> bundleFeatureMapping = new HashMap<>(); + for (Map.Entry<String, String> entry : bfm.entrySet()) { + String[] bv = entry.getKey().split(":"); + if (bv.length == 2) { + try { + Map.Entry<String, Version> k = new AbstractMap.SimpleEntry<String, Version>( + bv[0], Version.parseVersion(bv[1])); + bundleFeatureMapping.put(k, entry.getValue()); + } catch (IllegalArgumentException iae) { + // TODO log + } + } + } + + Features fs = new FeaturesServiceImpl(bundleFeatureMapping); + bundleContext.registerService(Features.class, fs, new Hashtable<>(bfm)); + } +} diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceManager.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceManager.java deleted file mode 100644 index 269528b..0000000 --- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceManager.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.service.impl; - -import org.apache.sling.feature.service.Features; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; -import org.osgi.framework.Version; -import org.osgi.service.cm.ConfigurationException; -import org.osgi.service.cm.ManagedService; - -import java.util.AbstractMap; -import java.util.Collection; -import java.util.Dictionary; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map; - -class FeaturesServiceManager implements ManagedService { - private final BundleContext bundleContext; - private ServiceRegistration<Features> reg; - - FeaturesServiceManager(BundleContext context) { - bundleContext = context; - } - - @Override - public synchronized void updated(Dictionary<String, ?> properties) throws ConfigurationException { - if (reg != null) - reg.unregister(); - - if (properties == null) - return; - - Map<Map.Entry<String, Version>, String> bundleIDFeatures = new HashMap<>(); - Dictionary<String, String> props = new Hashtable<>(); - for(Enumeration<String> e = properties.keys(); e.hasMoreElements(); ) { - String key = e.nextElement(); - if (key.startsWith(".")) - continue; - - if (Constants.SERVICE_PID.equals(key)) - continue; - - String[] bsnver = key.split(":"); - if (bsnver.length != 2) - continue; - - try { - Version ver = Version.valueOf(bsnver[1]); - - String value = getStringPlus(properties.get(key)); - AbstractMap.SimpleEntry<String, Version> newKey = new AbstractMap.SimpleEntry<>(bsnver[0], ver); - bundleIDFeatures.put(newKey, value); - props.put(key, value); - } catch (IllegalArgumentException iae) { - // TODO log - } - } - - FeaturesServiceImpl fs = new FeaturesServiceImpl(bundleIDFeatures); - reg = bundleContext.registerService(Features.class, fs, props); - } - - private String getStringPlus(Object obj) { - if (obj instanceof String) { - return (String) obj; - } - if (obj instanceof Collection) { - Iterator<?> it = ((Collection<?>) obj).iterator(); - if (it.hasNext()) - return it.next().toString(); - } - return null; - } - -}