You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MetadataAuditListener.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package org.apache.archiva.audit;
  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. import org.apache.archiva.metadata.model.facets.AuditEvent;
  21. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  22. import org.apache.archiva.metadata.repository.RepositorySession;
  23. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  24. import org.apache.archiva.repository.events.AuditListener;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.stereotype.Service;
  28. import javax.inject.Inject;
  29. /**
  30. *
  31. */
  32. @Service("auditListener#metadata")
  33. public class MetadataAuditListener
  34. implements AuditListener
  35. {
  36. private static final Logger log = LoggerFactory.getLogger( MetadataAuditListener.class );
  37. /**
  38. *
  39. */
  40. @Inject
  41. private AuditManager auditManager;
  42. /**
  43. * FIXME: this could be multiple implementations and needs to be configured.
  44. * It also starts a separate session to the originator of the audit event that we may rather want to pass through.
  45. */
  46. @Inject
  47. private RepositorySessionFactory repositorySessionFactory;
  48. @Override
  49. public void auditEvent( AuditEvent event )
  50. {
  51. // for now we only log upload events, some of the others are quite noisy
  52. if ( event.getAction().equals( AuditEvent.CREATE_FILE ) || event.getAction().equals( AuditEvent.UPLOAD_FILE ) ||
  53. event.getAction().equals( AuditEvent.MERGING_REPOSITORIES ) )
  54. {
  55. RepositorySession repositorySession = repositorySessionFactory.createSession();
  56. try
  57. {
  58. auditManager.addAuditEvent( repositorySession.getRepository(), event );
  59. repositorySession.save();
  60. }
  61. catch ( MetadataRepositoryException e )
  62. {
  63. log.warn( "Unable to write audit event to repository: {}", e.getMessage(), e );
  64. }
  65. finally
  66. {
  67. repositorySession.close();
  68. }
  69. }
  70. }
  71. }