1 package org.apache.archiva.repository.maven2;
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.AbstractRepositoryConfiguration;
23 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
25 import org.apache.archiva.repository.EditableRepository;
26 import org.apache.archiva.repository.ManagedRepository;
27 import org.apache.archiva.repository.PasswordCredentials;
28 import org.apache.archiva.repository.ReleaseScheme;
29 import org.apache.archiva.repository.RemoteRepository;
30 import org.apache.archiva.repository.RepositoryException;
31 import org.apache.archiva.repository.RepositoryProvider;
32 import org.apache.archiva.repository.RepositoryType;
33 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
34 import org.apache.archiva.repository.features.IndexCreationFeature;
35 import org.apache.archiva.repository.features.RemoteIndexFeature;
36 import org.apache.archiva.repository.features.StagingRepositoryFeature;
37 import org.apache.commons.lang.StringUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
43 import java.net.URISyntaxException;
44 import java.time.Duration;
45 import java.time.Period;
46 import java.util.HashSet;
47 import java.util.Locale;
49 import java.util.regex.Pattern;
52 * Provider for the maven2 repository implementations
54 @Service("mavenRepositoryProvider")
55 public class MavenRepositoryProvider implements RepositoryProvider
58 private static final Logger log = LoggerFactory.getLogger( MavenRepositoryProvider.class );
60 static final Set<RepositoryType> TYPES = new HashSet<>( );
62 TYPES.add( RepositoryType.MAVEN);
66 public Set<RepositoryType> provides( )
71 private URI getURIFromConfig(String config) throws RepositoryException {
74 uri = new URI(config);
75 if (uri.getScheme()==null) {
76 uri = new URI("file://"+config);
78 if (!"file".equals(uri.getScheme())) {
79 log.error("Bad URI scheme found: {}, URI={}", uri.getScheme(), uri);
80 throw new RepositoryException("The uri "+config+" is not valid. Only file:// URI is allowed for maven.");
82 } catch (URISyntaxException e) {
83 String newCfg = "file://"+config;
86 uri = new URI(newCfg);
88 catch ( URISyntaxException e1 )
90 log.error("Could not create URI from {} -> ", config, newCfg);
91 throw new RepositoryException( "The config entry "+config+" cannot be converted to URI." );
98 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration cfg ) throws RepositoryException
100 MavenManagedRepository repo = new MavenManagedRepository(cfg.getId() ,cfg.getName());
101 repo.setLocation( getURIFromConfig( cfg.getLocation() ) );
102 setBaseConfig( repo, cfg );
103 repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
104 repo.setBlocksRedeployment( cfg.isBlockRedeployments() );
105 repo.setScanned( cfg.isScanned() );
106 Set<ReleaseScheme> schemes = new HashSet<>( );
107 if (cfg.isReleases()) {
108 repo.addActiveReleaseScheme(ReleaseScheme.RELEASE);
110 if (cfg.isSnapshots()) {
111 repo.addActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
114 StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature( StagingRepositoryFeature.class ).get();
115 stagingRepositoryFeature.setStageRepoNeeded( cfg.isStageRepoNeeded() );
116 // TODO: staging repository -> here or in repositoryregistry?
119 IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get( );
120 indexCreationFeature.setSkipPackedIndexCreation( cfg.isSkipPackedIndexCreation() );
121 indexCreationFeature.setIndexPath( getURIFromConfig( cfg.getIndexDir() ) );
123 ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get();
125 artifactCleanupFeature.setDeleteReleasedSnapshots( cfg.isDeleteReleasedSnapshots() );
126 artifactCleanupFeature.setRetentionCount( cfg.getRetentionCount() );
127 artifactCleanupFeature.setRetentionTime( Period.ofDays( cfg.getRetentionTime() ) );
135 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration cfg )
137 MavenRemoteRepository repo = new MavenRemoteRepository( cfg.getId( ), cfg.getName( ) );
138 setBaseConfig( repo, cfg );
139 repo.setCheckPath( cfg.getCheckPath() );
140 repo.setSchedulingDefinition( cfg.getRefreshCronExpression() );
143 repo.setLocation(new URI(cfg.getUrl()));
145 catch ( URISyntaxException e )
147 log.error("Could not set remote url "+cfg.getUrl());
149 RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class ).get();
150 remoteIndexFeature.setDownloadRemoteIndex( cfg.isDownloadRemoteIndex() );
151 remoteIndexFeature.setDownloadRemoteIndexOnStartup( cfg.isDownloadRemoteIndexOnStartup() );
152 remoteIndexFeature.setDownloadTimeout( Duration.ofSeconds( cfg.getRemoteDownloadTimeout()) );
153 remoteIndexFeature.setProxyId( cfg.getRemoteDownloadNetworkProxyId() );
154 if (cfg.isDownloadRemoteIndex())
158 remoteIndexFeature.setIndexUri( new URI( cfg.getRemoteIndexUrl( ) ) );
160 catch ( URISyntaxException e )
162 log.error( "Could not set remote index url " + cfg.getRemoteIndexUrl( ) );
163 remoteIndexFeature.setDownloadRemoteIndex( false );
164 remoteIndexFeature.setDownloadRemoteIndexOnStartup( false );
167 repo.setExtraHeaders( cfg.getExtraHeaders() );
168 repo.setExtraParameters( cfg.getExtraParameters() );
169 PasswordCredentials credentials = new PasswordCredentials();
170 if (cfg.getPassword()!=null && cfg.getUsername()!=null)
172 credentials.setPassword( cfg.getPassword( ).toCharArray( ) );
173 credentials.setUsername( cfg.getUsername() );
174 repo.setCredentials( credentials );
176 credentials.setPassword( new char[0] );
182 private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg) {
183 repo.setDescription( Locale.getDefault( ), cfg.getDescription() );
184 String indexDir = cfg.getIndexDir();
187 if ( StringUtils.isEmpty( indexDir )) {
188 repo.setIndex( false );
191 if ( indexDir.startsWith( "file://" ) )
193 //repo.setIndexPath( new URI( indexDir ) );
197 //repo.setIndexPath( new URI( "file://" + indexDir ) );
201 catch ( Exception e )
203 log.error("Could not set index path "+cfg.getIndexDir());
204 repo.setIndex(false);
206 repo.setLayout( cfg.getLayout() );