1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.logging;
  27 
  28 import static java.nio.file.StandardOpenOption.APPEND;
  29 import static java.nio.file.StandardOpenOption.CREATE_NEW;
  30 import static java.nio.file.StandardOpenOption.WRITE;
  31 
  32 import java.io.BufferedOutputStream;
  33 import java.io.File;
  34 import java.io.FileOutputStream;
  35 import java.io.IOException;
  36 import java.io.OutputStream;
  37 import java.nio.channels.FileChannel;
  38 import java.nio.channels.OverlappingFileLockException;
  39 import java.nio.file.FileAlreadyExistsException;
  40 import java.nio.file.Files;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import java.security.AccessController;
  44 import java.security.PrivilegedAction;
  45 import java.util.HashSet;
  46 import java.util.Set;
  47 
  48 /**
  49  * Simple file logging <tt>Handler</tt>.
  50  * <p>
  51  * The <tt>FileHandler</tt> can either write to a specified file,
  52  * or it can write to a rotating set of files.
  53  * <p>
  54  * For a rotating set of files, as each file reaches a given size
  55  * limit, it is closed, rotated out, and a new file opened.
  56  * Successively older files are named by adding "0", "1", "2",
  57  * etc. into the base filename.
  58  * <p>
  59  * By default buffering is enabled in the IO libraries but each log
  60  * record is flushed out when it is complete.
  61  * <p>
  62  * By default the <tt>XMLFormatter</tt> class is used for formatting.
  63  * <p>
  64  * <b>Configuration:</b>
  65  * By default each <tt>FileHandler</tt> is initialized using the following
  66  * <tt>LogManager</tt> configuration properties where <tt>&lt;handler-name&gt;</tt>
  67  * refers to the fully-qualified class name of the handler.
  68  * If properties are not defined
  69  * (or have invalid values) then the specified default values are used.
  70  * <ul>
  71  * <li>   &lt;handler-name&gt;.level
  72  *        specifies the default level for the <tt>Handler</tt>
  73  *        (defaults to <tt>Level.ALL</tt>). </li>
  74  * <li>   &lt;handler-name&gt;.filter
  75  *        specifies the name of a <tt>Filter</tt> class to use
  76  *        (defaults to no <tt>Filter</tt>). </li>
  77  * <li>   &lt;handler-name&gt;.formatter
  78  *        specifies the name of a <tt>Formatter</tt> class to use
  79  *        (defaults to <tt>java.util.logging.XMLFormatter</tt>) </li>
  80  * <li>   &lt;handler-name&gt;.encoding
  81  *        the name of the character set encoding to use (defaults to
  82  *        the default platform encoding). </li>
  83  * <li>   &lt;handler-name&gt;.limit
  84  *        specifies an approximate maximum amount to write (in bytes)
  85  *        to any one file.  If this is zero, then there is no limit.
  86  *        (Defaults to no limit). </li>
  87  * <li>   &lt;handler-name&gt;.count
  88  *        specifies how many output files to cycle through (defaults to 1). </li>
  89  * <li>   &lt;handler-name&gt;.pattern
  90  *        specifies a pattern for generating the output file name.  See
  91  *        below for details. (Defaults to "%h/java%u.log"). </li>
  92  * <li>   &lt;handler-name&gt;.append
  93  *        specifies whether the FileHandler should append onto
  94  *        any existing files (defaults to false). </li>
  95  * </ul>
  96  * <p>
  97  * For example, the properties for {@code FileHandler} would be:
  98  * <ul>
  99  * <li>   java.util.logging.FileHandler.level=INFO </li>
 100  * <li>   java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter </li>
 101  * </ul>
 102  * <p>
 103  * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
 104  * <ul>
 105  * <li>   com.foo.MyHandler.level=INFO </li>
 106  * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
 107  * </ul>
 108  * <p>
 109  * A pattern consists of a string that includes the following special
 110  * components that will be replaced at runtime:
 111  * <ul>
 112  * <li>    "/"    the local pathname separator </li>
 113  * <li>     "%t"   the system temporary directory </li>
 114  * <li>     "%h"   the value of the "user.home" system property </li>
 115  * <li>     "%g"   the generation number to distinguish rotated logs </li>
 116  * <li>     "%u"   a unique number to resolve conflicts </li>
 117  * <li>     "%%"   translates to a single percent sign "%" </li>
 118  * </ul>
 119  * If no "%g" field has been specified and the file count is greater
 120  * than one, then the generation number will be added to the end of
 121  * the generated filename, after a dot.
 122  * <p>
 123  * Thus for example a pattern of "%t/java%g.log" with a count of 2
 124  * would typically cause log files to be written on Solaris to
 125  * /var/tmp/java0.log and /var/tmp/java1.log whereas on Windows 95 they
 126  * would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log
 127  * <p>
 128  * Generation numbers follow the sequence 0, 1, 2, etc.
 129  * <p>
 130  * Normally the "%u" unique field is set to 0.  However, if the <tt>FileHandler</tt>
 131  * tries to open the filename and finds the file is currently in use by
 132  * another process it will increment the unique number field and try
 133  * again.  This will be repeated until <tt>FileHandler</tt> finds a file name that
 134  * is  not currently in use. If there is a conflict and no "%u" field has
 135  * been specified, it will be added at the end of the filename after a dot.
 136  * (This will be after any automatically added generation number.)
 137  * <p>
 138  * Thus if three processes were all trying to log to fred%u.%g.txt then
 139  * they  might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as
 140  * the first file in their rotating sequences.
 141  * <p>
 142  * Note that the use of unique ids to avoid conflicts is only guaranteed
 143  * to work reliably when using a local disk file system.
 144  *
 145  * @since 1.4
 146  */
 147 
 148 public class FileHandler extends StreamHandler {
 149     private MeteredStream meter;
 150     private boolean append;
 151     private int limit;       // zero => no limit.
 152     private int count;
 153     private String pattern;
 154     private String lockFileName;
 155     private FileChannel lockFileChannel;
 156     private File files[];
 157     private static final int MAX_LOCKS = 100;
 158     private static final Set<String> locks = new HashSet<>();
 159 
 160     /**
 161      * A metered stream is a subclass of OutputStream that
 162      * (a) forwards all its output to a target stream
 163      * (b) keeps track of how many bytes have been written
 164      */
 165     private class MeteredStream extends OutputStream {
 166         final OutputStream out;
 167         int written;
 168 
 169         MeteredStream(OutputStream out, int written) {
 170             this.out = out;
 171             this.written = written;
 172         }
 173 
 174         @Override
 175         public void write(int b) throws IOException {
 176             out.write(b);
 177             written++;
 178         }
 179 
 180         @Override
 181         public void write(byte buff[]) throws IOException {
 182             out.write(buff);
 183             written += buff.length;
 184         }
 185 
 186         @Override
 187         public void write(byte buff[], int off, int len) throws IOException {
 188             out.write(buff,off,len);
 189             written += len;
 190         }
 191 
 192         @Override
 193         public void flush() throws IOException {
 194             out.flush();
 195         }
 196 
 197         @Override
 198         public void close() throws IOException {
 199             out.close();
 200         }
 201     }
 202 
 203     private void open(File fname, boolean append) throws IOException {
 204         int len = 0;
 205         if (append) {
 206             len = (int)fname.length();
 207         }
 208         FileOutputStream fout = new FileOutputStream(fname.toString(), append);
 209         BufferedOutputStream bout = new BufferedOutputStream(fout);
 210         meter = new MeteredStream(bout, len);
 211         setOutputStream(meter);
 212     }
 213 
 214     /**
 215      * Configure a FileHandler from LogManager properties and/or default values
 216      * as specified in the class javadoc.
 217      */
 218     private void configure() {
 219         LogManager manager = LogManager.getLogManager();
 220 
 221         String cname = getClass().getName();
 222 
 223         pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");
 224         limit = manager.getIntProperty(cname + ".limit", 0);
 225         if (limit < 0) {
 226             limit = 0;
 227         }
 228         count = manager.getIntProperty(cname + ".count", 1);
 229         if (count <= 0) {
 230             count = 1;
 231         }
 232         append = manager.getBooleanProperty(cname + ".append", false);
 233         setLevel(manager.getLevelProperty(cname + ".level", Level.ALL));
 234         setFilter(manager.getFilterProperty(cname + ".filter", null));
 235         setFormatter(manager.getFormatterProperty(cname + ".formatter", new XMLFormatter()));
 236         try {
 237             setEncoding(manager.getStringProperty(cname +".encoding", null));
 238         } catch (Exception ex) {
 239             try {
 240                 setEncoding(null);
 241             } catch (Exception ex2) {
 242                 // doing a setEncoding with null should always work.
 243                 // assert false;
 244             }
 245         }
 246     }
 247 
 248 
 249     /**
 250      * Construct a default <tt>FileHandler</tt>.  This will be configured
 251      * entirely from <tt>LogManager</tt> properties (or their default values).
 252      *
 253      * @exception  IOException if there are IO problems opening the files.
 254      * @exception  SecurityException  if a security manager exists and if
 255      *             the caller does not have <tt>LoggingPermission("control"))</tt>.
 256      * @exception  NullPointerException if pattern property is an empty String.
 257      */
 258     public FileHandler() throws IOException, SecurityException {
 259         checkPermission();
 260         configure();
 261         openFiles();
 262     }
 263 
 264     /**
 265      * Initialize a <tt>FileHandler</tt> to write to the given filename.
 266      * <p>
 267      * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
 268      * properties (or their default values) except that the given pattern
 269      * argument is used as the filename pattern, the file limit is
 270      * set to no limit, and the file count is set to one.
 271      * <p>
 272      * There is no limit on the amount of data that may be written,
 273      * so use this with care.
 274      *
 275      * @param pattern  the name of the output file
 276      * @exception  IOException if there are IO problems opening the files.
 277      * @exception  SecurityException  if a security manager exists and if
 278      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 279      * @exception  IllegalArgumentException if pattern is an empty string
 280      */
 281     public FileHandler(String pattern) throws IOException, SecurityException {
 282         if (pattern.length() < 1 ) {
 283             throw new IllegalArgumentException();
 284         }
 285         checkPermission();
 286         configure();
 287         this.pattern = pattern;
 288         this.limit = 0;
 289         this.count = 1;
 290         openFiles();
 291     }
 292 
 293     /**
 294      * Initialize a <tt>FileHandler</tt> to write to the given filename,
 295      * with optional append.
 296      * <p>
 297      * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
 298      * properties (or their default values) except that the given pattern
 299      * argument is used as the filename pattern, the file limit is
 300      * set to no limit, the file count is set to one, and the append
 301      * mode is set to the given <tt>append</tt> argument.
 302      * <p>
 303      * There is no limit on the amount of data that may be written,
 304      * so use this with care.
 305      *
 306      * @param pattern  the name of the output file
 307      * @param append  specifies append mode
 308      * @exception  IOException if there are IO problems opening the files.
 309      * @exception  SecurityException  if a security manager exists and if
 310      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 311      * @exception  IllegalArgumentException if pattern is an empty string
 312      */
 313     public FileHandler(String pattern, boolean append) throws IOException,
 314             SecurityException {
 315         if (pattern.length() < 1 ) {
 316             throw new IllegalArgumentException();
 317         }
 318         checkPermission();
 319         configure();
 320         this.pattern = pattern;
 321         this.limit = 0;
 322         this.count = 1;
 323         this.append = append;
 324         openFiles();
 325     }
 326 
 327     /**
 328      * Initialize a <tt>FileHandler</tt> to write to a set of files.  When
 329      * (approximately) the given limit has been written to one file,
 330      * another file will be opened.  The output will cycle through a set
 331      * of count files.
 332      * <p>
 333      * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
 334      * properties (or their default values) except that the given pattern
 335      * argument is used as the filename pattern, the file limit is
 336      * set to the limit argument, and the file count is set to the
 337      * given count argument.
 338      * <p>
 339      * The count must be at least 1.
 340      *
 341      * @param pattern  the pattern for naming the output file
 342      * @param limit  the maximum number of bytes to write to any one file
 343      * @param count  the number of files to use
 344      * @exception  IOException if there are IO problems opening the files.
 345      * @exception  SecurityException  if a security manager exists and if
 346      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 347      * @exception  IllegalArgumentException if {@code limit < 0}, or {@code count < 1}.
 348      * @exception  IllegalArgumentException if pattern is an empty string
 349      */
 350     public FileHandler(String pattern, int limit, int count)
 351                                         throws IOException, SecurityException {
 352         if (limit < 0 || count < 1 || pattern.length() < 1) {
 353             throw new IllegalArgumentException();
 354         }
 355         checkPermission();
 356         configure();
 357         this.pattern = pattern;
 358         this.limit = limit;
 359         this.count = count;
 360         openFiles();
 361     }
 362 
 363     /**
 364      * Initialize a <tt>FileHandler</tt> to write to a set of files
 365      * with optional append.  When (approximately) the given limit has
 366      * been written to one file, another file will be opened.  The
 367      * output will cycle through a set of count files.
 368      * <p>
 369      * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
 370      * properties (or their default values) except that the given pattern
 371      * argument is used as the filename pattern, the file limit is
 372      * set to the limit argument, and the file count is set to the
 373      * given count argument, and the append mode is set to the given
 374      * <tt>append</tt> argument.
 375      * <p>
 376      * The count must be at least 1.
 377      *
 378      * @param pattern  the pattern for naming the output file
 379      * @param limit  the maximum number of bytes to write to any one file
 380      * @param count  the number of files to use
 381      * @param append  specifies append mode
 382      * @exception  IOException if there are IO problems opening the files.
 383      * @exception  SecurityException  if a security manager exists and if
 384      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 385      * @exception  IllegalArgumentException if {@code limit < 0}, or {@code count < 1}.
 386      * @exception  IllegalArgumentException if pattern is an empty string
 387      *
 388      */
 389     public FileHandler(String pattern, int limit, int count, boolean append)
 390                                         throws IOException, SecurityException {
 391         if (limit < 0 || count < 1 || pattern.length() < 1) {
 392             throw new IllegalArgumentException();
 393         }
 394         checkPermission();
 395         configure();
 396         this.pattern = pattern;
 397         this.limit = limit;
 398         this.count = count;
 399         this.append = append;
 400         openFiles();
 401     }
 402 
 403     /**
 404      * Open the set of output files, based on the configured
 405      * instance variables.
 406      */
 407     private void openFiles() throws IOException {
 408         LogManager manager = LogManager.getLogManager();
 409         manager.checkPermission();
 410         if (count < 1) {
 411            throw new IllegalArgumentException("file count = " + count);
 412         }
 413         if (limit < 0) {
 414             limit = 0;
 415         }
 416 
 417         // We register our own ErrorManager during initialization
 418         // so we can record exceptions.
 419         InitializationErrorManager em = new InitializationErrorManager();
 420         setErrorManager(em);
 421 
 422         // Create a lock file.  This grants us exclusive access
 423         // to our set of output files, as long as we are alive.
 424         int unique = -1;
 425         for (;;) {
 426             unique++;
 427             if (unique > MAX_LOCKS) {
 428                 throw new IOException("Couldn't get lock for " + pattern);
 429             }
 430             // Generate a lock file name from the "unique" int.
 431             lockFileName = generate(pattern, 0, unique).toString() + ".lck";
 432             // Now try to lock that filename.
 433             // Because some systems (e.g., Solaris) can only do file locks
 434             // between processes (and not within a process), we first check
 435             // if we ourself already have the file locked.
 436             synchronized(locks) {
 437                 if (locks.contains(lockFileName)) {
 438                     // We already own this lock, for a different FileHandler
 439                     // object.  Try again.
 440                     continue;
 441                 }
 442 
 443                 final Path lockFilePath = Paths.get(lockFileName);
 444                 boolean fileCreated;
 445                 try {
 446                     lockFileChannel = FileChannel.open(lockFilePath,
 447                             CREATE_NEW, WRITE);
 448                     fileCreated = true;
 449                 } catch (FileAlreadyExistsException ix) {
 450                     // This may be a zombie file left over by a previous
 451                     // execution. Reuse it - but only if we can actually
 452                     // write to its directory.
 453                     if (Files.isRegularFile(lockFilePath) 
 454                             && Files.isWritable(lockFilePath)
 455                             && Files.isWritable(lockFilePath.getParent())) {
 456                         lockFileChannel = FileChannel.open(lockFilePath, 
 457                                 WRITE, APPEND);
 458                         fileCreated = false;
 459                     } else {
 460                         // try the next lock file name in the sequence
 461                         continue;
 462                     }
 463                 }
 464 
 465                 boolean available;
 466                 try {
 467                     available = lockFileChannel.tryLock() != null;
 468                     // We got the lock OK.
 469                     // At this point we could call File.deleteOnExit().
 470                     // However, this could have undesirable side effects
 471                     // as indicated by JDK-4872014. So we will instead
 472                     // rely on the fact that close() will remove the lock
 473                     // file and that whoever is creating FileHandlers should
 474                     // be responsible for closing them.
 475                 } catch (IOException ix) {
 476                     // We got an IOException while trying to get the lock.
 477                     // This normally indicates that locking is not supported
 478                     // on the target directory.  We have to proceed without
 479                     // getting a lock.   Drop through, but only if we did
 480                     // create the file...
 481                     available = fileCreated;
 482                 } catch (OverlappingFileLockException x) {
 483                     // someone already locked this file in this VM.
 484                     // continue searching for an available lock.
 485                     available = false;
 486                 }
 487                 if (available) {
 488                     // We got the lock.  Remember it.
 489                     locks.add(lockFileName);
 490                     break;
 491                 }
 492 
 493                 // We failed to get the lock.  Try next file.
 494                 lockFileChannel.close();
 495             }
 496         }
 497 
 498         files = new File[count];
 499         for (int i = 0; i < count; i++) {
 500             files[i] = generate(pattern, i, unique);
 501         }
 502 
 503         // Create the initial log file.
 504         if (append) {
 505             open(files[0], true);
 506         } else {
 507             rotate();
 508         }
 509 
 510         // Did we detect any exceptions during initialization?
 511         Exception ex = em.lastException;
 512         if (ex != null) {
 513             if (ex instanceof IOException) {
 514                 throw (IOException) ex;
 515             } else if (ex instanceof SecurityException) {
 516                 throw (SecurityException) ex;
 517             } else {
 518                 throw new IOException("Exception: " + ex);
 519             }
 520         }
 521 
 522         // Install the normal default ErrorManager.
 523         setErrorManager(new ErrorManager());
 524     }
 525 
 526     /**
 527      * Generate a file based on a user-supplied pattern, generation number,
 528      * and an integer uniqueness suffix
 529      * @param pattern the pattern for naming the output file
 530      * @param generation the generation number to distinguish rotated logs
 531      * @param unique a unique number to resolve conflicts
 532      * @return the generated File
 533      * @throws IOException
 534      */
 535     private File generate(String pattern, int generation, int unique)
 536             throws IOException {
 537         File file = null;
 538         String word = "";
 539         int ix = 0;
 540         boolean sawg = false;
 541         boolean sawu = false;
 542         while (ix < pattern.length()) {
 543             char ch = pattern.charAt(ix);
 544             ix++;
 545             char ch2 = 0;
 546             if (ix < pattern.length()) {
 547                 ch2 = Character.toLowerCase(pattern.charAt(ix));
 548             }
 549             if (ch == '/') {
 550                 if (file == null) {
 551                     file = new File(word);
 552                 } else {
 553                     file = new File(file, word);
 554                 }
 555                 word = "";
 556                 continue;
 557             } else  if (ch == '%') {
 558                 if (ch2 == 't') {
 559                     String tmpDir = System.getProperty("java.io.tmpdir");
 560                     if (tmpDir == null) {
 561                         tmpDir = System.getProperty("user.home");
 562                     }
 563                     file = new File(tmpDir);
 564                     ix++;
 565                     word = "";
 566                     continue;
 567                 } else if (ch2 == 'h') {
 568                     file = new File(System.getProperty("user.home"));
 569                     if (sun.misc.VM.isSetUID()) {
 570                         // Ok, we are in a set UID program.  For safety's sake
 571                         // we disallow attempts to open files relative to %h.
 572                         throw new IOException("can't use %h in set UID program");
 573                     }
 574                     ix++;
 575                     word = "";
 576                     continue;
 577                 } else if (ch2 == 'g') {
 578                     word = word + generation;
 579                     sawg = true;
 580                     ix++;
 581                     continue;
 582                 } else if (ch2 == 'u') {
 583                     word = word + unique;
 584                     sawu = true;
 585                     ix++;
 586                     continue;
 587                 } else if (ch2 == '%') {
 588                     word = word + "%";
 589                     ix++;
 590                     continue;
 591                 }
 592             }
 593             word = word + ch;
 594         }
 595         if (count > 1 && !sawg) {
 596             word = word + "." + generation;
 597         }
 598         if (unique > 0 && !sawu) {
 599             word = word + "." + unique;
 600         }
 601         if (word.length() > 0) {
 602             if (file == null) {
 603                 file = new File(word);
 604             } else {
 605                 file = new File(file, word);
 606             }
 607         }
 608         return file;
 609     }
 610 
 611     /**
 612      * Rotate the set of output files
 613      */
 614     private synchronized void rotate() {
 615         Level oldLevel = getLevel();
 616         setLevel(Level.OFF);
 617 
 618         super.close();
 619         for (int i = count-2; i >= 0; i--) {
 620             File f1 = files[i];
 621             File f2 = files[i+1];
 622             if (f1.exists()) {
 623                 if (f2.exists()) {
 624                     f2.delete();
 625                 }
 626                 f1.renameTo(f2);
 627             }
 628         }
 629         try {
 630             open(files[0], false);
 631         } catch (IOException ix) {
 632             // We don't want to throw an exception here, but we
 633             // report the exception to any registered ErrorManager.
 634             reportError(null, ix, ErrorManager.OPEN_FAILURE);
 635 
 636         }
 637         setLevel(oldLevel);
 638     }
 639 
 640     /**
 641      * Format and publish a <tt>LogRecord</tt>.
 642      *
 643      * @param  record  description of the log event. A null record is
 644      *                 silently ignored and is not published
 645      */
 646     @Override
 647     public synchronized void publish(LogRecord record) {
 648         if (!isLoggable(record)) {
 649             return;
 650         }
 651         super.publish(record);
 652         flush();
 653         if (limit > 0 && meter.written >= limit) {
 654             // We performed access checks in the "init" method to make sure
 655             // we are only initialized from trusted code.  So we assume
 656             // it is OK to write the target files, even if we are
 657             // currently being called from untrusted code.
 658             // So it is safe to raise privilege here.
 659             AccessController.doPrivileged(new PrivilegedAction<Object>() {
 660                 @Override
 661                 public Object run() {
 662                     rotate();
 663                     return null;
 664                 }
 665             });
 666         }
 667     }
 668 
 669     /**
 670      * Close all the files.
 671      *
 672      * @exception  SecurityException  if a security manager exists and if
 673      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 674      */
 675     @Override
 676     public synchronized void close() throws SecurityException {
 677         super.close();
 678         // Unlock any lock file.
 679         if (lockFileName == null) {
 680             return;
 681         }
 682         try {
 683             // Close the lock file channel (which also will free any locks)
 684             lockFileChannel.close();
 685         } catch (Exception ex) {
 686             // Problems closing the stream.  Punt.
 687         }
 688         synchronized(locks) {
 689             locks.remove(lockFileName);
 690         }
 691         new File(lockFileName).delete();
 692         lockFileName = null;
 693         lockFileChannel = null;
 694     }
 695 
 696     private static class InitializationErrorManager extends ErrorManager {
 697         Exception lastException;
 698         @Override
 699         public void error(String msg, Exception ex, int code) {
 700             lastException = ex;
 701         }
 702     }
 703 }