001    package org.apache.archiva.audit;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.archiva.metadata.repository.MetadataRepositoryException;
023    import org.apache.archiva.metadata.repository.RepositorySession;
024    import org.apache.archiva.metadata.repository.RepositorySessionFactory;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    import org.springframework.stereotype.Service;
028    
029    import javax.inject.Inject;
030    
031    /**
032     *
033     */
034    @Service("auditListener#metadata")
035    public class MetadataAuditListener
036        implements AuditListener
037    {
038        private static final Logger log = LoggerFactory.getLogger( MetadataAuditListener.class );
039    
040        /**
041         *
042         */
043        @Inject
044        private AuditManager auditManager;
045    
046        /**
047         * FIXME: this could be multiple implementations and needs to be configured.
048         *  It also starts a separate session to the originator of the audit event that we may rather want to pass through.
049         */
050        @Inject
051        private RepositorySessionFactory repositorySessionFactory;
052    
053        public void auditEvent( AuditEvent event )
054        {
055            // for now we only log upload events, some of the others are quite noisy
056            if ( event.getAction().equals( AuditEvent.CREATE_FILE ) || event.getAction().equals( AuditEvent.UPLOAD_FILE ) ||
057                event.getAction().equals( AuditEvent.MERGING_REPOSITORIES ) )
058            {
059                RepositorySession repositorySession = repositorySessionFactory.createSession();
060                try
061                {
062                    auditManager.addAuditEvent( repositorySession.getRepository(), event );
063                    repositorySession.save();
064                }
065                catch ( MetadataRepositoryException e )
066                {
067                    log.warn( "Unable to write audit event to repository: " + e.getMessage(), e );
068                }
069                finally
070                {
071                    repositorySession.close();
072                }
073            }
074        }
075    }