1 package com.soebes.maven.extensions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.apache.maven.execution.ExecutionEvent;
27 import org.apache.maven.project.MavenProject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34 class ProjectTimer
35 {
36 private final Logger LOGGER = LoggerFactory.getLogger( getClass() );
37
38 private Map<String, SystemTime> timerEvents;
39
40 public ProjectTimer()
41 {
42 this.timerEvents = new ConcurrentHashMap<>();
43 }
44
45 private String getProjectId( MavenProject mavenProject )
46 {
47 return mavenProject.getId();
48 }
49
50 public void projectStart( ExecutionEvent event )
51 {
52 String projectId = getProjectId( event.getProject() );
53 timerEvents.put( projectId, new SystemTime().start() );
54 }
55
56 public void projectStop( ExecutionEvent event )
57 {
58 String projectId = getProjectId( event.getProject() );
59 if ( !timerEvents.containsKey( projectId ) )
60 {
61 throw new IllegalArgumentException( "Unknown projectId (" + projectId + ")" );
62 }
63 timerEvents.get( projectId ).stop();
64 }
65
66 public long getTimeForProject( MavenProject project )
67 {
68 String projectId = getProjectId( project );
69 if ( !timerEvents.containsKey( projectId ) )
70 {
71 throw new IllegalArgumentException( "Unknown projectId (" + projectId + ")" );
72 }
73 return timerEvents.get( projectId ).getElapsedTime();
74 }
75
76 public void report()
77 {
78 for ( Entry<String, SystemTime> item : this.timerEvents.entrySet() )
79 {
80 LOGGER.info( "ProjectTimer: {} : {}", item.getKey(), item.getValue().getElapsedTime() );
81 }
82 }
83 }