1 package org.apache.maven.archiva.repository.project.resolvers;
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.maven.archiva.model.ArchivaProjectModel;
24 import org.apache.maven.archiva.model.VersionedReference;
25 import org.apache.maven.archiva.repository.project.ProjectModelException;
26 import org.apache.maven.archiva.repository.project.ProjectModelResolver;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
33 * Represents a stack of {@link ProjectModelResolver} resolvers for
34 * finding/resolving an ArchivaProjectModel from multiple sources.
36 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39 * @plexus.component role="org.apache.maven.archiva.repository.project.resolvers.ProjectModelResolverStack"
41 public class ProjectModelResolverStack
43 private List resolvers;
45 private List listeners;
47 public ProjectModelResolverStack()
49 this.resolvers = new ArrayList();
50 this.listeners = new ArrayList();
53 public void addListener( ProjectModelResolutionListener listener )
55 if ( listener == null )
60 this.listeners.add( listener );
63 public void addProjectModelResolver( ProjectModelResolver resolver )
65 if ( resolver == null )
70 this.resolvers.add( resolver );
73 public void clearResolvers()
75 this.resolvers.clear();
78 public ArchivaProjectModel findProject( VersionedReference projectRef )
80 if ( CollectionUtils.isEmpty( this.resolvers ) )
82 throw new IllegalStateException( "No resolvers have been defined." );
85 triggerResolutionStart( projectRef, this.resolvers );
87 Iterator it = this.resolvers.iterator();
89 while ( it.hasNext() )
91 ProjectModelResolver resolver = (ProjectModelResolver) it.next();
95 triggerResolutionAttempting( projectRef, resolver );
96 ArchivaProjectModel model = resolver.resolveProjectModel( projectRef );
100 // Project was found.
101 triggerResolutionSuccess( projectRef, resolver, model );
104 triggerResolutionMiss( projectRef, resolver );
106 catch ( ProjectModelException e )
108 triggerResolutionError( projectRef, resolver, e );
112 // Project was not found in entire resolver list.
113 triggerResolutionNotFound( projectRef, this.resolvers );
118 public boolean hasResolver( ProjectModelResolver resolver )
120 return this.resolvers.contains( resolver );
123 public boolean isEmpty()
125 return this.resolvers.isEmpty();
128 public void prependProjectModelResolver( ProjectModelResolver resolver )
130 if ( resolver == null )
135 this.resolvers.add( 0, resolver );
138 public void removeListener( ProjectModelResolutionListener listener )
140 if ( listener == null )
145 this.listeners.add( listener );
148 public void removeResolver( ProjectModelResolver resolver )
150 this.resolvers.remove( resolver );
153 private void triggerResolutionAttempting( VersionedReference projectRef, ProjectModelResolver resolver )
155 Iterator it = this.listeners.iterator();
156 while ( it.hasNext() )
158 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
162 listener.resolutionAttempting( projectRef, resolver );
164 catch ( Exception e )
166 // do nothing with exception.
171 private void triggerResolutionError( VersionedReference projectRef, ProjectModelResolver resolver, Exception cause )
173 Iterator it = this.listeners.iterator();
174 while ( it.hasNext() )
176 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
180 listener.resolutionError( projectRef, resolver, cause );
182 catch ( Exception e )
184 // do nothing with exception.
189 private void triggerResolutionMiss( VersionedReference projectRef, ProjectModelResolver resolver )
191 Iterator it = this.listeners.iterator();
192 while ( it.hasNext() )
194 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
198 listener.resolutionMiss( projectRef, resolver );
200 catch ( Exception e )
202 // do nothing with exception.
207 private void triggerResolutionNotFound( VersionedReference projectRef, List resolvers )
209 Iterator it = this.listeners.iterator();
210 while ( it.hasNext() )
212 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
216 listener.resolutionNotFound( projectRef, resolvers );
218 catch ( Exception e )
220 // do nothing with exception.
225 private void triggerResolutionStart( VersionedReference projectRef, List resolvers )
227 Iterator it = this.listeners.iterator();
228 while ( it.hasNext() )
230 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
234 listener.resolutionStart( projectRef, resolvers );
236 catch ( Exception e )
238 // do nothing with exception.
243 private void triggerResolutionSuccess( VersionedReference projectRef, ProjectModelResolver resolver,
244 ArchivaProjectModel model )
246 Iterator it = this.listeners.iterator();
247 while ( it.hasNext() )
249 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
253 listener.resolutionSuccess( projectRef, resolver, model );
255 catch ( Exception e )
257 // do nothing with exception.