]> source.dussan.org Git - archiva.git/blob
04ff8c071f32dbc8d4e63b3ddc9f5f7593fb1a1f
[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.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;
46
47 import java.io.IOException;
48 import java.net.URI;
49 import java.nio.file.Paths;
50 import java.time.Duration;
51 import java.time.Period;
52 import java.util.HashSet;
53 import java.util.Set;
54
55 /**
56  * Just a simple mock class for the repository provider
57  */
58 @Service("mockRepositoryProvider")
59 public class RepositoryProviderMock implements RepositoryProvider
60 {
61
62     private static final Set<RepositoryType> TYPES = new HashSet<>( );
63
64     static
65     {
66         TYPES.add( RepositoryType.MAVEN );
67         TYPES.add( RepositoryType.NPM );
68     }
69
70     @Override
71     public Set<RepositoryType> provides( )
72     {
73         return TYPES;
74     }
75
76     @Override
77     public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
78         return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
79     }
80
81     @Override
82     public EditableRemoteRepository createRemoteInstance( String id, String name )
83     {
84         try {
85             return BasicRemoteRepository.newFilesystemInstance( id, name, Paths.get("target/remotes") );
86         } catch (IOException e) {
87             throw new RuntimeException(e);
88         }
89     }
90
91     @Override
92     public EditableRepositoryGroup createRepositoryGroup( String id, String name )
93     {
94         return null;
95     }
96
97     @Override
98     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
99     {
100         BasicManagedRepository managedRepository = null;
101         try {
102             managedRepository = BasicManagedRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ) , Paths.get("target/repositories").resolve(configuration.getId()));
103         } catch (IOException e) {
104             throw new RepositoryException(e);
105         }
106         updateManagedInstance( managedRepository, configuration );
107         return managedRepository;
108     }
109
110
111     @Override
112     public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
113     {
114         try
115         {
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 );
126             }
127             if (configuration.isSnapshots()) {
128                 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
129             }
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( ) );
139         }
140         catch ( Exception e )
141         {
142             throw new RepositoryException( "Error", e );
143         }
144
145     }
146
147
148     @Override
149     public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
150     {
151         String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
152         BasicManagedRepository managedRepository = null;
153         try {
154             managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
155         } catch (IOException e) {
156             throw new RepositoryException(e);
157         }
158         updateManagedInstance( managedRepository, configuration );
159         return managedRepository;
160     }
161
162     @Override
163     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
164     {
165         BasicRemoteRepository remoteRepository = null;
166         try {
167             remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
168         } catch (IOException e) {
169             throw new RepositoryException(e);
170         }
171         updateRemoteInstance( remoteRepository, configuration );
172         return remoteRepository;
173     }
174
175     @SuppressWarnings("unchecked")
176     @Override
177     public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
178     {
179         try
180         {
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" ));
201         }
202         catch ( Exception e )
203         {
204             throw new RepositoryException( "Error while updating remote instance: "+e.getMessage(), e );
205         }
206
207     }
208
209     @Override
210     public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
211     {
212         return null;
213     }
214
215     @Override
216     public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
217     {
218
219     }
220
221     @Override
222     public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
223     {
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;
245     }
246
247     @Override
248     public RepositoryGroupConfiguration getRepositoryGroupConfiguration( RepositoryGroup repositoryGroup ) throws RepositoryException
249     {
250         return null;
251     }
252
253
254     @Override
255     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
256     {
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( );
268         if (creds!=null)
269         {
270             PasswordCredentials pwdCreds = (PasswordCredentials) creds;
271             configuration.setUsername( pwdCreds.getUsername( ) );
272             configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
273         }
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;
281     }
282
283     @Override
284     public void handle(Event event) {
285
286     }
287 }