/*******************************************************************************
 * Copyright (c) 2010, 2012 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.sisu.plexus.scanners;

import java.util.Map;

import org.codehaus.plexus.component.annotations.Configuration;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.sisu.plexus.annotations.ConfigurationImpl;
import org.eclipse.sisu.plexus.config.PlexusBeanMetadata;
import org.eclipse.sisu.reflect.BeanProperty;

/**
 * Runtime {@link PlexusBeanMetadata} based on {@link BeanProperty} annotations.
 */
public final class PlexusAnnotatedMetadata
    implements PlexusBeanMetadata
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    private final Map<?, ?> variables;

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    /**
     * Provides runtime Plexus metadata based on simple property annotations.
     * 
     * @param variables The filter variables
     */
    public PlexusAnnotatedMetadata( final Map<?, ?> variables )
    {
        this.variables = variables;
    }

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    public boolean isEmpty()
    {
        return false; // metadata comes from the properties themselves
    }

    public Configuration getConfiguration( final BeanProperty<?> property )
    {
        final Configuration configuration = property.getAnnotation( Configuration.class );
        if ( configuration != null && variables != null )
        {
            // support runtime interpolation of @Configuration values
            final String uninterpolatedValue = configuration.value();
            final String value = StringUtils.interpolate( uninterpolatedValue, variables );
            if ( !value.equals( uninterpolatedValue ) )
            {
                return new ConfigurationImpl( configuration.name(), value );
            }
        }
        return configuration;
    }

    public Requirement getRequirement( final BeanProperty<?> property )
    {
        return property.getAnnotation( Requirement.class );
    }
}

@@ -581,16 +581,96 @@
 nners;%0A%0A
+import java.io.IOException;%0Aimport java.io.Reader;%0Aimport java.io.StringReader;%0A
 import j
@@ -842,27 +842,81 @@
 us.util.
-StringUtils
+IOUtil;%0Aimport org.codehaus.plexus.util.InterpolationFilterReader
 ;%0Aimport
@@ -2617,20 +2617,8 @@
 e = 
-StringUtils.
 inte
@@ -2649,19 +2649,8 @@
 alue
-, variables
  );%0A
@@ -2927,32 +2927,32 @@
 roperty )%0A    %7B%0A
-
         return p
@@ -3000,11 +3000,805 @@
 );%0A    %7D
+%0A%0A    // ----------------------------------------------------------------------%0A    // Implementation methods%0A    // ----------------------------------------------------------------------%0A%0A    private String interpolate( final String text )%0A    %7B%0A        if ( null == text %7C%7C !text.contains( %22$%7B%22 ) )%0A        %7B%0A            return text;%0A        %7D%0A        // use same interpolation method as XML for sake of consistency%0A        final Reader r = new InterpolationFilterReader( new StringReader( text ), variables );%0A        try%0A        %7B%0A            return IOUtil.toString( r );%0A        %7D%0A        catch ( IOException e )%0A        %7B%0A            return text; // should never actually happen, as no actual I/O involved%0A        %7D%0A        finally%0A        %7B%0A            IOUtil.close( r );%0A        %7D%0A    %7D
 %0A%7D%0A

Use consistent interpolation method for Plexus configuration (regardless whether it's XML or annotation)