001 package org.apache.maven.configuration;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.maven.model.Build;
023 import org.apache.maven.model.Model;
024 import org.apache.maven.model.Plugin;
025 import org.apache.maven.model.PluginExecution;
026 import org.apache.maven.model.PluginManagement;
027 import org.codehaus.plexus.util.StringUtils;
028
029 /**
030 * A basic bean configuration request.
031 *
032 * @author Benjamin Bentmann
033 */
034 public class DefaultBeanConfigurationRequest
035 implements BeanConfigurationRequest
036 {
037
038 private Object bean;
039
040 private Object configuration;
041
042 private String configurationElement;
043
044 private ClassLoader classLoader;
045
046 private BeanConfigurationValuePreprocessor valuePreprocessor;
047
048 private BeanConfigurationPathTranslator pathTranslator;
049
050 public Object getBean()
051 {
052 return bean;
053 }
054
055 public DefaultBeanConfigurationRequest setBean( Object bean )
056 {
057 this.bean = bean;
058 return this;
059 }
060
061 public Object getConfiguration()
062 {
063 return configuration;
064 }
065
066 public String getConfigurationElement()
067 {
068 return configurationElement;
069 }
070
071 public DefaultBeanConfigurationRequest setConfiguration( Object configuration )
072 {
073 return setConfiguration( configuration, null );
074 }
075
076 public DefaultBeanConfigurationRequest setConfiguration( Object configuration, String element )
077 {
078 this.configuration = configuration;
079 this.configurationElement = element;
080 return this;
081 }
082
083 /**
084 * Sets the configuration to the configuration taken from the specified build plugin in the POM. First, the build
085 * plugins will be searched for the specified plugin, if that fails, the plugin management section will be searched.
086 *
087 * @param model The POM to extract the plugin configuration from, may be {@code null}.
088 * @param pluginGroupId The group id of the plugin whose configuration should be used, must not be {@code null} or
089 * empty.
090 * @param pluginArtifactId The artifact id of the plugin whose configuration should be used, must not be
091 * {@code null} or empty.
092 * @param pluginExecutionId The id of a plugin execution whose configuration should be used, may be {@code null} or
093 * empty to use the general plugin configuration.
094 * @return This request for chaining, never {@code null}.
095 */
096 public DefaultBeanConfigurationRequest setConfiguration( Model model, String pluginGroupId,
097 String pluginArtifactId, String pluginExecutionId )
098 {
099 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 }