Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Sonatype Nexus (TM) Open Source Version
   * Copyright (c) 2008-2015 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.plugins.capabilities.internal;
 
 import java.util.Map;
 
 import  javax.inject.Inject;
 import  javax.inject.Named;
 import  javax.inject.Singleton;
 
 import  org.sonatype.sisu.goodies.common.ComponentSupport;
 
 import  com.google.common.collect.Maps;
 import  com.google.inject.ConfigurationException;
 import  com.google.inject.Key;
 import  org.eclipse.sisu.BeanEntry;
 import  org.eclipse.sisu.inject.BeanLocator;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.inject.name.Names.named;

Default CapabilityFactoryRegistry implementation.

Since:
capabilities 2.0
 
 @Named
 @Singleton
     extends ComponentSupport
     implements CapabilityFactoryRegistry
 {
 
   private final Map<StringCapabilityFactoryfactories;
 
   private final Map<StringCapabilityFactorydynamicFactories;
 
 
   private final BeanLocator beanLocator;
 
   @Inject
                                    final CapabilityDescriptorRegistry capabilityDescriptorRegistry,
                                    final BeanLocator beanLocator)
   {
     this. = checkNotNull(beanLocator);
     this. = checkNotNull(capabilityDescriptorRegistry);
     this. = checkNotNull(factories);
     this. = Maps.newConcurrentMap();
   }
 
   @Override
   public CapabilityFactoryRegistry register(final CapabilityType typefinal CapabilityFactory factory) {
     checkNotNull(factory);
     checkArgument(!.containsKey(type), "Factory already registered for %s"type);
     checkArgument(!.containsKey(type), "Factory already registered for %s"type);
 
     .put(type.toString(), factory);
     log.debug("Added {} -> {}"typefactory);
 
     return this;
   }
 
   @Override
   public CapabilityFactoryRegistry unregister(final CapabilityType type) {
     if (type != null) {
       final CapabilityFactory factory = .remove(type);
       log.debug("Removed {} -> {}"typefactory);
     }
 
     return this;
   }
 
   @Override
   public CapabilityFactory get(final CapabilityType type) {
     CapabilityFactory factory = .get(checkNotNull(type).toString());
     if (factory == null) {
       factory = .get(checkNotNull(type).toString());
     }
     if (factory == null) {
      final CapabilityDescriptor descriptor = .get(type);
      if (descriptor != null && descriptor instanceof CapabilityFactory) {
        factory = (CapabilityFactorydescriptor;
      }
      if (factory == null) {
        try {
          final Iterable<? extends BeanEntry<?, Capability>> entries = .locate(
              Key.get(Capability.classnamed(type.toString()))
          );
          if (entries != null && entries.iterator().hasNext()) {
            factory = new CapabilityFactory()
            {
              @Override
              public Capability create() {
                return entries.iterator().next().getValue();
              }
            };
          }
        }
        catch (ConfigurationException ignore) {
          // ignore
        }
      }
    }
    return factory;
  }
New to GrepCode? Check out our FAQ X