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.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   * @author Karl Heinz Marbaise <a href="mailto:kama@soebes.de">kama@soebes.de</a>
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  }