1 /*
   2  * Copyright (c) 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.util.logger;
  28 
  29 import java.util.ResourceBundle;
  30 import java.util.function.Supplier;
  31 import java.lang.System.Logger;
  32 import java.lang.System.Logger.Level;
  33 
  34 /**
  35  * This implementation of {@link Logger} redirects all logging method
  36  * calls to calls to {@code log(Level, String,  ResourceBundle, ...)}
  37  * methods, passing the Logger's ResourceBundle as parameter.
  38  * So for instance a call to {@link Logger#log(Level, String)
  39  * log(Level.INFO, msg)} will be redirected
  40  * to a call to {@link #log(java.lang.System.Logger.Level,
  41  * java.util.ResourceBundle, java.lang.String, java.lang.Object...)
  42  * this.log(Level.INFO, this.bundle, msg, (Object[]) null)}.
  43  * <p>
  44  * Note that methods that take a {@link Supplier Supplier&lt;String&gt;}
  45  * or an Object are not redirected. It is assumed that a string returned
  46  * by a {@code Supplier<String>} is already localized, or cannot be localized.
  47  *
  48  * @param <L> Type of the wrapped Logger: {@code Logger} or an
  49  *        extension of the {@code Logger} interface.
  50  * @since 1.9
  51  */
  52 public class LocalizedLoggerWrapper<L extends Logger> extends LoggerWrapper<L> {
  53 
  54     private final ResourceBundle bundle;
  55 
  56     public LocalizedLoggerWrapper(L wrapped, ResourceBundle bundle) {
  57         super(wrapped);
  58         this.bundle = bundle;
  59     }
  60 
  61     public final ResourceBundle getBundle() {
  62         return bundle;
  63     }
  64 
  65     // We assume that messages returned by Supplier<String> and Object are
  66     // either already localized or not localizable. To be evaluated.
  67 
  68     // -----------------------------------------------------------------
  69     // Generic methods taking a Level as parameter
  70     // -----------------------------------------------------------------
  71 
  72     @Override
  73     public final void log(Level level, String msg) {
  74         log(level, bundle, msg, (Object[]) null);
  75     }
  76 
  77     @Override
  78     public final void log(Level level,
  79                    String msg, Throwable thrown) {
  80         log(level, bundle, msg, thrown);
  81     }
  82 
  83     @Override
  84     public final void log(Level level,
  85                     String format, Object... params) {
  86         log(level, bundle, format, params);
  87     }
  88 
  89     @Override
  90     public final void log(Level level, Object obj) {
  91         wrapped.log(level, obj);
  92     }
  93 
  94     @Override
  95     public final void log(Level level, Supplier<String> msgSupplier) {
  96         wrapped.log(level, msgSupplier);
  97     }
  98 
  99     @Override
 100     public final void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
 101         wrapped.log(level, msgSupplier, thrown);
 102     }
 103 
 104     @Override
 105     public final void log(Level level, ResourceBundle bundle, String format, Object... params) {
 106         wrapped.log(level, bundle, format, params);
 107     }
 108 
 109     @Override
 110     public final void log(Level level, ResourceBundle bundle, String key, Throwable thrown) {
 111         wrapped.log(level, bundle, key, thrown);
 112     }
 113 
 114     @Override
 115     public final boolean isLoggable(Level level) {
 116         return wrapped.isLoggable(level);
 117     }
 118 
 119     // Override methods from PlatformLoggerBridge that don't take a
 120     // resource bundle...
 121 
 122     @Override
 123     public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,
 124             String key) {
 125         logrb(level, sourceClass, sourceMethod, bundle, key, (Object[]) null);
 126     }
 127 
 128     @Override
 129     public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,
 130             String key, Throwable thrown) {
 131         logrb(level, sourceClass, sourceMethod, bundle, key, thrown);
 132     }
 133 
 134     @Override
 135     public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,
 136             String key, Object... params) {
 137         logrb(level, sourceClass, sourceMethod, bundle, key, params);
 138     }
 139 
 140     @Override
 141     public final void log(sun.util.logging.PlatformLogger.Level level, String msg, Throwable thrown) {
 142         logrb(level, bundle, msg, thrown);
 143     }
 144 
 145     @Override
 146     public final void log(sun.util.logging.PlatformLogger.Level level, String msg) {
 147         logrb(level, bundle, msg, (Object[]) null);
 148     }
 149 
 150     @Override
 151     public final void log(sun.util.logging.PlatformLogger.Level level, String format, Object... params) {
 152         logrb(level, bundle, format, params);
 153     }
 154 
 155 
 156 }