This project has retired. For details please refer to its
Attic page.
RepositoryContentFactory xref
1 package org.apache.archiva.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ConfigurationNames;
24 import org.apache.archiva.components.registry.Registry;
25 import org.apache.archiva.components.registry.RegistryListener;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.stereotype.Service;
28
29 import javax.annotation.PostConstruct;
30 import javax.inject.Inject;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34
35
36
37
38 @Service( "repositoryContentFactory#default" )
39 public class RepositoryContentFactory
40 implements RegistryListener
41 {
42
43
44
45 @Inject
46 private ArchivaConfiguration archivaConfiguration;
47
48 @Inject
49 private ApplicationContext applicationContext;
50
51 @Inject
52 private List<RepositoryContentProvider> repositoryContentProviders;
53
54 private final Map<String, ManagedRepositoryContent> managedContentMap;
55
56 private final Map<String, RemoteRepositoryContent> remoteContentMap;
57
58
59 public RepositoryContentFactory( )
60 {
61 managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>( );
62 remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>( );
63 }
64
65
66
67
68
69
70
71
72
73 public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
74 throws RepositoryException
75 {
76 ManagedRepositoryContent repo = managedContentMap.get( repoId );
77
78 if ( repo != null )
79 {
80 return repo;
81 }
82 else
83 {
84 throw new RepositoryNotFoundException(
85 "Unable to find managed repository configuration for id " + repoId );
86 }
87
88 }
89
90 private RepositoryContentProvider getProvider(final String layout, final RepositoryType repoType) throws RepositoryException
91 {
92 return repositoryContentProviders.stream().filter(p->p.supports( repoType ) && p.supportsLayout( layout )).
93 findFirst().orElseThrow( ( ) -> new RepositoryException( "Could not find content provider for repository type "+repoType+" and layout "+layout ) );
94 }
95
96 public ManagedRepositoryContent getManagedRepositoryContent( org.apache.archiva.repository.ManagedRepository mRepo )
97 throws RepositoryException
98 {
99 final String id = mRepo.getId();
100 ManagedRepositoryContent content = managedContentMap.get( id );
101
102 if ( content != null && content.getRepository()==mRepo)
103 {
104 return content;
105 }
106
107 RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
108 content = contentProvider.createManagedContent( mRepo );
109 if (content==null) {
110 throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
111 }
112 ManagedRepositoryContent previousContent = managedContentMap.put( id, content );
113 if (previousContent!=null) {
114 previousContent.setRepository( null );
115 }
116
117 return content;
118 }
119
120 public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
121 throws RepositoryException
122 {
123 RemoteRepositoryContent repo = remoteContentMap.get( repoId );
124
125 if ( repo != null )
126 {
127 return repo;
128 }
129 else
130 {
131 throw new RepositoryNotFoundException(
132 "Unable to find remote repository configuration for id:" + repoId );
133 }
134
135 }
136
137 public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepository mRepo )
138 throws RepositoryException
139 {
140 final String id = mRepo.getId();
141 RemoteRepositoryContent content = remoteContentMap.get( id );
142
143 if ( content != null && content.getRepository()==mRepo)
144 {
145 return content;
146 }
147
148 RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
149 content = contentProvider.createRemoteContent( mRepo );
150 if (content==null) {
151 throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
152 }
153 RemoteRepositoryContent previousContent = remoteContentMap.put( id, content );
154 if (previousContent!=null) {
155 previousContent.setRepository( null );
156 }
157 return content;
158 }
159
160
161 @Override
162 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
163 {
164 if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
165 propertyName ) )
166 {
167 initMaps( );
168 }
169 }
170
171 @Override
172 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
173 {
174
175 }
176
177 @PostConstruct
178 public void initialize( )
179 {
180 archivaConfiguration.addChangeListener( this );
181 }
182
183 private void initMaps( )
184 {
185
186
187
188 managedContentMap.clear( );
189
190
191
192
193 remoteContentMap.clear( );
194
195 }
196
197 public ArchivaConfiguration getArchivaConfiguration( )
198 {
199 return archivaConfiguration;
200 }
201
202 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
203 {
204 this.archivaConfiguration = archivaConfiguration;
205 }
206
207 public void setRepositoryContentProviders(List<RepositoryContentProvider> providers) {
208 this.repositoryContentProviders = providers;
209 }
210
211 public List<RepositoryContentProvider> getRepositoryContentProviders() {
212 return repositoryContentProviders;
213 }
214 }