Package org.sonatype.nexus.capability.support

Source Code of org.sonatype.nexus.capability.support.CapabilityBooterSupport

/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2014 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.capability.support;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.sonatype.nexus.capability.CapabilityReference;
import org.sonatype.nexus.capability.CapabilityRegistry;
import org.sonatype.nexus.capability.CapabilityRegistryEvent.Ready;
import org.sonatype.nexus.capability.CapabilityType;
import org.sonatype.nexus.events.EventSubscriber;
import org.sonatype.sisu.goodies.common.ComponentSupport;

import com.google.common.base.Throwables;
import com.google.common.eventbus.Subscribe;

/**
* Support for components which need to handle capability registration upon booting.
*
* @since capabilities 2.2
*/
public abstract class CapabilityBooterSupport
  extends ComponentSupport
  implements EventSubscriber
{
  @Subscribe
  public void handle(final Ready event) {
    final CapabilityRegistry registry = event.getEventSender();
    try {
      boot(registry);
    }
    catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  protected abstract void boot(final CapabilityRegistry registry) throws Exception;

  protected void maybeAddCapability(final CapabilityRegistry capabilityRegistry,
                                    final CapabilityType type,
                                    final boolean enabled,
                                    final String notes,
                                    final Map<String, String> properties)
      throws Exception
  {
    CapabilityReference reference = findCapability(capabilityRegistry, type);
    if (reference == null) {
      log.debug("Automatically adding capability type: {}; enabled: {}", type, enabled);
      addCapability(capabilityRegistry, type, enabled, notes, properties);
    }
  }

  protected CapabilityReference findCapability(final CapabilityRegistry capabilityRegistry,
                                               final CapabilityType type)
  {
    return findCapability(capabilityRegistry, type, true);
  }

  protected CapabilityReference findCapability(final CapabilityRegistry capabilityRegistry,
                                               final CapabilityType type,
                                               final boolean includeNotExposed)
  {
    CapabilityReferenceFilterBuilder.CapabilityReferenceFilter filter = CapabilityReferenceFilterBuilder.capabilities().withType(type);

    if (includeNotExposed) {
      filter.includeNotExposed();
    }

    Collection<? extends CapabilityReference> capabilities = capabilityRegistry.get(filter);
    if (capabilities != null && !capabilities.isEmpty()) {
      return capabilities.iterator().next();
    }
    return null;
  }

  protected void addCapability(final CapabilityRegistry capabilityRegistry,
                               final CapabilityType type,
                               final boolean enabled,
                               final String notes,
                               final Map<String, String> properties)
      throws Exception
  {
    capabilityRegistry.add(
        type, enabled, notes == null ? "Automatically added on " + new Date() : notes, properties
    );
  }
}
TOP

Related Classes of org.sonatype.nexus.capability.support.CapabilityBooterSupport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.