]> source.dussan.org Git - archiva.git/blob
780b95367208644dac93438b27f2fe4cb9cf68ed
[archiva.git] /
1 package org.apache.maven.archiva.repository.scanner;
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 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;
26
27 import java.io.File;
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;
34
35 /**
36  * ScanStatistics 
37  *
38  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
39  * @version $Id$
40  */
41 public class ScanStatistics
42 {
43     private static final String PROP_FILES_CONSUMED = "scan.consumed.files";
44
45     private static final String PROP_FILES_INCLUDED = "scan.included.files";
46
47     private static final String PROP_FILES_SKIPPED = "scan.skipped.files";
48
49     private static final String PROP_TIMESTAMP_STARTED = "scan.started.timestamp";
50
51     private static final String PROP_TIMESTAMP_FINISHED = "scan.finished.timestamp";
52
53     protected long timestampStarted = 0;
54
55     protected long timestampFinished = 0;
56
57     protected long filesIncluded = 0;
58
59     protected long filesConsumed = 0;
60
61     protected long filesSkipped = 0;
62
63     private ArtifactRepository repository;
64
65     public ScanStatistics( ArtifactRepository repository )
66     {
67         this.repository = repository;
68     }
69
70     public void load( String filename )
71         throws IOException
72     {
73         File repositoryBase = new File( this.repository.getBasedir() );
74
75         File scanProperties = new File( repositoryBase, filename );
76         FileInputStream fis = null;
77         try
78         {
79             Properties props = new Properties();
80             fis = new FileInputStream( scanProperties );
81             props.load( fis );
82
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 );
88         }
89         catch ( IOException e )
90         {
91             reset();
92             throw e;
93         }
94         finally
95         {
96             IOUtil.close( fis );
97         }
98     }
99
100     public void save( String filename )
101         throws IOException
102     {
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 ) );
109
110         File repositoryBase = new File( this.repository.getBasedir() );
111         File statsFile = new File( repositoryBase, filename );
112
113         FileOutputStream fos = null;
114         try
115         {
116             fos = new FileOutputStream( statsFile );
117             props.store( fos, "Last Scan Information, managed by Archiva. DO NOT EDIT" );
118             fos.flush();
119         }
120         finally
121         {
122             IOUtil.close( fos );
123         }
124     }
125
126     public void reset()
127     {
128         timestampStarted = 0;
129         timestampFinished = 0;
130         filesIncluded = 0;
131         filesConsumed = 0;
132         filesSkipped = 0;
133     }
134
135     public long getElapsedMilliseconds()
136     {
137         return timestampFinished - timestampStarted;
138     }
139
140     public long getFilesConsumed()
141     {
142         return filesConsumed;
143     }
144
145     public long getFilesIncluded()
146     {
147         return filesIncluded;
148     }
149
150     public ArtifactRepository getRepository()
151     {
152         return repository;
153     }
154
155     public long getTimestampFinished()
156     {
157         return timestampFinished;
158     }
159
160     public long getTimestampStarted()
161     {
162         return timestampStarted;
163     }
164
165     public long getFilesSkipped()
166     {
167         return filesSkipped;
168     }
169
170     public void setTimestampFinished( long timestampFinished )
171     {
172         this.timestampFinished = timestampFinished;
173     }
174
175     public void setTimestampStarted( long timestampStarted )
176     {
177         this.timestampStarted = timestampStarted;
178     }
179
180     public void dump( Logger logger )
181     {
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() );
191     }
192     
193     private String toHumanTimestamp( long timestamp )
194     {
195         SimpleDateFormat dateFormat = new SimpleDateFormat();
196         return dateFormat.format( new Date( timestamp ) );
197     }
198 }