mchung | 0cea8e9 | 2013-01-10 19:43:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 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 | import java.io.*; |
| 25 | import java.util.*; |
| 26 | import java.util.logging.*; |
| 27 | |
| 28 | /* |
| 29 | * Custom LogManager implementation to verify that the implementation delegates |
| 30 | * to the LogManager subclass to register both system logger and user logger. |
| 31 | * |
| 32 | * The LogManager implementation is the one configuring the logger's property |
| 33 | * such as level, handler, etc. |
| 34 | */ |
| 35 | public class CustomLogManager extends LogManager { |
| 36 | static LogManager INSTANCE; |
| 37 | Map<String,Logger> namedLoggers = new HashMap<>(); |
| 38 | Properties props = initConfig(); |
| 39 | public CustomLogManager() { |
| 40 | if (INSTANCE != null) { |
| 41 | throw new RuntimeException("CustomLogManager already created"); |
| 42 | } |
| 43 | INSTANCE = this; |
| 44 | } |
| 45 | |
| 46 | public synchronized boolean addLogger(Logger logger) { |
| 47 | String name = logger.getName(); |
| 48 | if (namedLoggers.containsKey(name)) { |
| 49 | return false; |
| 50 | } |
| 51 | namedLoggers.put(name, logger); |
| 52 | // set level |
| 53 | if (props.get(name + ".level") != null) { |
| 54 | logger.setLevel(Level.parse(props.getProperty(name + ".level"))); |
| 55 | } |
| 56 | // add handlers |
| 57 | if (props.get(name + ".handlers") != null && logger.getHandlers().length == 0) { |
| 58 | logger.addHandler(new CustomHandler()); |
| 59 | } |
| 60 | // add parent loggers |
| 61 | int ix = 1; |
| 62 | for (;;) { |
| 63 | int ix2 = name.indexOf(".", ix); |
| 64 | if (ix2 < 0) { |
| 65 | break; |
| 66 | } |
| 67 | String pname = name.substring(0, ix2); |
| 68 | if (props.get(pname + ".level") != null || |
| 69 | props.get(pname + ".handlers") != null) { |
| 70 | // This pname has a level/handlers definition. |
| 71 | // Make sure it exists. |
| 72 | // |
| 73 | // The test doesn't set the parent for simplicity. |
| 74 | if (!namedLoggers.containsKey(pname)) { |
| 75 | Logger.getLogger(pname); |
| 76 | } |
| 77 | } |
| 78 | ix = ix2 + 1; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | public synchronized Logger getLogger(String name) { |
| 84 | return namedLoggers.get(name); |
| 85 | } |
| 86 | |
| 87 | public synchronized Enumeration<String> getLoggerNames() { |
| 88 | return Collections.enumeration(namedLoggers.keySet()); |
| 89 | } |
| 90 | |
| 91 | public String getProperty(String name) { |
| 92 | return props.getProperty(name); |
| 93 | } |
| 94 | |
| 95 | public void readConfiguration() { |
| 96 | // do nothing |
| 97 | } |
| 98 | |
| 99 | public void readConfiguration(InputStream ins) { |
| 100 | // do nothing |
| 101 | } |
| 102 | |
| 103 | private Properties initConfig() { |
| 104 | Properties props = new Properties(); |
| 105 | props.put(".level", "CONFIG"); |
| 106 | props.put("CustomLogManagerTest.level", "WARNING"); |
| 107 | props.put("CustomLogManagerTest.handlers", "CustomLogManager$CustomHandler"); |
| 108 | props.put("SimpleLogManager.level", "INFO"); |
| 109 | props.put("SimpleLogManager.handlers", "CustomLogManager$CustomHandler"); |
| 110 | props.put("CustomLogManager$CustomHandler.level", "WARNING"); |
| 111 | props.put(".handlers", "CustomLogManager$CustomHandler"); |
| 112 | props.put("org.foo.bar.level", "SEVERE"); |
| 113 | props.put("org.foo.handlers", "CustomLogManager$CustomHandler"); |
| 114 | props.put("org.openjdk.level", "SEVERE"); |
| 115 | props.put("org.openjdk.handlers", "CustomLogManager$CustomHandler"); |
| 116 | props.put("org.openjdk.core.level", "INFO"); |
| 117 | |
| 118 | return props; |
| 119 | } |
| 120 | |
| 121 | public static void checkLogger(String name) { |
| 122 | checkLogger(name, null); |
| 123 | } |
| 124 | |
| 125 | public static void checkLogger(String name, String resourceBundleName) { |
| 126 | Logger logger = INSTANCE.getLogger(name); |
| 127 | if (logger == null) { |
| 128 | throw new RuntimeException("Logger \"" + name + "\" not exist"); |
| 129 | } |
| 130 | System.out.format("Logger \"%s\" level=%s handlers=%s resourcebundle=%s%n", |
| 131 | name, logger.getLevel(), |
| 132 | Arrays.toString(logger.getHandlers()), |
| 133 | logger.getResourceBundleName()); |
| 134 | String rb = logger.getResourceBundleName(); |
| 135 | if (rb != resourceBundleName && (rb == null || rb.equals(resourceBundleName))) { |
| 136 | throw new RuntimeException("Logger \"" + name + |
| 137 | "\" unexpected resource bundle: " + rb); |
| 138 | } |
| 139 | |
| 140 | String value = INSTANCE.getProperty(name + ".level"); |
| 141 | String level = logger.getLevel() != null ? logger.getLevel().getName() : null; |
| 142 | if (level != value && (level == null || level.equals(value))) { |
| 143 | throw new RuntimeException("Logger \"" + name + "\" unexpected level: " + level); |
| 144 | } |
| 145 | |
| 146 | Handler[] handlers = logger.getHandlers(); |
| 147 | String hdl = INSTANCE.getProperty(name + ".handlers"); |
| 148 | if ((hdl == null && handlers.length != 0) || |
| 149 | (hdl != null && handlers.length != 1)) { |
| 150 | throw new RuntimeException("Logger \"" + name + "\" unexpected handler: " + |
| 151 | Arrays.toString(handlers)); |
| 152 | } |
| 153 | checkParents(name); |
| 154 | } |
| 155 | |
| 156 | private static void checkParents(String name) { |
| 157 | int ix = 1; |
| 158 | for (;;) { |
| 159 | int ix2 = name.indexOf(".", ix); |
| 160 | if (ix2 < 0) { |
| 161 | break; |
| 162 | } |
| 163 | String pname = name.substring(0, ix2); |
| 164 | if (INSTANCE.getProperty(pname + ".level") != null || |
| 165 | INSTANCE.getProperty(pname + ".handlers") != null) { |
| 166 | // This pname has a level/handlers definition. |
| 167 | // Make sure it exists. |
| 168 | checkLogger(pname); |
| 169 | } |
| 170 | ix = ix2 + 1; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // only CustomLogManager can create an instance of CustomHandler |
| 175 | private class CustomHandler extends StreamHandler { |
| 176 | } |
| 177 | } |