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