@Override public LoggingBackend run() { final Iterator<LoggerFinder> iterator = ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader()) .iterator(); if (iterator.hasNext()) { return LoggingBackend.CUSTOM; // Custom Logger Provider is registered } // No custom logger provider: we will be using the default // backend. final Iterator<DefaultLoggerFinder> iterator2 = ServiceLoader.loadInstalled(DefaultLoggerFinder.class) .iterator(); if (iterator2.hasNext()) { // LoggingProviderImpl is registered. The default // implementation is java.util.logging String cname = System.getProperty("java.util.logging.config.class"); String fname = System.getProperty("java.util.logging.config.file"); return (cname != null || fname != null) ? LoggingBackend.JUL_WITH_CONFIG : LoggingBackend.JUL_DEFAULT; } else { // SimpleConsoleLogger is used return LoggingBackend.NONE; } }
private static LoggerFinder accessLoggerFinder() { LoggerFinder prov = provider; if (prov == null) { // no need to lock: it doesn't matter if we call // getLoggerFinder() twice - since LoggerFinder already caches // the result. // This is just an optimization to avoid the cost of calling // doPrivileged every time. final SecurityManager sm = System.getSecurityManager(); prov = sm == null ? LoggerFinder.getLoggerFinder() : AccessController.doPrivileged( (PrivilegedAction<LoggerFinder>)LoggerFinder::getLoggerFinder); provider = prov; } return prov; }
static void checkLogEvent(LoggerFinder provider, String desc, LogEvent expected, boolean expectNotNull) { LogEvent actual = eventQueue.poll(); if (actual == null && !expectNotNull) return; if (actual != null && !expectNotNull) { throw new RuntimeException("Unexpected log event found for " + desc + "\n\tgot: " + actual); } if (!expected.equals(actual)) { throw new RuntimeException("mismatch for " + desc + "\n\texpected=" + expected + "\n\t actual=" + actual); } else { verbose("Got expected results for " + desc + "\n\t" + expected); } }
static Logger getLogger(LoggerFinder provider, String name, Module caller) { Logger logger; try { logger = Logger.class.cast(lazyGetLogger.invoke(null, name, caller)); } catch (Throwable x) { Throwable t = (x instanceof InvocationTargetException) ? ((InvocationTargetException)x).getTargetException() : x; if (t instanceof RuntimeException) { throw (RuntimeException)t; } else if (t instanceof Exception) { throw new RuntimeException(t); } else { throw (Error)t; } } // The method above does not throw exception... // call the provider here to verify that an exception would have // been thrown by the provider. if (logger != null && caller == Thread.class.getModule()) { Logger log = provider.getLogger(name, caller); } return logger; }
public static void test(LoggerFinder provider, boolean hasRequiredPermissions) { ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName()); final Map<Logger, String> loggerDescMap = new HashMap<>(); System.Logger sysLogger = accessSystemLogger.getLogger("foo"); loggerDescMap.put(sysLogger, "accessSystemLogger.getLogger(\"foo\")"); System.Logger localizedSysLogger = accessSystemLogger.getLogger("fox", loggerBundle); loggerDescMap.put(localizedSysLogger, "accessSystemLogger.getLogger(\"fox\", loggerBundle)"); System.Logger appLogger = System.getLogger("bar"); loggerDescMap.put(appLogger,"System.getLogger(\"bar\")"); System.Logger localizedAppLogger = System.getLogger("baz", loggerBundle); loggerDescMap.put(localizedAppLogger,"System.getLogger(\"baz\", loggerBundle)"); testLogger(provider, loggerDescMap, "foo", null, sysLogger); testLogger(provider, loggerDescMap, "foo", loggerBundle, localizedSysLogger); testLogger(provider, loggerDescMap, "foo", null, appLogger); testLogger(provider, loggerDescMap, "foo", loggerBundle, localizedAppLogger); }
public static Logger retrieve() { final Class clazz = StackWalker .getInstance(RETAIN_CLASS_REFERENCE) .getCallerClass(); return LoggerFinder.getLoggerFinder().getLogger( clazz.getCanonicalName(), clazz.getModule()); }
public static void main(String[] args) throws Exception { LoggerFinder provider = LoggerFinder.getLoggerFinder(); java.util.logging.Logger appSink = LoggingProviderImpl.getLogManagerAccess() .demandLoggerFor(LogManager.getLogManager(), "foo", DefaultPlatformLoggerTest.class.getModule()); java.util.logging.Logger sysSink = LoggingProviderImpl.getLogManagerAccess() .demandLoggerFor(LogManager.getLogManager(),"foo", Thread.class.getModule()); java.util.logging.Logger sink = Logger.getLogger("foo"); sink.addHandler(new MyHandler()); sink.setUseParentHandlers(VERBOSE); System.out.println("\n*** Without Security Manager\n"); test(provider, true, appSink, sysSink); System.out.println("Tetscase count: " + sequencer.get()); Policy.setPolicy(new SimplePolicy(allowAll, allowControl)); System.setSecurityManager(new SecurityManager()); System.out.println("\n*** With Security Manager, without permissions\n"); test(provider, false, appSink, sysSink); System.out.println("Tetscase count: " + sequencer.get()); System.out.println("\n*** With Security Manager, with control permission\n"); allowControl.get().set(true); test(provider, true, appSink, sysSink); System.out.println("\nPASSED: Tested " + sequencer.get() + " cases."); }
public static void test(LoggerFinder provider, boolean hasRequiredPermissions, java.util.logging.Logger appSink, java.util.logging.Logger sysSink) throws Exception { // No way to give a resource bundle to a platform logger. // ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName()); final Map<PlatformLogger, String> loggerDescMap = new HashMap<>(); PlatformLogger platform = PlatformLogger.getLogger("foo"); loggerDescMap.put(platform, "PlatformLogger.getLogger(\"foo\")"); testLogger(provider, loggerDescMap, "foo", null, platform, sysSink); }
static void checkLogEvent(LoggerFinder provider, String desc, LogEvent expected) { LogEvent actual = eventQueue.poll(); if (!expected.equals(actual)) { throw new RuntimeException("mismatch for " + desc + "\n\texpected=" + expected + "\n\t actual=" + actual); } else { verbose("Got expected results for " + desc + "\n\t" + expected); } }
static Logger getLogger(LoggerFinder provider, String name, ResourceBundle bundle, Module caller) { if (getClassLoader(caller) != null) { return System.getLogger(name,bundle); } else { return provider.getLocalizedLogger(name, bundle, caller); } }
static TestLoggerFinder getLoggerFinder(Class<?> expectedClass) { LoggerFinder provider = null; try { TestLoggerFinder.sequencer.incrementAndGet(); provider = LoggerFinder.getLoggerFinder(); } catch(AccessControlException a) { throw a; } ErrorStream.errorStream.store(); System.out.println("*** Actual LoggerFinder class is: " + provider.getClass().getName()); expectedClass.cast(provider); return TestLoggerFinder.class.cast(provider); }
boolean accept(String name) { final boolean res = !name.endsWith(LoggerFinder.class.getName()); if (res == false) { System.out.println("Hiding " + name); } return res; }
static java.lang.System.Logger getSystemLogger(String name, ResourceBundle bundle, Module caller) throws Exception { try { LoggerFinder provider = LoggerFinder.class.cast(accessLoggerFinder.invoke(null)); return provider.getLocalizedLogger(name, bundle, caller); } catch (InvocationTargetException x) { Throwable t = x.getTargetException(); if (t instanceof Exception) { throw (Exception)t; } else { throw (Error)t; } } }
public static void main(String[] argv) throws Exception { final AtomicInteger nb = new AtomicInteger(0); final boolean hidesProvider = Boolean.getBoolean("test.logger.hidesProvider"); System.out.println(ClassLoader.getSystemClassLoader()); final BackendTesterFactory factory; if (java.lang.System.LoggerFinder.getLoggerFinder() instanceof CustomLoggerFinder) { if (hidesProvider) { System.err.println("Custom backend " + java.lang.System.LoggerFinder.getLoggerFinder() + " should have been hidden!"); throw new RuntimeException( "Custom backend should have been hidden: " + "check value of java.system.class.loader property"); } System.out.println("Using custom backend"); factory = new CustomBackendTesterFactory(); } else { if (!hidesProvider) { System.err.println("Default JUL backend " + java.lang.System.LoggerFinder.getLoggerFinder() + " should have been hidden!"); throw new RuntimeException( "Default JUL backend should have been hidden: " + "check value of java.system.class.loader property"); } System.out.println("Using JUL backend"); factory = new JULBackendTesterFactory(); } testBackend(nb, factory); }
void testGetLogger(String desc, String name, Module mod, Class<? extends Throwable> thrown) { try { LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); Logger logger = finder.getLogger(name, mod); if (thrown != null) { throw new AssertionError("Exception " + thrown.getName() + " not thrown for " + "LoggerFinder.getLogger" + " with " + desc); } // Make sure we don't fail if tests are run in parallel synchronized(RecordStream.LOCK) { LOG_STREAM.startRecording(); byte[] logged = null; try { logger.log(Level.INFO, "{0} with {1}: PASSED", "LoggerFinder.getLogger", desc); } finally { logged = LOG_STREAM.stopRecording(); } check(logged, "testGetLogger", desc, null, "LoggerFinder.getLogger"); } } catch (Throwable x) { if (thrown != null && thrown.isInstance(x)) { System.out.printf("Got expected exception for %s with %s: %s\n", "LoggerFinder.getLogger", desc, String.valueOf(x)); } else throw x; } }
void testGetLocalizedLogger(String desc, String name, ResourceBundle bundle, Module mod, Class<? extends Throwable> thrown) { try { LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); Logger logger = finder.getLocalizedLogger(name, bundle, mod); if (thrown != null) { throw new AssertionError("Exception " + thrown.getName() + " not thrown for " + "LoggerFinder.getLocalizedLogger" + " with " + desc); } // Make sure we don't fail if tests are run in parallel synchronized(RecordStream.LOCK) { LOG_STREAM.startRecording(); byte[] logged = null; try { logger.log(Level.INFO, MESSAGE, "LoggerFinder.getLocalizedLogger", desc); } finally { logged = LOG_STREAM.stopRecording(); } check(logged, "testGetLocalizedLogger", desc, bundle, "LoggerFinder.getLocalizedLogger"); } } catch (Throwable x) { if (thrown != null && thrown.isInstance(x)) { System.out.printf("Got expected exception for %s with %s: %s\n", "LoggerFinder.getLocalizedLogger", desc, String.valueOf(x)); } else throw x; } }
public static void main(String[] args) throws Exception { LoggerFinder provider = LoggerFinder.getLoggerFinder(); java.util.logging.Logger appSink = LoggingProviderImpl.getLogManagerAccess() .demandLoggerFor(LogManager.getLogManager(), "foo", DefaultPlatformLoggerTest.class.getModule()); java.util.logging.Logger sysSink = LoggingProviderImpl.getLogManagerAccess() .demandLoggerFor(LogManager.getLogManager(),"foo", Thread.class.getModule()); appSink.addHandler(new MyHandler()); sysSink.addHandler(new MyHandler()); appSink.setUseParentHandlers(VERBOSE); sysSink.setUseParentHandlers(VERBOSE); System.out.println("\n*** Without Security Manager\n"); test(provider, true, appSink, sysSink); System.out.println("Tetscase count: " + sequencer.get()); Policy.setPolicy(new SimplePolicy(allowAll, allowControl)); System.setSecurityManager(new SecurityManager()); System.out.println("\n*** With Security Manager, without permissions\n"); test(provider, false, appSink, sysSink); System.out.println("Tetscase count: " + sequencer.get()); System.out.println("\n*** With Security Manager, with control permission\n"); allowControl.get().set(true); test(provider, true, appSink, sysSink); System.out.println("\nPASSED: Tested " + sequencer.get() + " cases."); }
public static void test(LoggerFinder provider, boolean hasRequiredPermissions, java.util.logging.Logger appSink, java.util.logging.Logger sysSink) throws Exception { // No way to giva a resource bundle to a platform logger. // ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName()); final Map<PlatformLogger, String> loggerDescMap = new HashMap<>(); PlatformLogger platform = PlatformLogger.getLogger("foo"); loggerDescMap.put(platform, "PlatformLogger.getLogger(\"foo\")"); testLogger(provider, loggerDescMap, "foo", null, platform, sysSink); }