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 public ArchivaProjectModel filter( final ArchivaProjectModel model )
72 throws ProjectModelException
74 Properties props = new Properties();
76 if ( model.getProperties() != null )
78 props.putAll( model.getProperties() );
81 ArchivaProjectModel ret = ArchivaModelCloner.clone( model );
83 // TODO: should probably clone evaluator to prevent threading issues.
84 synchronized ( evaluator )
86 // TODO: create .resetSources() method in ExpressionEvaluator project on plexus side.
87 // Remove previous expression sources.
88 List<ExpressionSource> oldSources = new ArrayList<ExpressionSource>();
89 oldSources.addAll( evaluator.getExpressionSourceList() );
90 for ( ExpressionSource exprSrc : oldSources )
92 evaluator.removeExpressionSource( exprSrc );
95 // Setup new sources (based on current model)
96 PropertiesExpressionSource propsSource = new PropertiesExpressionSource();
97 propsSource.setProperties( props );
98 evaluator.addExpressionSource( propsSource );
100 // Add system properties to the mix.
101 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
105 // Setup some common properties.
106 VersionedReference parent = model.getParentProject();
107 if ( parent != null )
109 String parentGroupId = StringUtils.defaultString( evaluator.expand( parent.getGroupId() ) );
110 String parentArtifactId = StringUtils.defaultString( evaluator.expand( parent.getArtifactId() ) );
111 String parentVersion = StringUtils.defaultString( evaluator.expand( parent.getVersion() ) );
113 props.setProperty( "parent.groupId", parentGroupId );
114 props.setProperty( "parent.artifactId", parentArtifactId );
115 props.setProperty( "parent.version", parentVersion );
118 String groupId = StringUtils.defaultString( evaluator.expand( model.getGroupId() ) );
119 String artifactId = StringUtils.defaultString( evaluator.expand( model.getArtifactId() ) );
120 String version = StringUtils.defaultString( evaluator.expand( model.getVersion() ) );
121 String name = StringUtils.defaultString( evaluator.expand( model.getName() ) );
124 /* Archiva doesn't need to handle a full expression language with object tree walking
125 * as the requirements within Archiva are much smaller, a quick replacement of the
126 * important fields (groupId, artifactId, version, name) are handled specifically.
128 props.setProperty( "pom.groupId", groupId );
129 props.setProperty( "pom.artifactId", artifactId );
130 props.setProperty( "pom.version", version );
131 props.setProperty( "pom.name", name );
132 props.setProperty( "project.groupId", groupId );
133 props.setProperty( "project.artifactId", artifactId );
134 props.setProperty( "project.version", version );
135 props.setProperty( "project.name", name );
137 // Evaluate everything.
138 ret.setVersion( evaluator.expand( ret.getVersion() ) );
139 ret.setGroupId( evaluator.expand( ret.getGroupId() ) );
140 ret.setName( evaluator.expand( ret.getName() ) );
141 ret.setDescription( evaluator.expand( ret.getDescription() ) );
142 ret.setPackaging( evaluator.expand( ret.getPackaging() ) );
143 ret.setUrl( evaluator.expand( ret.getUrl() ) );
145 evaluateParentProject( evaluator, ret.getParentProject() );
147 evaluateBuildExtensions( evaluator, ret.getBuildExtensions() );
148 evaluateCiManagement( evaluator, ret.getCiManagement() );
149 evaluateDependencyList( evaluator, ret.getDependencies() );
150 evaluateDependencyList( evaluator, ret.getDependencyManagement() );
151 evaluateIndividuals( evaluator, ret.getIndividuals() );
152 evaluateIssueManagement( evaluator, ret.getIssueManagement() );
153 evaluateLicenses( evaluator, ret.getLicenses() );
154 evaluateMailingLists( evaluator, ret.getMailingLists() );
155 evaluateOrganization( evaluator, ret.getOrganization() );
156 evaluatePlugins( evaluator, ret.getPlugins() );
157 evaluateRelocation( evaluator, ret.getRelocation() );
158 evaluateReports( evaluator, ret.getReports() );
159 evaluateRepositories( evaluator, ret.getRepositories() );
160 evaluateScm( evaluator, ret.getScm() );
162 catch ( EvaluatorException e )
164 throw new ProjectModelException( "Unable to evaluate expression in model: " + e.getMessage(), e );
171 private void evaluateArtifactReferenceList( ExpressionEvaluator eval, List<ArtifactReference> refs )
172 throws EvaluatorException
174 if ( CollectionUtils.isEmpty( refs ) )
179 for ( ArtifactReference ref : refs )
181 ref.setGroupId( eval.expand( ref.getGroupId() ) );
182 ref.setArtifactId( eval.expand( ref.getArtifactId() ) );
183 ref.setVersion( eval.expand( ref.getVersion() ) );
184 ref.setClassifier( eval.expand( ref.getClassifier() ) );
185 ref.setType( eval.expand( ref.getType() ) );
189 private void evaluateBuildExtensions( ExpressionEvaluator eval, List<ArtifactReference> buildExtensions )
190 throws EvaluatorException
192 if ( CollectionUtils.isEmpty( buildExtensions ) )
197 for ( ArtifactReference ref : buildExtensions )
199 ref.setGroupId( eval.expand( ref.getGroupId() ) );
200 ref.setArtifactId( eval.expand( ref.getArtifactId() ) );
201 ref.setVersion( eval.expand( ref.getVersion() ) );
202 ref.setClassifier( eval.expand( ref.getClassifier() ) );
203 ref.setType( eval.expand( ref.getType() ) );
207 private void evaluateCiManagement( ExpressionEvaluator eval, CiManagement ciManagement )
208 throws EvaluatorException
210 if ( ciManagement == null )
215 ciManagement.setSystem( eval.expand( ciManagement.getSystem() ) );
216 ciManagement.setUrl( eval.expand( ciManagement.getUrl() ) );
219 private void evaluateDependencyList( ExpressionEvaluator eval, List<Dependency> dependencies )
220 throws EvaluatorException
222 if ( CollectionUtils.isEmpty( dependencies ) )
227 for ( Dependency dependency : dependencies )
229 dependency.setGroupId( eval.expand( dependency.getGroupId() ) );
230 dependency.setArtifactId( eval.expand( dependency.getArtifactId() ) );
231 dependency.setVersion( eval.expand( dependency.getVersion() ) );
232 dependency.setScope( eval.expand( dependency.getScope() ) );
233 dependency.setType( eval.expand( dependency.getType() ) );
234 dependency.setUrl( eval.expand( dependency.getUrl() ) );
236 evaluateExclusions( eval, dependency.getExclusions() );
240 private void evaluateExclusions( ExpressionEvaluator eval, List<Exclusion> exclusions )
241 throws EvaluatorException
243 if ( CollectionUtils.isEmpty( exclusions ) )
248 for ( Exclusion exclusion : exclusions )
250 exclusion.setGroupId( eval.expand( exclusion.getGroupId() ) );
251 exclusion.setArtifactId( eval.expand( exclusion.getArtifactId() ) );
255 private void evaluateIndividuals( ExpressionEvaluator eval, List<Individual> individuals )
256 throws EvaluatorException
258 if ( CollectionUtils.isEmpty( individuals ) )
263 for ( Individual individual : individuals )
265 individual.setPrincipal( eval.expand( individual.getPrincipal() ) );
266 individual.setName( eval.expand( individual.getName() ) );
267 individual.setEmail( eval.expand( individual.getEmail() ) );
268 individual.setTimezone( eval.expand( individual.getTimezone() ) );
269 individual.setOrganization( eval.expand( individual.getOrganization() ) );
270 individual.setOrganizationUrl( eval.expand( individual.getOrganizationUrl() ) );
271 individual.setUrl( eval.expand( individual.getUrl() ) );
273 evaluateProperties( eval, individual.getProperties() );
274 evaluateStringList( eval, individual.getRoles() );
278 private void evaluateIssueManagement( ExpressionEvaluator eval, IssueManagement issueManagement )
279 throws EvaluatorException
281 if ( issueManagement == null )
286 issueManagement.setSystem( eval.expand( issueManagement.getSystem() ) );
287 issueManagement.setUrl( eval.expand( issueManagement.getUrl() ) );
290 private void evaluateLicenses( ExpressionEvaluator eval, List<License> licenses )
291 throws EvaluatorException
293 if ( CollectionUtils.isEmpty( licenses ) )
298 for ( License license : licenses )
300 license.setName( eval.expand( license.getName() ) );
301 license.setUrl( eval.expand( license.getUrl() ) );
302 license.setComments( eval.expand( license.getComments() ) );
306 private void evaluateMailingLists( ExpressionEvaluator eval, List<MailingList> mailingLists )
307 throws EvaluatorException
309 if ( CollectionUtils.isEmpty( mailingLists ) )
314 for ( MailingList mlist : mailingLists )
316 mlist.setName( eval.expand( mlist.getName() ) );
317 mlist.setSubscribeAddress( eval.expand( mlist.getSubscribeAddress() ) );
318 mlist.setUnsubscribeAddress( eval.expand( mlist.getUnsubscribeAddress() ) );
319 mlist.setPostAddress( eval.expand( mlist.getPostAddress() ) );
320 mlist.setMainArchiveUrl( eval.expand( mlist.getMainArchiveUrl() ) );
322 evaluateStringList( eval, mlist.getOtherArchives() );
326 private void evaluateOrganization( ExpressionEvaluator eval, Organization organization )
327 throws EvaluatorException
329 if ( organization == null )
334 organization.setName( eval.expand( organization.getName() ) );
335 organization.setUrl( eval.expand( organization.getUrl() ) );
336 organization.setFavicon( eval.expand( organization.getFavicon() ) );
339 private void evaluateParentProject( ExpressionEvaluator eval, VersionedReference parentProject )
340 throws EvaluatorException
342 if ( parentProject == null )
347 parentProject.setGroupId( eval.expand( parentProject.getGroupId() ) );
348 parentProject.setArtifactId( eval.expand( parentProject.getArtifactId() ) );
349 parentProject.setVersion( eval.expand( parentProject.getVersion() ) );
352 private void evaluatePlugins( ExpressionEvaluator eval, List<ArtifactReference> plugins )
353 throws EvaluatorException
355 evaluateArtifactReferenceList( eval, plugins );
358 private void evaluateProperties( ExpressionEvaluator eval, Properties props )
359 throws EvaluatorException
366 // Only evaluate the values, not the keys.
368 // Collect the key names. (Done ahead of time to prevent iteration / concurrent modification exceptions)
369 Set<String> keys = new HashSet<String>();
370 for ( Object obj : props.keySet() )
372 keys.add( (String) obj );
375 // Evaluate all of the values.
376 for ( String key : keys )
378 String value = props.getProperty( key );
379 props.setProperty( key, eval.expand( value ) );
383 private void evaluateRelocation( ExpressionEvaluator eval, VersionedReference relocation )
384 throws EvaluatorException
386 if ( relocation == null )
391 relocation.setGroupId( eval.expand( relocation.getGroupId() ) );
392 relocation.setArtifactId( eval.expand( relocation.getArtifactId() ) );
393 relocation.setVersion( eval.expand( relocation.getVersion() ) );
396 private void evaluateReports( ExpressionEvaluator eval, List<ArtifactReference> reports )
397 throws EvaluatorException
399 evaluateArtifactReferenceList( eval, reports );
402 private void evaluateRepositories( ExpressionEvaluator eval, List<ProjectRepository> repositories )
403 throws EvaluatorException
405 if ( CollectionUtils.isEmpty( repositories ) )
410 for ( ProjectRepository repository : repositories )
412 repository.setId( eval.expand( repository.getId() ) );
413 repository.setLayout( eval.expand( repository.getLayout() ) );
414 repository.setName( eval.expand( repository.getName() ) );
415 repository.setUrl( eval.expand( repository.getUrl() ) );
419 private void evaluateScm( ExpressionEvaluator eval, Scm scm )
420 throws EvaluatorException
427 scm.setConnection( eval.expand( scm.getConnection() ) );
428 scm.setDeveloperConnection( eval.expand( scm.getDeveloperConnection() ) );
429 scm.setUrl( eval.expand( scm.getUrl() ) );
432 private void evaluateStringList( ExpressionEvaluator eval, List<String> strings )
433 throws EvaluatorException
435 if ( CollectionUtils.isEmpty( strings ) )
440 // Create new list to hold post-evaluated strings.
441 List<String> evaluated = new ArrayList<String>();
444 for ( String str : strings )
446 evaluated.add( eval.expand( str ) );
449 // Populate the original list with the post-evaluated list.
451 strings.addAll( evaluated );