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.inject.Inject;
43 import java.io.IOException;
44 import java.net.MalformedURLException;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
51 * @author Olivier Lamy
54 @Service( "remoteRepositoryAdmin#default" )
55 public class DefaultRemoteRepositoryAdmin
56 extends AbstractRepositoryAdmin
57 implements RemoteRepositoryAdmin
61 private PlexusSisuBridge plexusSisuBridge;
64 private MavenIndexerUtils mavenIndexerUtils;
67 private void initialize()
68 throws RepositoryAdminException
70 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
72 createIndexContext( remoteRepository );
77 public List<RemoteRepository> getRemoteRepositories()
78 throws RepositoryAdminException
80 List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
81 for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
83 RemoteRepository remoteRepository =
84 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
85 repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
86 repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
87 repositoryConfiguration.getTimeout() );
88 remoteRepository.setDownloadRemoteIndex( repositoryConfiguration.isDownloadRemoteIndex() );
89 remoteRepository.setRemoteIndexUrl( repositoryConfiguration.getRemoteIndexUrl() );
90 remoteRepository.setCronExpression( repositoryConfiguration.getRefreshCronExpression() );
91 remoteRepository.setIndexDirectory( repositoryConfiguration.getIndexDir() );
92 remoteRepository.setRemoteDownloadNetworkProxyId(
93 repositoryConfiguration.getRemoteDownloadNetworkProxyId() );
94 remoteRepository.setRemoteDownloadTimeout( repositoryConfiguration.getRemoteDownloadTimeout() );
95 remoteRepositories.add( remoteRepository );
97 return remoteRepositories;
100 public RemoteRepository getRemoteRepository( String repositoryId )
101 throws RepositoryAdminException
103 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
105 if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
107 return remoteRepository;
113 public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
114 throws RepositoryAdminException
116 triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
117 getRepositoryCommonValidator().basicValidation( remoteRepository, false );
119 //TODO we can validate it's a good uri/url
120 if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
122 throw new RepositoryAdminException( "url cannot be null" );
125 //MRM-752 - url needs trimming
126 remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
128 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
129 getRemoteRepositoryConfiguration( remoteRepository );
131 Configuration configuration = getArchivaConfiguration().getConfiguration();
132 configuration.addRemoteRepository( remoteRepositoryConfiguration );
133 saveConfiguration( configuration );
138 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
139 throws RepositoryAdminException
142 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
144 Configuration configuration = getArchivaConfiguration().getConfiguration();
146 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
147 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
148 if ( remoteRepositoryConfiguration == null )
150 throw new RepositoryAdminException(
151 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
154 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
156 // TODO use ProxyConnectorAdmin interface ?
157 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
158 List<ProxyConnectorConfiguration> proxyConnectors =
159 new ArrayList<ProxyConnectorConfiguration>( configuration.getProxyConnectors() );
160 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
162 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
164 configuration.removeProxyConnector( proxyConnector );
168 saveConfiguration( configuration );
173 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
174 throws RepositoryAdminException
177 String repositoryId = remoteRepository.getId();
179 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
181 // update means : remove and add
183 Configuration configuration = getArchivaConfiguration().getConfiguration();
185 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
186 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
187 if ( remoteRepositoryConfiguration == null )
189 throw new RepositoryAdminException(
190 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
193 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
195 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
196 configuration.addRemoteRepository( remoteRepositoryConfiguration );
197 saveConfiguration( configuration );
202 public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
203 throws RepositoryAdminException
205 java.util.Map<String, RemoteRepository> map = new HashMap<String, RemoteRepository>();
207 for ( RemoteRepository repo : getRemoteRepositories() )
209 map.put( repo.getId(), repo );
215 public IndexingContext createIndexContext( RemoteRepository remoteRepository )
216 throws RepositoryAdminException
220 // FIXME get this from ArchivaAdministration
221 String appServerBase = System.getProperty( "appserver.base" );
223 List<? extends IndexCreator> indexCreators = mavenIndexerUtils.getAllIndexCreators();
224 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
226 String contextKey = "remote-" + remoteRepository.getId();
227 IndexingContext indexingContext = nexusIndexer.getIndexingContexts().get( contextKey );
228 if ( indexingContext != null )
230 return indexingContext;
233 File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
234 if ( !repoDir.exists() )
238 File indexDirectory = new File( repoDir, ".index" );
239 if ( !indexDirectory.exists() )
241 indexDirectory.mkdirs();
243 return nexusIndexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
244 remoteRepository.getUrl(),
245 calculateIndexRemoteUrl( remoteRepository ),
246 mavenIndexerUtils.getAllIndexCreators() );
248 catch ( MalformedURLException e )
250 throw new RepositoryAdminException( e.getMessage(), e );
252 catch ( IOException e )
254 throw new RepositoryAdminException( e.getMessage(), e );
256 catch ( PlexusSisuBridgeException e )
258 throw new RepositoryAdminException( e.getMessage(), e );
260 catch ( UnsupportedExistingLuceneIndexException e )
262 throw new RepositoryAdminException( e.getMessage(), e );
267 protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
269 if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
271 String baseUrl = remoteRepository.getRemoteIndexUrl();
272 return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
274 String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
275 remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
277 baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
278 ? baseUrl + "/.index"
279 : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
284 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
286 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
287 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
288 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
289 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
290 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
291 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
292 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
293 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
294 remoteRepositoryConfiguration.setDownloadRemoteIndex( remoteRepository.isDownloadRemoteIndex() );
295 remoteRepositoryConfiguration.setRemoteIndexUrl( remoteRepository.getRemoteIndexUrl() );
296 remoteRepositoryConfiguration.setRefreshCronExpression( remoteRepository.getCronExpression() );
297 remoteRepositoryConfiguration.setIndexDir( remoteRepository.getIndexDirectory() );
298 remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
299 remoteRepository.getRemoteDownloadNetworkProxyId() );
300 remoteRepositoryConfiguration.setRemoteDownloadTimeout( remoteRepository.getRemoteDownloadTimeout() );
301 return remoteRepositoryConfiguration;