HalDocsBundleTracker.java

/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2015 wcm.io
 * %%
 * Licensed 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.
 * #L%
 */
package io.wcm.caravan.hal.docs.impl;

import io.wcm.caravan.jaxrs.publisher.ApplicationPath;

import java.util.Dictionary;
import java.util.Hashtable;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.service.component.ComponentConstants;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.ComponentFactory;
import org.osgi.service.component.ComponentInstance;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.util.tracker.BundleTracker;
import org.osgi.util.tracker.BundleTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Tracks bundles that contain HAL documentation files generated by hal-docs-maven-plugin, and mounts them
 * via HTTP service to /docs/api/{serviceId}/*.
 */
@Component(immediate = true)
public class HalDocsBundleTracker implements BundleTrackerCustomizer<ComponentInstance> {

  static final String DOCS_RESOURCES_URI_PREFIX = "/docs/resources";
  static final String CLASSPATH_FRONTEND = "HALDOCS-TEMPLATE-INF/frontend";

  private static final Logger log = LoggerFactory.getLogger(HalDocsBundleTracker.class);

  private BundleContext bundleContext;
  private BundleTracker bundleTracker;

  @Reference(target = "(" + ComponentConstants.COMPONENT_FACTORY + "=" + HalDocsServlet.FACTORY + ")")
  private ComponentFactory servletFactory;

  @Reference
  private HttpService httpService;

  @Activate
  void activate(ComponentContext componentContext) throws NamespaceException {
    bundleContext = componentContext.getBundleContext();
    this.bundleTracker = new BundleTracker<ComponentInstance>(bundleContext, Bundle.ACTIVE, this);
    this.bundleTracker.open();

    // mount static resources for docs frontend
    httpService.registerResources(DOCS_RESOURCES_URI_PREFIX, CLASSPATH_FRONTEND, null);
  }

  @Deactivate
  void deactivate(ComponentContext componentContext) {
    this.bundleTracker.close();
    httpService.unregister(DOCS_RESOURCES_URI_PREFIX);
  }

  @Override
  public ComponentInstance addingBundle(Bundle bundle, BundleEvent event) {
    String docsPath = DocsPath.get(bundle);
    if (docsPath != null) {

      if (log.isInfoEnabled()) {
        log.info("Mount HAL docs for {} to {}", bundle.getSymbolicName(), docsPath);
      }

      // register HAL docs servlet on HTTP whiteboard
      Dictionary<String, Object> serviceConfig = new Hashtable<>();
      serviceConfig.put("alias", docsPath);
      serviceConfig.put(HalDocsServlet.PROPERTY_BUNDLE, bundle);
      return servletFactory.newInstance(serviceConfig);
    }
    return null;
  }

  @Override
  public void modifiedBundle(Bundle bundle, BundleEvent event, ComponentInstance componentInstance) {
    // nothing to do
  }

  @Override
  public void removedBundle(Bundle bundle, BundleEvent event, ComponentInstance componentInstance) {
    if (componentInstance == null) {
      return;
    }
    if (log.isInfoEnabled()) {
      String applicationPath = ApplicationPath.get(bundle);
      String docsPath = DocsPath.get(applicationPath);
      log.info("Unmount HAL docs for {} from {}", bundle.getSymbolicName(), docsPath);
    }
    componentInstance.dispose();
  }

}