1   package org.apache.maven.configuration;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
31  
32  
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  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
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 }