1 package org.apache.archiva.admin.repository.admin;
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 net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.model.AuditInformation;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
25 import org.apache.archiva.admin.model.beans.FileType;
26 import org.apache.archiva.admin.model.beans.LegacyArtifactPath;
27 import org.apache.archiva.admin.model.beans.NetworkConfiguration;
28 import org.apache.archiva.admin.model.beans.OrganisationInformation;
29 import org.apache.archiva.admin.model.beans.UiConfiguration;
30 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
31 import org.apache.archiva.audit.AuditEvent;
32 import org.apache.archiva.configuration.Configuration;
33 import org.apache.archiva.configuration.UserInterfaceOptions;
34 import org.apache.archiva.configuration.WebappConfiguration;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.http.impl.conn.PoolingClientConnectionManager;
37 import org.apache.maven.wagon.providers.http.HttpWagon;
38 import org.springframework.stereotype.Service;
40 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
47 * @author Olivier Lamy
49 @Service( "archivaAdministration#default" )
50 public class DefaultArchivaAdministration
51 extends AbstractRepositoryAdmin
52 implements ArchivaAdministration
55 private PoolingClientConnectionManager poolingClientConnectionManager;
58 public void initialize()
59 throws RepositoryAdminException
61 // setup wagon on start with initial values
62 NetworkConfiguration networkConfiguration = getNetworkConfiguration();
63 setupWagon( networkConfiguration );
67 public void shutdown()
69 if ( this.poolingClientConnectionManager != null )
71 this.poolingClientConnectionManager.shutdown();
76 public List<LegacyArtifactPath> getLegacyArtifactPaths()
77 throws RepositoryAdminException
79 List<LegacyArtifactPath> legacyArtifactPaths = new ArrayList<LegacyArtifactPath>(
80 getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths().size() );
81 for ( org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath : getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths() )
83 legacyArtifactPaths.add(
84 new BeanReplicator().replicateBean( legacyArtifactPath, LegacyArtifactPath.class ) );
86 return legacyArtifactPaths;
89 public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath, AuditInformation auditInformation )
90 throws RepositoryAdminException
92 Configuration configuration = getArchivaConfiguration().getConfiguration();
94 configuration.addLegacyArtifactPath( new BeanReplicator().replicateBean( legacyArtifactPath,
95 org.apache.archiva.configuration.LegacyArtifactPath.class ) );
97 saveConfiguration( configuration );
98 triggerAuditEvent( "", "", AuditEvent.ADD_LEGACY_PATH, auditInformation );
101 public void deleteLegacyArtifactPath( String path, AuditInformation auditInformation )
102 throws RepositoryAdminException
104 Configuration configuration = getArchivaConfiguration().getConfiguration();
105 org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath =
106 new org.apache.archiva.configuration.LegacyArtifactPath();
108 legacyArtifactPath.setPath( path );
109 configuration.removeLegacyArtifactPath( legacyArtifactPath );
111 saveConfiguration( configuration );
112 triggerAuditEvent( "", "", AuditEvent.REMOVE_LEGACY_PATH, auditInformation );
116 public void addFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
117 throws RepositoryAdminException
119 Configuration configuration = getArchivaConfiguration().getConfiguration();
121 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
122 if ( fileType == null )
127 if ( fileType.getPatterns().contains( pattern ) )
129 throw new RepositoryAdminException(
130 "File type [" + fileTypeId + "] already contains pattern [" + pattern + "]" );
132 fileType.addPattern( pattern );
134 saveConfiguration( configuration );
135 triggerAuditEvent( "", "", AuditEvent.ADD_PATTERN, auditInformation );
138 public void removeFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
139 throws RepositoryAdminException
141 Configuration configuration = getArchivaConfiguration().getConfiguration();
143 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
144 if ( fileType == null )
148 fileType.removePattern( pattern );
150 saveConfiguration( configuration );
151 triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
154 public FileType getFileType( String fileTypeId )
155 throws RepositoryAdminException
157 org.apache.archiva.configuration.FileType fileType =
158 getFileTypeById( fileTypeId, getArchivaConfiguration().getConfiguration() );
159 if ( fileType == null )
163 return new BeanReplicator().replicateBean( fileType, FileType.class );
166 public void addFileType( FileType fileType, AuditInformation auditInformation )
167 throws RepositoryAdminException
169 Configuration configuration = getArchivaConfiguration().getConfiguration();
170 if ( getFileTypeById( fileType.getId(), configuration ) != null )
172 throw new RepositoryAdminException(
173 "impossible to FileType with id " + fileType.getId() + " already exists" );
176 configuration.getRepositoryScanning().addFileType(
177 new BeanReplicator().replicateBean( fileType, org.apache.archiva.configuration.FileType.class ) );
178 saveConfiguration( configuration );
181 public void removeFileType( String fileTypeId, AuditInformation auditInformation )
182 throws RepositoryAdminException
184 Configuration configuration = getArchivaConfiguration().getConfiguration();
185 org.apache.archiva.configuration.FileType fileType = new org.apache.archiva.configuration.FileType();
186 fileType.setId( fileTypeId );
187 configuration.getRepositoryScanning().removeFileType( fileType );
188 saveConfiguration( configuration );
191 public void addKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
192 throws RepositoryAdminException
194 Configuration configuration = getArchivaConfiguration().getConfiguration();
195 if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
197 log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
200 configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
201 saveConfiguration( configuration );
202 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
205 public void removeKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
206 throws RepositoryAdminException
208 Configuration configuration = getArchivaConfiguration().getConfiguration();
209 configuration.getRepositoryScanning().removeKnownContentConsumer( knownContentConsumer );
210 saveConfiguration( configuration );
211 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
214 public void addInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
215 throws RepositoryAdminException
217 Configuration configuration = getArchivaConfiguration().getConfiguration();
218 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
220 log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
223 configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
224 saveConfiguration( configuration );
225 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
228 public void removeInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
229 throws RepositoryAdminException
231 Configuration configuration = getArchivaConfiguration().getConfiguration();
232 configuration.getRepositoryScanning().removeInvalidContentConsumer( invalidContentConsumer );
233 saveConfiguration( configuration );
234 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
237 public void setKnownContentConsumers( List<String> knownContentConsumers, AuditInformation auditInformation )
238 throws RepositoryAdminException
240 if ( knownContentConsumers == null )
244 for ( String knowContentConsumer : knownContentConsumers )
246 addKnownContentConsumer( knowContentConsumer, auditInformation );
250 public void setInvalidContentConsumers( List<String> invalidContentConsumers, AuditInformation auditInformation )
251 throws RepositoryAdminException
253 if ( invalidContentConsumers == null )
257 for ( String invalidContentConsumer : invalidContentConsumers )
259 addKnownContentConsumer( invalidContentConsumer, auditInformation );
263 public List<FileType> getFileTypes()
264 throws RepositoryAdminException
266 List<org.apache.archiva.configuration.FileType> configFileTypes =
267 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getFileTypes();
268 if ( configFileTypes == null || configFileTypes.isEmpty() )
270 return Collections.emptyList();
272 List<FileType> fileTypes = new ArrayList<FileType>( configFileTypes.size() );
273 for ( org.apache.archiva.configuration.FileType fileType : configFileTypes )
275 fileTypes.add( new BeanReplicator().replicateBean( fileType, FileType.class ) );
280 public List<String> getKnownContentConsumers()
281 throws RepositoryAdminException
283 return new ArrayList<String>(
284 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getKnownContentConsumers() );
287 public List<String> getInvalidContentConsumers()
288 throws RepositoryAdminException
290 return new ArrayList<String>(
291 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getInvalidContentConsumers() );
294 public OrganisationInformation getOrganisationInformation()
295 throws RepositoryAdminException
297 org.apache.archiva.configuration.OrganisationInformation organisationInformation =
298 getArchivaConfiguration().getConfiguration().getOrganisationInfo();
299 if ( organisationInformation == null )
303 return new BeanReplicator().replicateBean( organisationInformation, OrganisationInformation.class );
306 public void setOrganisationInformation( OrganisationInformation organisationInformation )
307 throws RepositoryAdminException
309 Configuration configuration = getArchivaConfiguration().getConfiguration();
310 if ( organisationInformation != null )
312 org.apache.archiva.configuration.OrganisationInformation organisationInformationModel =
313 new BeanReplicator().replicateBean( organisationInformation,
314 org.apache.archiva.configuration.OrganisationInformation.class );
315 configuration.setOrganisationInfo( organisationInformationModel );
319 configuration.setOrganisationInfo( null );
321 saveConfiguration( configuration );
324 public UiConfiguration getUiConfiguration()
325 throws RepositoryAdminException
327 WebappConfiguration webappConfiguration = getArchivaConfiguration().getConfiguration().getWebapp();
328 if ( webappConfiguration == null )
332 UserInterfaceOptions userInterfaceOptions = webappConfiguration.getUi();
333 if ( userInterfaceOptions == null )
337 return new BeanReplicator().replicateBean( userInterfaceOptions, UiConfiguration.class );
340 public void updateUiConfiguration( UiConfiguration uiConfiguration )
341 throws RepositoryAdminException
343 Configuration configuration = getArchivaConfiguration().getConfiguration();
344 if ( uiConfiguration != null )
347 UserInterfaceOptions userInterfaceOptions =
348 new BeanReplicator().replicateBean( uiConfiguration, UserInterfaceOptions.class );
349 configuration.getWebapp().setUi( userInterfaceOptions );
353 configuration.getWebapp().setUi( null );
355 saveConfiguration( configuration );
359 public NetworkConfiguration getNetworkConfiguration()
360 throws RepositoryAdminException
362 org.apache.archiva.configuration.NetworkConfiguration networkConfiguration =
363 getArchivaConfiguration().getConfiguration().getNetworkConfiguration();
365 if ( networkConfiguration == null )
369 return new BeanReplicator().replicateBean( networkConfiguration, NetworkConfiguration.class );
372 public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
373 throws RepositoryAdminException
375 Configuration configuration = getArchivaConfiguration().getConfiguration();
376 if ( networkConfiguration == null )
378 configuration.setNetworkConfiguration( null );
382 configuration.setNetworkConfiguration( new BeanReplicator().replicateBean( networkConfiguration,
383 org.apache.archiva.configuration.NetworkConfiguration.class ) );
385 setupWagon( networkConfiguration );
386 saveConfiguration( configuration );
389 protected void setupWagon( NetworkConfiguration networkConfiguration )
391 if ( networkConfiguration == null )
393 // back to default values
394 HttpWagon.setUseClientManagerPooled( true );
395 poolingClientConnectionManager = new PoolingClientConnectionManager();
396 poolingClientConnectionManager.setDefaultMaxPerRoute( 30 );
397 poolingClientConnectionManager.setMaxTotal( 30 );
398 HttpWagon.setConnectionManagerPooled( poolingClientConnectionManager );
403 HttpWagon.setUseClientManagerPooled( networkConfiguration.isUsePooling() );
404 poolingClientConnectionManager = new PoolingClientConnectionManager();
405 poolingClientConnectionManager.setDefaultMaxPerRoute( networkConfiguration.getMaxTotalPerHost() );
406 poolingClientConnectionManager.setMaxTotal( networkConfiguration.getMaxTotal() );
407 HttpWagon.setConnectionManagerPooled( poolingClientConnectionManager );
411 //-------------------------
413 //-------------------------
415 private org.apache.archiva.configuration.FileType getFileTypeById( String id, Configuration configuration )
417 for ( org.apache.archiva.configuration.FileType fileType : configuration.getRepositoryScanning().getFileTypes() )
419 if ( StringUtils.equals( id, fileType.getId() ) )