001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2009 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.loggers; 018 019import java.io.File; 020import java.io.FilenameFilter; 021 022import org.forgerock.i18n.slf4j.LocalizedLogger; 023import org.opends.server.util.TimeThread; 024 025/** A file name policy that names files suffixed by the time it was created. */ 026public class TimeStampNaming implements FileNamingPolicy 027{ 028 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 029 030 private File file; 031 private TimeStampNamingFilter filter; 032 033 /** 034 * The FilenameFilter implementation for this naming policy to filter 035 * for all the files named by this policy. 036 */ 037 private class TimeStampNamingFilter implements FilenameFilter 038 { 039 /** 040 * Select only files that are named by this policy. 041 * 042 * @param dir The directory to search. 043 * @param name The filename to which to apply the filter. 044 * 045 * @return <CODE>true</CODE> if the given filename matches the filter, or 046 * <CODE>false</CODE> if it does not. 047 */ 048 @Override 049 public boolean accept(File dir, String name) 050 { 051 if(new File(dir, name).isDirectory()) 052 { 053 return false; 054 } 055 056 String initialFileName = file.getName(); 057 058 // Make sure it is the expected length. 059 if(name.length() != initialFileName.length() + 16) 060 { 061 return false; 062 } 063 064 int pos; 065 // Make sure we got the expected name prefix. 066 for(pos = 0; pos < initialFileName.length(); pos++) 067 { 068 if(name.charAt(pos) != initialFileName.charAt(pos)) 069 { 070 return false; 071 } 072 } 073 074 // Make sure there is a period between the prefix and timestamp. 075 if(name.charAt(pos) != '.') 076 { 077 return false; 078 } 079 080 char c; 081 // Make sure there are 14 numbers for the timestamp. 082 for(pos++; pos < name.length() - 1; pos++) 083 { 084 c = name.charAt(pos); 085 if(c < 48 || c > 57) 086 { 087 return false; 088 } 089 } 090 091 // And ends with an Z. 092 return name.charAt(pos) == 'Z'; 093 094 } 095 } 096 097 /** 098 * Create a new instance of the TimeStampNaming policy. Files will be created 099 * with the names in the prefix.utctime format. 100 * 101 * @param file the file to use as the naming prefix. 102 */ 103 public TimeStampNaming(File file) 104 { 105 this.file = file; 106 this.filter = new TimeStampNamingFilter(); 107 } 108 109 @Override 110 public File getInitialName() 111 { 112 return file; 113 } 114 115 @Override 116 public File getNextName() 117 { 118 return new File(file + "." + TimeThread.getGMTTime()); 119 } 120 121 @Override 122 public FilenameFilter getFilenameFilter() 123 { 124 return filter; 125 } 126 127 @Override 128 public File[] listFiles() 129 { 130 File directory = file.getParentFile(); 131 File[] files = directory.listFiles(filter); 132 133 if(files == null) 134 { 135 logger.trace("Unable to list files named by policy " + 136 "with initial file %s in directory %s", file, directory); 137 } 138 139 return files; 140 } 141 142}