Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2013 smartics, Kronseder & Reiner GmbH
   *
   * 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.
  */
 package com.redhat.rcm.maven.plugin.buildmetadata.data;
 
 import java.util.Map;
 
 
Configuration instance to create instances of MetaDataProvider.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class MetaDataProviderBuilder
 {
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
   // --- members --------------------------------------------------------------
 
  
The Maven project.
 
   private final MavenProject project;

  
The Maven session instance.
 
   private final MavenSession session;

  
The runtime information of the Maven instance being executed for the build.
 
   private final RuntimeInformation runtime;

  
The information for the SCM provided to the build plugin.
 
   private final ScmInfo scmInfo;
 
   // ****************************** Initializer *******************************
 
   // ****************************** Constructors ******************************
 
  
Default constructor.

Parameters:
project the Maven project.
session the Maven session instance.
runtime the runtime information of the Maven instance being executed for the build.
scmInfo the information for the SCM provided to the build plugin.
 
   public MetaDataProviderBuilder(final MavenProject project,
       final MavenSession sessionfinal RuntimeInformation runtime,
       final ScmInfo scmInfo)
   {
     this. = project;
     this. = session;
     this. = runtime;
     this. = scmInfo;
   }
 
   // ****************************** Inner Classes *****************************
 
   // ********************************* Methods ********************************
 
   // --- init -----------------------------------------------------------------
 
   // --- get&set --------------------------------------------------------------
 
   // --- business -------------------------------------------------------------
 
  
Creates a configured instance of the MetaDataProvider interface.

Parameters:
config the configuration for the build.
Returns:
the created instance.
Throws:
org.apache.maven.plugin.MojoExecutionException if the instance cannot be created.
  public MetaDataProvider build(final Provider config)
  {
    final MetaDataProvider instance = create(config.getType());
    initialize(instanceconfig.getProperties());
    return instance;
  }
  @SuppressWarnings("unchecked")
  private MetaDataProvider create(final String metaDataProviderClassName)
  {
    try
    {
      final Class<? extends MetaDataProvidermetaDataProviderClass =
          (Class<? extends MetaDataProvider>) Class
              .forName(metaDataProviderClassName);
      final MetaDataProvider instance = metaDataProviderClass.newInstance();
      return instance;
    }
    catch (final Exception e)
    {
      throw new MojoExecutionException(
          "Cannot create instance for meta data provider class '"
              + metaDataProviderClassName + "'."e);
    }
  }
  private void initialize(final MetaDataProvider instance,
      final Map<StringStringpropertiesthrows MojoExecutionException
  {
    setNonNullProperty(instance"project"MavenProject.class);
    setNonNullProperty(instance"session"MavenSession.class);
    setNonNullProperty(instance"runtime"RuntimeInformation.class);
    setNonNullProperty(instance"scmInfo"ScmInfo.class);
    setProperties(instanceproperties);
  }
  private void setNonNullProperty(final MetaDataProvider instance,
      final String propertyNamefinal Object propertyValue,
      final Class<?> propertyTypethrows MojoExecutionException
  {
    if (propertyValue != null)
    {
      final Class<? extends MetaDataProvidermetaDataProviderClass =
          instance.getClass();
      try
      {
        final Field field = findField(metaDataProviderClasspropertyName);
        final Class<?> type = field.getType();
        if (type.isAssignableFrom(propertyType))
        {
          field.setAccessible(true);
          field.set(instancepropertyValue);
        }
      }
      catch (final NoSuchFieldException e)
      {
        // OK, no such field, so we do not set it.
      }
      catch (final Exception e)
      {
        throw new MojoExecutionException("Cannot set property '" + propertyName
                                         + "' for the instance of class '"
                                         + metaDataProviderClass.getName()
                                         + "'."e);
      }
    }
  }
  private Field findField(final Class<?> typefinal String propertyName)
    throws NoSuchFieldException
  {
    try
    {
      return type.getDeclaredField(propertyName);
    }
    catch (final NoSuchFieldException e)
    {
      if (type.getSuperclass().equals(Object.class))
      {
        throw e;
      }
    }
    return findField(type.getSuperclass(), propertyName);
  }
  private void setProperties(final MetaDataProvider instance,
      final Map<StringStringpropertiesthrows MojoExecutionException
  {
    if (properties != null && !properties.isEmpty())
    {
      for (final Map.Entry<StringStringentry : properties.entrySet())
      {
        final String propertyName = entry.getKey();
        if (!..equals(propertyName))
        {
          final String propertyValue = entry.getValue();
          setProperty(instancepropertyNamepropertyValue);
        }
      }
    }
  }
  private void setProperty(final MetaDataProvider instance,
      final String propertyNamefinal String propertyValue)
  {
    final Class<? extends MetaDataProvidermetaDataProviderClass =
        instance.getClass();
    try
    {
      final Field field = findField(metaDataProviderClasspropertyName);
      field.setAccessible(true);
      final Class<?> type = field.getType();
      final Object typedPropertyValue =
          ConvertUtils.convert(propertyValuetype);
      field.set(instancetypedPropertyValue);
    }
    catch (final Exception e)
    {
      throw new MojoExecutionException(
          "Cannot set property '" + propertyName + "' to value '"
              + propertyValue + "' for the instance of class '"
              + metaDataProviderClass.getName() + "'."e);
    }
  }
  // --- object basics --------------------------------------------------------
New to GrepCode? Check out our FAQ X