]> source.dussan.org Git - archiva.git/blob
2f862fee46f4ed353648c5193f226d13d8009251
[archiva.git] /
1 package org.apache.archiva.repository.maven2;
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.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;
41
42 import java.net.URI;
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;
48 import java.util.Set;
49 import java.util.regex.Pattern;
50
51 /**
52  * Provider for the maven2 repository implementations
53  */
54 @Service("mavenRepositoryProvider")
55 public class MavenRepositoryProvider implements RepositoryProvider
56 {
57
58     private static final Logger log = LoggerFactory.getLogger( MavenRepositoryProvider.class );
59
60     static final Set<RepositoryType> TYPES = new HashSet<>(  );
61     static {
62         TYPES.add( RepositoryType.MAVEN);
63     }
64
65     @Override
66     public Set<RepositoryType> provides( )
67     {
68         return TYPES;
69     }
70
71     private URI getURIFromConfig(String config) throws RepositoryException {
72         URI uri;
73         try {
74             uri = new URI(config);
75             if (uri.getScheme()==null) {
76                 uri = new URI("file://"+config);
77             }
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.");
81             }
82         } catch (URISyntaxException e) {
83             String newCfg = "file://"+config;
84             try
85             {
86                 uri = new URI(newCfg);
87             }
88             catch ( URISyntaxException e1 )
89             {
90                 log.error("Could not create URI from {} -> ", config, newCfg);
91                 throw new RepositoryException( "The config entry "+config+" cannot be converted to URI." );
92             }
93         }
94         return uri;
95     }
96
97     @Override
98     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration cfg ) throws RepositoryException
99     {
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);
109         }
110         if (cfg.isSnapshots()) {
111             repo.addActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
112         }
113
114         StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature( StagingRepositoryFeature.class ).get();
115         stagingRepositoryFeature.setStageRepoNeeded( cfg.isStageRepoNeeded() );
116         // TODO: staging repository  -> here or in repositoryregistry?
117
118
119         IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get( );
120         indexCreationFeature.setSkipPackedIndexCreation( cfg.isSkipPackedIndexCreation() );
121         indexCreationFeature.setIndexPath( getURIFromConfig( cfg.getIndexDir() ) );
122
123         ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get();
124
125         artifactCleanupFeature.setDeleteReleasedSnapshots( cfg.isDeleteReleasedSnapshots() );
126         artifactCleanupFeature.setRetentionCount( cfg.getRetentionCount() );
127         artifactCleanupFeature.setRetentionTime( Period.ofDays( cfg.getRetentionTime() ) );
128
129         return repo;
130     }
131
132
133
134     @Override
135     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration cfg )
136     {
137         MavenRemoteRepository repo = new MavenRemoteRepository( cfg.getId( ), cfg.getName( ) );
138         setBaseConfig( repo, cfg );
139         repo.setCheckPath( cfg.getCheckPath() );
140         repo.setSchedulingDefinition( cfg.getRefreshCronExpression() );
141         try
142         {
143             repo.setLocation(new URI(cfg.getUrl()));
144         }
145         catch ( URISyntaxException e )
146         {
147             log.error("Could not set remote url "+cfg.getUrl());
148         }
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())
155         {
156             try
157             {
158                 remoteIndexFeature.setIndexUri( new URI( cfg.getRemoteIndexUrl( ) ) );
159             }
160             catch ( URISyntaxException e )
161             {
162                 log.error( "Could not set remote index url " + cfg.getRemoteIndexUrl( ) );
163                 remoteIndexFeature.setDownloadRemoteIndex( false );
164                 remoteIndexFeature.setDownloadRemoteIndexOnStartup( false );
165             }
166         }
167         repo.setExtraHeaders( cfg.getExtraHeaders() );
168         repo.setExtraParameters( cfg.getExtraParameters() );
169         PasswordCredentials credentials = new PasswordCredentials();
170         if (cfg.getPassword()!=null && cfg.getUsername()!=null)
171         {
172             credentials.setPassword( cfg.getPassword( ).toCharArray( ) );
173             credentials.setUsername( cfg.getUsername() );
174             repo.setCredentials( credentials );
175         } else {
176             credentials.setPassword( new char[0] );
177         }
178
179         return repo;
180     }
181
182     private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg) {
183         repo.setDescription( Locale.getDefault( ), cfg.getDescription() );
184         String indexDir = cfg.getIndexDir();
185         try
186         {
187             if ( StringUtils.isEmpty( indexDir )) {
188                 repo.setIndex( false );
189             } else
190             {
191                 if ( indexDir.startsWith( "file://" ) )
192                 {
193                     //repo.setIndexPath( new URI( indexDir ) );
194                 }
195                 else
196                 {
197                     //repo.setIndexPath( new URI( "file://" + indexDir ) );
198                 }
199             }
200         }
201         catch ( Exception e )
202         {
203             log.error("Could not set index path "+cfg.getIndexDir());
204             repo.setIndex(false);
205         }
206         repo.setLayout( cfg.getLayout() );
207
208     }
209 }