This project has retired. For details please refer to its Attic page.
AbstractRepositoryAdmin xref
View Javadoc
1   package org.apache.archiva.admin.repository;
2   /*
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
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
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
18   * under the License.
19   */
20  
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.configuration.ArchivaConfiguration;
25  import org.apache.archiva.configuration.Configuration;
26  import org.apache.archiva.configuration.IndeterminateConfigurationException;
27  import org.apache.archiva.metadata.model.facets.AuditEvent;
28  import org.apache.archiva.redback.users.User;
29  import org.apache.archiva.redback.components.registry.Registry;
30  import org.apache.archiva.repository.events.AuditListener;
31  import org.modelmapper.ModelMapper;
32  import org.modelmapper.convention.MatchingStrategies;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.springframework.beans.factory.annotation.Autowired;
36  
37  import javax.inject.Inject;
38  import javax.inject.Named;
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * @author Olivier Lamy
44   * @since 1.4-M1
45   */
46  public abstract class AbstractRepositoryAdmin
47  {
48      protected Logger log = LoggerFactory.getLogger( getClass() );
49  
50      @Inject
51      @Autowired(required = false)
52      private List<AuditListener> auditListeners = new ArrayList<>();
53  
54      @Inject
55      private RepositoryCommonValidator repositoryCommonValidator;
56  
57      @Inject
58      private ArchivaConfiguration archivaConfiguration;
59  
60      @Inject
61      @Named(value = "commons-configuration")
62      private Registry registry;
63  
64      protected void triggerAuditEvent( String repositoryId, String resource, String action,
65                                        AuditInformation auditInformation )
66      {
67          User user = auditInformation == null ? null : auditInformation.getUser();
68          AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action );
69          event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
70  
71          for ( AuditListener listener : getAuditListeners() )
72          {
73              listener.auditEvent( event );
74          }
75  
76      }
77  
78      protected void saveConfiguration( Configuration config )
79          throws RepositoryAdminException
80      {
81          try
82          {
83              getArchivaConfiguration().save( config );
84          }
85          catch ( org.apache.archiva.redback.components.registry.RegistryException e )
86          {
87              throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e );
88          }
89          catch ( IndeterminateConfigurationException e )
90          {
91              throw new RepositoryAdminException(
92                  "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e );
93          }
94      }
95  
96      private static class ModelMapperHolder
97      {
98          private static ModelMapper MODEL_MAPPER = new ModelMapper();
99  
100         static
101         {
102             MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
103         }
104 
105     }
106 
107     protected ModelMapper getModelMapper()
108     {
109         return ModelMapperHolder.MODEL_MAPPER;
110     }
111 
112     public List<AuditListener> getAuditListeners()
113     {
114         return auditListeners;
115     }
116 
117     public void setAuditListeners( List<AuditListener> auditListeners )
118     {
119         this.auditListeners = auditListeners;
120     }
121 
122     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
123     {
124         this.archivaConfiguration = archivaConfiguration;
125     }
126 
127     public ArchivaConfiguration getArchivaConfiguration()
128     {
129         return archivaConfiguration;
130     }
131 
132     public RepositoryCommonValidator getRepositoryCommonValidator()
133     {
134         return repositoryCommonValidator;
135     }
136 
137     public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
138     {
139         this.repositoryCommonValidator = repositoryCommonValidator;
140     }
141 
142     public Registry getRegistry()
143     {
144         return registry;
145     }
146 
147     public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry )
148     {
149         this.registry = registry;
150     }
151 }