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.*;
27 import org.apache.archiva.event.Event;
28 import org.apache.archiva.repository.base.group.BasicRepositoryGroup;
29 import org.apache.archiva.repository.event.RepositoryEvent;
30 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
31 import org.apache.archiva.repository.features.IndexCreationFeature;
32 import org.apache.archiva.repository.features.RemoteIndexFeature;
33 import org.apache.archiva.repository.features.StagingRepositoryFeature;
34 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
35 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
36 import org.apache.archiva.repository.base.PasswordCredentials;
37 import org.springframework.stereotype.Service;
39 import java.io.IOException;
41 import java.nio.file.Paths;
42 import java.time.Duration;
43 import java.time.Period;
44 import java.util.HashSet;
48 * Just a simple mock class for the repository provider
50 @Service("mockRepositoryProvider")
51 public class RepositoryProviderMock implements RepositoryProvider
54 private static final Set<RepositoryType> TYPES = new HashSet<>( );
58 TYPES.add( RepositoryType.MAVEN );
59 TYPES.add( RepositoryType.NPM );
63 public Set<RepositoryType> provides( )
69 public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
70 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
74 public EditableRemoteRepository createRemoteInstance( String id, String name )
77 return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
78 } catch (IOException e) {
79 throw new RuntimeException(e);
84 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
87 return BasicRepositoryGroup.newFilesystemInstance( id, name, Paths.get( "target/groups" ) );
89 catch ( IOException e )
91 throw new RuntimeException( );
97 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
98 BasicManagedRepository managedRepository = null;
100 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
101 } catch (IOException e) {
102 throw new RepositoryException(e);
104 updateManagedInstance( managedRepository, configuration );
105 return managedRepository;
110 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
114 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
115 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
116 managedRepository.setBaseUri( new URI( "" ) );
117 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
118 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
119 managedRepository.setLayout( configuration.getLayout( ) );
120 managedRepository.setScanned( configuration.isScanned( ) );
121 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
122 if (configuration.isReleases()) {
123 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
125 if (configuration.isSnapshots()) {
126 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
128 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
129 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
130 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
131 acf.setRetentionCount( configuration.getRetentionCount( ) );
132 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
133 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
134 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
135 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
136 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
138 catch ( Exception e )
140 throw new RepositoryException( "Error", e );
147 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
148 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
149 BasicManagedRepository managedRepository = null;
151 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
152 } catch (IOException e) {
153 throw new RepositoryException(e);
155 updateManagedInstance( managedRepository, configuration );
156 managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
157 return managedRepository;
161 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
163 BasicRemoteRepository remoteRepository = null;
165 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
166 } catch (IOException e) {
167 throw new RepositoryException(e);
169 updateRemoteInstance( remoteRepository, configuration );
170 return remoteRepository;
173 @SuppressWarnings( "unchecked" )
175 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
179 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
180 remoteRepository.setBaseUri( new URI( "" ) );
181 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
182 remoteRepository.setLayout( configuration.getLayout( ) );
183 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
184 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
185 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
186 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
187 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
188 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
189 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
190 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
191 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
192 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
193 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
194 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
195 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
196 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
198 catch ( Exception e )
200 throw new RepositoryException( "Error", e );
206 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
207 EditableRepositoryGroup group = createRepositoryGroup( configuration.getId( ), configuration.getName( ) );
208 updateGroupInstance( group, configuration );
212 private void updateGroupInstance( EditableRepositoryGroup group, RepositoryGroupConfiguration configuration )
214 group.setMergedIndexTTL( configuration.getMergedIndexTtl() );
215 group.setSchedulingDefinition( configuration.getCronExpression() );
219 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
224 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
226 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
227 configuration.setId( managedRepository.getId( ) );
228 configuration.setName(managedRepository.getName());
229 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
230 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
231 configuration.setDescription( managedRepository.getDescription( ) );
232 configuration.setLayout( managedRepository.getLayout( ) );
233 configuration.setScanned( managedRepository.isScanned( ) );
234 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
235 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
236 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
237 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
238 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
239 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
240 configuration.setRetentionCount( acf.getRetentionCount( ) );
241 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
242 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
243 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
244 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
245 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
246 return configuration;
250 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
255 public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
262 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
264 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
265 configuration.setId( remoteRepository.getId( ) );
266 configuration.setName( remoteRepository.getName( ) );
267 configuration.setDescription( remoteRepository.getDescription( ) );
268 configuration.setLayout( remoteRepository.getLayout( ) );
269 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
270 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
271 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
272 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
273 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
274 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
277 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
278 configuration.setUsername( pwdCreds.getUsername( ) );
279 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
281 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
282 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
283 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
284 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
285 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
286 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
287 return configuration;
291 public void handle(Event event) {