1 /*
   2  * Copyright (c) 2015, 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 jdk.internal.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  */
  51 public class LocalizedLoggerWrapper<L extends Logger> extends LoggerWrapper<L> {
  52 
  53     private final ResourceBundle bundle;
  54 
  55     public LocalizedLoggerWrapper(L wrapped, ResourceBundle bundle) {
  56         super(wrapped);
  57         this.bundle = bundle;
  58     }
  59 
  60     public final ResourceBundle getBundle() {
  61         return bundle;
  62     }
  63 
  64     // We assume that messages returned by Supplier<String> and Object are
  65     // either already localized or not localizable. To be evaluated.
  66 
  67     // -----------------------------------------------------------------
  68     // Generic methods taking a Level as parameter
  69     // -----------------------------------------------------------------
  70 
  71     @Override
  72     public final void log(Level level, String msg) {
  73         log(level, bundle, msg, (Object[]) null);
  74     }
  75 
  76     @Override
  77     public final void log(Level level,
  78                    String msg, Throwable thrown) {
  79         log(level, bundle, msg, thrown);
  80     }
  81 
  82     @Override
  83     public final void log(Level level,
  84                     String format, Object... params) {
  85         log(level, bundle, format, params);
  86     }
  87 
  88     @Override
  89     public final void log(Level level, Object obj) {
  90         wrapped.log(level, obj);
  91     }
  92 
  93     @Override
  94     public final void log(Level level, Supplier<String> msgSupplier) {
  95         wrapped.log(level, msgSupplier);
  96     }
  97 
  98     @Override
  99     public final void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
 100         wrapped.log(level, msgSupplier, thrown);
 101     }
 102 
 103     @Override
 104     public final void log(Level level, ResourceBundle bundle, String format, Object... params) {
 105         wrapped.log(level, bundle, format, params);
 106     }
 107 
 108     @Override
 109     public final void log(Level level, ResourceBundle bundle, String key, Throwable thrown) {
 110         wrapped.log(level, bundle, key, thrown);
 111     }
 112 
 113     @Override
 114     public final boolean isLoggable(Level level) {
 115         return wrapped.isLoggable(level);
 116     }
 117 
 118     // Override methods from PlatformLogger.Bridge that don't take a
 119     // resource bundle...
 120 
 121     @Override
 122     public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,
 123             String key) {
 124         logrb(level, sourceClass, sourceMethod, bundle, key, (Object[]) null);
 125     }
 126 
 127     @Override
 128     public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,
 129             String key, Throwable thrown) {
 130         logrb(level, sourceClass, sourceMethod, bundle, key, thrown);
 131     }
 132 
 133     @Override
 134     public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,
 135             String key, Object... params) {
 136         logrb(level, sourceClass, sourceMethod, bundle, key, params);
 137     }
 138 
 139     @Override
 140     public final void log(sun.util.logging.PlatformLogger.Level level, String msg, Throwable thrown) {
 141         logrb(level, bundle, msg, thrown);
 142     }
 143 
 144     @Override
 145     public final void log(sun.util.logging.PlatformLogger.Level level, String msg) {
 146         logrb(level, bundle, msg, (Object[]) null);
 147     }
 148 
 149     @Override
 150     public final void log(sun.util.logging.PlatformLogger.Level level, String format, Object... params) {
 151         logrb(level, bundle, format, params);
 152     }
 153 
 154 
 155 }