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