]> source.dussan.org Git - archiva.git/blob
1c02e1f75392f237dfb32368f008da43df894390
[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.Repository;
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
50 /**
51  * Provider for the maven2 repository implementations
52  */
53 @Service("mavenRepositoryProvider")
54 public class MavenRepositoryProvider implements RepositoryProvider
55 {
56     private static final Logger log = LoggerFactory.getLogger( MavenRepositoryProvider.class );
57
58     static final Set<RepositoryType> TYPES = new HashSet<>(  );
59     static {
60         TYPES.add( RepositoryType.MAVEN);
61     }
62
63     @Override
64     public Set<RepositoryType> provides( )
65     {
66         return TYPES;
67     }
68
69     @Override
70     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration cfg )
71     {
72         MavenManagedRepository repo = new MavenManagedRepository(cfg.getId() ,cfg.getName());
73         try
74         {
75             if (cfg.getLocation().startsWith("file:")) {
76                     repo.setLocation( new URI(cfg.getLocation()) );
77             } else {
78                 repo.setLocation( new URI("file://"+cfg.getLocation()) );
79             }
80         }
81         catch ( URISyntaxException e )
82         {
83             log.error("Could not set repository uri "+cfg.getLocation());
84         }
85         setBaseConfig( repo, cfg );
86         repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
87         repo.setBlocksRedeployment( cfg.isBlockRedeployments() );
88         repo.setScanned( cfg.isScanned() );
89         Set<ReleaseScheme> schemes = new HashSet<>(  );
90         if (cfg.isReleases()) {
91             repo.addActiveReleaseScheme(ReleaseScheme.RELEASE);
92         }
93         if (cfg.isSnapshots()) {
94             repo.addActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
95         }
96
97         StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature( StagingRepositoryFeature.class ).get();
98         stagingRepositoryFeature.setStageRepoNeeded( cfg.isStageRepoNeeded() );
99         // TODO: staging repository  -> here or in repositoryregistry?
100
101
102         IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get( );
103         indexCreationFeature.setSkipPackedIndexCreation( cfg.isSkipPackedIndexCreation() );
104
105         ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get();
106
107         artifactCleanupFeature.setDeleteReleasedSnapshots( cfg.isDeleteReleasedSnapshots() );
108         artifactCleanupFeature.setRetentionCount( cfg.getRetentionCount() );
109         artifactCleanupFeature.setRetentionTime( Period.ofDays( cfg.getRetentionTime() ) );
110
111         return repo;
112     }
113
114     @Override
115     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration cfg )
116     {
117         MavenRemoteRepository repo = new MavenRemoteRepository( cfg.getId( ), cfg.getName( ) );
118         setBaseConfig( repo, cfg );
119         repo.setCheckPath( cfg.getCheckPath() );
120         repo.setSchedulingDefinition( cfg.getRefreshCronExpression() );
121         try
122         {
123             repo.setLocation(new URI(cfg.getUrl()));
124         }
125         catch ( URISyntaxException e )
126         {
127             log.error("Could not set remote url "+cfg.getUrl());
128         }
129         RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class ).get();
130         remoteIndexFeature.setDownloadRemoteIndex( cfg.isDownloadRemoteIndex() );
131         remoteIndexFeature.setDownloadRemoteIndexOnStartup( cfg.isDownloadRemoteIndexOnStartup() );
132         remoteIndexFeature.setDownloadTimeout( Duration.ofSeconds( cfg.getRemoteDownloadTimeout()) );
133         remoteIndexFeature.setProxyId( cfg.getRemoteDownloadNetworkProxyId() );
134         if (cfg.isDownloadRemoteIndex())
135         {
136             try
137             {
138                 remoteIndexFeature.setIndexUri( new URI( cfg.getRemoteIndexUrl( ) ) );
139             }
140             catch ( URISyntaxException e )
141             {
142                 log.error( "Could not set remote index url " + cfg.getRemoteIndexUrl( ) );
143                 remoteIndexFeature.setDownloadRemoteIndex( false );
144                 remoteIndexFeature.setDownloadRemoteIndexOnStartup( false );
145             }
146         }
147         repo.setExtraHeaders( cfg.getExtraHeaders() );
148         repo.setExtraParameters( cfg.getExtraParameters() );
149         PasswordCredentials credentials = new PasswordCredentials();
150         credentials.setPassword( cfg.getPassword().toCharArray() );
151         credentials.setUsername( cfg.getUsername() );
152         repo.setCredentials( credentials );
153
154         return repo;
155     }
156
157     private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg) {
158         repo.setDescription( Locale.getDefault( ), cfg.getDescription() );
159         String indexDir = cfg.getIndexDir();
160         try
161         {
162             if ( StringUtils.isEmpty( indexDir )) {
163                 repo.setIndex( false );
164                 repo.setIndexPath( null );
165             } else
166             {
167                 if ( indexDir.startsWith( "file://" ) )
168                 {
169                     repo.setIndexPath( new URI( indexDir ) );
170                 }
171                 else
172                 {
173                     repo.setIndexPath( new URI( "file://" + indexDir ) );
174                 }
175             }
176         }
177         catch ( URISyntaxException e )
178         {
179             log.error("Could not set index path "+cfg.getIndexDir());
180             repo.setIndex(false);
181         }
182         repo.setLayout( cfg.getLayout() );
183
184     }
185 }