1 package org.apache.maven.archiva.repository.project.filters;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.model.ArchivaModelCloner;
25 import org.apache.maven.archiva.model.ArchivaProjectModel;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.model.CiManagement;
28 import org.apache.maven.archiva.model.Dependency;
29 import org.apache.maven.archiva.model.Exclusion;
30 import org.apache.maven.archiva.model.Individual;
31 import org.apache.maven.archiva.model.IssueManagement;
32 import org.apache.maven.archiva.model.License;
33 import org.apache.maven.archiva.model.MailingList;
34 import org.apache.maven.archiva.model.Organization;
35 import org.apache.maven.archiva.model.ProjectRepository;
36 import org.apache.maven.archiva.model.Scm;
37 import org.apache.maven.archiva.model.VersionedReference;
38 import org.apache.maven.archiva.repository.project.ProjectModelException;
39 import org.apache.maven.archiva.repository.project.ProjectModelFilter;
40 import org.codehaus.plexus.evaluator.DefaultExpressionEvaluator;
41 import org.codehaus.plexus.evaluator.EvaluatorException;
42 import org.codehaus.plexus.evaluator.ExpressionEvaluator;
43 import org.codehaus.plexus.evaluator.ExpressionSource;
44 import org.codehaus.plexus.evaluator.sources.PropertiesExpressionSource;
45 import org.codehaus.plexus.evaluator.sources.SystemPropertyExpressionSource;
47 import java.util.ArrayList;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.Properties;
54 * ProjectModelExpressionFilter
57 * @plexus.component role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
58 * role-hint="expression"
59 * instantiation-strategy="per-lookup"
61 public class ProjectModelExpressionFilter
62 implements ProjectModelFilter
64 private ExpressionEvaluator evaluator = new DefaultExpressionEvaluator();
67 * Find and Evaluate the Expressions present in the model.
69 * @param model the model to correct.
71 @SuppressWarnings("unchecked")
72 public ArchivaProjectModel filter( final ArchivaProjectModel model )
73 throws ProjectModelException
75 Properties props = new Properties();
77 if ( model.getProperties() != null )
79 props.putAll( model.getProperties() );
82 ArchivaProjectModel ret = ArchivaModelCloner.clone( model );
84 // TODO: should probably clone evaluator to prevent threading issues.
85 synchronized ( evaluator )
87 // TODO: create .resetSources() method in ExpressionEvaluator project on plexus side.
88 // Remove previous expression sources.
89 List<ExpressionSource> oldSources = new ArrayList<ExpressionSource>();
90 oldSources.addAll( evaluator.getExpressionSourceList() );
91 for ( ExpressionSource exprSrc : oldSources )
93 evaluator.removeExpressionSource( exprSrc );
96 // Setup new sources (based on current model)
97 PropertiesExpressionSource propsSource = new PropertiesExpressionSource();
98 propsSource.setProperties( props );
99 evaluator.addExpressionSource( propsSource );
101 // Add system properties to the mix.
102 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
106 // Setup some common properties.
107 VersionedReference parent = model.getParentProject();
108 if ( parent != null )
110 String parentGroupId = StringUtils.defaultString( evaluator.expand( parent.getGroupId() ) );
111 String parentArtifactId = StringUtils.defaultString( evaluator.expand( parent.getArtifactId() ) );
112 String parentVersion = StringUtils.defaultString( evaluator.expand( parent.getVersion() ) );
114 props.setProperty( "parent.groupId", parentGroupId );
115 props.setProperty( "parent.artifactId", parentArtifactId );
116 props.setProperty( "parent.version", parentVersion );
119 String groupId = StringUtils.defaultString( evaluator.expand( model.getGroupId() ) );
120 String artifactId = StringUtils.defaultString( evaluator.expand( model.getArtifactId() ) );
121 String version = StringUtils.defaultString( evaluator.expand( model.getVersion() ) );
122 String name = StringUtils.defaultString( evaluator.expand( model.getName() ) );
125 /* Archiva doesn't need to handle a full expression language with object tree walking
126 * as the requirements within Archiva are much smaller, a quick replacement of the
127 * important fields (groupId, artifactId, version, name) are handled specifically.
129 props.setProperty( "pom.groupId", groupId );
130 props.setProperty( "pom.artifactId", artifactId );
131 props.setProperty( "pom.version", version );
132 props.setProperty( "pom.name", name );
133 props.setProperty( "project.groupId", groupId );
134 props.setProperty( "project.artifactId", artifactId );
135 props.setProperty( "project.version", version );
136 props.setProperty( "project.name", name );
138 // Evaluate everything.
139 ret.setVersion( evaluator.expand( ret.getVersion() ) );
140 ret.setGroupId( evaluator.expand( ret.getGroupId() ) );
141 ret.setName( evaluator.expand( ret.getName() ) );
142 ret.setDescription( evaluator.expand( ret.getDescription() ) );
143 ret.setPackaging( evaluator.expand( ret.getPackaging() ) );
144 ret.setUrl( evaluator.expand( ret.getUrl() ) );
146 evaluateParentProject( evaluator, ret.getParentProject() );
148 evaluateBuildExtensions( evaluator, ret.getBuildExtensions() );
149 evaluateCiManagement( evaluator, ret.getCiManagement() );
150 evaluateDependencyList( evaluator, ret.getDependencies() );
151 evaluateDependencyList( evaluator, ret.getDependencyManagement() );
152 evaluateIndividuals( evaluator, ret.getIndividuals() );
153 evaluateIssueManagement( evaluator, ret.getIssueManagement() );
154 evaluateLicenses( evaluator, ret.getLicenses() );
155 evaluateMailingLists( evaluator, ret.getMailingLists() );
156 evaluateOrganization( evaluator, ret.getOrganization() );
157 evaluatePlugins( evaluator, ret.getPlugins() );
158 evaluateRelocation( evaluator, ret.getRelocation() );
159 evaluateReports( evaluator, ret.getReports() );
160 evaluateRepositories( evaluator, ret.getRepositories() );
161 evaluateScm( evaluator, ret.getScm() );
163 catch ( EvaluatorException e )
165 throw new ProjectModelException( "Unable to evaluate expression in model: " + e.getMessage(), e );
172 private void evaluateArtifactReferenceList( ExpressionEvaluator eval, List<ArtifactReference> refs )
173 throws EvaluatorException
175 if ( CollectionUtils.isEmpty( refs ) )
180 for ( ArtifactReference ref : refs )
182 ref.setGroupId( eval.expand( ref.getGroupId() ) );
183 ref.setArtifactId( eval.expand( ref.getArtifactId() ) );
184 ref.setVersion( eval.expand( ref.getVersion() ) );
185 ref.setClassifier( eval.expand( ref.getClassifier() ) );
186 ref.setType( eval.expand( ref.getType() ) );
190 private void evaluateBuildExtensions( ExpressionEvaluator eval, List<ArtifactReference> buildExtensions )
191 throws EvaluatorException
193 if ( CollectionUtils.isEmpty( buildExtensions ) )
198 for ( ArtifactReference ref : buildExtensions )
200 ref.setGroupId( eval.expand( ref.getGroupId() ) );
201 ref.setArtifactId( eval.expand( ref.getArtifactId() ) );
202 ref.setVersion( eval.expand( ref.getVersion() ) );
203 ref.setClassifier( eval.expand( ref.getClassifier() ) );
204 ref.setType( eval.expand( ref.getType() ) );
208 private void evaluateCiManagement( ExpressionEvaluator eval, CiManagement ciManagement )
209 throws EvaluatorException
211 if ( ciManagement == null )
216 ciManagement.setSystem( eval.expand( ciManagement.getSystem() ) );
217 ciManagement.setUrl( eval.expand( ciManagement.getUrl() ) );
220 private void evaluateDependencyList( ExpressionEvaluator eval, List<Dependency> dependencies )
221 throws EvaluatorException
223 if ( CollectionUtils.isEmpty( dependencies ) )
228 for ( Dependency dependency : dependencies )
230 dependency.setGroupId( eval.expand( dependency.getGroupId() ) );
231 dependency.setArtifactId( eval.expand( dependency.getArtifactId() ) );
232 dependency.setVersion( eval.expand( dependency.getVersion() ) );
233 dependency.setScope( eval.expand( dependency.getScope() ) );
234 dependency.setType( eval.expand( dependency.getType() ) );
235 dependency.setUrl( eval.expand( dependency.getUrl() ) );
237 evaluateExclusions( eval, dependency.getExclusions() );
241 private void evaluateExclusions( ExpressionEvaluator eval, List<Exclusion> exclusions )
242 throws EvaluatorException
244 if ( CollectionUtils.isEmpty( exclusions ) )
249 for ( Exclusion exclusion : exclusions )
251 exclusion.setGroupId( eval.expand( exclusion.getGroupId() ) );
252 exclusion.setArtifactId( eval.expand( exclusion.getArtifactId() ) );
256 private void evaluateIndividuals( ExpressionEvaluator eval, List<Individual> individuals )
257 throws EvaluatorException
259 if ( CollectionUtils.isEmpty( individuals ) )
264 for ( Individual individual : individuals )
266 individual.setPrincipal( eval.expand( individual.getPrincipal() ) );
267 individual.setName( eval.expand( individual.getName() ) );
268 individual.setEmail( eval.expand( individual.getEmail() ) );
269 individual.setTimezone( eval.expand( individual.getTimezone() ) );
270 individual.setOrganization( eval.expand( individual.getOrganization() ) );
271 individual.setOrganizationUrl( eval.expand( individual.getOrganizationUrl() ) );
272 individual.setUrl( eval.expand( individual.getUrl() ) );
274 evaluateProperties( eval, individual.getProperties() );
275 evaluateStringList( eval, individual.getRoles() );
279 private void evaluateIssueManagement( ExpressionEvaluator eval, IssueManagement issueManagement )
280 throws EvaluatorException
282 if ( issueManagement == null )
287 issueManagement.setSystem( eval.expand( issueManagement.getSystem() ) );
288 issueManagement.setUrl( eval.expand( issueManagement.getUrl() ) );
291 private void evaluateLicenses( ExpressionEvaluator eval, List<License> licenses )
292 throws EvaluatorException
294 if ( CollectionUtils.isEmpty( licenses ) )
299 for ( License license : licenses )
301 license.setName( eval.expand( license.getName() ) );
302 license.setUrl( eval.expand( license.getUrl() ) );
303 license.setComments( eval.expand( license.getComments() ) );
307 private void evaluateMailingLists( ExpressionEvaluator eval, List<MailingList> mailingLists )
308 throws EvaluatorException
310 if ( CollectionUtils.isEmpty( mailingLists ) )
315 for ( MailingList mlist : mailingLists )
317 mlist.setName( eval.expand( mlist.getName() ) );
318 mlist.setSubscribeAddress( eval.expand( mlist.getSubscribeAddress() ) );
319 mlist.setUnsubscribeAddress( eval.expand( mlist.getUnsubscribeAddress() ) );
320 mlist.setPostAddress( eval.expand( mlist.getPostAddress() ) );
321 mlist.setMainArchiveUrl( eval.expand( mlist.getMainArchiveUrl() ) );
323 evaluateStringList( eval, mlist.getOtherArchives() );
327 private void evaluateOrganization( ExpressionEvaluator eval, Organization organization )
328 throws EvaluatorException
330 if ( organization == null )
335 organization.setName( eval.expand( organization.getName() ) );
336 organization.setUrl( eval.expand( organization.getUrl() ) );
337 organization.setFavicon( eval.expand( organization.getFavicon() ) );
340 private void evaluateParentProject( ExpressionEvaluator eval, VersionedReference parentProject )
341 throws EvaluatorException
343 if ( parentProject == null )
348 parentProject.setGroupId( eval.expand( parentProject.getGroupId() ) );
349 parentProject.setArtifactId( eval.expand( parentProject.getArtifactId() ) );
350 parentProject.setVersion( eval.expand( parentProject.getVersion() ) );
353 private void evaluatePlugins( ExpressionEvaluator eval, List<ArtifactReference> plugins )
354 throws EvaluatorException
356 evaluateArtifactReferenceList( eval, plugins );
359 private void evaluateProperties( ExpressionEvaluator eval, Properties props )
360 throws EvaluatorException
367 // Only evaluate the values, not the keys.
369 // Collect the key names. (Done ahead of time to prevent iteration / concurrent modification exceptions)
370 Set<String> keys = new HashSet<String>();
371 for ( Object obj : props.keySet() )
373 keys.add( (String) obj );
376 // Evaluate all of the values.
377 for ( String key : keys )
379 String value = props.getProperty( key );
380 props.setProperty( key, eval.expand( value ) );
384 private void evaluateRelocation( ExpressionEvaluator eval, VersionedReference relocation )
385 throws EvaluatorException
387 if ( relocation == null )
392 relocation.setGroupId( eval.expand( relocation.getGroupId() ) );
393 relocation.setArtifactId( eval.expand( relocation.getArtifactId() ) );
394 relocation.setVersion( eval.expand( relocation.getVersion() ) );
397 private void evaluateReports( ExpressionEvaluator eval, List<ArtifactReference> reports )
398 throws EvaluatorException
400 evaluateArtifactReferenceList( eval, reports );
403 private void evaluateRepositories( ExpressionEvaluator eval, List<ProjectRepository> repositories )
404 throws EvaluatorException
406 if ( CollectionUtils.isEmpty( repositories ) )
411 for ( ProjectRepository repository : repositories )
413 repository.setId( eval.expand( repository.getId() ) );
414 repository.setLayout( eval.expand( repository.getLayout() ) );
415 repository.setName( eval.expand( repository.getName() ) );
416 repository.setUrl( eval.expand( repository.getUrl() ) );
420 private void evaluateScm( ExpressionEvaluator eval, Scm scm )
421 throws EvaluatorException
428 scm.setConnection( eval.expand( scm.getConnection() ) );
429 scm.setDeveloperConnection( eval.expand( scm.getDeveloperConnection() ) );
430 scm.setUrl( eval.expand( scm.getUrl() ) );
433 private void evaluateStringList( ExpressionEvaluator eval, List<String> strings )
434 throws EvaluatorException
436 if ( CollectionUtils.isEmpty( strings ) )
441 // Create new list to hold post-evaluated strings.
442 List<String> evaluated = new ArrayList<String>();
445 for ( String str : strings )
447 evaluated.add( eval.expand( str ) );
450 // Populate the original list with the post-evaluated list.
452 strings.addAll( evaluated );