jdk/src/share/classes/sun/rmi/runtime/Log.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 19211 32a04c562026
child 23333 b0af2c7c8c91
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.rmi.runtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.ConsoleHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.logging.Handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.logging.Formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.SimpleFormatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.logging.StreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.logging.LogManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.logging.LogRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.logging.StreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Utility which provides an abstract "logger" like RMI internal API
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * which can be directed to use one of two types of logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * infrastructure: the java.util.logging API or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * java.rmi.server.LogStream API.  The default behavior is to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * java.util.logging API.  The LogStream API may be used instead by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * setting the system property sun.rmi.log.useOld to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * For backwards compatibility, supports the RMI system logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * properties which pre-1.4 comprised the only way to configure RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * logging.  If the java.util.logging API is used and RMI system log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * properties are set, the system properties override initial RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * logger values as appropriate. If the java.util.logging API is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * turned off, pre-1.4 logging behavior is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 5506
diff changeset
    64
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public abstract class Log {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /** Logger re-definition of old RMI log values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final Level BRIEF = Level.FINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final Level VERBOSE = Level.FINER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* selects log implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final LogFactory logFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        boolean useOld =
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    75
            Boolean.valueOf(java.security.AccessController.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                doPrivileged(new sun.security.action.GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    "sun.rmi.log.useOld"))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* set factory to select the logging facility to use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        logFactory = (useOld ? (LogFactory) new LogStreamLogFactory() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                      (LogFactory) new LoggerLogFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /** "logger like" API to be used by RMI implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public abstract boolean isLoggable(Level level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public abstract void log(Level level, String message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public abstract void log(Level level, String message, Throwable thrown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** get and set the RMI server call output stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public abstract void setOutputStream(OutputStream stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public abstract PrintStream getPrintStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** factory interface enables Logger and LogStream implementations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static interface LogFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Log createLog(String loggerName, String oldLogName, Level level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* access log objects */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Access log for a tri-state system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Need to first convert override value to a log level, taking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * care to interpret a range of values between BRIEF, VERBOSE and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * SILENT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * An override < 0 is interpreted to mean that the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * configuration should not be overridden. The level passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * factories createLog method will be null in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Note that if oldLogName is null and old logging is on, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * returned LogStreamLog will ignore the override parameter - the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * log will never log messages.  This permits new logs that only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * write to Loggers to do nothing when old logging is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Do not call getLog multiple times on the same logger name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Since this is an internal API, no checks are made to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * that multiple logs do not exist for the same logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static Log getLog(String loggerName, String oldLogName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                             int override)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Level level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (override < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            level = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } else if (override == LogStream.SILENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            level = Level.OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else if ((override > LogStream.SILENT) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                   (override <= LogStream.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            level = BRIEF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else if ((override > LogStream.BRIEF) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                   (override <= LogStream.VERBOSE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            level = VERBOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            level = Level.FINEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return logFactory.createLog(loggerName, oldLogName, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Access logs associated with boolean properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Do not call getLog multiple times on the same logger name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Since this is an internal API, no checks are made to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * that multiple logs do not exist for the same logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static Log getLog(String loggerName, String oldLogName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                             boolean override)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Level level = (override ? VERBOSE : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return logFactory.createLog(loggerName, oldLogName, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Factory to create Log objects which deliver log messages to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * java.util.logging API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static class LoggerLogFactory implements LogFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        LoggerLogFactory() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * Accessor to obtain an arbitrary RMI logger with name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * loggerName.  If the level of the logger is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * level for the system property with name, the logger level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * will be set to the value of system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public Log createLog(final String loggerName, String oldLogName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                             final Level level)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Logger logger = Logger.getLogger(loggerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return new LoggerLog(logger, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Class specialized to log messages to the java.util.logging API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static class LoggerLog extends Log {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /* alternate console handler for RMI loggers */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   183
        private static final Handler alternateConsole =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   185
                new java.security.PrivilegedAction<Handler>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   186
                    public Handler run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            InternalStreamHandler alternate =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                new InternalStreamHandler(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            alternate.setLevel(Level.ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            return alternate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   192
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /** handler to which messages are copied */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        private InternalStreamHandler copyHandler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /* logger to which log messages are written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        private final Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /* used as return value of RemoteServer.getLog */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        private LoggerPrintStream loggerSandwich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /** creates a Log which will delegate to the given logger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        private LoggerLog(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            this.logger = logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (level != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   209
                    new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   210
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                            if (!logger.isLoggable(level)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            logger.addHandler(alternateConsole);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public boolean isLoggable(Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return logger.isLoggable(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public void log(Level level, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (isLoggable(level)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                String[] source = getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                logger.logp(level, source[0], source[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                           Thread.currentThread().getName() + ": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public void log(Level level, String message, Throwable thrown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (isLoggable(level)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                String[] source = getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                logger.logp(level, source[0], source[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    Thread.currentThread().getName() + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                           message, thrown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * Set the output stream associated with the RMI server call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * Calling code needs LoggingPermission "control".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public synchronized void setOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (out != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (!logger.isLoggable(VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    logger.setLevel(VERBOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                copyHandler = new InternalStreamHandler(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                copyHandler.setLevel(Log.VERBOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                logger.addHandler(copyHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                /* ensure that messages are not logged */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (copyHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    logger.removeHandler(copyHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                copyHandler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        public synchronized PrintStream getPrintStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (loggerSandwich == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                loggerSandwich = new LoggerPrintStream(logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return loggerSandwich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Subclass of StreamHandler for redirecting log output.  flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * must be called in the publish and close methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private static class InternalStreamHandler extends StreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        InternalStreamHandler(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            super(out, new SimpleFormatter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            super.publish(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * PrintStream which forwards log messages to the logger.  Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * is needed to maintain backwards compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * RemoteServer.{set|get}Log().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private static class LoggerPrintStream extends PrintStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /** logger where output of this log is sent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        private final Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        /** record the last character written to this stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        private int last = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /** stream used for buffering lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        private final ByteArrayOutputStream bufOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        private LoggerPrintStream(Logger logger)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            super(new ByteArrayOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            bufOut = (ByteArrayOutputStream) super.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            this.logger = logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public void write(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if ((last == '\r') && (b == '\n')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                last = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } else if ((b == '\n') || (b == '\r')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    /* write the converted bytes of the log message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    String message =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        Thread.currentThread().getName() + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        bufOut.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    logger.logp(Level.INFO, "LogStream", "print", message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    bufOut.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                super.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            last = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        public void write(byte b[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                throw new ArrayIndexOutOfBoundsException(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                write(b[off + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return "RMI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Factory to create Log objects which deliver log messages to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * java.rmi.server.LogStream API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private static class LogStreamLogFactory implements LogFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        LogStreamLogFactory() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /* create a new LogStreamLog for the specified log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        public Log createLog(String loggerName, String oldLogName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                             Level level)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            LogStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (oldLogName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                stream = LogStream.log(oldLogName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return new LogStreamLog(stream, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Class specialized to log messages to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * java.rmi.server.LogStream API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private static class LogStreamLog extends Log {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        /** Log stream to which log messages are written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        private final LogStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        /** the level of the log as set by associated property */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        private int levelValue = Level.OFF.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        private LogStreamLog(LogStream stream, Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if ((stream != null) && (level != null)) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19211
diff changeset
   382
                /* if the stream or level is null, don't log any
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                 * messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                levelValue = level.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        public synchronized boolean isLoggable(Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            return (level.intValue() >= levelValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public void log(Level messageLevel, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (isLoggable(messageLevel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                String[] source = getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                stream.println(unqualifiedName(source[0]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                               "." + source[1] + ": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        public void log(Level level, String message, Throwable thrown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (isLoggable(level)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                 * keep output contiguous and maintain the contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                 * RemoteServer.getLog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                synchronized (stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    String[] source = getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    stream.println(unqualifiedName(source[0]) + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                   source[1] + ": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    thrown.printStackTrace(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        public PrintStream getPrintStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        public synchronized void setOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (out != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (VERBOSE.intValue() < levelValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    levelValue = VERBOSE.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                stream.setOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                /* ensure that messages are not logged */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                levelValue = Level.OFF.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * Mimic old log messages that only contain unqualified names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        private static String unqualifiedName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            int lastDot = name.lastIndexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (lastDot >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                name = name.substring(lastDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            name = name.replace('$', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Obtain class and method names of code calling a log method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    private static String[] getSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        StackTraceElement[] trace = (new Exception()).getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            trace[3].getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            trace[3].getMethodName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
}