Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
51   214   22   6.38
26   94   0.57   8
8     3.62  
1    
 
 
  LogFilenameClass       Line # 58 51 22 69.4% 0.69411767
 
No Tests
 
1    /*
2    * Copyright (c) 2006, University of Kent
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * 1. Neither the name of the University of Kent nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22    * PURPOSE ARE DISCLAIMED.
23    *
24    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    * POSSIBILITY OF SUCH DAMAGE.
32    *
33    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
34    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
35    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
36    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
37    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
38    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
39    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
40    * SERIOUS FAULTS, IN THIS SOFTWARE.
41    *
42    * 5. This license is governed, except to the extent that local laws
43    * necessarily apply, by the laws of England and Wales.
44    */
45   
46    package issrg.SAWS;
47   
48    import issrg.SAWS.util.SAWSLogWriter;
49   
50    import java.util.*;
51    import java.io.*;
52    import java.io.File;
53   
54    /**
55    *
56    * @author W.Xu
57    */
 
58    public class LogFilenameClass {
59   
60    private String latestLogFilename;
61    private static int lastLogSequence = -1;
62    private int debugLevel = 1;
63    private java.text.SimpleDateFormat sDateFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH_mm_ssz", Locale.UK);
64   
65    //adding log4j logging
66   
67    /**
68    * @aggregation composite
69    */
70    private static SAWSLogWriter sawsDebugLog = new SAWSLogWriter(LogFilenameClass.class.getName());
71   
72    /** Creates a new instance of LogFilenameClass */
 
73  4 toggle public LogFilenameClass() {
74    }
 
75  0 toggle public LogFilenameClass(int debugLevel) {
76  0 this.debugLevel = debugLevel;
77    }
78   
79    /**
80    * This method is for generating the new log file name in UTC format
81    * in the log root directory.
82    *
83    * @param null
84    *
85    * @return the new log file name in UTC format.
86    */
 
87  11 toggle public String generateNewLogFileName(){
88  11 Calendar today = new GregorianCalendar(TimeZone.getTimeZone("GMT+1"));
89   
90  11 StringBuffer tmp = sDateFormat.format(today.getTime(),new StringBuffer(), new java.text.FieldPosition(0));
91  11 lastLogSequence += 1;
92  11 String newFileName = "saws" + tmp + getLastLogSequence() + ".dat";
93   
94  11 if (debugLevel >= SAWSConstant.VerboseInfo){
95    //System.err.println("Now the UTC time is : " + tmp );
96    //System.err.println("The new log file name is: " + newFileName );
97    //adding log4j logging
98  0 sawsDebugLog.write("Now the UTC time is : " + tmp );
99  0 sawsDebugLog.write("The new log file name is: " + newFileName );
100    }
101   
102  11 return (newFileName);
103    }
104   
105    /**
106    * This method is for finding the latest log file name created prior to the given logFilename
107    * in the log root directory.
108    *
109    * @param logFilename String is the given log filename.
110    *
111    * @return the latest log file name created prior to the given logFilename. If not exists, return null.
112    */
 
113  3 toggle public String getPreviousLogFilename(String logFileRoot, String logFilename) {
114   
115  3 File currentPath = new File(logFileRoot);
116  3 String[] files = currentPath.list(new WildCardFilter("saws*-*-*.dat") );
117  3 int len = files.length;
118  3 if (len == 0 ) {
119  0 return (null); // no file exists
120    }
121   
122  3 Calendar currentDate = ( (logFilename==null) ? null : getDatestamp(logFilename) );
123  3 int currentSeq = ( (logFilename==null) ? 0 : getSequence(logFilename) );
124  3 if (logFilename!=null && getSequence(logFilename) == 0) {
125  0 return null; // it is already the very first log file.
126    }
127  3 Calendar latestDate = null;
128  3 Calendar nextDate = null;
129  3 int latest = 0, latestSeq = 0, nextSeq=0;
130   
131  20 for (int i = 0; i<len; i++){
132  17 if (files[i].endsWith(".dat") && files[i].substring(0,4).compareTo("saws")==0 ) {
133  14 nextDate = getDatestamp(files[i]);
134  14 nextSeq = getSequence(files[i] );
135  14 if ( currentDate ==null ){
136  14 if( latestDate==null || (latestDate.before(nextDate))
137    || ( (latestSeq < nextSeq) && ( !latestDate.after(nextDate)) ) ) {
138  14 latestDate = nextDate;
139  14 latestSeq = nextSeq;
140  14 latest = i;
141    }
142    } else {
143  0 if( latestDate==null
144    || ( (latestDate.before(nextDate)) && (nextDate.before(currentDate)) )
145    || ( (latestSeq < nextSeq) && ( nextSeq < currentSeq ) ) ) {
146  0 latestDate = nextDate;
147  0 latestSeq = nextSeq;
148  0 latest = i;
149    }
150   
151    }
152    }
153    }
154  3 if (logFilename == null) {
155  3 lastLogSequence = latestSeq;
156    }
157   
158  0 if (latestDate == currentDate) return null;
159  3 return(files[latest]);
160   
161    }
162   
 
163  14 toggle private int getSequence(String sawsLogFilename){
164  14 String sequenceStr = sawsLogFilename.substring(26, sawsLogFilename.indexOf('.'));
165  14 return Integer.parseInt(sequenceStr);
166    }
167   
168   
169    /**
170    * This method is for finding the latest log file name
171    * in the log root directory.
172    *
173    * @param null
174    *
175    * @return the current log file name. If not exists, return null.
176    */
 
177  3 toggle public String findLatestLogFileName(String logFileRoot){
178   
179  3 return(getPreviousLogFilename(logFileRoot, null));
180    }
181   
 
182  14 toggle private Calendar getDatestamp(String sawsLogFilename){ //saws2005-01-25T14_53_28GMT258.dat
183   
184  14 Calendar thisDate = new GregorianCalendar();
185  14 sDateFormat.setCalendar(thisDate);
186   
187  14 try {
188  14 Date d1 = sDateFormat.parse(sawsLogFilename, new java.text.ParsePosition(4) );
189  14 thisDate.setTime(d1);
190   
191    } catch (Exception e){
192  0 if (debugLevel >= SAWSConstant.ErrorInfo){
193  0 sawsDebugLog.write(e.getMessage());
194    }
195    }
196   
197  14 return thisDate;
198   
199    }
200   
201    /**
202    * This method is for finding out the last sequence number of the log files in
203    * the destination directory.
204    *
205    * @param null
206    *
207    * @return the sequence number in the name string of the last log file; if non exists, then return -1.
208    */
 
209  11 toggle public int getLastLogSequence(){
210  11 return lastLogSequence;
211    }
212   
213   
214    }