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 List<? extends IndexCreator> indexCreators;
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 // close index on shutdown
98 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
100 IndexingContext context = indexer.getIndexingContexts().get( remoteRepository.getId() );
101 if ( context != null )
103 indexer.removeIndexingContext( context, false );
107 catch ( IOException e )
109 throw new RepositoryAdminException( e.getMessage(), e );
114 public List<RemoteRepository> getRemoteRepositories()
115 throws RepositoryAdminException
117 List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>(
118 getArchivaConfiguration().getConfiguration().getRemoteRepositories().size() );
119 for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
121 RemoteRepository remoteRepository =
122 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
123 repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
124 repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
125 repositoryConfiguration.getTimeout() );
126 remoteRepository.setDownloadRemoteIndex( repositoryConfiguration.isDownloadRemoteIndex() );
127 remoteRepository.setRemoteIndexUrl( repositoryConfiguration.getRemoteIndexUrl() );
128 remoteRepository.setCronExpression( repositoryConfiguration.getRefreshCronExpression() );
129 remoteRepository.setIndexDirectory( repositoryConfiguration.getIndexDir() );
130 remoteRepository.setRemoteDownloadNetworkProxyId(
131 repositoryConfiguration.getRemoteDownloadNetworkProxyId() );
132 remoteRepository.setRemoteDownloadTimeout( repositoryConfiguration.getRemoteDownloadTimeout() );
133 remoteRepository.setDownloadRemoteIndexOnStartup(
134 repositoryConfiguration.isDownloadRemoteIndexOnStartup() );
135 remoteRepositories.add( remoteRepository );
137 return remoteRepositories;
140 public RemoteRepository getRemoteRepository( String repositoryId )
141 throws RepositoryAdminException
143 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
145 if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
147 return remoteRepository;
153 public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
154 throws RepositoryAdminException
156 triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
157 getRepositoryCommonValidator().basicValidation( remoteRepository, false );
159 //TODO we can validate it's a good uri/url
160 if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
162 throw new RepositoryAdminException( "url cannot be null" );
165 //MRM-752 - url needs trimming
166 remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
168 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
169 getRemoteRepositoryConfiguration( remoteRepository );
171 Configuration configuration = getArchivaConfiguration().getConfiguration();
172 configuration.addRemoteRepository( remoteRepositoryConfiguration );
173 saveConfiguration( configuration );
178 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
179 throws RepositoryAdminException
182 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
184 Configuration configuration = getArchivaConfiguration().getConfiguration();
186 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
187 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
188 if ( remoteRepositoryConfiguration == null )
190 throw new RepositoryAdminException(
191 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
194 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
196 // TODO use ProxyConnectorAdmin interface ?
197 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
198 List<ProxyConnectorConfiguration> proxyConnectors =
199 new ArrayList<ProxyConnectorConfiguration>( configuration.getProxyConnectors() );
200 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
202 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
204 configuration.removeProxyConnector( proxyConnector );
208 saveConfiguration( configuration );
213 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
214 throws RepositoryAdminException
217 String repositoryId = remoteRepository.getId();
219 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
221 // update means : remove and add
223 Configuration configuration = getArchivaConfiguration().getConfiguration();
225 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
226 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
227 if ( remoteRepositoryConfiguration == null )
229 throw new RepositoryAdminException(
230 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
233 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
235 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
236 configuration.addRemoteRepository( remoteRepositoryConfiguration );
237 saveConfiguration( configuration );
242 public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
243 throws RepositoryAdminException
245 java.util.Map<String, RemoteRepository> map = new HashMap<String, RemoteRepository>();
247 for ( RemoteRepository repo : getRemoteRepositories() )
249 map.put( repo.getId(), repo );
255 public IndexingContext createIndexContext( RemoteRepository remoteRepository )
256 throws RepositoryAdminException
260 // FIXME get this from ArchivaAdministration
261 String appServerBase = System.getProperty( "appserver.base" );
263 String contextKey = "remote-" + remoteRepository.getId();
264 IndexingContext indexingContext = indexer.getIndexingContexts().get( contextKey );
265 if ( indexingContext != null )
267 return indexingContext;
270 File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
271 if ( !repoDir.exists() )
275 File indexDirectory = new File( repoDir, ".index" );
276 if ( !indexDirectory.exists() )
278 indexDirectory.mkdirs();
280 return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
281 remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
282 mavenIndexerUtils.getAllIndexCreators() );
284 catch ( MalformedURLException e )
286 throw new RepositoryAdminException( e.getMessage(), e );
288 catch ( IOException e )
290 throw new RepositoryAdminException( e.getMessage(), e );
292 catch ( UnsupportedExistingLuceneIndexException e )
294 throw new RepositoryAdminException( e.getMessage(), e );
299 protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
301 if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
303 String baseUrl = remoteRepository.getRemoteIndexUrl();
304 return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
306 String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
307 remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
309 baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
310 ? baseUrl + "/.index"
311 : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
316 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
318 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
319 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
320 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
321 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
322 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
323 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
324 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
325 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
326 remoteRepositoryConfiguration.setDownloadRemoteIndex( remoteRepository.isDownloadRemoteIndex() );
327 remoteRepositoryConfiguration.setRemoteIndexUrl( remoteRepository.getRemoteIndexUrl() );
328 remoteRepositoryConfiguration.setRefreshCronExpression( remoteRepository.getCronExpression() );
329 remoteRepositoryConfiguration.setIndexDir( remoteRepository.getIndexDirectory() );
330 remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
331 remoteRepository.getRemoteDownloadNetworkProxyId() );
332 remoteRepositoryConfiguration.setRemoteDownloadTimeout( remoteRepository.getRemoteDownloadTimeout() );
333 remoteRepositoryConfiguration.setDownloadRemoteIndexOnStartup(
334 remoteRepository.isDownloadRemoteIndexOnStartup() );
335 return remoteRepositoryConfiguration;