001package org.apache.maven.configuration.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023
024import org.apache.commons.lang3.Validate;
025import org.apache.maven.configuration.BeanConfigurationException;
026import org.apache.maven.configuration.BeanConfigurationPathTranslator;
027import org.apache.maven.configuration.BeanConfigurationRequest;
028import org.apache.maven.configuration.BeanConfigurationValuePreprocessor;
029import org.apache.maven.configuration.BeanConfigurator;
030import org.codehaus.plexus.component.annotations.Component;
031import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
032import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
033import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
034import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
035import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
036import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
037import org.codehaus.plexus.configuration.PlexusConfiguration;
038import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
039import org.codehaus.plexus.util.xml.Xpp3Dom;
040
041/**
042 * <strong>Warning:</strong> This is an internal class that is only public for technical reasons, it is not part of the
043 * public API. In particular, this class can be changed or deleted without prior notice.
044 *
045 * @author Benjamin Bentmann
046 */
047@Component( role = BeanConfigurator.class )
048public class DefaultBeanConfigurator
049    implements BeanConfigurator
050{
051
052    private final ConverterLookup converterLookup = new DefaultConverterLookup();
053
054    public void configureBean( BeanConfigurationRequest request )
055        throws BeanConfigurationException
056    {
057        Validate.notNull( request, "request cannot be null" );
058        Validate.notNull( request.getBean(), "request.bean cannot be null" );
059
060        Object configuration = request.getConfiguration();
061        if ( configuration == null )
062        {
063            return;
064        }
065
066        PlexusConfiguration plexusConfig;
067        if ( configuration instanceof PlexusConfiguration )
068        {
069            plexusConfig = (PlexusConfiguration) configuration;
070        }
071        else if ( configuration instanceof Xpp3Dom )
072        {
073            plexusConfig = new XmlPlexusConfiguration( (Xpp3Dom) configuration );
074        }
075        else
076        {
077            throw new BeanConfigurationException( "unsupported bean configuration source ("
078                + configuration.getClass().getName() + ")" );
079        }
080
081        if ( request.getConfigurationElement() != null )
082        {
083            plexusConfig = plexusConfig.getChild( request.getConfigurationElement() );
084        }
085
086        ClassLoader classLoader = request.getClassLoader();
087        if ( classLoader == null )
088        {
089            classLoader = request.getBean().getClass().getClassLoader();
090        }
091
092        BeanExpressionEvaluator evaluator = new BeanExpressionEvaluator( request );
093
094        ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
095
096        try
097        {
098            converter.processConfiguration( converterLookup, request.getBean(), classLoader, plexusConfig, evaluator );
099        }
100        catch ( ComponentConfigurationException e )
101        {
102            throw new BeanConfigurationException( e.getMessage(), e );
103        }
104    }
105
106    static class BeanExpressionEvaluator
107        implements TypeAwareExpressionEvaluator
108    {
109
110        private final BeanConfigurationValuePreprocessor preprocessor;
111
112        private final BeanConfigurationPathTranslator translator;
113
114        public BeanExpressionEvaluator( BeanConfigurationRequest request )
115        {
116            preprocessor = request.getValuePreprocessor();
117            translator = request.getPathTranslator();
118        }
119
120        public Object evaluate( String expression, Class<?> type )
121            throws ExpressionEvaluationException
122        {
123            if ( preprocessor != null )
124            {
125                try
126                {
127                    return preprocessor.preprocessValue( expression, type );
128                }
129                catch ( BeanConfigurationException e )
130                {
131                    throw new ExpressionEvaluationException( e.getMessage(), e );
132                }
133            }
134            return expression;
135        }
136
137        public Object evaluate( String expression )
138            throws ExpressionEvaluationException
139        {
140            return evaluate( expression, null );
141        }
142
143        public File alignToBaseDirectory( File file )
144        {
145            if ( translator != null )
146            {
147                return translator.translatePath( file );
148            }
149            return file;
150        }
151
152    }
153
154}