This is an automated email from the ASF dual-hosted git repository. csierra pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/aries-jax-rs-whiteboard.git
commit 2cbdcdf35db933435c8186ef6a94389ce659ba60 Author: Carlos Sierra <[email protected]> AuthorDate: Fri Sep 21 15:17:18 2018 +0200 Do not register anything until start is called Better pass the bundleContext on the start method instead of passing it on the constructor. Matches better the lifecycle of the activators. --- .../activator/CxfJaxrsBundleActivator.java | 6 +- .../jax/rs/whiteboard/internal/TargetFilter.java | 66 ----------------- .../jax/rs/whiteboard/internal/Whiteboard.java | 83 +++++++++++++--------- 3 files changed, 54 insertions(+), 101 deletions(-) diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java index 0fd458a..48ca3e0 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java @@ -193,7 +193,7 @@ public class CxfJaxrsBundleActivator implements BundleActivator { endpointFilter(configuration::get), __ -> false //never reload ).then( - just(createWhiteboard(bundleContext, configuration)). + just(createWhiteboard(configuration)). effects( ifInfoEnabled( _log, @@ -207,7 +207,9 @@ public class CxfJaxrsBundleActivator implements BundleActivator { .flatMap( whiteboard -> all( - effects(whiteboard::start, whiteboard::stop), + effects( + () -> whiteboard.start(bundleContext), + whiteboard::stop), ignore( endpoints.effects( whiteboard::addHttpEndpoints, diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java deleted file mode 100644 index c647955..0000000 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java +++ /dev/null @@ -1,66 +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.aries.jax.rs.whiteboard.internal; - -import static org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET; - -import java.util.function.Predicate; - -import org.apache.aries.component.dsl.CachingServiceReference; -import org.osgi.framework.Filter; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TargetFilter<T> implements Predicate<CachingServiceReference<T>> { - - public TargetFilter(ServiceReference<?> serviceRuntimeReference) { - _serviceRuntimeReference = serviceRuntimeReference; - } - - @Override - public boolean test(CachingServiceReference<T> ref) { - String target = (String)ref.getProperty(JAX_RS_WHITEBOARD_TARGET); - - if (target == null) { - return true; - } - - Filter filter; - - try { - filter = FrameworkUtil.createFilter(target); - } - catch (InvalidSyntaxException ise) { - if (_log.isErrorEnabled()) { - _log.error("Invalid '{}' filter syntax in {}", JAX_RS_WHITEBOARD_TARGET, ref); - } - - return false; - } - - return filter.match(_serviceRuntimeReference); - } - - private static final Logger _log = LoggerFactory.getLogger(TargetFilter.class); - - private final ServiceReference<?> _serviceRuntimeReference; - -} \ No newline at end of file diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java index b97c0e2..70f5e8f 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java @@ -149,28 +149,21 @@ public class Whiteboard { private final AriesJaxrsServiceRuntime _runtime; private final Map<String, ?> _configurationMap; - private final BundleContext _bundleContext; - private final ServiceRegistrationChangeCounter _counter; - private final ServiceReference<?> _runtimeReference; + private volatile BundleContext _bundleContext; + private volatile ServiceRegistrationChangeCounter _counter; + private volatile ServiceReference<?> _runtimeReference; private final OSGi<Void> _program; private final List<Object> _endpoints; - private final ServiceRegistration<?> _runtimeRegistration; + private volatile ServiceRegistration<?> _runtimeRegistration; private OSGiResult _osgiResult; - private Whiteboard( - BundleContext bundleContext, Dictionary<String, ?> configuration) { - - _bundleContext = bundleContext; - _runtime = new AriesJaxrsServiceRuntime(); + private Whiteboard(Dictionary<String, ?> configuration) { + _runtime = new AriesJaxrsServiceRuntime(this); _configurationMap = Maps.from(configuration); _endpoints = new ArrayList<>(); - _runtimeRegistration = registerJaxRSServiceRuntime( - new HashMap<>(_configurationMap)); - _runtimeReference = _runtimeRegistration.getReference(); - _counter = new ServiceRegistrationChangeCounter(_runtimeRegistration); + _applicationExtensionRegistry = new ApplicationExtensionRegistry(); _extensionRegistry = new ExtensionRegistry(); - _applicationBasePrefix = canonicalizeAddress( getString(_configurationMap.get("application.base.prefix"))); @@ -182,13 +175,18 @@ public class Whiteboard { } public static Whiteboard createWhiteboard( - BundleContext bundleContext, Dictionary<String, ?> configuration) { + Dictionary<String, ?> configuration) { - return new Whiteboard(bundleContext, configuration); + return new Whiteboard(configuration); } - public void start() { - _osgiResult = _program.run(_bundleContext); + public void start(BundleContext bundleContext) { + _bundleContext = bundleContext; + _runtimeRegistration = registerJaxRSServiceRuntime( + new HashMap<>(_configurationMap)); + _runtimeReference = _runtimeRegistration.getReference(); + _counter = new ServiceRegistrationChangeCounter(_runtimeRegistration); + _osgiResult = _program.run(bundleContext); } public void stop() { @@ -276,8 +274,7 @@ public class Whiteboard { getResourcesForWhiteboard(), getApplicationExtensionsForWhiteboard(), applicationsForWhiteboard - ), - _counter + ) ), this::registerShadowedService, this::unregisterShadowedService @@ -305,6 +302,31 @@ public class Whiteboard { return _resourcesFilter.match(sr.getServiceReference()); } + private <T> boolean matchesWhiteboard(CachingServiceReference<T> ref) { + String target = (String)ref.getProperty(JAX_RS_WHITEBOARD_TARGET); + + if (target == null) { + return true; + } + + Filter filter; + + try { + filter = FrameworkUtil.createFilter(target); + } + catch (InvalidSyntaxException ise) { + if (_log.isErrorEnabled()) { + _log.error( + "Invalid '{}' filter syntax in {}", + JAX_RS_WHITEBOARD_TARGET, ref); + } + + return false; + } + + return filter.match(_runtimeReference); + } + private void registerShadowedService(CachingServiceReference<?> sr) { if (isApplication(sr)) { _runtime.addClashingApplication(sr); @@ -582,9 +604,7 @@ public class Whiteboard { serviceReferences( CxfJaxrsServiceRegistrator.class, String.format("(%s=%s)", JAX_RS_NAME, DEFAULT_NAME) - ).filter( - new TargetFilter<>(_runtimeReference) - ) + ).filter(this::matchesWhiteboard) ); } @@ -656,9 +676,7 @@ public class Whiteboard { } } } - ).filter( - new TargetFilter<>(_runtimeReference) - ), + ).filter(this::matchesWhiteboard), __ -> {}, __ -> {}, _log); return accumulateInMap( @@ -696,7 +714,7 @@ public class Whiteboard { getApplicationExtensionsForWhiteboard() { return serviceReferences(_extensionsFilter.toString()). - filter(new TargetFilter<>(_runtimeReference)); + filter(this::matchesWhiteboard); } private OSGi<CachingServiceReference<Application>> @@ -705,12 +723,12 @@ public class Whiteboard { return serviceReferences( Application.class, _applicationsFilter.toString()). - filter(new TargetFilter<>(_runtimeReference)); + filter(this::matchesWhiteboard); } private OSGi<CachingServiceReference<Object>> getResourcesForWhiteboard() { return serviceReferences(_resourcesFilter.toString()). - filter(new TargetFilter<>(_runtimeReference)); + filter(this::matchesWhiteboard); } private OSGi<ServiceRegistration<Application>> @@ -1145,14 +1163,13 @@ public class Whiteboard { )); } - private static <T> OSGi<T> countChanges( - OSGi<T> program, ChangeCounter counter) { + private <T> OSGi<T> countChanges(OSGi<T> program) { return program.effects( __ -> {}, - __ -> counter.inc(), + __ -> _counter.inc(), __ -> {}, - __ -> counter.inc() + __ -> _counter.inc() ); }
