1 /*
2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8153955
27  * @summary test the FileHandler's new property
28  *  "java.util.logging.FileHandler.maxLocks" which will be present in
29  *  "logging.properties" file with default value of 100. This property can be
30  *  overriden by specifying this property in the custom config file.
31  * @library /test/lib
32  * @build jdk.test.lib.Platform
33  *        jdk.test.lib.util.FileUtils
34  * @author rpatil
35  * @run main/othervm FileHandlerMaxLocksTest
36  */
37 import java.io.File;
38 import java.io.FileWriter;
39 import java.io.IOException;
40 import java.nio.file.Paths;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.logging.FileHandler;
44 import jdk.test.lib.util.FileUtils;
45 
46 public class FileHandlerMaxLocksTest {
47 
48     private static final String LOGGER_DIR = "logger-dir";
49     private static final String MAX_LOCK_PROPERTY = "java.util.logging.FileHandler.maxLocks = 200";
50     private static final String CONFIG_FILE_NAME = "logging.properties";
51 
main(String[] args)52     public static void main(String[] args) throws Exception {
53         File loggerDir = createLoggerDir();
54         String configFilePath = loggerDir.getPath() + File.separator + CONFIG_FILE_NAME;
55         File configFile = new File(configFilePath);
56         createFile(configFile, false);
57         System.setProperty("java.util.logging.config.file", configFilePath);
58         List<FileHandler> fileHandlers = new ArrayList<>();
59         try (FileWriter writer = new FileWriter(configFile)) {
60             writer.write(MAX_LOCK_PROPERTY);
61             writer.flush();
62             // 200 raises the default limit of 100, we try 102 times
63             for (int i = 0; i < 102; i++) {
64                 fileHandlers.add(new FileHandler(loggerDir.getPath() + File.separator + "test_%u.log"));
65             }
66         } catch (IOException ie) {
67             throw new RuntimeException("Test Failed: " + ie.getMessage());
68         } finally {
69             for (FileHandler fh : fileHandlers) {
70                 fh.close();
71             }
72             FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
73         }
74     }
75 
76     /**
77      * Create a writable directory in user directory for the test
78      *
79      * @return writable directory created that needs to be deleted when done
80      * @throws RuntimeException
81      */
createLoggerDir()82     private static File createLoggerDir() throws RuntimeException {
83         String userDir = System.getProperty("user.dir", ".");
84         File loggerDir = new File(userDir, LOGGER_DIR);
85         if (!createFile(loggerDir, true)) {
86             throw new RuntimeException("Test failed: unable to create"
87                     + " writable working directory "
88                     + loggerDir.getAbsolutePath());
89         }
90         // System.out.println("Created Logger Directory: " + loggerDir.getPath());
91         return loggerDir;
92     }
93 
94     /**
95      * @param newFile  File to be created
96      * @param makeDirectory  is File to be created is directory
97      * @return true if file already exists or creation succeeded
98      */
createFile(File newFile, boolean makeDirectory)99     private static boolean createFile(File newFile, boolean makeDirectory) {
100         if (newFile.exists()) {
101             return true;
102         }
103         if (makeDirectory) {
104             return newFile.mkdir();
105         } else {
106             try {
107                 return newFile.createNewFile();
108             } catch (IOException ie) {
109                 System.err.println("Not able to create file: " + newFile
110                         + ", IOException: " + ie.getMessage());
111                 return false;
112             }
113         }
114     }
115 }
116