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