]> source.dussan.org Git - archiva.git/blob
459d905061660e015e4c7de6a0a0e247b530c041
[archiva.git] /
1 package org.apache.maven.archiva.repository.project.resolvers;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
27
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 /**
33  * Represents a stack of {@link ProjectModelResolver} resolvers for
34  * finding/resolving an ArchivaProjectModel from multiple sources. 
35  *
36  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
37  * @version $Id$
38  * 
39  * @plexus.component role="org.apache.maven.archiva.repository.project.resolvers.ProjectModelResolverStack"
40  */
41 public class ProjectModelResolverStack
42 {
43     private List resolvers;
44
45     private List listeners;
46
47     public ProjectModelResolverStack()
48     {
49         this.resolvers = new ArrayList();
50         this.listeners = new ArrayList();
51     }
52
53     public void addListener( ProjectModelResolutionListener listener )
54     {
55         if ( listener == null )
56         {
57             return;
58         }
59
60         this.listeners.add( listener );
61     }
62
63     public void addProjectModelResolver( ProjectModelResolver resolver )
64     {
65         if ( resolver == null )
66         {
67             return;
68         }
69
70         this.resolvers.add( resolver );
71     }
72
73     public void clearResolvers()
74     {
75         this.resolvers.clear();
76     }
77
78     public ArchivaProjectModel findProject( VersionedReference projectRef )
79     {
80         if ( CollectionUtils.isEmpty( this.resolvers ) )
81         {
82             throw new IllegalStateException( "No resolvers have been defined." );
83         }
84
85         triggerResolutionStart( projectRef, this.resolvers );
86
87         Iterator it = this.resolvers.iterator();
88
89         while ( it.hasNext() )
90         {
91             ProjectModelResolver resolver = (ProjectModelResolver) it.next();
92
93             try
94             {
95                 triggerResolutionAttempting( projectRef, resolver );
96                 ArchivaProjectModel model = resolver.resolveProjectModel( projectRef );
97
98                 if ( model != null )
99                 {
100                     // Project was found.
101                     triggerResolutionSuccess( projectRef, resolver, model );
102                     return model;
103                 }
104                 triggerResolutionMiss( projectRef, resolver );
105             }
106             catch ( ProjectModelException e )
107             {
108                 triggerResolutionError( projectRef, resolver, e );
109             }
110         }
111
112         // Project was not found in entire resolver list.
113         triggerResolutionNotFound( projectRef, this.resolvers );
114
115         return null;
116     }
117
118     public boolean hasResolver( ProjectModelResolver resolver )
119     {
120         return this.resolvers.contains( resolver );
121     }
122
123     public boolean isEmpty()
124     {
125         return this.resolvers.isEmpty();
126     }
127
128     public void prependProjectModelResolver( ProjectModelResolver resolver )
129     {
130         if ( resolver == null )
131         {
132             return;
133         }
134
135         this.resolvers.add( 0, resolver );
136     }
137
138     public void removeListener( ProjectModelResolutionListener listener )
139     {
140         if ( listener == null )
141         {
142             return;
143         }
144
145         this.listeners.add( listener );
146     }
147
148     public void removeResolver( ProjectModelResolver resolver )
149     {
150         this.resolvers.remove( resolver );
151     }
152
153     private void triggerResolutionAttempting( VersionedReference projectRef, ProjectModelResolver resolver )
154     {
155         Iterator it = this.listeners.iterator();
156         while ( it.hasNext() )
157         {
158             ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
159
160             try
161             {
162                 listener.resolutionAttempting( projectRef, resolver );
163             }
164             catch ( Exception e )
165             {
166                 // do nothing with exception.
167             }
168         }
169     }
170
171     private void triggerResolutionError( VersionedReference projectRef, ProjectModelResolver resolver, Exception cause )
172     {
173         Iterator it = this.listeners.iterator();
174         while ( it.hasNext() )
175         {
176             ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
177
178             try
179             {
180                 listener.resolutionError( projectRef, resolver, cause );
181             }
182             catch ( Exception e )
183             {
184                 // do nothing with exception.
185             }
186         }
187     }
188
189     private void triggerResolutionMiss( VersionedReference projectRef, ProjectModelResolver resolver )
190     {
191         Iterator it = this.listeners.iterator();
192         while ( it.hasNext() )
193         {
194             ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
195
196             try
197             {
198                 listener.resolutionMiss( projectRef, resolver );
199             }
200             catch ( Exception e )
201             {
202                 // do nothing with exception.
203             }
204         }
205     }
206
207     private void triggerResolutionNotFound( VersionedReference projectRef, List resolvers )
208     {
209         Iterator it = this.listeners.iterator();
210         while ( it.hasNext() )
211         {
212             ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
213
214             try
215             {
216                 listener.resolutionNotFound( projectRef, resolvers );
217             }
218             catch ( Exception e )
219             {
220                 // do nothing with exception.
221             }
222         }
223     }
224
225     private void triggerResolutionStart( VersionedReference projectRef, List resolvers )
226     {
227         Iterator it = this.listeners.iterator();
228         while ( it.hasNext() )
229         {
230             ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
231
232             try
233             {
234                 listener.resolutionStart( projectRef, resolvers );
235             }
236             catch ( Exception e )
237             {
238                 // do nothing with exception.
239             }
240         }
241     }
242
243     private void triggerResolutionSuccess( VersionedReference projectRef, ProjectModelResolver resolver,
244                                            ArchivaProjectModel model )
245     {
246         Iterator it = this.listeners.iterator();
247         while ( it.hasNext() )
248         {
249             ProjectModelResolutionListener listener = (ProjectModelResolutionListener) it.next();
250
251             try
252             {
253                 listener.resolutionSuccess( projectRef, resolver, model );
254             }
255             catch ( Exception e )
256             {
257                 // do nothing with exception.
258             }
259         }
260     }
261 }