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.repository.BasicManagedRepository;
26 import org.apache.archiva.repository.BasicRemoteRepository;
27 import org.apache.archiva.repository.EditableManagedRepository;
28 import org.apache.archiva.repository.EditableRemoteRepository;
29 import org.apache.archiva.repository.EditableRepositoryGroup;
30 import org.apache.archiva.repository.ManagedRepository;
31 import org.apache.archiva.repository.PasswordCredentials;
32 import org.apache.archiva.repository.ReleaseScheme;
33 import org.apache.archiva.repository.RemoteRepository;
34 import org.apache.archiva.repository.RepositoryCredentials;
35 import org.apache.archiva.repository.events.Event;
36 import org.apache.archiva.repository.events.RepositoryValueEvent;
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.features.ArtifactCleanupFeature;
42 import org.apache.archiva.repository.features.IndexCreationFeature;
43 import org.apache.archiva.repository.features.RemoteIndexFeature;
44 import org.apache.archiva.repository.features.StagingRepositoryFeature;
45 import org.springframework.stereotype.Service;
47 import java.io.IOException;
49 import java.nio.file.Paths;
50 import java.time.Duration;
51 import java.time.Period;
52 import java.util.HashSet;
56 * Just a simple mock class for the repository provider
58 @Service("mockRepositoryProvider")
59 public class RepositoryProviderMock implements RepositoryProvider
62 private static final Set<RepositoryType> TYPES = new HashSet<>( );
66 TYPES.add( RepositoryType.MAVEN );
67 TYPES.add( RepositoryType.NPM );
71 public Set<RepositoryType> provides( )
77 public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
78 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
82 public EditableRemoteRepository createRemoteInstance( String id, String name )
85 return BasicRemoteRepository.newFilesystemInstance( id, name, Paths.get("target/remotes") );
86 } catch (IOException e) {
87 throw new RuntimeException(e);
92 public EditableRepositoryGroup createRepositoryGroup( String id, String name )
98 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
100 BasicManagedRepository managedRepository = null;
102 managedRepository = BasicManagedRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ) , Paths.get("target/repositories").resolve(configuration.getId()));
103 } catch (IOException e) {
104 throw new RepositoryException(e);
106 updateManagedInstance( managedRepository, configuration );
107 return managedRepository;
112 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
116 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
117 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
118 managedRepository.setBaseUri( new URI( "" ) );
119 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
120 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
121 managedRepository.setLayout( configuration.getLayout( ) );
122 managedRepository.setScanned( configuration.isScanned( ) );
123 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
124 if (configuration.isReleases()) {
125 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
127 if (configuration.isSnapshots()) {
128 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
130 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
131 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
132 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
133 acf.setRetentionCount( configuration.getRetentionCount( ) );
134 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
135 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
136 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
137 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
138 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
140 catch ( Exception e )
142 throw new RepositoryException( "Error", e );
149 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
151 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
152 BasicManagedRepository managedRepository = null;
154 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
155 } catch (IOException e) {
156 throw new RepositoryException(e);
158 updateManagedInstance( managedRepository, configuration );
159 return managedRepository;
163 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
165 BasicRemoteRepository remoteRepository = null;
167 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
168 } catch (IOException e) {
169 throw new RepositoryException(e);
171 updateRemoteInstance( remoteRepository, configuration );
172 return remoteRepository;
175 @SuppressWarnings("unchecked")
177 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
181 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
182 remoteRepository.setBaseUri( new URI( "" ) );
183 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
184 remoteRepository.setLayout( configuration.getLayout( ) );
185 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
186 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
187 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
188 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
189 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
190 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
191 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
192 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
193 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
194 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
195 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
196 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
197 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
198 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
199 IndexCreationFeature icf = remoteRepository.getFeature(IndexCreationFeature.class).get();
200 icf.setIndexPath(new URI(".index" ));
202 catch ( Exception e )
204 throw new RepositoryException( "Error while updating remote instance: "+e.getMessage(), e );
210 public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
216 public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
222 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
224 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
225 configuration.setId( managedRepository.getId( ) );
226 configuration.setName(managedRepository.getName());
227 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
228 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
229 configuration.setDescription( managedRepository.getDescription( ) );
230 configuration.setLayout( managedRepository.getLayout( ) );
231 configuration.setScanned( managedRepository.isScanned( ) );
232 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
233 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
234 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
235 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
236 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
237 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
238 configuration.setRetentionCount( acf.getRetentionCount( ) );
239 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
240 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
241 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
242 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
243 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
244 return configuration;
248 public RepositoryGroupConfiguration getRepositoryGroupConfiguration( RepositoryGroup repositoryGroup ) throws RepositoryException
255 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
257 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
258 configuration.setId( remoteRepository.getId( ) );
259 configuration.setName( remoteRepository.getName( ) );
260 configuration.setDescription( remoteRepository.getDescription( ) );
261 configuration.setLayout( remoteRepository.getLayout( ) );
262 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
263 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
264 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
265 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
266 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
267 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
270 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
271 configuration.setUsername( pwdCreds.getUsername( ) );
272 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
274 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
275 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
276 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
277 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
278 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
279 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
280 return configuration;
284 public void handle(Event event) {