View Javadoc
1   package com.soebes.maven.extensions.metadata;
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.metadata.Metadata;
27  
28  import com.soebes.maven.extensions.TimePlusSize;
29  
30  /**
31   * @author Karl Heinz Marbaise <a href="mailto:kama@soebes.de">kama@soebes.de</a>
32   */
33  public abstract class AbstractMetadataTimer
34  {
35      private Map<String, TimePlusSize> timerEvents;
36  
37      public AbstractMetadataTimer()
38      {
39          this.timerEvents = new ConcurrentHashMap<>();
40      }
41  
42      protected String getArtifactId( Metadata artifact )
43      {
44          StringBuilder sb = new StringBuilder( 128 );
45          sb.append( artifact.getGroupId() ) //
46            .append( ":" ).append( artifact.getArtifactId() ) //
47            .append( ":" ).append( artifact.getVersion() ) //
48            .append( ':' ).append( artifact.getType() ) //
49            .append( ':' ).append( artifact.getNature() );
50  
51          return sb.toString();
52      }
53  
54      protected Map<String, TimePlusSize> getTimerEvents()
55      {
56          return timerEvents;
57      }
58  
59      public void start( RepositoryEvent event )
60      {
61          String metadataId = getArtifactId( event.getMetadata() );
62          TimePlusSize systemTime = new TimePlusSize();
63          systemTime.start();
64          getTimerEvents().put( metadataId, systemTime );
65      }
66  
67      public void stop( RepositoryEvent event )
68      {
69          String metadataId = getArtifactId( event.getMetadata() );
70          if ( !getTimerEvents().containsKey( metadataId ) )
71          {
72              throw new IllegalArgumentException( "Unknown metadataId (" + metadataId + ")" );
73          }
74          getTimerEvents().get( metadataId ).stop();
75  
76          long size = 0;
77          if ( event.getMetadata().getFile() != null )
78          {
79              size = event.getMetadata().getFile().length();
80          }
81          getTimerEvents().get( metadataId ).setSize( size );
82      }
83  
84  }