001package org.apache.maven.configuration;
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 org.apache.commons.lang3.Validate;
023import org.apache.maven.model.Build;
024import org.apache.maven.model.Model;
025import org.apache.maven.model.Plugin;
026import org.apache.maven.model.PluginExecution;
027import org.apache.maven.model.PluginManagement;
028import org.codehaus.plexus.util.StringUtils;
029
030/**
031 * A basic bean configuration request.
032 *
033 * @author Benjamin Bentmann
034 */
035public class DefaultBeanConfigurationRequest
036    implements BeanConfigurationRequest
037{
038
039    private Object bean;
040
041    private Object configuration;
042
043    private String configurationElement;
044
045    private ClassLoader classLoader;
046
047    private BeanConfigurationValuePreprocessor valuePreprocessor;
048
049    private BeanConfigurationPathTranslator pathTranslator;
050
051    public Object getBean()
052    {
053        return bean;
054    }
055
056    public DefaultBeanConfigurationRequest setBean( Object bean )
057    {
058        this.bean = bean;
059        return this;
060    }
061
062    public Object getConfiguration()
063    {
064        return configuration;
065    }
066
067    public String getConfigurationElement()
068    {
069        return configurationElement;
070    }
071
072    public DefaultBeanConfigurationRequest setConfiguration( Object configuration )
073    {
074        return setConfiguration( configuration, null );
075    }
076
077    public DefaultBeanConfigurationRequest setConfiguration( Object configuration, String element )
078    {
079        this.configuration = configuration;
080        this.configurationElement = element;
081        return this;
082    }
083
084    /**
085     * Sets the configuration to the configuration taken from the specified build plugin in the POM. First, the build
086     * plugins will be searched for the specified plugin, if that fails, the plugin management section will be searched.
087     *
088     * @param model The POM to extract the plugin configuration from, may be {@code null}.
089     * @param pluginGroupId The group id of the plugin whose configuration should be used, must not be {@code null} or
090     *            empty.
091     * @param pluginArtifactId The artifact id of the plugin whose configuration should be used, must not be
092     *            {@code null} or empty.
093     * @param pluginExecutionId The id of a plugin execution whose configuration should be used, may be {@code null} or
094     *            empty to use the general plugin configuration.
095     * @return This request for chaining, never {@code null}.
096     */
097    public DefaultBeanConfigurationRequest setConfiguration( Model model, String pluginGroupId,
098                                                             String pluginArtifactId, String pluginExecutionId )
099    {
100        Plugin plugin = findPlugin( model, pluginGroupId, pluginArtifactId );
101        if ( plugin != null )
102        {
103            if ( StringUtils.isNotEmpty( pluginExecutionId ) )
104            {
105                for ( PluginExecution execution : plugin.getExecutions() )
106                {
107                    if ( pluginExecutionId.equals( execution.getId() ) )
108                    {
109                        setConfiguration( execution.getConfiguration() );
110                        break;
111                    }
112                }
113            }
114            else
115            {
116                setConfiguration( plugin.getConfiguration() );
117            }
118        }
119        return this;
120    }
121
122    private Plugin findPlugin( Model model, String groupId, String artifactId )
123    {
124        Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" );
125        Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
126
127        if ( model != null )
128        {
129            Build build = model.getBuild();
130            if ( build != null )
131            {
132                for ( Plugin plugin : build.getPlugins() )
133                {
134                    if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
135                    {
136                        return plugin;
137                    }
138                }
139
140                PluginManagement mngt = build.getPluginManagement();
141                if ( mngt != null )
142                {
143                    for ( Plugin plugin : mngt.getPlugins() )
144                    {
145                        if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
146                        {
147                            return plugin;
148                        }
149                    }
150                }
151            }
152        }
153
154        return null;
155    }
156
157    public ClassLoader getClassLoader()
158    {
159        return classLoader;
160    }
161
162    public DefaultBeanConfigurationRequest setClassLoader( ClassLoader classLoader )
163    {
164        this.classLoader = classLoader;
165        return this;
166    }
167
168    public BeanConfigurationValuePreprocessor getValuePreprocessor()
169    {
170        return valuePreprocessor;
171    }
172
173    public DefaultBeanConfigurationRequest setValuePreprocessor( BeanConfigurationValuePreprocessor valuePreprocessor )
174    {
175        this.valuePreprocessor = valuePreprocessor;
176        return this;
177    }
178
179    public BeanConfigurationPathTranslator getPathTranslator()
180    {
181        return pathTranslator;
182    }
183
184    public DefaultBeanConfigurationRequest setPathTranslator( BeanConfigurationPathTranslator pathTranslator )
185    {
186        this.pathTranslator = pathTranslator;
187        return this;
188    }
189
190}