View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.configuration;
20  
21  import java.util.Objects;
22  
23  import org.apache.maven.model.Build;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.model.Plugin;
26  import org.apache.maven.model.PluginExecution;
27  import org.apache.maven.model.PluginManagement;
28  
29  /**
30   * A basic bean configuration request.
31   *
32   */
33  public class DefaultBeanConfigurationRequest implements BeanConfigurationRequest {
34  
35      private Object bean;
36  
37      private Object configuration;
38  
39      private String configurationElement;
40  
41      private ClassLoader classLoader;
42  
43      private BeanConfigurationValuePreprocessor valuePreprocessor;
44  
45      private BeanConfigurationPathTranslator pathTranslator;
46  
47      @Override
48      public Object getBean() {
49          return bean;
50      }
51  
52      @Override
53      public DefaultBeanConfigurationRequest setBean(Object bean) {
54          this.bean = bean;
55          return this;
56      }
57  
58      @Override
59      public Object getConfiguration() {
60          return configuration;
61      }
62  
63      @Override
64      public String getConfigurationElement() {
65          return configurationElement;
66      }
67  
68      @Override
69      public DefaultBeanConfigurationRequest setConfiguration(Object configuration) {
70          return setConfiguration(configuration, null);
71      }
72  
73      @Override
74      public DefaultBeanConfigurationRequest setConfiguration(Object configuration, String element) {
75          this.configuration = configuration;
76          this.configurationElement = element;
77          return this;
78      }
79  
80      /**
81       * Sets the configuration to the configuration taken from the specified build plugin in the POM. First, the build
82       * plugins will be searched for the specified plugin, if that fails, the plugin management section will be searched.
83       *
84       * @param model The POM to extract the plugin configuration from, may be {@code null}.
85       * @param pluginGroupId The group id of the plugin whose configuration should be used, must not be {@code null} or
86       *            empty.
87       * @param pluginArtifactId The artifact id of the plugin whose configuration should be used, must not be
88       *            {@code null} or empty.
89       * @param pluginExecutionId The id of a plugin execution whose configuration should be used, may be {@code null} or
90       *            empty to use the general plugin configuration.
91       * @return This request for chaining, never {@code null}.
92       */
93      public DefaultBeanConfigurationRequest setConfiguration(
94              Model model, String pluginGroupId, String pluginArtifactId, String pluginExecutionId) {
95          Plugin plugin = findPlugin(model, pluginGroupId, pluginArtifactId);
96          if (plugin != null) {
97              if (pluginExecutionId != null && !pluginExecutionId.isEmpty()) {
98                  for (PluginExecution execution : plugin.getExecutions()) {
99                      if (pluginExecutionId.equals(execution.getId())) {
100                         setConfiguration(execution.getConfiguration());
101                         break;
102                     }
103                 }
104             } else {
105                 setConfiguration(plugin.getConfiguration());
106             }
107         }
108         return this;
109     }
110 
111     private Plugin findPlugin(Model model, String groupId, String artifactId) {
112         if (Objects.requireNonNull(groupId, "groupId cannot be null").isEmpty()) {
113             throw new IllegalArgumentException("groupId cannot be empty");
114         }
115         if (Objects.requireNonNull(artifactId, "artifactId cannot be null").isEmpty()) {
116             throw new IllegalArgumentException("artifactId cannot be empty");
117         }
118 
119         if (model != null) {
120             Build build = model.getBuild();
121             if (build != null) {
122                 for (Plugin plugin : build.getPlugins()) {
123                     if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) {
124                         return plugin;
125                     }
126                 }
127 
128                 PluginManagement mgmt = build.getPluginManagement();
129                 if (mgmt != null) {
130                     for (Plugin plugin : mgmt.getPlugins()) {
131                         if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) {
132                             return plugin;
133                         }
134                     }
135                 }
136             }
137         }
138 
139         return null;
140     }
141 
142     @Override
143     public ClassLoader getClassLoader() {
144         return classLoader;
145     }
146 
147     @Override
148     public DefaultBeanConfigurationRequest setClassLoader(ClassLoader classLoader) {
149         this.classLoader = classLoader;
150         return this;
151     }
152 
153     @Override
154     public BeanConfigurationValuePreprocessor getValuePreprocessor() {
155         return valuePreprocessor;
156     }
157 
158     @Override
159     public DefaultBeanConfigurationRequest setValuePreprocessor(BeanConfigurationValuePreprocessor valuePreprocessor) {
160         this.valuePreprocessor = valuePreprocessor;
161         return this;
162     }
163 
164     @Override
165     public BeanConfigurationPathTranslator getPathTranslator() {
166         return pathTranslator;
167     }
168 
169     @Override
170     public DefaultBeanConfigurationRequest setPathTranslator(BeanConfigurationPathTranslator pathTranslator) {
171         this.pathTranslator = pathTranslator;
172         return this;
173     }
174 }