1 package org.apache.archiva.admin.repository;
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.RepositoryCommonValidator;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.archiva.audit.AuditListener;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.archiva.redback.users.User;
30 import org.apache.archiva.redback.components.registry.Registry;
31 import org.modelmapper.ModelMapper;
32 import org.modelmapper.convention.MatchingStrategies;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.util.ArrayList;
39 import java.util.List;
42 * @author Olivier Lamy
45 public abstract class AbstractRepositoryAdmin
47 protected Logger log = LoggerFactory.getLogger( getClass() );
50 private List<AuditListener> auditListeners = new ArrayList<>();
53 private RepositoryCommonValidator repositoryCommonValidator;
56 private ArchivaConfiguration archivaConfiguration;
59 @Named(value = "commons-configuration")
60 private Registry registry;
62 protected void triggerAuditEvent( String repositoryId, String resource, String action,
63 AuditInformation auditInformation )
65 User user = auditInformation == null ? null : auditInformation.getUser();
66 AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action );
67 event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
69 for ( AuditListener listener : getAuditListeners() )
71 listener.auditEvent( event );
76 protected void saveConfiguration( Configuration config )
77 throws RepositoryAdminException
81 getArchivaConfiguration().save( config );
83 catch ( org.apache.archiva.redback.components.registry.RegistryException e )
85 throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e );
87 catch ( IndeterminateConfigurationException e )
89 throw new RepositoryAdminException(
90 "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e );
94 private static class ModelMapperHolder
96 private static ModelMapper MODEL_MAPPER = new ModelMapper();
100 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
105 protected ModelMapper getModelMapper()
107 return ModelMapperHolder.MODEL_MAPPER;
110 public List<AuditListener> getAuditListeners()
112 return auditListeners;
115 public void setAuditListeners( List<AuditListener> auditListeners )
117 this.auditListeners = auditListeners;
120 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
122 this.archivaConfiguration = archivaConfiguration;
125 public ArchivaConfiguration getArchivaConfiguration()
127 return archivaConfiguration;
130 public RepositoryCommonValidator getRepositoryCommonValidator()
132 return repositoryCommonValidator;
135 public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
137 this.repositoryCommonValidator = repositoryCommonValidator;
140 public Registry getRegistry()
145 public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry )
147 this.registry = registry;