001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.loggers;
028
029 import org.opends.server.util.TimeThread;
030 import org.opends.server.loggers.debug.DebugTracer;
031 import static org.opends.server.loggers.debug.DebugLogger.getTracer;
032
033 import java.io.File;
034 import java.io.FilenameFilter;
035
036 /**
037 * A file name policy that names files suffixed by the time it was created.
038 */
039 public class TimeStampNaming implements FileNamingPolicy
040 {
041 /**
042 * The tracer object for the debug logger.
043 */
044 private static final DebugTracer TRACER = getTracer();
045
046 File file;
047
048 /**
049 * The FilenameFilter implementation for this naming policy to filter
050 * for all the files named by this policy.
051 */
052 private class TimeStampNamingFilter implements FilenameFilter
053 {
054 /**
055 * Select only files that are named by this policy.
056 *
057 * @param dir The directory to search.
058 * @param name The filename to which to apply the filter.
059 *
060 * @return <CODE>true</CODE> if the given filename matches the filter, or
061 * <CODE>false</CODE> if it does not.
062 */
063 public boolean accept(File dir, String name)
064 {
065 if(new File(dir, name).isDirectory())
066 {
067 return false;
068 }
069 name = name.toLowerCase();
070 return name.startsWith(file.getName().toLowerCase());
071 }
072 }
073
074 /**
075 * Create a new instance of the TimeStampNaming policy. Files will be created
076 * with the names in the prefix.utctime format.
077 *
078 * @param file the file to use as the naming prefix.
079 */
080 public TimeStampNaming(File file)
081 {
082 this.file = file;
083 }
084
085 /**
086 * {@inheritDoc}
087 */
088 public File getInitialName()
089 {
090 return file;
091 }
092
093 /**
094 * {@inheritDoc}
095 */
096 public File getNextName()
097 {
098 return new File(file + "." + TimeThread.getGMTTime());
099 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public FilenameFilter getFilenameFilter()
105 {
106 return new TimeStampNamingFilter();
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public File[] listFiles()
113 {
114 File directory = file.getParentFile();
115 File[] files = directory.listFiles(getFilenameFilter());
116
117 if(files == null)
118 {
119 TRACER.debugError("Unable to list files named by policy " +
120 "with initial file %s in directory %s", file, directory);
121 }
122
123 return files;
124 }
125
126 }