]> source.dussan.org Git - archiva.git/blob
55474585c753dc24611aa14bbb40f1266925ee7d
[archiva.git] /
1 package org.apache.archiva.repository;
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.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ConfigurationNames;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.redback.components.registry.Registry;
27 import org.apache.archiva.redback.components.registry.RegistryListener;
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.stereotype.Service;
30
31 import javax.annotation.PostConstruct;
32 import javax.inject.Inject;
33 import java.util.Map;
34 import java.util.concurrent.ConcurrentHashMap;
35
36 /**
37  * RepositoryContentRequest
38  */
39 @Service( "repositoryContentFactory#default" )
40 public class RepositoryContentFactory
41     implements RegistryListener
42 {
43     /**
44      *
45      */
46     @Inject
47     private ArchivaConfiguration archivaConfiguration;
48
49     @Inject
50     private ApplicationContext applicationContext;
51
52     private final Map<String, ManagedRepositoryContent> managedContentMap;
53
54     private final Map<String, RemoteRepositoryContent> remoteContentMap;
55
56
57     public RepositoryContentFactory( )
58     {
59         managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>( );
60         remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>( );
61     }
62
63     /**
64      * Get the ManagedRepositoryContent object for the repository Id specified.
65      *
66      * @param repoId the repository id to fetch.
67      * @return the ManagedRepositoryContent object associated with the repository id.
68      * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
69      * @throws RepositoryException         the repository content object cannot be loaded due to configuration issue.
70      */
71     public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
72         throws RepositoryNotFoundException, RepositoryException
73     {
74         ManagedRepositoryContent repo = managedContentMap.get( repoId );
75
76         if ( repo != null )
77         {
78             return repo;
79         }
80         else
81         {
82             throw new RepositoryNotFoundException(
83                 "Unable to find managed repository configuration for id " + repoId );
84         }
85
86     }
87
88     public ManagedRepositoryContent getManagedRepositoryContent( ManagedRepositoryConfiguration repoConfig, org.apache.archiva.repository.ManagedRepository mRepo )
89         throws RepositoryNotFoundException, RepositoryException
90     {
91         ManagedRepositoryContent repo = managedContentMap.get( repoConfig.getId( ) );
92
93         if ( repo != null && repo.getRepository()==mRepo)
94         {
95             return repo;
96         }
97
98         repo = applicationContext.getBean( "managedRepositoryContent#" + repoConfig.getLayout( ),
99             ManagedRepositoryContent.class );
100         repo.setRepository( mRepo );
101         ManagedRepositoryContent previousRepo = managedContentMap.put( repoConfig.getId( ), repo );
102         if (previousRepo!=null) {
103             ManagedRepository previousMRepo = previousRepo.getRepository( );
104             if (previousMRepo!=null && previousMRepo instanceof EditableManagedRepository) {
105                 ((EditableManagedRepository)previousMRepo).setContent( null );
106             }
107             previousRepo.setRepository( null );
108         }
109
110         return repo;
111     }
112
113     public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
114         throws RepositoryNotFoundException, RepositoryException
115     {
116         RemoteRepositoryContent repo = remoteContentMap.get( repoId );
117
118         if ( repo != null )
119         {
120             return repo;
121         }
122         else
123         {
124             throw new RepositoryNotFoundException(
125                 "Unable to find remote repository configuration for id:" + repoId );
126         }
127
128     }
129
130     public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepositoryConfiguration repoConfig, RemoteRepository mRepo )
131         throws RepositoryNotFoundException, RepositoryException
132     {
133         RemoteRepositoryContent repo = remoteContentMap.get( repoConfig.getId( ) );
134
135         if ( repo != null && repo.getRepository()==mRepo)
136         {
137             return repo;
138         }
139
140         repo = applicationContext.getBean( "remoteRepositoryContent#" + repoConfig.getLayout( ),
141             RemoteRepositoryContent.class );
142         repo.setRepository( mRepo );
143         RemoteRepositoryContent previousRepo = remoteContentMap.put( repoConfig.getId( ), repo );
144         if (previousRepo!=null) {
145             RemoteRepository previousMRepo = previousRepo.getRepository( );
146             if (previousMRepo!=null && previousMRepo instanceof EditableRemoteRepository) {
147                 ((EditableRemoteRepository)previousMRepo).setContent( null );
148             }
149             previousRepo.setRepository( null );
150         }
151
152
153         return repo;
154     }
155
156
157     @Override
158     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
159     {
160         if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
161             propertyName ) )
162         {
163             initMaps( );
164         }
165     }
166
167     @Override
168     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
169     {
170         /* do nothing */
171     }
172
173     @PostConstruct
174     public void initialize( )
175     {
176         archivaConfiguration.addChangeListener( this );
177     }
178
179     private void initMaps( )
180     {
181         // olamy we use concurent so no need of synchronize
182         //synchronized ( managedContentMap )
183         //{
184         managedContentMap.clear( );
185         //}
186
187         //synchronized ( remoteContentMap )
188         //{
189         remoteContentMap.clear( );
190         //}
191     }
192
193     public ArchivaConfiguration getArchivaConfiguration( )
194     {
195         return archivaConfiguration;
196     }
197
198     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
199     {
200         this.archivaConfiguration = archivaConfiguration;
201     }
202 }