View Javadoc
1   package com.soebes.maven.extensions.artifact;
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.concurrent.ConcurrentHashMap;
24  
25  import org.eclipse.aether.RepositoryEvent;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import com.soebes.maven.extensions.TimePlusSize;
31  
32  /**
33   * @author Karl Heinz Marbaise <a href="mailto:kama@soebes.de">kama@soebes.de</a>
34   */
35  public abstract class AbstractArtifactTimer
36  {
37      private final Logger LOGGER = LoggerFactory.getLogger( getClass() );
38  
39      private Map<String, TimePlusSize> timerEvents;
40  
41      public AbstractArtifactTimer()
42      {
43          this.timerEvents = new ConcurrentHashMap<>();
44      }
45  
46      protected String getArtifactId( Artifact artifact )
47      {
48          StringBuilder sb = new StringBuilder( 128 );
49          sb.append( artifact.getGroupId() ) //
50            .append( ":" ).append( artifact.getArtifactId() ) //
51            .append( ":" ).append( artifact.getVersion() );
52  
53          if ( artifact.getClassifier() != null )
54          {
55              sb.append( ':' ).append( artifact.getClassifier() );
56          }
57          
58          sb.append( ':' ).append( artifact.getExtension() );
59          return sb.toString();
60      }
61  
62      protected Map<String, TimePlusSize> getTimerEvents()
63      {
64          return timerEvents;
65      }
66  
67      public void start( RepositoryEvent event )
68      {
69          String artifactId = getArtifactId( event.getArtifact() );
70          TimePlusSize systemTime = new TimePlusSize();
71          systemTime.start();
72          getTimerEvents().put( artifactId, systemTime );
73      }
74  
75      public void stop( RepositoryEvent event )
76      {
77          String artifactId = getArtifactId( event.getArtifact() );
78          if ( !getTimerEvents().containsKey( artifactId ) )
79          {
80              throw new IllegalArgumentException( "Unknown artifactId (" + artifactId + ")" );
81          }
82          getTimerEvents().get( artifactId ).stop();
83  
84          long size = 0;
85          // This could happen if an artifact could not be found for download (like site_..xml etc.)
86          if ( event.getArtifact().getFile() != null )
87          {
88              size = event.getArtifact().getFile().length();
89          }
90          getTimerEvents().get( artifactId ).setSize( size );
91      }
92  
93      private final double MiB = 1024 * 1024;
94  
95      protected double calculateMegabytesPerSeconds( long timeInMilliseconds, long sizeInBytes )
96      {
97          double dividerTime = ( timeInMilliseconds / 1000.0 );
98          return (double) sizeInBytes / dividerTime / MiB;
99      }
100 
101 }