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-2015 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/**
026 * A file name policy that names files suffixed by the time it was created.
027 */
028public class TimeStampNaming implements FileNamingPolicy
029{
030  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
031
032  private File file;
033  private TimeStampNamingFilter filter;
034
035  /**
036   * The FilenameFilter implementation for this naming policy to filter
037   * for all the files named by this policy.
038   */
039  private class TimeStampNamingFilter implements FilenameFilter
040  {
041    /**
042     * Select only files that are named by this policy.
043     *
044     * @param dir  The directory to search.
045     * @param name The filename to which to apply the filter.
046     *
047     * @return  <CODE>true</CODE> if the given filename matches the filter, or
048     *          <CODE>false</CODE> if it does not.
049     */
050    public boolean accept(File dir, String name)
051    {
052      if(new File(dir, name).isDirectory())
053      {
054        return false;
055      }
056
057      String initialFileName = file.getName();
058
059      // Make sure it is the expected length.
060      if(name.length() != initialFileName.length() + 16)
061      {
062        return false;
063      }
064
065      int pos;
066      // Make sure we got the expected name prefix.
067      for(pos = 0; pos < initialFileName.length(); pos++)
068      {
069        if(name.charAt(pos) != initialFileName.charAt(pos))
070        {
071          return false;
072        }
073      }
074
075      // Make sure there is a period between the prefix and timestamp.
076      if(name.charAt(pos) != '.')
077      {
078        return false;
079      }
080
081      char c;
082      // Make sure there are 14 numbers for the timestamp.
083      for(pos++; pos < name.length() - 1; pos++)
084      {
085        c = name.charAt(pos);
086        if(c < 48 || c > 57)
087        {
088          return false;
089        }
090      }
091
092      // And ends with an Z.
093      return name.charAt(pos) == 'Z';
094
095    }
096  }
097
098  /**
099   * Create a new instance of the TimeStampNaming policy. Files will be created
100   * with the names in the prefix.utctime format.
101   *
102   * @param file the file to use as the naming prefix.
103   */
104  public TimeStampNaming(File file)
105  {
106    this.file = file;
107    this.filter = new TimeStampNamingFilter();
108  }
109
110  /** {@inheritDoc} */
111  public File getInitialName()
112  {
113    return file;
114  }
115
116  /** {@inheritDoc} */
117  public File getNextName()
118  {
119    return new File(file + "." + TimeThread.getGMTTime());
120  }
121
122  /** {@inheritDoc} */
123  public FilenameFilter getFilenameFilter()
124  {
125    return filter;
126  }
127
128  /** {@inheritDoc} */
129  public File[] listFiles()
130  {
131    File directory = file.getParentFile();
132    File[] files =  directory.listFiles(filter);
133
134    if(files == null)
135    {
136      logger.trace("Unable to list files named by policy " +
137          "with initial file %s in directory %s", file, directory);
138    }
139
140    return files;
141  }
142
143}