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