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 org.apache.commons.lang3.Validate;
22  import org.apache.maven.model.Build;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.model.Plugin;
25  import org.apache.maven.model.PluginExecution;
26  import org.apache.maven.model.PluginManagement;
27  import org.codehaus.plexus.util.StringUtils;
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 (StringUtils.isNotEmpty(pluginExecutionId)) {
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         Validate.notBlank(groupId, "groupId can neither be null, empty nor blank");
108         Validate.notBlank(artifactId, "artifactId can neither be null, empty nor blank");
109 
110         if (model != null) {
111             Build build = model.getBuild();
112             if (build != null) {
113                 for (Plugin plugin : build.getPlugins()) {
114                     if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) {
115                         return plugin;
116                     }
117                 }
118 
119                 PluginManagement mgmt = build.getPluginManagement();
120                 if (mgmt != null) {
121                     for (Plugin plugin : mgmt.getPlugins()) {
122                         if (groupId.equals(plugin.getGroupId()) && artifactId.equals(plugin.getArtifactId())) {
123                             return plugin;
124                         }
125                     }
126                 }
127             }
128         }
129 
130         return null;
131     }
132 
133     public ClassLoader getClassLoader() {
134         return classLoader;
135     }
136 
137     public DefaultBeanConfigurationRequest setClassLoader(ClassLoader classLoader) {
138         this.classLoader = classLoader;
139         return this;
140     }
141 
142     public BeanConfigurationValuePreprocessor getValuePreprocessor() {
143         return valuePreprocessor;
144     }
145 
146     public DefaultBeanConfigurationRequest setValuePreprocessor(BeanConfigurationValuePreprocessor valuePreprocessor) {
147         this.valuePreprocessor = valuePreprocessor;
148         return this;
149     }
150 
151     public BeanConfigurationPathTranslator getPathTranslator() {
152         return pathTranslator;
153     }
154 
155     public DefaultBeanConfigurationRequest setPathTranslator(BeanConfigurationPathTranslator pathTranslator) {
156         this.pathTranslator = pathTranslator;
157         return this;
158     }
159 }