]> source.dussan.org Git - archiva.git/blob
68988af67c25f973807ee580af99b5ff2b572350
[archiva.git] /
1 package org.apache.maven.archiva.repository.project;
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.lang.StringUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ConfigurationNames;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.repository.RepositoryException;
27 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
28 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
29 import org.apache.maven.archiva.repository.layout.LayoutException;
30 import org.apache.maven.archiva.repository.project.resolvers.NopProjectResolver;
31 import org.apache.maven.archiva.repository.project.resolvers.ProjectModelResolverStack;
32 import org.apache.maven.archiva.repository.project.resolvers.ManagedRepositoryProjectResolver;
33 import org.codehaus.plexus.logging.AbstractLogEnabled;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
36 import org.codehaus.plexus.registry.Registry;
37 import org.codehaus.plexus.registry.RegistryListener;
38
39 import java.util.List;
40
41 /**
42  * Factory for ProjectModelResolver objects
43  *
44  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
45  * @version $Id$
46  * @plexus.component role="org.apache.maven.archiva.repository.project.ProjectModelResolverFactory"
47  */
48 public class ProjectModelResolverFactory
49     extends AbstractLogEnabled
50     implements RegistryListener, Initializable
51 {
52     /**
53      * @plexus.requirement
54      */
55     private ArchivaConfiguration archivaConfiguration;
56
57     /**
58      * @plexus.requirement
59      */
60     private BidirectionalRepositoryLayoutFactory layoutFactory;
61
62     /**
63      * @plexus.requirement role-hint="model400"
64      */
65     private ProjectModelReader project400Reader;
66
67     /**
68      * @plexus.requirement role-hint="model300"
69      */
70     private ProjectModelReader project300Reader;
71
72     private ProjectModelResolverStack currentResolverStack = new ProjectModelResolverStack();
73
74     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
75     {
76         if ( ConfigurationNames.isManagedRepositories( propertyName ) )
77         {
78             update();
79         }
80     }
81
82     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
83     {
84         /* do nothing */
85     }
86
87     public ProjectModelResolverStack getCurrentResolverStack()
88     {
89         return currentResolverStack;
90     }
91
92     public void initialize()
93         throws InitializationException
94     {
95         update();
96         archivaConfiguration.addChangeListener( this );
97     }
98
99     private ManagedRepositoryProjectResolver toResolver( ManagedRepositoryConfiguration repo )
100         throws RepositoryException
101     {
102         try
103         {
104             BidirectionalRepositoryLayout layout = layoutFactory.getLayout( repo.getLayout() );
105             ProjectModelReader reader = project400Reader;
106
107             if ( StringUtils.equals( "legacy", repo.getLayout() ) )
108             {
109                 reader = project300Reader;
110             }
111
112             ManagedRepositoryProjectResolver resolver = new ManagedRepositoryProjectResolver( repo, reader, layout );
113             return resolver;
114         }
115         catch ( LayoutException e )
116         {
117             throw new RepositoryException(
118                 "Unable to create RepositoryProjectResolver due to invalid layout spec: " + repo );
119         }
120     }
121
122     private void update()
123     {
124         synchronized ( currentResolverStack )
125         {
126             this.currentResolverStack.clearResolvers();
127
128             List<ManagedRepositoryConfiguration> list =
129                 archivaConfiguration.getConfiguration().getManagedRepositories();
130             for ( ManagedRepositoryConfiguration repo : list )
131             {
132                 try
133                 {
134                     ManagedRepositoryProjectResolver resolver = toResolver( repo );
135
136                     // Add filesystem based resolver.
137                     this.currentResolverStack.addProjectModelResolver( resolver );
138                 }
139                 catch ( RepositoryException e )
140                 {
141                     getLogger().warn( e.getMessage(), e );
142                 }
143             }
144
145             // Add no-op resolver.
146             this.currentResolverStack.addProjectModelResolver( NopProjectResolver.getInstance() );
147         }
148     }
149 }