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