aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/maven/apache-maven-3.3.3/maven-core/src/main/java/org/apache/maven/configuration/DefaultBeanConfigurationRequest.java
blob: 32def48f357a1a9544e80ec6a37a2f856759be22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package org.apache.maven.configuration;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.PluginManagement;
import org.codehaus.plexus.util.StringUtils;

/**
 * A basic bean configuration request.
 *
 * @author Benjamin Bentmann
 */
public class DefaultBeanConfigurationRequest
    implements BeanConfigurationRequest
{

    private Object bean;

    private Object configuration;

    private String configurationElement;

    private ClassLoader classLoader;

    private BeanConfigurationValuePreprocessor valuePreprocessor;

    private BeanConfigurationPathTranslator pathTranslator;

    public Object getBean()
    {
        return bean;
    }

    public DefaultBeanConfigurationRequest setBean( Object bean )
    {
        this.bean = bean;
        return this;
    }

    public Object getConfiguration()
    {
        return configuration;
    }

    public String getConfigurationElement()
    {
        return configurationElement;
    }

    public DefaultBeanConfigurationRequest setConfiguration( Object configuration )
    {
        return setConfiguration( configuration, null );
    }

    public DefaultBeanConfigurationRequest setConfiguration( Object configuration, String element )
    {
        this.configuration = configuration;
        this.configurationElement = element;
        return this;
    }

    /**
     * Sets the configuration to the configuration taken from the specified build plugin in the POM. First, the build
     * plugins will be searched for the specified plugin, if that fails, the plugin management section will be searched.
     *
     * @param model The POM to extract the plugin configuration from, may be {@code null}.
     * @param pluginGroupId The group id of the plugin whose configuration should be used, must not be {@code null} or
     *            empty.
     * @param pluginArtifactId The artifact id of the plugin whose configuration should be used, must not be
     *            {@code null} or empty.
     * @param pluginExecutionId The id of a plugin execution whose configuration should be used, may be {@code null} or
     *            empty to use the general plugin configuration.
     * @return This request for chaining, never {@code null}.
     */
    public DefaultBeanConfigurationRequest setConfiguration( Model model, String pluginGroupId,
                                                             String pluginArtifactId, String pluginExecutionId )
    {
        Plugin plugin = findPlugin( model, pluginGroupId, pluginArtifactId );
        if ( plugin != null )
        {
            if ( StringUtils.isNotEmpty( pluginExecutionId ) )
            {
                for ( PluginExecution execution : plugin.getExecutions() )
                {
                    if ( pluginExecutionId.equals( execution.getId() ) )
                    {
                        setConfiguration( execution.getConfiguration() );
                        break;
                    }
                }
            }
            else
            {
                setConfiguration( plugin.getConfiguration() );
            }
        }
        return this;
    }

    private Plugin findPlugin( Model model, String groupId, String artifactId )
    {
        if ( StringUtils.isEmpty( groupId ) )
        {
            throw new IllegalArgumentException( "group id for plugin has not been specified" );
        }
        if ( StringUtils.isEmpty( artifactId ) )
        {
            throw new IllegalArgumentException( "artifact id for plugin has not been specified" );
        }

        if ( model != null )
        {
            Build build = model.getBuild();
            if ( build != null )
            {
                for ( Plugin plugin : build.getPlugins() )
                {
                    if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
                    {
                        return plugin;
                    }
                }

                PluginManagement mngt = build.getPluginManagement();
                if ( mngt != null )
                {
                    for ( Plugin plugin : mngt.getPlugins() )
                    {
                        if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
                        {
                            return plugin;
                        }
                    }
                }
            }
        }

        return null;
    }

    public ClassLoader getClassLoader()
    {
        return classLoader;
    }

    public DefaultBeanConfigurationRequest setClassLoader( ClassLoader classLoader )
    {
        this.classLoader = classLoader;
        return this;
    }

    public BeanConfigurationValuePreprocessor getValuePreprocessor()
    {
        return valuePreprocessor;
    }

    public DefaultBeanConfigurationRequest setValuePreprocessor( BeanConfigurationValuePreprocessor valuePreprocessor )
    {
        this.valuePreprocessor = valuePreprocessor;
        return this;
    }

    public BeanConfigurationPathTranslator getPathTranslator()
    {
        return pathTranslator;
    }

    public DefaultBeanConfigurationRequest setPathTranslator( BeanConfigurationPathTranslator pathTranslator )
    {
        this.pathTranslator = pathTranslator;
        return this;
    }

}