JaxRsClassesProviderTracker.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2019 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.caravan.jaxrs.publisher.impl;

  21. import static io.wcm.caravan.jaxrs.publisher.impl.ServletContainerBridge.isJaxRsGlobal;

  22. import org.osgi.framework.Bundle;
  23. import org.osgi.framework.BundleContext;
  24. import org.osgi.framework.ServiceReference;
  25. import org.osgi.util.tracker.ServiceTracker;

  26. import io.wcm.caravan.jaxrs.publisher.JaxRsClassesProvider;

  27. /**
  28.  * Tracks {@link JaxRsClassesProvider} instances.
  29.  */
  30. class JaxRsClassesProviderTracker extends ServiceTracker<JaxRsClassesProvider, Object> {

  31.   private final ServletContainerBridge bridge;
  32.   private final BundleContext bundleContext;
  33.   private final Bundle bundle;

  34.   JaxRsClassesProviderTracker(ServletContainerBridge bridge) {
  35.     super(bridge.getBundleContext(), JaxRsClassesProvider.class, null);
  36.     this.bridge = bridge;
  37.     this.bundleContext = bridge.getBundleContext();
  38.     this.bundle = bridge.getBundle();
  39.   }

  40.   @Override
  41.   public Object addingService(ServiceReference<JaxRsClassesProvider> reference) {
  42.     if (isJaxRsGlobal(reference)) {
  43.       JaxRsClassesProvider serviceInstance = bundle.getBundleContext().getService(reference);
  44.       ServletContainerBridge.log.debug("Register global classes provider {} for {}", serviceInstance.getClass().getName(), bundle.getSymbolicName());
  45.       bridge.getGlobalClassesProviders().add(serviceInstance);
  46.       bridge.markAsDirty();
  47.     }
  48.     else if (reference.getBundle() == bundle) {
  49.       JaxRsClassesProvider serviceInstance = bundle.getBundleContext().getService(reference);
  50.       ServletContainerBridge.log.debug("Register classes provider {} for {}", serviceInstance.getClass().getName(), bundle.getSymbolicName());
  51.       bridge.getLocalClassesProviders().add(serviceInstance);
  52.       bridge.markAsDirty();
  53.     }
  54.     return super.addingService(reference);
  55.   }

  56.   @Override
  57.   public void removedService(ServiceReference<JaxRsClassesProvider> reference, Object service) {
  58.     if (isJaxRsGlobal(reference)) {
  59.       JaxRsClassesProvider serviceInstance = bundle.getBundleContext().getService(reference);
  60.       ServletContainerBridge.log.debug("Unregister global classes provider {} for {}", serviceInstance.getClass().getName(), bundle.getSymbolicName());
  61.       bridge.getGlobalClassesProviders().remove(serviceInstance);
  62.       bundleContext.ungetService(reference);
  63.       bridge.markAsDirty();
  64.     }
  65.     else if (reference.getBundle() == bundle) {
  66.       JaxRsClassesProvider serviceInstance = bundle.getBundleContext().getService(reference);
  67.       ServletContainerBridge.log.debug("Unregister classes provider {} for {}", serviceInstance.getClass().getName(), bundle.getSymbolicName());
  68.       bridge.getLocalClassesProviders().remove(serviceInstance);
  69.       bundleContext.ungetService(reference);
  70.       bridge.markAsDirty();
  71.     }
  72.     super.removedService(reference, service);
  73.   }

  74. }