1 package com.soebes.maven.extensions.artifact;
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.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
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
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 }