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 ) throws IOException {
79 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
83 public EditableRemoteRepository createRemoteInstance( String id, String name )
86 return BasicRemoteRepository.newFilesystemInstance( id, name, Paths.get("target/remotes") );
87 } catch (IOException e) {
88 throw new RuntimeException(e);
93 public EditableRepositoryGroup createRepositoryGroup( String id, String name )
99 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
101 BasicManagedRepository managedRepository = null;
103 managedRepository = BasicManagedRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ) , Paths.get("target/repositories").resolve(configuration.getId()));
104 } catch (IOException e) {
105 throw new RepositoryException(e);
107 updateManagedInstance( managedRepository, configuration );
108 return managedRepository;
113 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
117 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
118 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
119 managedRepository.setBaseUri( new URI( "" ) );
120 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
121 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
122 managedRepository.setLayout( configuration.getLayout( ) );
123 managedRepository.setScanned( configuration.isScanned( ) );
124 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
125 if (configuration.isReleases()) {
126 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
128 if (configuration.isSnapshots()) {
129 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
131 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
132 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
133 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
134 acf.setRetentionCount( configuration.getRetentionCount( ) );
135 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
136 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
137 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
138 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
139 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
141 catch ( Exception e )
143 throw new RepositoryException( "Error", e );
150 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
152 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
153 BasicManagedRepository managedRepository = null;
155 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
156 } catch (IOException e) {
157 throw new RepositoryException(e);
159 updateManagedInstance( managedRepository, configuration );
160 return managedRepository;
164 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
166 BasicRemoteRepository remoteRepository = null;
168 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
169 } catch (IOException e) {
170 throw new RepositoryException(e);
172 updateRemoteInstance( remoteRepository, configuration );
173 return remoteRepository;
176 @SuppressWarnings("unchecked")
178 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
182 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
183 remoteRepository.setBaseUri( new URI( "" ) );
184 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
185 remoteRepository.setLayout( configuration.getLayout( ) );
186 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
187 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
188 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
189 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
190 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
191 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
192 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
193 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
194 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
195 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
196 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
197 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
198 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
199 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
200 IndexCreationFeature icf = remoteRepository.getFeature(IndexCreationFeature.class).get();
201 icf.setIndexPath(new URI(".index" ));
203 catch ( Exception e )
205 throw new RepositoryException( "Error while updating remote instance: "+e.getMessage(), e );
211 public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
217 public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
223 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
225 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
226 configuration.setId( managedRepository.getId( ) );
227 configuration.setName(managedRepository.getName());
228 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
229 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
230 configuration.setDescription( managedRepository.getDescription( ) );
231 configuration.setLayout( managedRepository.getLayout( ) );
232 configuration.setScanned( managedRepository.isScanned( ) );
233 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
234 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
235 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
236 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
237 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
238 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
239 configuration.setRetentionCount( acf.getRetentionCount( ) );
240 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
241 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
242 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
243 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
244 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
245 return configuration;
249 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) {