View Javadoc
1   package com.soebes.maven.extensions;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Karl Heinz Marbaise <a href="mailto:kama@soebes.de">kama@soebes.de</a>
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 }