1 17 package org.apache.log.output.io.rotate; 18 19 import java.io.File ; 20 import java.text.DecimalFormat ; 21 import java.text.FieldPosition ; 22 import java.text.NumberFormat ; 23 24 39 public class RevolvingFileStrategy 40 implements FileStrategy 41 { 42 private static final String PATTERN = "'.'000000"; 44 45 private DecimalFormat m_decimalFormat; 47 48 private int m_rotation; 50 51 private int m_maxRotations; 53 54 private File m_baseFile; 56 57 62 public RevolvingFileStrategy( final File baseFile, final int maxRotations ) 63 { 64 this( baseFile, -1, maxRotations ); 65 } 66 67 73 public RevolvingFileStrategy( final File baseFile, 74 final int initialRotation, 75 final int maxRotations ) 76 { 77 m_decimalFormat = new DecimalFormat ( PATTERN ); 78 79 m_baseFile = baseFile; 80 m_rotation = initialRotation; 81 m_maxRotations = maxRotations; 82 83 if( -1 == m_maxRotations ) 84 { 85 m_maxRotations = Integer.MAX_VALUE; 86 } 87 88 if( -1 == initialRotation ) 89 { 90 m_rotation = calculateInitialRotation(); 91 } 92 93 if( m_rotation > m_maxRotations ) 94 { 95 m_rotation = m_maxRotations; 96 } 97 98 if( m_rotation < 0 ) 99 { 100 m_rotation = 0; 101 } 102 } 103 104 109 public File nextFile() 110 { 111 final StringBuffer sb = new StringBuffer (); 112 final FieldPosition position = 113 new FieldPosition ( NumberFormat.INTEGER_FIELD ); 114 sb.append( m_baseFile ); 115 116 final StringBuffer result = 117 m_decimalFormat.format( m_rotation, sb, position ); 118 m_rotation += 1; 119 120 if( m_rotation >= m_maxRotations ) 121 { 122 m_rotation = 0; 123 } 124 125 return new File ( result.toString() ); 126 } 127 128 133 public int getCurrentRotation() 134 { 135 return m_rotation; 136 } 137 138 145 private int calculateInitialRotation() 146 { 147 final File [] matchingFiles = getMatchingFiles(); 148 if( null == matchingFiles || 0 == matchingFiles.length ) 149 { 150 return 0; 151 } 152 153 final int[] rotations = calculateRotations( matchingFiles ); 154 155 int maxRotation = 0; 157 for( int i = 0; i < rotations.length; i++ ) 158 { 159 final int rotation = rotations[ i ]; 160 if( rotation > maxRotation ) 161 { 162 maxRotation = rotation; 163 } 164 } 165 166 if( m_maxRotations != maxRotation ) 170 { 171 return maxRotation + 1; 172 } 173 174 long time = matchingFiles[ 0 ].lastModified(); 176 177 int oldest = rotations[ 0 ]; 179 for( int i = 0; i < matchingFiles.length; i++ ) 180 { 181 final File file = matchingFiles[ i ]; 182 final long lastModified = file.lastModified(); 183 if( lastModified < time ) 184 { 185 time = lastModified; 186 oldest = rotations[ i ]; 187 } 188 } 189 190 return oldest; 191 } 192 193 199 private int[] calculateRotations( final File [] matchingFiles ) 200 { 201 final int[] results = new int[ matchingFiles.length ]; 202 203 for( int i = 0; i < matchingFiles.length; i++ ) 204 { 205 final File file = matchingFiles[ i ]; 206 207 try 210 { 211 results[ i ] = calculateRotationForFile( file ); 212 } 213 catch( final NumberFormatException nfe ) 214 { 215 results[ i ] = -1; 217 } 218 } 219 return results; 220 } 221 222 228 private int calculateRotationForFile( final File file ) 229 { 230 final String filename = file.toString(); 231 final int length = filename.length(); 232 final int minDigits = 233 m_decimalFormat.getMinimumIntegerDigits(); 234 final String rotation = filename.substring( length - minDigits ); 235 236 return Integer.parseInt( rotation ); 237 } 238 239 244 private File [] getMatchingFiles() 245 { 246 final String fullFilePathName = m_baseFile.getPath(); 249 250 final int fileSeparatorPosition = fullFilePathName.lastIndexOf( File.separator ); 252 253 File basePath; 255 String baseFileName; 256 if( fileSeparatorPosition < 0 ) 257 { 258 basePath = new File ( "." ); 260 baseFileName = fullFilePathName; 261 } 262 else 263 { 264 String m_parentPath = fullFilePathName.substring( 0, fileSeparatorPosition ); 266 baseFileName = fullFilePathName.substring( fileSeparatorPosition + 1 ); 267 basePath = new File ( m_parentPath ); 268 } 269 270 return basePath.listFiles( new BaseFileNameFilter( baseFileName ) ); 271 } 272 } 273 | Popular Tags |