This project has retired. For details please refer to its
Attic page.
DefaultAuditManager xref
1 package org.apache.archiva.audit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.metadata.model.facets.AuditEvent;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Service;
28
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.Comparator;
35 import java.util.Date;
36 import java.util.List;
37 import java.util.TimeZone;
38
39
40
41
42 @Service("auditManager#default")
43 public class DefaultAuditManager
44 implements AuditManager
45 {
46 private static final int NUM_RECENT_EVENTS = 10;
47
48 private static final Logger log = LoggerFactory.getLogger( DefaultAuditManager.class );
49
50 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
51
52 @Override
53 public List<AuditEvent> getMostRecentAuditEvents( MetadataRepository metadataRepository,
54 List<String> repositoryIds )
55 throws MetadataRepositoryException
56 {
57
58 List<AuditRecord> records = new ArrayList<>();
59 for ( String repositoryId : repositoryIds )
60 {
61 List<String> names = metadataRepository.getMetadataFacets( repositoryId, AuditEvent.FACET_ID );
62 for ( String name : names )
63 {
64 records.add( new AuditRecord( repositoryId, name ) );
65 }
66 }
67 Collections.sort( records );
68 records = records.subList( 0, records.size() < NUM_RECENT_EVENTS ? records.size() : NUM_RECENT_EVENTS );
69
70 List<AuditEvent> events = new ArrayList<>( records.size() );
71 for ( AuditRecord record : records )
72 {
73 AuditEvent auditEvent = (AuditEvent) metadataRepository.getMetadataFacet( record.repositoryId,
74 AuditEvent.FACET_ID,
75 record.name );
76 events.add( auditEvent );
77 }
78 return events;
79 }
80
81 @Override
82 public void addAuditEvent( MetadataRepository repository, AuditEvent event )
83 throws MetadataRepositoryException
84 {
85
86 if ( event.getRepositoryId() != null )
87 {
88 repository.addMetadataFacet( event.getRepositoryId(), event );
89 }
90 }
91
92 @Override
93 public void deleteAuditEvents( MetadataRepository metadataRepository, String repositoryId )
94 throws MetadataRepositoryException
95 {
96 metadataRepository.removeMetadataFacets( repositoryId, AuditEvent.FACET_ID );
97 }
98
99 @Override
100 public List<AuditEvent> getAuditEventsInRange( MetadataRepository metadataRepository,
101 Collection<String> repositoryIds, Date startTime, Date endTime )
102 throws MetadataRepositoryException
103 {
104 return getAuditEventsInRange( metadataRepository, repositoryIds, null, startTime, endTime );
105 }
106
107 @Override
108 public List<AuditEvent> getAuditEventsInRange( MetadataRepository metadataRepository,
109 Collection<String> repositoryIds, String resource, Date startTime,
110 Date endTime )
111 throws MetadataRepositoryException
112 {
113 List<AuditEvent> results = new ArrayList<>();
114 for ( String repositoryId : repositoryIds )
115 {
116 List<String> list = metadataRepository.getMetadataFacets( repositoryId, AuditEvent.FACET_ID );
117 for ( String name : list )
118 {
119 try
120 {
121 Date date = createNameFormat().parse( name );
122 if ( ( startTime == null || !date.before( startTime ) ) && ( endTime == null || !date.after(
123 endTime ) ) )
124 {
125 AuditEvent event = (AuditEvent) metadataRepository.getMetadataFacet( repositoryId,
126 AuditEvent.FACET_ID,
127 name );
128
129 if ( resource == null || event.getResource().startsWith( resource ) )
130 {
131 results.add( event );
132 }
133 }
134 }
135 catch ( ParseException e )
136 {
137 log.error( "Invalid audit event found in the metadata repository: " + e.getMessage() );
138
139 }
140 }
141 }
142 Collections.sort( results, new Comparator<AuditEvent>()
143 {
144 @Override
145 public int compare( AuditEvent o1, AuditEvent o2 )
146 {
147 return o2.getTimestamp().compareTo( o1.getTimestamp() );
148 }
149 } );
150 return results;
151 }
152
153 private static SimpleDateFormat createNameFormat()
154 {
155 SimpleDateFormat fmt = new SimpleDateFormat( AuditEvent.TIMESTAMP_FORMAT );
156 fmt.setTimeZone( UTC_TIME_ZONE );
157 return fmt;
158 }
159
160 private static final class AuditRecord
161 implements Comparable<AuditRecord>
162 {
163 private String repositoryId;
164
165 private String name;
166
167 public AuditRecord( String repositoryId, String name )
168 {
169 this.repositoryId = repositoryId;
170 this.name = name;
171 }
172
173 @Override
174 public int compareTo( AuditRecord other )
175 {
176
177 return other.name.compareTo( name );
178 }
179 }
180 }