]> source.dussan.org Git - archiva.git/blob
90dd593e643a3ddbd7881ba2452c94d015bcc85f
[archiva.git] /
1 package org.apache.archiva.repository.mock;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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.event.RepositoryEvent;
29 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
30 import org.apache.archiva.repository.features.IndexCreationFeature;
31 import org.apache.archiva.repository.features.RemoteIndexFeature;
32 import org.apache.archiva.repository.features.StagingRepositoryFeature;
33 import org.apache.archiva.repository.base.BasicManagedRepository;
34 import org.apache.archiva.repository.base.BasicRemoteRepository;
35 import org.apache.archiva.repository.base.PasswordCredentials;
36 import org.springframework.stereotype.Service;
37
38 import java.io.IOException;
39 import java.net.URI;
40 import java.nio.file.Paths;
41 import java.time.Duration;
42 import java.time.Period;
43 import java.util.HashSet;
44 import java.util.Set;
45
46 /**
47  * Just a simple mock class for the repository provider
48  */
49 @Service("mockRepositoryProvider")
50 public class RepositoryProviderMock implements RepositoryProvider
51 {
52
53     private static final Set<RepositoryType> TYPES = new HashSet<>( );
54
55     static
56     {
57         TYPES.add( RepositoryType.MAVEN );
58         TYPES.add( RepositoryType.NPM );
59     }
60
61     @Override
62     public Set<RepositoryType> provides( )
63     {
64         return TYPES;
65     }
66
67     @Override
68     public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
69         return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
70     }
71
72     @Override
73     public EditableRemoteRepository createRemoteInstance( String id, String name )
74     {
75         try {
76             return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
77         } catch (IOException e) {
78             throw new RuntimeException(e);
79         }
80     }
81
82     @Override
83     public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
84         return null;
85     }
86
87     @Override
88     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
89         BasicManagedRepository managedRepository = null;
90         try {
91             managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
92         } catch (IOException e) {
93             throw new RepositoryException(e);
94         }
95         updateManagedInstance( managedRepository, configuration );
96         return managedRepository;
97     }
98
99
100     @Override
101     public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
102     {
103         try
104         {
105             managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
106             managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
107             managedRepository.setBaseUri( new URI( "" ) );
108             managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
109             managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
110             managedRepository.setLayout( configuration.getLayout( ) );
111             managedRepository.setScanned( configuration.isScanned( ) );
112             managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
113             if (configuration.isReleases()) {
114                 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
115             }
116             if (configuration.isSnapshots()) {
117                 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
118             }
119             ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
120             acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
121             acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
122             acf.setRetentionCount( configuration.getRetentionCount( ) );
123             IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
124             icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
125             icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
126             StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
127             srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
128         }
129         catch ( Exception e )
130         {
131             throw new RepositoryException( "Error", e );
132         }
133
134     }
135
136
137     @Override
138     public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
139         String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
140         BasicManagedRepository managedRepository = null;
141         try {
142             managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
143         } catch (IOException e) {
144             throw new RepositoryException(e);
145         }
146         updateManagedInstance( managedRepository, configuration );
147         managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
148         return managedRepository;
149     }
150
151     @Override
152     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
153     {
154         BasicRemoteRepository remoteRepository = null;
155         try {
156             remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
157         } catch (IOException e) {
158             throw new RepositoryException(e);
159         }
160         updateRemoteInstance( remoteRepository, configuration );
161         return remoteRepository;
162     }
163
164     @SuppressWarnings( "unchecked" )
165     @Override
166     public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
167     {
168         try
169         {
170             remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
171             remoteRepository.setBaseUri( new URI( "" ) );
172             remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
173             remoteRepository.setLayout( configuration.getLayout( ) );
174             remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
175             remoteRepository.setCheckPath( configuration.getCheckPath( ) );
176             remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
177             remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
178             remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
179             char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
180             remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
181             remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
182             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
183             rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
184             rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
185             rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
186             rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
187             rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
188         }
189         catch ( Exception e )
190         {
191             throw new RepositoryException( "Error", e );
192         }
193
194     }
195
196     @Override
197     public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
198         return null;
199     }
200
201     @Override
202     public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
203
204     }
205
206     @Override
207     public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
208     {
209         ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
210         configuration.setId( managedRepository.getId( ) );
211         configuration.setName(managedRepository.getName());
212         configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
213         configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
214         configuration.setDescription( managedRepository.getDescription( ) );
215         configuration.setLayout( managedRepository.getLayout( ) );
216         configuration.setScanned( managedRepository.isScanned( ) );
217         configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
218         configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
219         configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
220         ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
221         configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
222         configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
223         configuration.setRetentionCount( acf.getRetentionCount( ) );
224         IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
225         configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
226         configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
227         StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
228         configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
229         return configuration;
230     }
231
232     @Override
233     public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
234         return null;
235     }
236
237     @Override
238     public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
239     {
240         // do nothing
241     }
242
243
244     @Override
245     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
246     {
247         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
248         configuration.setId( remoteRepository.getId( ) );
249         configuration.setName( remoteRepository.getName( ) );
250         configuration.setDescription( remoteRepository.getDescription( ) );
251         configuration.setLayout( remoteRepository.getLayout( ) );
252         configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
253         configuration.setCheckPath( remoteRepository.getCheckPath( ) );
254         configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
255         configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
256         configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
257         RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
258         if (creds!=null)
259         {
260             PasswordCredentials pwdCreds = (PasswordCredentials) creds;
261             configuration.setUsername( pwdCreds.getUsername( ) );
262             configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
263         }
264         configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
265         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
266         configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
267         configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
268         configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
269         configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
270         return configuration;
271     }
272
273     @Override
274     public void handle(Event event) {
275
276     }
277 }