1 package org.apache.maven.archiva.repository.scanner;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.math.NumberUtils;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.codehaus.plexus.logging.Logger;
25 import org.codehaus.plexus.util.IOUtil;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.Properties;
38 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
41 public class ScanStatistics
43 private static final String PROP_FILES_CONSUMED = "scan.consumed.files";
45 private static final String PROP_FILES_INCLUDED = "scan.included.files";
47 private static final String PROP_FILES_SKIPPED = "scan.skipped.files";
49 private static final String PROP_TIMESTAMP_STARTED = "scan.started.timestamp";
51 private static final String PROP_TIMESTAMP_FINISHED = "scan.finished.timestamp";
53 protected long timestampStarted = 0;
55 protected long timestampFinished = 0;
57 protected long filesIncluded = 0;
59 protected long filesConsumed = 0;
61 protected long filesSkipped = 0;
63 private ArtifactRepository repository;
65 public ScanStatistics( ArtifactRepository repository )
67 this.repository = repository;
70 public void load( String filename )
73 File repositoryBase = new File( this.repository.getBasedir() );
75 File scanProperties = new File( repositoryBase, filename );
76 FileInputStream fis = null;
79 Properties props = new Properties();
80 fis = new FileInputStream( scanProperties );
83 timestampFinished = NumberUtils.toLong( props.getProperty( PROP_TIMESTAMP_FINISHED ), 0 );
84 timestampStarted = NumberUtils.toLong( props.getProperty( PROP_TIMESTAMP_STARTED ), 0 );
85 filesIncluded = NumberUtils.toLong( props.getProperty( PROP_FILES_INCLUDED ), 0 );
86 filesConsumed = NumberUtils.toLong( props.getProperty( PROP_FILES_CONSUMED ), 0 );
87 filesSkipped = NumberUtils.toLong( props.getProperty( PROP_FILES_SKIPPED ), 0 );
89 catch ( IOException e )
100 public void save( String filename )
103 Properties props = new Properties();
104 props.setProperty( PROP_TIMESTAMP_FINISHED, String.valueOf( timestampFinished ) );
105 props.setProperty( PROP_TIMESTAMP_STARTED, String.valueOf( timestampStarted ) );
106 props.setProperty( PROP_FILES_INCLUDED, String.valueOf( filesIncluded ) );
107 props.setProperty( PROP_FILES_CONSUMED, String.valueOf( filesConsumed ) );
108 props.setProperty( PROP_FILES_SKIPPED, String.valueOf( filesSkipped ) );
110 File repositoryBase = new File( this.repository.getBasedir() );
111 File statsFile = new File( repositoryBase, filename );
113 FileOutputStream fos = null;
116 fos = new FileOutputStream( statsFile );
117 props.store( fos, "Last Scan Information, managed by Archiva. DO NOT EDIT" );
128 timestampStarted = 0;
129 timestampFinished = 0;
135 public long getElapsedMilliseconds()
137 return timestampFinished - timestampStarted;
140 public long getFilesConsumed()
142 return filesConsumed;
145 public long getFilesIncluded()
147 return filesIncluded;
150 public ArtifactRepository getRepository()
155 public long getTimestampFinished()
157 return timestampFinished;
160 public long getTimestampStarted()
162 return timestampStarted;
165 public long getFilesSkipped()
170 public void setTimestampFinished( long timestampFinished )
172 this.timestampFinished = timestampFinished;
175 public void setTimestampStarted( long timestampStarted )
177 this.timestampStarted = timestampStarted;
180 public void dump( Logger logger )
182 logger.info( "----------------------------------------------------" );
183 logger.info( "Scan of Repository: " + repository.getId() );
184 logger.info( " Started : " + toHumanTimestamp( this.getTimestampStarted() ) );
185 logger.info( " Finished: " + toHumanTimestamp( this.getTimestampFinished() ) );
186 // TODO: pretty print ellapsed time.
187 logger.info( " Duration: " + this.getElapsedMilliseconds() + "ms" );
188 logger.info( " Files : " + this.getFilesIncluded() );
189 logger.info( " Consumed: " + this.getFilesConsumed() );
190 logger.info( " Skipped : " + this.getFilesSkipped() );
193 private String toHumanTimestamp( long timestamp )
195 SimpleDateFormat dateFormat = new SimpleDateFormat();
196 return dateFormat.format( new Date( timestamp ) );