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