View Javadoc
1   package org.apache.maven.configuration;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.lang3.Validate;
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
36      implements BeanConfigurationRequest
37  {
38  
39      private Object bean;
40  
41      private Object configuration;
42  
43      private String configurationElement;
44  
45      private ClassLoader classLoader;
46  
47      private BeanConfigurationValuePreprocessor valuePreprocessor;
48  
49      private BeanConfigurationPathTranslator pathTranslator;
50  
51      public Object getBean()
52      {
53          return bean;
54      }
55  
56      public DefaultBeanConfigurationRequest setBean( Object bean )
57      {
58          this.bean = bean;
59          return this;
60      }
61  
62      public Object getConfiguration()
63      {
64          return configuration;
65      }
66  
67      public String getConfigurationElement()
68      {
69          return configurationElement;
70      }
71  
72      public DefaultBeanConfigurationRequest setConfiguration( Object configuration )
73      {
74          return setConfiguration( configuration, null );
75      }
76  
77      public DefaultBeanConfigurationRequest setConfiguration( Object configuration, String element )
78      {
79          this.configuration = configuration;
80          this.configurationElement = element;
81          return this;
82      }
83  
84      /**
85       * Sets the configuration to the configuration taken from the specified build plugin in the POM. First, the build
86       * plugins will be searched for the specified plugin, if that fails, the plugin management section will be searched.
87       *
88       * @param model The POM to extract the plugin configuration from, may be {@code null}.
89       * @param pluginGroupId The group id of the plugin whose configuration should be used, must not be {@code null} or
90       *            empty.
91       * @param pluginArtifactId The artifact id of the plugin whose configuration should be used, must not be
92       *            {@code null} or empty.
93       * @param pluginExecutionId The id of a plugin execution whose configuration should be used, may be {@code null} or
94       *            empty to use the general plugin configuration.
95       * @return This request for chaining, never {@code null}.
96       */
97      public DefaultBeanConfigurationRequest setConfiguration( Model model, String pluginGroupId,
98                                                               String pluginArtifactId, String pluginExecutionId )
99      {
100         Plugin plugin = findPlugin( model, pluginGroupId, pluginArtifactId );
101         if ( plugin != null )
102         {
103             if ( StringUtils.isNotEmpty( pluginExecutionId ) )
104             {
105                 for ( PluginExecution execution : plugin.getExecutions() )
106                 {
107                     if ( pluginExecutionId.equals( execution.getId() ) )
108                     {
109                         setConfiguration( execution.getConfiguration() );
110                         break;
111                     }
112                 }
113             }
114             else
115             {
116                 setConfiguration( plugin.getConfiguration() );
117             }
118         }
119         return this;
120     }
121 
122     private Plugin findPlugin( Model model, String groupId, String artifactId )
123     {
124         Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" );
125         Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
126 
127         if ( model != null )
128         {
129             Build build = model.getBuild();
130             if ( build != null )
131             {
132                 for ( Plugin plugin : build.getPlugins() )
133                 {
134                     if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
135                     {
136                         return plugin;
137                     }
138                 }
139 
140                 PluginManagement mgmt = build.getPluginManagement();
141                 if ( mgmt != null )
142                 {
143                     for ( Plugin plugin : mgmt.getPlugins() )
144                     {
145                         if ( groupId.equals( plugin.getGroupId() ) && artifactId.equals( plugin.getArtifactId() ) )
146                         {
147                             return plugin;
148                         }
149                     }
150                 }
151             }
152         }
153 
154         return null;
155     }
156 
157     public ClassLoader getClassLoader()
158     {
159         return classLoader;
160     }
161 
162     public DefaultBeanConfigurationRequest setClassLoader( ClassLoader classLoader )
163     {
164         this.classLoader = classLoader;
165         return this;
166     }
167 
168     public BeanConfigurationValuePreprocessor getValuePreprocessor()
169     {
170         return valuePreprocessor;
171     }
172 
173     public DefaultBeanConfigurationRequest setValuePreprocessor( BeanConfigurationValuePreprocessor valuePreprocessor )
174     {
175         this.valuePreprocessor = valuePreprocessor;
176         return this;
177     }
178 
179     public BeanConfigurationPathTranslator getPathTranslator()
180     {
181         return pathTranslator;
182     }
183 
184     public DefaultBeanConfigurationRequest setPathTranslator( BeanConfigurationPathTranslator pathTranslator )
185     {
186         this.pathTranslator = pathTranslator;
187         return this;
188     }
189 
190 }