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
56 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
58 * @plexus.component role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
59 * role-hint="expression"
60 * instantiation-strategy="per-lookup"
62 public class ProjectModelExpressionFilter
63 implements ProjectModelFilter
65 private ExpressionEvaluator evaluator = new DefaultExpressionEvaluator();
68 * Find and Evaluate the Expressions present in the model.
70 * @param model the model to correct.
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 String groupId = StringUtils.defaultString( evaluator.expand( model.getGroupId() ) );
108 String artifactId = StringUtils.defaultString( evaluator.expand( model.getArtifactId() ) );
109 String version = StringUtils.defaultString( evaluator.expand( model.getVersion() ) );
110 String name = StringUtils.defaultString( evaluator.expand( model.getName() ) );
112 /* Archiva doesn't need to handle a full expression language with object tree walking
113 * as the requirements within Archiva are much smaller, a quick replacement of the
114 * important fields (groupId, artifactId, version, name) are handled specifically.
116 props.setProperty( "pom.groupId", groupId );
117 props.setProperty( "pom.artifactId", artifactId );
118 props.setProperty( "pom.version", version );
119 props.setProperty( "pom.name", name );
120 props.setProperty( "project.groupId", groupId );
121 props.setProperty( "project.artifactId", artifactId );
122 props.setProperty( "project.version", version );
123 props.setProperty( "project.name", name );
125 // Evaluate everything.
126 ret.setVersion( evaluator.expand( ret.getVersion() ) );
127 ret.setGroupId( evaluator.expand( ret.getGroupId() ) );
128 ret.setName( evaluator.expand( ret.getName() ) );
129 ret.setDescription( evaluator.expand( ret.getDescription() ) );
130 ret.setPackaging( evaluator.expand( ret.getPackaging() ) );
131 ret.setUrl( evaluator.expand( ret.getUrl() ) );
133 evaluateParentProject( evaluator, ret.getParentProject() );
135 evaluateBuildExtensions( evaluator, ret.getBuildExtensions() );
136 evaluateCiManagement( evaluator, ret.getCiManagement() );
137 evaluateDependencyList( evaluator, ret.getDependencies() );
138 evaluateDependencyList( evaluator, ret.getDependencyManagement() );
139 evaluateIndividuals( evaluator, ret.getIndividuals() );
140 evaluateIssueManagement( evaluator, ret.getIssueManagement() );
141 evaluateLicenses( evaluator, ret.getLicenses() );
142 evaluateMailingLists( evaluator, ret.getMailingLists() );
143 evaluateOrganization( evaluator, ret.getOrganization() );
144 evaluatePlugins( evaluator, ret.getPlugins() );
145 evaluateRelocation( evaluator, ret.getRelocation() );
146 evaluateReports( evaluator, ret.getReports() );
147 evaluateRepositories( evaluator, ret.getRepositories() );
148 evaluateScm( evaluator, ret.getScm() );
150 catch ( EvaluatorException e )
152 throw new ProjectModelException( "Unable to evaluate expression in model: " + e.getMessage(), e );
159 private void evaluateArtifactReferenceList( ExpressionEvaluator eval, List<ArtifactReference> refs )
160 throws EvaluatorException
162 if ( CollectionUtils.isEmpty( refs ) )
167 for ( ArtifactReference ref : refs )
169 ref.setGroupId( eval.expand( ref.getGroupId() ) );
170 ref.setArtifactId( eval.expand( ref.getArtifactId() ) );
171 ref.setVersion( eval.expand( ref.getVersion() ) );
172 ref.setClassifier( eval.expand( ref.getClassifier() ) );
173 ref.setType( eval.expand( ref.getType() ) );
177 private void evaluateBuildExtensions( ExpressionEvaluator eval, List<ArtifactReference> buildExtensions )
178 throws EvaluatorException
180 if ( CollectionUtils.isEmpty( buildExtensions ) )
185 for ( ArtifactReference ref : buildExtensions )
187 ref.setGroupId( eval.expand( ref.getGroupId() ) );
188 ref.setArtifactId( eval.expand( ref.getArtifactId() ) );
189 ref.setVersion( eval.expand( ref.getVersion() ) );
190 ref.setClassifier( eval.expand( ref.getClassifier() ) );
191 ref.setType( eval.expand( ref.getType() ) );
195 private void evaluateCiManagement( ExpressionEvaluator eval, CiManagement ciManagement )
196 throws EvaluatorException
198 if ( ciManagement == null )
203 ciManagement.setSystem( eval.expand( ciManagement.getSystem() ) );
204 ciManagement.setUrl( eval.expand( ciManagement.getUrl() ) );
207 private void evaluateDependencyList( ExpressionEvaluator eval, List<Dependency> dependencies )
208 throws EvaluatorException
210 if ( CollectionUtils.isEmpty( dependencies ) )
215 for ( Dependency dependency : dependencies )
217 dependency.setGroupId( eval.expand( dependency.getGroupId() ) );
218 dependency.setArtifactId( eval.expand( dependency.getArtifactId() ) );
219 dependency.setVersion( eval.expand( dependency.getVersion() ) );
220 dependency.setScope( eval.expand( dependency.getScope() ) );
221 dependency.setType( eval.expand( dependency.getType() ) );
222 dependency.setUrl( eval.expand( dependency.getUrl() ) );
224 evaluateExclusions( eval, dependency.getExclusions() );
228 private void evaluateExclusions( ExpressionEvaluator eval, List<Exclusion> exclusions )
229 throws EvaluatorException
231 if ( CollectionUtils.isEmpty( exclusions ) )
236 for ( Exclusion exclusion : exclusions )
238 exclusion.setGroupId( eval.expand( exclusion.getGroupId() ) );
239 exclusion.setArtifactId( eval.expand( exclusion.getArtifactId() ) );
243 private void evaluateIndividuals( ExpressionEvaluator eval, List<Individual> individuals )
244 throws EvaluatorException
246 if ( CollectionUtils.isEmpty( individuals ) )
251 for ( Individual individual : individuals )
253 individual.setPrincipal( eval.expand( individual.getPrincipal() ) );
254 individual.setName( eval.expand( individual.getName() ) );
255 individual.setEmail( eval.expand( individual.getEmail() ) );
256 individual.setTimezone( eval.expand( individual.getTimezone() ) );
257 individual.setOrganization( eval.expand( individual.getOrganization() ) );
258 individual.setOrganizationUrl( eval.expand( individual.getOrganizationUrl() ) );
259 individual.setUrl( eval.expand( individual.getUrl() ) );
261 evaluateProperties( eval, individual.getProperties() );
262 evaluateStringList( eval, individual.getRoles() );
266 private void evaluateIssueManagement( ExpressionEvaluator eval, IssueManagement issueManagement )
267 throws EvaluatorException
269 if ( issueManagement == null )
274 issueManagement.setSystem( eval.expand( issueManagement.getSystem() ) );
275 issueManagement.setUrl( eval.expand( issueManagement.getUrl() ) );
278 private void evaluateLicenses( ExpressionEvaluator eval, List<License> licenses )
279 throws EvaluatorException
281 if ( CollectionUtils.isEmpty( licenses ) )
286 for ( License license : licenses )
288 license.setName( eval.expand( license.getName() ) );
289 license.setUrl( eval.expand( license.getUrl() ) );
290 license.setComments( eval.expand( license.getComments() ) );
294 private void evaluateMailingLists( ExpressionEvaluator eval, List<MailingList> mailingLists )
295 throws EvaluatorException
297 if ( CollectionUtils.isEmpty( mailingLists ) )
302 for ( MailingList mlist : mailingLists )
304 mlist.setName( eval.expand( mlist.getName() ) );
305 mlist.setSubscribeAddress( eval.expand( mlist.getSubscribeAddress() ) );
306 mlist.setUnsubscribeAddress( eval.expand( mlist.getUnsubscribeAddress() ) );
307 mlist.setPostAddress( eval.expand( mlist.getPostAddress() ) );
308 mlist.setMainArchiveUrl( eval.expand( mlist.getMainArchiveUrl() ) );
310 evaluateStringList( eval, mlist.getOtherArchives() );
314 private void evaluateOrganization( ExpressionEvaluator eval, Organization organization )
315 throws EvaluatorException
317 if ( organization == null )
322 organization.setName( eval.expand( organization.getName() ) );
323 organization.setUrl( eval.expand( organization.getUrl() ) );
324 organization.setFavicon( eval.expand( organization.getFavicon() ) );
327 private void evaluateParentProject( ExpressionEvaluator eval, VersionedReference parentProject )
328 throws EvaluatorException
330 if ( parentProject == null )
335 parentProject.setGroupId( eval.expand( parentProject.getGroupId() ) );
336 parentProject.setArtifactId( eval.expand( parentProject.getArtifactId() ) );
337 parentProject.setVersion( eval.expand( parentProject.getVersion() ) );
340 private void evaluatePlugins( ExpressionEvaluator eval, List<ArtifactReference> plugins )
341 throws EvaluatorException
343 evaluateArtifactReferenceList( eval, plugins );
346 private void evaluateProperties( ExpressionEvaluator eval, Properties props )
347 throws EvaluatorException
354 // Only evaluate the values, not the keys.
356 // Collect the key names. (Done ahead of time to prevent iteration / concurrent modification exceptions)
357 Set<String> keys = new HashSet<String>();
358 for ( Object obj : props.keySet() )
360 keys.add( (String) obj );
363 // Evaluate all of the values.
364 for ( String key : keys )
366 String value = props.getProperty( key );
367 props.setProperty( key, eval.expand( value ) );
371 private void evaluateRelocation( ExpressionEvaluator eval, VersionedReference relocation )
372 throws EvaluatorException
374 if ( relocation == null )
379 relocation.setGroupId( eval.expand( relocation.getGroupId() ) );
380 relocation.setArtifactId( eval.expand( relocation.getArtifactId() ) );
381 relocation.setVersion( eval.expand( relocation.getVersion() ) );
384 private void evaluateReports( ExpressionEvaluator eval, List<ArtifactReference> reports )
385 throws EvaluatorException
387 evaluateArtifactReferenceList( eval, reports );
390 private void evaluateRepositories( ExpressionEvaluator eval, List<ProjectRepository> repositories )
391 throws EvaluatorException
393 if ( CollectionUtils.isEmpty( repositories ) )
398 for ( ProjectRepository repository : repositories )
400 repository.setId( eval.expand( repository.getId() ) );
401 repository.setLayout( eval.expand( repository.getLayout() ) );
402 repository.setName( eval.expand( repository.getName() ) );
403 repository.setUrl( eval.expand( repository.getUrl() ) );
407 private void evaluateScm( ExpressionEvaluator eval, Scm scm )
408 throws EvaluatorException
415 scm.setConnection( eval.expand( scm.getConnection() ) );
416 scm.setDeveloperConnection( eval.expand( scm.getDeveloperConnection() ) );
417 scm.setUrl( eval.expand( scm.getUrl() ) );
420 private void evaluateStringList( ExpressionEvaluator eval, List<String> strings )
421 throws EvaluatorException
423 if ( CollectionUtils.isEmpty( strings ) )
428 // Create new list to hold post-evaluated strings.
429 List<String> evaluated = new ArrayList<String>();
432 for ( String str : strings )
434 evaluated.add( eval.expand( str ) );
437 // Populate the original list with the post-evaluated list.
439 strings.addAll( evaluated );