1 package org.apache.archiva.admin.repository.remote;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.audit.AuditEvent;
27 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.maven.index.NexusIndexer;
35 import org.apache.maven.index.context.IndexCreator;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
38 import org.springframework.stereotype.Service;
40 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy;
42 import javax.inject.Inject;
44 import java.io.IOException;
45 import java.net.MalformedURLException;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
52 * @author Olivier Lamy
55 @Service("remoteRepositoryAdmin#default")
56 public class DefaultRemoteRepositoryAdmin
57 extends AbstractRepositoryAdmin
58 implements RemoteRepositoryAdmin
62 private PlexusSisuBridge plexusSisuBridge;
65 private MavenIndexerUtils mavenIndexerUtils;
68 private List<? extends IndexCreator> indexCreators;
70 private NexusIndexer indexer;
73 private void initialize()
74 throws RepositoryAdminException
78 indexCreators = mavenIndexerUtils.getAllIndexCreators();
79 indexer = plexusSisuBridge.lookup( NexusIndexer.class );
81 catch ( PlexusSisuBridgeException e )
83 throw new RepositoryAdminException( e.getMessage(), e );
85 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
87 createIndexContext( remoteRepository );
92 private void shutdown()
93 throws RepositoryAdminException
97 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
98 // close index on shutdown
99 for ( RemoteRepository remoteRepository : remoteRepositories )
101 IndexingContext context = indexer.getIndexingContexts().get( remoteRepository.getId() );
102 if ( context != null )
104 indexer.removeIndexingContext( context, false );
108 catch ( IOException e )
110 throw new RepositoryAdminException( e.getMessage(), e );
116 public List<RemoteRepository> getRemoteRepositories()
117 throws RepositoryAdminException
119 List<RemoteRepository> remoteRepositories =
120 new ArrayList<>( getArchivaConfiguration().getConfiguration().getRemoteRepositories().size() );
121 for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
123 RemoteRepository remoteRepository =
124 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
125 repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
126 repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
127 repositoryConfiguration.getTimeout() );
128 remoteRepository.setDownloadRemoteIndex( repositoryConfiguration.isDownloadRemoteIndex() );
129 remoteRepository.setRemoteIndexUrl( repositoryConfiguration.getRemoteIndexUrl() );
130 remoteRepository.setCronExpression( repositoryConfiguration.getRefreshCronExpression() );
131 remoteRepository.setIndexDirectory( repositoryConfiguration.getIndexDir() );
132 remoteRepository.setRemoteDownloadNetworkProxyId(
133 repositoryConfiguration.getRemoteDownloadNetworkProxyId() );
134 remoteRepository.setRemoteDownloadTimeout( repositoryConfiguration.getRemoteDownloadTimeout() );
135 remoteRepository.setDownloadRemoteIndexOnStartup(
136 repositoryConfiguration.isDownloadRemoteIndexOnStartup() );
137 remoteRepository.setDescription( repositoryConfiguration.getDescription() );
138 remoteRepository.setExtraHeaders( repositoryConfiguration.getExtraHeaders() );
139 remoteRepository.setExtraParameters( repositoryConfiguration.getExtraParameters() );
140 remoteRepositories.add( remoteRepository );
142 return remoteRepositories;
146 public RemoteRepository getRemoteRepository( String repositoryId )
147 throws RepositoryAdminException
149 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
151 if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
153 return remoteRepository;
160 public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
161 throws RepositoryAdminException
163 triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
164 getRepositoryCommonValidator().basicValidation( remoteRepository, false );
166 //TODO we can validate it's a good uri/url
167 if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
169 throw new RepositoryAdminException( "url cannot be null" );
172 //MRM-752 - url needs trimming
173 remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
175 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
176 getRemoteRepositoryConfiguration( remoteRepository );
178 Configuration configuration = getArchivaConfiguration().getConfiguration();
179 configuration.addRemoteRepository( remoteRepositoryConfiguration );
180 saveConfiguration( configuration );
186 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
187 throws RepositoryAdminException
190 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
192 Configuration configuration = getArchivaConfiguration().getConfiguration();
194 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
195 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
196 if ( remoteRepositoryConfiguration == null )
198 throw new RepositoryAdminException(
199 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
202 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
204 // TODO use ProxyConnectorAdmin interface ?
205 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
206 List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( configuration.getProxyConnectors() );
207 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
209 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
211 configuration.removeProxyConnector( proxyConnector );
215 saveConfiguration( configuration );
221 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
222 throws RepositoryAdminException
225 String repositoryId = remoteRepository.getId();
227 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
229 // update means : remove and add
231 Configuration configuration = getArchivaConfiguration().getConfiguration();
233 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
234 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
235 if ( remoteRepositoryConfiguration == null )
237 throw new RepositoryAdminException(
238 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
241 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
243 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
244 configuration.addRemoteRepository( remoteRepositoryConfiguration );
245 saveConfiguration( configuration );
251 public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
252 throws RepositoryAdminException
254 java.util.Map<String, RemoteRepository> map = new HashMap<>();
256 for ( RemoteRepository repo : getRemoteRepositories() )
258 map.put( repo.getId(), repo );
265 public IndexingContext createIndexContext( RemoteRepository remoteRepository )
266 throws RepositoryAdminException
270 String appServerBase = getRegistry().getString( "appserver.base" );
272 String contextKey = "remote-" + remoteRepository.getId();
273 IndexingContext indexingContext = indexer.getIndexingContexts().get( contextKey );
274 if ( indexingContext != null )
276 return indexingContext;
278 // create remote repository path
279 File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
280 if ( !repoDir.exists() )
285 File indexDirectory = null;
287 // is there configured indexDirectory ?
288 String indexDirectoryPath = remoteRepository.getIndexDirectory();
290 if ( StringUtils.isNotBlank( indexDirectoryPath ) )
292 if ( new File( indexDirectoryPath ).isAbsolute() )
294 indexDirectory = new File( indexDirectoryPath );
298 indexDirectory = new File( repoDir, indexDirectoryPath );
301 // if not configured use a default value
302 if ( indexDirectory == null )
304 indexDirectory = new File( repoDir, ".index" );
306 if ( !indexDirectory.exists() )
308 indexDirectory.mkdirs();
310 return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
311 remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
312 mavenIndexerUtils.getAllIndexCreators() );
314 catch ( MalformedURLException e )
316 throw new RepositoryAdminException( e.getMessage(), e );
318 catch ( IOException e )
320 throw new RepositoryAdminException( e.getMessage(), e );
322 catch ( UnsupportedExistingLuceneIndexException e )
324 throw new RepositoryAdminException( e.getMessage(), e );
329 protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
331 if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
333 String baseUrl = remoteRepository.getRemoteIndexUrl();
334 return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
336 String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
337 remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
339 baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
340 ? baseUrl + "/.index"
341 : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
346 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
348 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
349 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
350 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
351 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
352 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
353 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
354 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
355 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
356 remoteRepositoryConfiguration.setDownloadRemoteIndex( remoteRepository.isDownloadRemoteIndex() );
357 remoteRepositoryConfiguration.setRemoteIndexUrl( remoteRepository.getRemoteIndexUrl() );
358 remoteRepositoryConfiguration.setRefreshCronExpression( remoteRepository.getCronExpression() );
359 remoteRepositoryConfiguration.setIndexDir( remoteRepository.getIndexDirectory() );
360 remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
361 remoteRepository.getRemoteDownloadNetworkProxyId() );
362 remoteRepositoryConfiguration.setRemoteDownloadTimeout( remoteRepository.getRemoteDownloadTimeout() );
363 remoteRepositoryConfiguration.setDownloadRemoteIndexOnStartup(
364 remoteRepository.isDownloadRemoteIndexOnStartup() );
365 remoteRepositoryConfiguration.setDescription( remoteRepository.getDescription() );
366 remoteRepositoryConfiguration.setExtraHeaders( remoteRepository.getExtraHeaders() );
367 remoteRepositoryConfiguration.setExtraParameters( remoteRepository.getExtraParameters() );
368 return remoteRepositoryConfiguration;