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