This project has retired. For details please refer to its Attic page.
MetadataAuditListener xref
View Javadoc
1   package org.apache.archiva.audit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.metadata.model.facets.AuditEvent;
23  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24  import org.apache.archiva.metadata.repository.RepositorySession;
25  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26  import org.apache.archiva.repository.events.AuditListener;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.stereotype.Service;
30  
31  import javax.inject.Inject;
32  
33  /**
34   *
35   */
36  @Service("auditListener#metadata")
37  public class MetadataAuditListener
38      implements AuditListener
39  {
40      private static final Logger log = LoggerFactory.getLogger( MetadataAuditListener.class );
41  
42      /**
43       *
44       */
45      @Inject
46      private AuditManager auditManager;
47  
48      /**
49       * FIXME: this could be multiple implementations and needs to be configured.
50       *  It also starts a separate session to the originator of the audit event that we may rather want to pass through.
51       */
52      @Inject
53      private RepositorySessionFactory repositorySessionFactory;
54  
55      @Override
56      public void auditEvent( AuditEvent event )
57      {
58          // for now we only log upload events, some of the others are quite noisy
59          if ( event.getAction().equals( AuditEvent.CREATE_FILE ) || event.getAction().equals( AuditEvent.UPLOAD_FILE ) ||
60              event.getAction().equals( AuditEvent.MERGING_REPOSITORIES ) )
61          {
62              RepositorySession repositorySession = repositorySessionFactory.createSession();
63              try
64              {
65                  auditManager.addAuditEvent( repositorySession.getRepository(), event );
66                  repositorySession.save();
67              }
68              catch ( MetadataRepositoryException e )
69              {
70                  log.warn( "Unable to write audit event to repository: {}", e.getMessage(), e );
71              }
72              finally
73              {
74                  repositorySession.close();
75              }
76          }
77      }
78  }