]> source.dussan.org Git - archiva.git/blob
df7bd0fa2e29b4e979418c92c27c309b7fc65541
[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 java.util.List;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ConfigurationNames;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
29 import org.apache.maven.archiva.repository.RepositoryContentFactory;
30 import org.apache.maven.archiva.repository.RepositoryException;
31 import org.apache.maven.archiva.repository.project.resolvers.ManagedRepositoryProjectResolver;
32 import org.apache.maven.archiva.repository.project.resolvers.NopProjectResolver;
33 import org.apache.maven.archiva.repository.project.resolvers.ProjectModelResolverStack;
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 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
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     implements RegistryListener, Initializable
50 {
51     private Logger log = LoggerFactory.getLogger( ProjectModelResolverFactory.class );
52     
53     /**
54      * @plexus.requirement
55      */
56     private ArchivaConfiguration archivaConfiguration;
57     
58     /**
59      * @plexus.requirement
60      */
61     private RepositoryContentFactory repositoryFactory;
62
63     /**
64      * @plexus.requirement role-hint="model400"
65      */
66     private ProjectModelReader project400Reader;
67
68     /**
69      * @plexus.requirement role-hint="model300"
70      */
71     private ProjectModelReader project300Reader;
72     
73     private ProjectModelResolverStack currentResolverStack = new ProjectModelResolverStack();
74
75     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
76     {
77         if ( ConfigurationNames.isManagedRepositories( propertyName ) )
78         {
79             update();
80         }
81     }
82
83     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
84     {
85         /* do nothing */
86     }
87
88     public ProjectModelResolverStack getCurrentResolverStack()
89     {
90         return currentResolverStack;
91     }
92
93     public void initialize()
94         throws InitializationException
95     {
96         update();
97         archivaConfiguration.addChangeListener( this );
98     }
99
100     private ManagedRepositoryProjectResolver toResolver( ManagedRepositoryConfiguration repo )
101         throws RepositoryException
102     {
103         ManagedRepositoryContent repoContent = repositoryFactory.getManagedRepositoryContent( repo.getId() );
104         ProjectModelReader reader = project400Reader;
105
106         if ( StringUtils.equals( "legacy", repo.getLayout() ) )
107         {
108             reader = project300Reader;
109         }
110
111         return new ManagedRepositoryProjectResolver( repoContent, reader );
112     }
113
114     private void update()
115     {
116         synchronized ( currentResolverStack )
117         {
118             this.currentResolverStack.clearResolvers();
119
120             List<ManagedRepositoryConfiguration> list =
121                 archivaConfiguration.getConfiguration().getManagedRepositories();
122             for ( ManagedRepositoryConfiguration repo : list )
123             {
124                 try
125                 {
126                     ManagedRepositoryProjectResolver resolver = toResolver( repo );
127
128                     // Add filesystem based resolver.
129                     this.currentResolverStack.addProjectModelResolver( resolver );
130                 }
131                 catch ( RepositoryException e )
132                 {
133                     log.warn( e.getMessage(), e );
134                 }
135             }
136
137             // Add no-op resolver.
138             this.currentResolverStack.addProjectModelResolver( NopProjectResolver.getInstance() );
139         }
140     }
141 }