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.
38 public class ProjectModelResolverStack
40 private List resolvers;
42 private List listeners;
44 public ProjectModelResolverStack()
46 this.resolvers = new ArrayList();
47 this.listeners = new ArrayList();
50 public void addListener( ProjectModelResolutionListener listener )
52 if ( listener == null )
57 this.listeners.add( listener );
60 public void addProjectModelResolver( ProjectModelResolver resolver )
62 if ( resolver == null )
67 this.resolvers.add( resolver );
70 public void clearResolvers()
72 this.resolvers.clear();
75 public ArchivaProjectModel findProject( VersionedReference projectRef )
77 if ( CollectionUtils.isEmpty( this.resolvers ) )
79 throw new IllegalStateException( "No resolvers have been defined." );
82 triggerResolutionStart( projectRef, this.resolvers );
84 Iterator it = this.resolvers.iterator();
86 while ( it.hasNext() )
88 ProjectModelResolver resolver = (ProjectModelResolver) it.next();
92 triggerResolutionAttempting( projectRef, resolver );
93 ArchivaProjectModel model = resolver.resolveProjectModel( projectRef );
98 triggerResolutionSuccess( projectRef, resolver, model );
101 triggerResolutionMiss( projectRef, resolver );
103 catch ( ProjectModelException e )
105 triggerResolutionError( projectRef, resolver, e );
109 // Project was not found in entire resolver list.
110 triggerResolutionNotFound( projectRef, this.resolvers );
115 public boolean hasResolver( ProjectModelResolver resolver )
117 return this.resolvers.contains( resolver );
120 public boolean isEmpty()
122 return this.resolvers.isEmpty();
125 public void prependProjectModelResolver( ProjectModelResolver resolver )
127 if ( resolver == null )
132 this.resolvers.add( 0, resolver );
135 public void removeListener( ProjectModelResolutionListener listener )
137 if ( listener == null )
142 this.listeners.add( listener );
145 public void removeResolver( ProjectModelResolver resolver )
147 this.resolvers.remove( resolver );
150 private void triggerResolutionAttempting( VersionedReference projectRef, ProjectModelResolver resolver )
152 Iterator it = this.listeners.iterator();
153 while ( it.hasNext() )
155 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
159 listener.resolutionAttempting( projectRef, resolver );
161 catch ( Exception e )
163 // do nothing with exception.
168 private void triggerResolutionError( VersionedReference projectRef, ProjectModelResolver resolver, Exception cause )
170 Iterator it = this.listeners.iterator();
171 while ( it.hasNext() )
173 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
177 listener.resolutionError( projectRef, resolver, cause );
179 catch ( Exception e )
181 // do nothing with exception.
186 private void triggerResolutionMiss( VersionedReference projectRef, ProjectModelResolver resolver )
188 Iterator it = this.listeners.iterator();
189 while ( it.hasNext() )
191 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
195 listener.resolutionMiss( projectRef, resolver );
197 catch ( Exception e )
199 // do nothing with exception.
204 private void triggerResolutionNotFound( VersionedReference projectRef, List resolvers )
206 Iterator it = this.listeners.iterator();
207 while ( it.hasNext() )
209 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
213 listener.resolutionNotFound( projectRef, resolvers );
215 catch ( Exception e )
217 // do nothing with exception.
222 private void triggerResolutionStart( VersionedReference projectRef, List resolvers )
224 Iterator it = this.listeners.iterator();
225 while ( it.hasNext() )
227 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
231 listener.resolutionStart( projectRef, resolvers );
233 catch ( Exception e )
235 // do nothing with exception.
240 private void triggerResolutionSuccess( VersionedReference projectRef, ProjectModelResolver resolver,
241 ArchivaProjectModel model )
243 Iterator it = this.listeners.iterator();
244 while ( it.hasNext() )
246 ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
250 listener.resolutionSuccess( projectRef, resolver, model );
252 catch ( Exception e )
254 // do nothing with exception.