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.LinkedHashMap;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import org.apache.maven.execution.ExecutionEvent;
28 import org.apache.maven.plugin.MojoExecution;
29 import org.apache.maven.project.MavenProject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36 class MojoTimer
37 {
38 private final Logger LOGGER = LoggerFactory.getLogger( getClass() );
39
40 private Map<ProjectMojo, SystemTime> timerEvents;
41
42 public MojoTimer()
43 {
44 this.timerEvents = new ConcurrentHashMap<>();
45 }
46
47 private ProjectKey createProjectKey( MavenProject project )
48 {
49 return new ProjectKey( project.getGroupId(), project.getArtifactId(), project.getVersion() );
50 }
51
52 private MojoKey createMojoKey( MojoExecution mojo )
53 {
54 return new MojoKey( mojo.getGroupId(), mojo.getArtifactId(), mojo.getVersion(), mojo.getGoal(),
55 mojo.getExecutionId(), mojo.getLifecyclePhase() );
56 }
57
58 public boolean hasEvents()
59 {
60 return !this.timerEvents.isEmpty();
61 }
62
63 public void mojoStart( ExecutionEvent event )
64 {
65
66 ProjectMojo pm =
67 new ProjectMojo( createProjectKey( event.getProject() ), createMojoKey( event.getMojoExecution() ) );
68 timerEvents.put( pm, new SystemTime().start() );
69 }
70
71 public void mojoStop( ExecutionEvent event )
72 {
73 ProjectMojo pm =
74 new ProjectMojo( createProjectKey( event.getProject() ), createMojoKey( event.getMojoExecution() ) );
75 if ( !timerEvents.containsKey( pm ) )
76 {
77 throw new IllegalArgumentException( "Unknown mojoId (" + pm + ")" );
78 }
79 timerEvents.get( pm ).stop();
80 }
81
82 public long getTimeForPhaseInMillis( String phase )
83 {
84 long result = 0;
85
86 for ( Entry<ProjectMojo, SystemTime> item : this.timerEvents.entrySet() )
87 {
88 if ( phase.equals( item.getKey().getMojo().getPhase() ) )
89 {
90 result += item.getValue().getElapsedTime();
91 }
92 }
93 return result;
94 }
95
96 public Map<ProjectMojo, SystemTime> getPluginsInPhase( String phase )
97 {
98 Map<ProjectMojo, SystemTime> result = new LinkedHashMap<ProjectMojo, SystemTime>();
99
100 for ( Entry<ProjectMojo, SystemTime> item : this.timerEvents.entrySet() )
101 {
102 if ( phase.equals( item.getKey().getMojo().getPhase() ) )
103 {
104 result.put( item.getKey(), item.getValue() );
105 }
106 }
107 return result;
108 }
109
110 public boolean hasTimeForProjectAndPhase( ProjectKey proKey, String phase )
111 {
112 boolean result = false;
113 for ( Entry<ProjectMojo, SystemTime> item : this.timerEvents.entrySet() )
114 {
115 if ( item.getKey().getProject().equals( proKey ) && phase.equals( item.getKey().getMojo().getPhase() ) )
116 {
117 result = true;
118 }
119 }
120 return result;
121 }
122
123 public long getTimeForProjectAndPhaseInMillis( ProjectKey proKey, String phase )
124 {
125 long result = 0;
126
127 for ( Entry<ProjectMojo, SystemTime> item : this.timerEvents.entrySet() )
128 {
129 if ( item.getKey().getProject().equals( proKey ) && phase.equals( item.getKey().getMojo().getPhase() ) )
130 {
131 result += item.getValue().getElapsedTime();
132 }
133 }
134
135 return result;
136 }
137
138 public void report()
139 {
140 for ( Entry<ProjectMojo, SystemTime> item : this.timerEvents.entrySet() )
141 {
142 LOGGER.info( "{} : {}", item.getKey().getId(), item.getValue().getElapsedTime() );
143 }
144 }
145 }