1 package org.apache.archiva.repository.mock;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
23 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
24 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
25 import org.apache.archiva.event.EventHandler;
26 import org.apache.archiva.repository.base.BasicManagedRepository;
27 import org.apache.archiva.repository.base.BasicRemoteRepository;
28 import org.apache.archiva.repository.EditableManagedRepository;
29 import org.apache.archiva.repository.EditableRemoteRepository;
30 import org.apache.archiva.repository.EditableRepositoryGroup;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.base.PasswordCredentials;
33 import org.apache.archiva.repository.ReleaseScheme;
34 import org.apache.archiva.repository.RemoteRepository;
35 import org.apache.archiva.repository.RepositoryCredentials;
36 import org.apache.archiva.event.Event;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryGroup;
39 import org.apache.archiva.repository.RepositoryProvider;
40 import org.apache.archiva.repository.RepositoryType;
41 import org.apache.archiva.repository.event.RepositoryEvent;
42 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
43 import org.apache.archiva.repository.features.IndexCreationFeature;
44 import org.apache.archiva.repository.features.RemoteIndexFeature;
45 import org.apache.archiva.repository.features.StagingRepositoryFeature;
46 import org.springframework.stereotype.Service;
48 import java.io.IOException;
50 import java.nio.file.Paths;
51 import java.time.Duration;
52 import java.time.Period;
53 import java.util.HashSet;
57 * Just a simple mock class for the repository provider
59 @Service("mockRepositoryProvider")
60 public class RepositoryProviderMock implements RepositoryProvider
63 private static final Set<RepositoryType> TYPES = new HashSet<>( );
67 TYPES.add( RepositoryType.MAVEN );
68 TYPES.add( RepositoryType.NPM );
72 public Set<RepositoryType> provides( )
78 public EditableManagedRepository createManagedInstance( String id, String name )
81 return BasicManagedRepository.newFilesystemInstance( id, name, Paths.get("target/repositories").resolve(id) );
82 } catch (IOException e) {
83 throw new RuntimeException(e);
88 public EditableRemoteRepository createRemoteInstance( String id, String name )
91 return BasicRemoteRepository.newFilesystemInstance( id, name, Paths.get("target/remotes") );
92 } catch (IOException e) {
93 throw new RuntimeException(e);
98 public EditableRepositoryGroup createRepositoryGroup( String id, String name )
105 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
107 BasicManagedRepository managedRepository = null;
109 managedRepository = BasicManagedRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/repositories").resolve(configuration.getId()) );
110 } catch (IOException e) {
111 throw new RepositoryException(e);
113 updateManagedInstance( managedRepository, configuration );
114 return managedRepository;
119 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
123 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
124 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
125 managedRepository.setBaseUri( new URI( "" ) );
126 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
127 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
128 managedRepository.setLayout( configuration.getLayout( ) );
129 managedRepository.setScanned( configuration.isScanned( ) );
130 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
131 if (configuration.isReleases()) {
132 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
134 if (configuration.isSnapshots()) {
135 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
137 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
138 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
139 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
140 acf.setRetentionCount( configuration.getRetentionCount( ) );
141 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
142 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
143 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
144 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
145 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
147 catch ( Exception e )
149 throw new RepositoryException( "Error", e );
156 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
158 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
159 BasicManagedRepository managedRepository = null;
161 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
162 } catch (IOException e) {
163 throw new RepositoryException(e);
165 updateManagedInstance( managedRepository, configuration );
166 return managedRepository;
170 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
172 BasicRemoteRepository remoteRepository = null;
174 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
175 } catch (IOException e) {
176 throw new RepositoryException(e);
178 updateRemoteInstance( remoteRepository, configuration );
179 return remoteRepository;
182 @SuppressWarnings("unchecked")
184 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
188 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
189 remoteRepository.setBaseUri( new URI( "" ) );
190 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
191 remoteRepository.setLayout( configuration.getLayout( ) );
192 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
193 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
194 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
195 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
196 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
197 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
198 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
199 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
200 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
201 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
202 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
203 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
204 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
205 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
207 catch ( Exception e )
209 throw new RepositoryException( "Error", e );
215 public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
221 public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
227 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository )
229 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
230 configuration.setId( managedRepository.getId( ) );
231 configuration.setName(managedRepository.getName());
232 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
233 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
234 configuration.setDescription( managedRepository.getDescription( ) );
235 configuration.setScanned( managedRepository.isScanned( ) );
236 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
237 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
238 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
239 configuration.setLayout( managedRepository.getLayout() );
240 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
241 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
242 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
243 configuration.setRetentionCount( acf.getRetentionCount( ) );
244 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
245 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
246 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
247 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
248 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
249 return configuration;
253 public RepositoryGroupConfiguration getRepositoryGroupConfiguration( RepositoryGroup repositoryGroup ) throws RepositoryException
259 public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
266 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository )
268 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
269 configuration.setId( remoteRepository.getId( ) );
270 configuration.setName( remoteRepository.getName( ) );
271 configuration.setDescription( remoteRepository.getDescription( ) );
272 configuration.setLayout( remoteRepository.getLayout( ) );
273 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
274 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
275 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
276 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
277 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
278 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
281 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
282 configuration.setUsername( pwdCreds.getUsername( ) );
283 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
285 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
286 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
287 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
288 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
289 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
290 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
291 return configuration;
295 public void handle(Event event) {