1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
32  
33  
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  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
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 }