212 return new CloseOnReset(logger);
213 }
214 }
215 private final CopyOnWriteArrayList<CloseOnReset> closeOnResetLoggers =
216 new CopyOnWriteArrayList<>();
217
218
219 private final Map<Object, Runnable> listeners =
220 Collections.synchronizedMap(new IdentityHashMap<>());
221
222 static {
223 manager = AccessController.doPrivileged(new PrivilegedAction<LogManager>() {
224 @Override
225 public LogManager run() {
226 LogManager mgr = null;
227 String cname = null;
228 try {
229 cname = System.getProperty("java.util.logging.manager");
230 if (cname != null) {
231 try {
232 Class<?> clz = ClassLoader.getSystemClassLoader()
233 .loadClass(cname);
234 mgr = (LogManager) clz.newInstance();
235 } catch (ClassNotFoundException ex) {
236 Class<?> clz = Thread.currentThread()
237 .getContextClassLoader().loadClass(cname);
238 mgr = (LogManager) clz.newInstance();
239 }
240 }
241 } catch (Exception ex) {
242 System.err.println("Could not load Logmanager \"" + cname + "\"");
243 ex.printStackTrace();
244 }
245 if (mgr == null) {
246 mgr = new LogManager();
247 }
248 return mgr;
249
250 }
251 });
252 }
253
254 // This private class is used as a shutdown hook.
255 // It does a "reset" to close all open handlers.
256 private class Cleaner extends Thread {
257
258 private Cleaner() {
959 List<Handler> handlers)
960 {
961 final boolean ensureCloseOnReset = ! handlers.isEmpty()
962 && getBooleanProperty(handlersPropertyName + ".ensureCloseOnReset",true);
963 int count = 0;
964 for (Handler hdl : handlers) {
965 logger.addHandler(hdl);
966 if (++count == 1 && ensureCloseOnReset) {
967 // add this logger to the closeOnResetLoggers list.
968 closeOnResetLoggers.addIfAbsent(CloseOnReset.create(logger));
969 }
970 }
971 }
972
973 private List<Handler> createLoggerHandlers(final String name, final String handlersPropertyName)
974 {
975 String names[] = parseClassNames(handlersPropertyName);
976 List<Handler> handlers = new ArrayList<>(names.length);
977 for (String type : names) {
978 try {
979 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(type);
980 Handler hdl = (Handler) clz.newInstance();
981 // Check if there is a property defining the
982 // this handler's level.
983 String levs = getProperty(type + ".level");
984 if (levs != null) {
985 Level l = Level.findLevel(levs);
986 if (l != null) {
987 hdl.setLevel(l);
988 } else {
989 // Probably a bad level. Drop through.
990 System.err.println("Can't set level for " + type);
991 }
992 }
993 // Add this Handler to the logger
994 handlers.add(hdl);
995 } catch (Exception ex) {
996 System.err.println("Can't load log handler \"" + type + "\"");
997 System.err.println("" + ex);
998 ex.printStackTrace();
999 }
1000 }
1298 * {@link #updateConfiguration(java.util.function.Function)} or
1299 * {@link #updateConfiguration(java.io.InputStream, java.util.function.Function)}
1300 * methods instead.
1301 *
1302 * @throws SecurityException if a security manager exists and if
1303 * the caller does not have LoggingPermission("control").
1304 * @throws IOException if there are IO problems reading the configuration.
1305 */
1306 public void readConfiguration() throws IOException, SecurityException {
1307 checkPermission();
1308
1309 // if a configuration class is specified, load it and use it.
1310 String cname = System.getProperty("java.util.logging.config.class");
1311 if (cname != null) {
1312 try {
1313 // Instantiate the named class. It is its constructor's
1314 // responsibility to initialize the logging configuration, by
1315 // calling readConfiguration(InputStream) with a suitable stream.
1316 try {
1317 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
1318 clz.newInstance();
1319 return;
1320 } catch (ClassNotFoundException ex) {
1321 Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
1322 clz.newInstance();
1323 return;
1324 }
1325 } catch (Exception ex) {
1326 System.err.println("Logging configuration class \"" + cname + "\" failed");
1327 System.err.println("" + ex);
1328 // keep going and useful config file.
1329 }
1330 }
1331
1332 String fname = getConfigurationFileName();
1333 try (final InputStream in = new FileInputStream(fname)) {
1334 final BufferedInputStream bin = new BufferedInputStream(in);
1335 readConfiguration(bin);
1336 }
1337 }
1338
1339 String getConfigurationFileName() throws IOException {
1340 String fname = System.getProperty("java.util.logging.config.file");
1341 if (fname == null) {
1342 fname = System.getProperty("java.home");
1529 try {
1530 // Load the properties
1531 props.load(ins);
1532 } catch (IllegalArgumentException x) {
1533 // props.load may throw an IllegalArgumentException if the stream
1534 // contains malformed Unicode escape sequences.
1535 // We wrap that in an IOException as readConfiguration is
1536 // specified to throw IOException if there are problems reading
1537 // from the stream.
1538 // Note: new IOException(x.getMessage(), x) allow us to get a more
1539 // concise error message than new IOException(x);
1540 throw new IOException(x.getMessage(), x);
1541 }
1542
1543 // Instantiate new configuration objects.
1544 String names[] = parseClassNames("config");
1545
1546 for (String word : names) {
1547 try {
1548 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
1549 clz.newInstance();
1550 } catch (Exception ex) {
1551 System.err.println("Can't load config class \"" + word + "\"");
1552 System.err.println("" + ex);
1553 // ex.printStackTrace();
1554 }
1555 }
1556
1557 // Set levels on any pre-existing loggers, based on the new properties.
1558 setLevelsOnExistingLoggers();
1559
1560 // Note that we need to reinitialize global handles when
1561 // they are first referenced.
1562 globalHandlersState = STATE_UNINITIALIZED;
1563 } catch (Throwable t) {
1564 // If there were any trouble, then set state to STATE_INITIALIZED
1565 // so that no global handlers reinitialization is performed on not fully
1566 // initialized configuration.
1567 globalHandlersState = STATE_INITIALIZED;
1568 // re-throw
1569 throw t;
2275 // Package private method to get a Level property.
2276 // If the property is not defined or cannot be parsed
2277 // we return the given default value.
2278 Level getLevelProperty(String name, Level defaultValue) {
2279 String val = getProperty(name);
2280 if (val == null) {
2281 return defaultValue;
2282 }
2283 Level l = Level.findLevel(val.trim());
2284 return l != null ? l : defaultValue;
2285 }
2286
2287 // Package private method to get a filter property.
2288 // We return an instance of the class named by the "name"
2289 // property. If the property is not defined or has problems
2290 // we return the defaultValue.
2291 Filter getFilterProperty(String name, Filter defaultValue) {
2292 String val = getProperty(name);
2293 try {
2294 if (val != null) {
2295 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2296 return (Filter) clz.newInstance();
2297 }
2298 } catch (Exception ex) {
2299 // We got one of a variety of exceptions in creating the
2300 // class or creating an instance.
2301 // Drop through.
2302 }
2303 // We got an exception. Return the defaultValue.
2304 return defaultValue;
2305 }
2306
2307
2308 // Package private method to get a formatter property.
2309 // We return an instance of the class named by the "name"
2310 // property. If the property is not defined or has problems
2311 // we return the defaultValue.
2312 Formatter getFormatterProperty(String name, Formatter defaultValue) {
2313 String val = getProperty(name);
2314 try {
2315 if (val != null) {
2316 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2317 return (Formatter) clz.newInstance();
2318 }
2319 } catch (Exception ex) {
2320 // We got one of a variety of exceptions in creating the
2321 // class or creating an instance.
2322 // Drop through.
2323 }
2324 // We got an exception. Return the defaultValue.
2325 return defaultValue;
2326 }
2327
2328 // Private method to load the global handlers.
2329 // We do the real work lazily, when the global handlers
2330 // are first used.
2331 private void initializeGlobalHandlers() {
2332 int state = globalHandlersState;
2333 if (state == STATE_INITIALIZED ||
2334 state == STATE_SHUTDOWN) {
2335 // Nothing to do: return.
2336 return;
2337 }
|
212 return new CloseOnReset(logger);
213 }
214 }
215 private final CopyOnWriteArrayList<CloseOnReset> closeOnResetLoggers =
216 new CopyOnWriteArrayList<>();
217
218
219 private final Map<Object, Runnable> listeners =
220 Collections.synchronizedMap(new IdentityHashMap<>());
221
222 static {
223 manager = AccessController.doPrivileged(new PrivilegedAction<LogManager>() {
224 @Override
225 public LogManager run() {
226 LogManager mgr = null;
227 String cname = null;
228 try {
229 cname = System.getProperty("java.util.logging.manager");
230 if (cname != null) {
231 try {
232 @SuppressWarnings("deprecation")
233 Object tmp = ClassLoader.getSystemClassLoader()
234 .loadClass(cname).newInstance();
235 mgr = (LogManager) tmp;
236 } catch (ClassNotFoundException ex) {
237 @SuppressWarnings("deprecation")
238 Object tmp = Thread.currentThread()
239 .getContextClassLoader().loadClass(cname).newInstance();
240 mgr = (LogManager) tmp;
241 }
242 }
243 } catch (Exception ex) {
244 System.err.println("Could not load Logmanager \"" + cname + "\"");
245 ex.printStackTrace();
246 }
247 if (mgr == null) {
248 mgr = new LogManager();
249 }
250 return mgr;
251
252 }
253 });
254 }
255
256 // This private class is used as a shutdown hook.
257 // It does a "reset" to close all open handlers.
258 private class Cleaner extends Thread {
259
260 private Cleaner() {
961 List<Handler> handlers)
962 {
963 final boolean ensureCloseOnReset = ! handlers.isEmpty()
964 && getBooleanProperty(handlersPropertyName + ".ensureCloseOnReset",true);
965 int count = 0;
966 for (Handler hdl : handlers) {
967 logger.addHandler(hdl);
968 if (++count == 1 && ensureCloseOnReset) {
969 // add this logger to the closeOnResetLoggers list.
970 closeOnResetLoggers.addIfAbsent(CloseOnReset.create(logger));
971 }
972 }
973 }
974
975 private List<Handler> createLoggerHandlers(final String name, final String handlersPropertyName)
976 {
977 String names[] = parseClassNames(handlersPropertyName);
978 List<Handler> handlers = new ArrayList<>(names.length);
979 for (String type : names) {
980 try {
981 @SuppressWarnings("deprecation")
982 Object o = ClassLoader.getSystemClassLoader().loadClass(type).newInstance();
983 Handler hdl = (Handler) o;
984 // Check if there is a property defining the
985 // this handler's level.
986 String levs = getProperty(type + ".level");
987 if (levs != null) {
988 Level l = Level.findLevel(levs);
989 if (l != null) {
990 hdl.setLevel(l);
991 } else {
992 // Probably a bad level. Drop through.
993 System.err.println("Can't set level for " + type);
994 }
995 }
996 // Add this Handler to the logger
997 handlers.add(hdl);
998 } catch (Exception ex) {
999 System.err.println("Can't load log handler \"" + type + "\"");
1000 System.err.println("" + ex);
1001 ex.printStackTrace();
1002 }
1003 }
1301 * {@link #updateConfiguration(java.util.function.Function)} or
1302 * {@link #updateConfiguration(java.io.InputStream, java.util.function.Function)}
1303 * methods instead.
1304 *
1305 * @throws SecurityException if a security manager exists and if
1306 * the caller does not have LoggingPermission("control").
1307 * @throws IOException if there are IO problems reading the configuration.
1308 */
1309 public void readConfiguration() throws IOException, SecurityException {
1310 checkPermission();
1311
1312 // if a configuration class is specified, load it and use it.
1313 String cname = System.getProperty("java.util.logging.config.class");
1314 if (cname != null) {
1315 try {
1316 // Instantiate the named class. It is its constructor's
1317 // responsibility to initialize the logging configuration, by
1318 // calling readConfiguration(InputStream) with a suitable stream.
1319 try {
1320 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
1321 @SuppressWarnings("deprecation")
1322 Object witness = clz.newInstance();
1323 return;
1324 } catch (ClassNotFoundException ex) {
1325 Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
1326 @SuppressWarnings("deprecation")
1327 Object witness = clz.newInstance();
1328 return;
1329 }
1330 } catch (Exception ex) {
1331 System.err.println("Logging configuration class \"" + cname + "\" failed");
1332 System.err.println("" + ex);
1333 // keep going and useful config file.
1334 }
1335 }
1336
1337 String fname = getConfigurationFileName();
1338 try (final InputStream in = new FileInputStream(fname)) {
1339 final BufferedInputStream bin = new BufferedInputStream(in);
1340 readConfiguration(bin);
1341 }
1342 }
1343
1344 String getConfigurationFileName() throws IOException {
1345 String fname = System.getProperty("java.util.logging.config.file");
1346 if (fname == null) {
1347 fname = System.getProperty("java.home");
1534 try {
1535 // Load the properties
1536 props.load(ins);
1537 } catch (IllegalArgumentException x) {
1538 // props.load may throw an IllegalArgumentException if the stream
1539 // contains malformed Unicode escape sequences.
1540 // We wrap that in an IOException as readConfiguration is
1541 // specified to throw IOException if there are problems reading
1542 // from the stream.
1543 // Note: new IOException(x.getMessage(), x) allow us to get a more
1544 // concise error message than new IOException(x);
1545 throw new IOException(x.getMessage(), x);
1546 }
1547
1548 // Instantiate new configuration objects.
1549 String names[] = parseClassNames("config");
1550
1551 for (String word : names) {
1552 try {
1553 Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
1554 @SuppressWarnings("deprecation")
1555 Object witness = clz.newInstance();
1556 } catch (Exception ex) {
1557 System.err.println("Can't load config class \"" + word + "\"");
1558 System.err.println("" + ex);
1559 // ex.printStackTrace();
1560 }
1561 }
1562
1563 // Set levels on any pre-existing loggers, based on the new properties.
1564 setLevelsOnExistingLoggers();
1565
1566 // Note that we need to reinitialize global handles when
1567 // they are first referenced.
1568 globalHandlersState = STATE_UNINITIALIZED;
1569 } catch (Throwable t) {
1570 // If there were any trouble, then set state to STATE_INITIALIZED
1571 // so that no global handlers reinitialization is performed on not fully
1572 // initialized configuration.
1573 globalHandlersState = STATE_INITIALIZED;
1574 // re-throw
1575 throw t;
2281 // Package private method to get a Level property.
2282 // If the property is not defined or cannot be parsed
2283 // we return the given default value.
2284 Level getLevelProperty(String name, Level defaultValue) {
2285 String val = getProperty(name);
2286 if (val == null) {
2287 return defaultValue;
2288 }
2289 Level l = Level.findLevel(val.trim());
2290 return l != null ? l : defaultValue;
2291 }
2292
2293 // Package private method to get a filter property.
2294 // We return an instance of the class named by the "name"
2295 // property. If the property is not defined or has problems
2296 // we return the defaultValue.
2297 Filter getFilterProperty(String name, Filter defaultValue) {
2298 String val = getProperty(name);
2299 try {
2300 if (val != null) {
2301 @SuppressWarnings("deprecation")
2302 Object o = ClassLoader.getSystemClassLoader().loadClass(val).newInstance();
2303 return (Filter) o;
2304 }
2305 } catch (Exception ex) {
2306 // We got one of a variety of exceptions in creating the
2307 // class or creating an instance.
2308 // Drop through.
2309 }
2310 // We got an exception. Return the defaultValue.
2311 return defaultValue;
2312 }
2313
2314
2315 // Package private method to get a formatter property.
2316 // We return an instance of the class named by the "name"
2317 // property. If the property is not defined or has problems
2318 // we return the defaultValue.
2319 Formatter getFormatterProperty(String name, Formatter defaultValue) {
2320 String val = getProperty(name);
2321 try {
2322 if (val != null) {
2323 @SuppressWarnings("deprecation")
2324 Object o = ClassLoader.getSystemClassLoader().loadClass(val).newInstance();
2325 return (Formatter) o;
2326 }
2327 } catch (Exception ex) {
2328 // We got one of a variety of exceptions in creating the
2329 // class or creating an instance.
2330 // Drop through.
2331 }
2332 // We got an exception. Return the defaultValue.
2333 return defaultValue;
2334 }
2335
2336 // Private method to load the global handlers.
2337 // We do the real work lazily, when the global handlers
2338 // are first used.
2339 private void initializeGlobalHandlers() {
2340 int state = globalHandlersState;
2341 if (state == STATE_INITIALIZED ||
2342 state == STATE_SHUTDOWN) {
2343 // Nothing to do: return.
2344 return;
2345 }
|