1package org.apache.archiva.audit;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.metadata.model.facets.AuditEvent;
23import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24import org.apache.archiva.metadata.repository.RepositorySession;
25import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26import org.apache.archiva.repository.events.AuditListener;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29import org.springframework.stereotype.Service;
3031import javax.inject.Inject;
3233/**34 *35 */36 @Service("auditListener#metadata")
37publicclassMetadataAuditListener38implementsAuditListener39 {
40privatestaticfinal Logger log = LoggerFactory.getLogger( MetadataAuditListener.class );
4142/**43 *44 */45 @Inject
46privateAuditManager auditManager;
4748/**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
53privateRepositorySessionFactory repositorySessionFactory;
5455 @Override
56publicvoid auditEvent( AuditEvent event )
57 {
58// for now we only log upload events, some of the others are quite noisy59if ( event.getAction().equals( AuditEvent.CREATE_FILE ) || event.getAction().equals( AuditEvent.UPLOAD_FILE ) ||
60 event.getAction().equals( AuditEvent.MERGING_REPOSITORIES ) )
61 {
62RepositorySession repositorySession = repositorySessionFactory.createSession();
63try64 {
65 auditManager.addAuditEvent( repositorySession.getRepository(), event );
66 repositorySession.save();
67 }
68catch ( MetadataRepositoryException e )
69 {
70 log.warn( "Unable to write audit event to repository: {}", e.getMessage(), e );
71 }
72finally73 {
74 repositorySession.close();
75 }
76 }
77 }
78 }