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      public Object getBean() {
48          return bean;
49      }
50  
51      public DefaultBeanConfigurationRequest setBean(Object bean) {
52          this.bean = bean;
53          return this;
54      }
55  
56      public Object getConfiguration() {
57          return configuration;
58      }
59  
60      public String getConfigurationElement() {
61          return configurationElement;
62      }
63  
64      public DefaultBeanConfigurationRequest setConfiguration(Object configuration) {
65          return setConfiguration(configuration, null);
66      }
67  
68      public DefaultBeanConfigurationRequest setConfiguration(Object configuration, String element) {
69          this.configuration = configuration;
70          this.configurationElement = element;
71          return this;
72      }
73  
74      /**
75       * Sets the configuration to the configuration taken from the specified build plugin in the POM. First, the build
76       * plugins will be searched for the specified plugin, if that fails, the plugin management section will be searched.
77       *
78       * @param model The POM to extract the plugin configuration from, may be {@code null}.
79       * @param pluginGroupId The group id of the plugin whose configuration should be used, must not be {@code null} or
80       *            empty.
81       * @param pluginArtifactId The artifact id of the plugin whose configuration should be used, must not be
82       *            {@code null} or empty.
83       * @param pluginExecutionId The id of a plugin execution whose configuration should be used, may be {@code null} or
84       *            empty to use the general plugin configuration.
85       * @return This request for chaining, never {@code null}.
86       */
87      public DefaultBeanConfigurationRequest setConfiguration(
88              Model model, String pluginGroupId, String pluginArtifactId, String pluginExecutionId) {
89          Plugin plugin = findPlugin(model, pluginGroupId, pluginArtifactId);
90          if (plugin != null) {
91              if (pluginExecutionId != null && !pluginExecutionId.isEmpty()) {
92                  for (PluginExecution execution : plugin.getExecutions()) {
93                      if (pluginExecutionId.equals(execution.getId())) {
94                          setConfiguration(execution.getConfiguration());
95                          break;
96                      }
97                  }
98              } else {
99                  setConfiguration(plugin.getConfiguration());
100             }
101         }
102         return this;
103     }
104 
105     private Plugin findPlugin(Model model, String groupId, String artifactId) {
106         if (Objects.requireNonNull(groupId, "groupId cannot be null").isEmpty()) {
107             throw new IllegalArgumentException("groupId cannot be empty");
108         }
109         if (Objects.requireNonNull(artifactId, "artifactId cannot be null").isEmpty()) {
110             throw new IllegalArgumentException("artifactId cannot be empty");
111         }
112 
113         if (model != null) {
114             Build build = model.getBuild();
115             if (build != null) {
116                 for (Plugin plugin : build.getPlugins()) {
117                     if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) {
118                         return plugin;
119                     }
120                 }
121 
122                 PluginManagement mgmt = build.getPluginManagement();
123                 if (mgmt != null) {
124                     for (Plugin plugin : mgmt.getPlugins()) {
125                         if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) {
126                             return plugin;
127                         }
128                     }
129                 }
130             }
131         }
132 
133         return null;
134     }
135 
136     public ClassLoader getClassLoader() {
137         return classLoader;
138     }
139 
140     public DefaultBeanConfigurationRequest setClassLoader(ClassLoader classLoader) {
141         this.classLoader = classLoader;
142         return this;
143     }
144 
145     public BeanConfigurationValuePreprocessor getValuePreprocessor() {
146         return valuePreprocessor;
147     }
148 
149     public DefaultBeanConfigurationRequest setValuePreprocessor(BeanConfigurationValuePreprocessor valuePreprocessor) {
150         this.valuePreprocessor = valuePreprocessor;
151         return this;
152     }
153 
154     public BeanConfigurationPathTranslator getPathTranslator() {
155         return pathTranslator;
156     }
157 
158     public DefaultBeanConfigurationRequest setPathTranslator(BeanConfigurationPathTranslator pathTranslator) {
159         this.pathTranslator = pathTranslator;
160         return this;
161     }
162 }