1 | |
package org.apache.maven.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.maven.archiva.configuration.ArchivaConfiguration; |
23 | |
import org.apache.maven.archiva.configuration.ConfigurationNames; |
24 | |
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; |
25 | |
import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration; |
26 | |
import org.codehaus.plexus.PlexusContainer; |
27 | |
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; |
28 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
29 | |
import org.codehaus.plexus.context.Context; |
30 | |
import org.codehaus.plexus.context.ContextException; |
31 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
32 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
33 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
34 | |
import org.codehaus.plexus.registry.Registry; |
35 | |
import org.codehaus.plexus.registry.RegistryListener; |
36 | |
|
37 | |
import java.util.HashMap; |
38 | |
import java.util.Map; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class RepositoryContentFactory |
49 | |
implements Contextualizable, RegistryListener, Initializable |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
private ArchivaConfiguration archivaConfiguration; |
55 | |
|
56 | |
private final Map<String, ManagedRepositoryContent> managedContentMap; |
57 | |
|
58 | |
private final Map<String, RemoteRepositoryContent> remoteContentMap; |
59 | |
|
60 | |
private PlexusContainer container; |
61 | |
|
62 | |
public RepositoryContentFactory() |
63 | 0 | { |
64 | 0 | managedContentMap = new HashMap<String, ManagedRepositoryContent>(); |
65 | 0 | remoteContentMap = new HashMap<String, RemoteRepositoryContent>(); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public ManagedRepositoryContent getManagedRepositoryContent( String repoId ) |
77 | |
throws RepositoryNotFoundException, RepositoryException |
78 | |
{ |
79 | 0 | ManagedRepositoryContent repo = managedContentMap.get( repoId ); |
80 | |
|
81 | 0 | if ( repo != null ) |
82 | |
{ |
83 | 0 | return repo; |
84 | |
} |
85 | |
|
86 | 0 | ManagedRepositoryConfiguration repoConfig = archivaConfiguration.getConfiguration() |
87 | |
.findManagedRepositoryById( repoId ); |
88 | 0 | if ( repoConfig == null ) |
89 | |
{ |
90 | 0 | throw new RepositoryNotFoundException( "Unable to find managed repository configuration for id:" + repoId ); |
91 | |
} |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 0 | repo = (ManagedRepositoryContent) container.lookup( ManagedRepositoryContent.class, repoConfig.getLayout() ); |
96 | 0 | repo.setRepository( repoConfig ); |
97 | 0 | managedContentMap.put( repoId, repo ); |
98 | |
} |
99 | 0 | catch ( ComponentLookupException e ) |
100 | |
{ |
101 | 0 | throw new RepositoryException( "Specified layout [" + repoConfig.getLayout() |
102 | |
+ "] on managed repository id [" + repoId + "] is not valid.", e ); |
103 | 0 | } |
104 | |
|
105 | 0 | return repo; |
106 | |
} |
107 | |
|
108 | |
public RemoteRepositoryContent getRemoteRepositoryContent( String repoId ) |
109 | |
throws RepositoryNotFoundException, RepositoryException |
110 | |
{ |
111 | 0 | RemoteRepositoryContent repo = remoteContentMap.get( repoId ); |
112 | |
|
113 | 0 | if ( repo != null ) |
114 | |
{ |
115 | 0 | return repo; |
116 | |
} |
117 | |
|
118 | 0 | RemoteRepositoryConfiguration repoConfig = archivaConfiguration.getConfiguration() |
119 | |
.findRemoteRepositoryById( repoId ); |
120 | 0 | if ( repoConfig == null ) |
121 | |
{ |
122 | 0 | throw new RepositoryNotFoundException( "Unable to find remote repository configuration for id:" + repoId ); |
123 | |
} |
124 | |
|
125 | |
try |
126 | |
{ |
127 | 0 | repo = (RemoteRepositoryContent) container.lookup( RemoteRepositoryContent.class, repoConfig.getLayout() ); |
128 | 0 | repo.setRepository( repoConfig ); |
129 | 0 | remoteContentMap.put( repoId, repo ); |
130 | |
} |
131 | 0 | catch ( ComponentLookupException e ) |
132 | |
{ |
133 | 0 | throw new RepositoryException( "Specified layout [" + repoConfig.getLayout() |
134 | |
+ "] on remote repository id [" + repoId + "] is not valid.", e ); |
135 | 0 | } |
136 | |
|
137 | 0 | return repo; |
138 | |
} |
139 | |
|
140 | |
public void contextualize( Context context ) |
141 | |
throws ContextException |
142 | |
{ |
143 | 0 | container = (PlexusContainer) context.get( "plexus" ); |
144 | 0 | } |
145 | |
|
146 | |
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
147 | |
{ |
148 | 0 | if ( ConfigurationNames.isManagedRepositories( propertyName ) |
149 | |
|| ConfigurationNames.isRemoteRepositories( propertyName ) ) |
150 | |
{ |
151 | 0 | initMaps(); |
152 | |
} |
153 | 0 | } |
154 | |
|
155 | |
public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
156 | |
{ |
157 | |
|
158 | 0 | } |
159 | |
|
160 | |
public void initialize() |
161 | |
throws InitializationException |
162 | |
{ |
163 | 0 | archivaConfiguration.addChangeListener( this ); |
164 | 0 | } |
165 | |
|
166 | |
private void initMaps() |
167 | |
{ |
168 | 0 | synchronized ( managedContentMap ) |
169 | |
{ |
170 | |
|
171 | 0 | for ( ManagedRepositoryContent repo : managedContentMap.values() ) |
172 | |
{ |
173 | |
try |
174 | |
{ |
175 | 0 | container.release( repo ); |
176 | |
} |
177 | 0 | catch ( ComponentLifecycleException e ) |
178 | |
{ |
179 | |
|
180 | 0 | } |
181 | |
} |
182 | |
|
183 | |
|
184 | 0 | managedContentMap.clear(); |
185 | 0 | } |
186 | |
|
187 | 0 | synchronized ( remoteContentMap ) |
188 | |
{ |
189 | |
|
190 | 0 | for ( RemoteRepositoryContent repo : remoteContentMap.values() ) |
191 | |
{ |
192 | |
try |
193 | |
{ |
194 | 0 | container.release( repo ); |
195 | |
} |
196 | 0 | catch ( ComponentLifecycleException e ) |
197 | |
{ |
198 | |
|
199 | 0 | } |
200 | |
} |
201 | |
|
202 | |
|
203 | 0 | remoteContentMap.clear(); |
204 | 0 | } |
205 | 0 | } |
206 | |
} |