1 /*
   2  * Copyright (c) 1996, 2004, 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 package java.rmi.server;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 
  30 /**
  31  * <code>LogStream</code> provides a mechanism for logging errors that are
  32  * of possible interest to those monitoring a system.
  33  *
  34  * @author  Ann Wollrath (lots of code stolen from Ken Arnold)
  35  * @since   JDK1.1
  36  * @deprecated no replacement
  37  */
  38 @Deprecated
  39 public class LogStream extends PrintStream {
  40 
  41     /** table mapping known log names to log stream objects */
  42     private static Map<String,LogStream> known = new HashMap<>(5);
  43     /** default output stream for new logs */
  44     private static PrintStream  defaultStream = System.err;
  45 
  46     /** log name for this log */
  47     private String name;
  48 
  49     /** stream where output of this log is sent to */
  50     private OutputStream logOut;
  51 
  52     /** string writer for writing message prefixes to log stream */
  53     private OutputStreamWriter logWriter;
  54 
  55     /** string buffer used for constructing log message prefixes */
  56     private StringBuffer buffer = new StringBuffer();
  57 
  58     /** stream used for buffering lines */
  59     private ByteArrayOutputStream bufOut;
  60 
  61     /**
  62      * Create a new LogStream object.  Since this only constructor is
  63      * private, users must have a LogStream created through the "log"
  64      * method.
  65      * @param name string identifying messages from this log
  66      * @out output stream that log messages will be sent to
  67      * @since JDK1.1
  68      * @deprecated no replacement
  69      */
  70     @Deprecated
  71     private LogStream(String name, OutputStream out)
  72     {
  73         super(new ByteArrayOutputStream());
  74         bufOut = (ByteArrayOutputStream) super.out;
  75 
  76         this.name = name;
  77         setOutputStream(out);
  78     }
  79 
  80     /**
  81      * Return the LogStream identified by the given name.  If
  82      * a log corresponding to "name" does not exist, a log using
  83      * the default stream is created.
  84      * @param name name identifying the desired LogStream
  85      * @return log associated with given name
  86      * @since JDK1.1
  87      * @deprecated no replacement
  88      */
  89     @Deprecated
  90     public static LogStream log(String name) {
  91         LogStream stream;
  92         synchronized (known) {
  93             stream = known.get(name);
  94             if (stream == null) {
  95                 stream = new LogStream(name, defaultStream);
  96             }
  97             known.put(name, stream);
  98         }
  99         return stream;
 100     }
 101 
 102     /**
 103      * Return the current default stream for new logs.
 104      * @return default log stream
 105      * @see #setDefaultStream
 106      * @since JDK1.1
 107      * @deprecated no replacement
 108      */
 109     @Deprecated
 110     public static synchronized PrintStream getDefaultStream() {
 111         return defaultStream;
 112     }
 113 
 114     /**
 115      * Set the default stream for new logs.
 116      * @param newDefault new default log stream
 117      * @see #getDefaultStream
 118      * @since JDK1.1
 119      * @deprecated no replacement
 120      */
 121     @Deprecated
 122     public static synchronized void setDefaultStream(PrintStream newDefault) {
 123         defaultStream = newDefault;
 124     }
 125 
 126     /**
 127      * Return the current stream to which output from this log is sent.
 128      * @return output stream for this log
 129      * @see #setOutputStream
 130      * @since JDK1.1
 131      * @deprecated no replacement
 132      */
 133     @Deprecated
 134     public synchronized OutputStream getOutputStream()
 135     {
 136         return logOut;
 137     }
 138 
 139     /**
 140      * Set the stream to which output from this log is sent.
 141      * @param out new output stream for this log
 142      * @see #getOutputStream
 143      * @since JDK1.1
 144      * @deprecated no replacement
 145      */
 146     @Deprecated
 147     public synchronized void setOutputStream(OutputStream out)
 148     {
 149         logOut = out;
 150         // Maintain an OutputStreamWriter with default CharToByteConvertor
 151         // (just like new PrintStream) for writing log message prefixes.
 152         logWriter = new OutputStreamWriter(logOut);
 153     }
 154 
 155     /**
 156      * Write a byte of data to the stream.  If it is not a newline, then
 157      * the byte is appended to the internal buffer.  If it is a newline,
 158      * then the currently buffered line is sent to the log's output
 159      * stream, prefixed with the appropriate logging information.
 160      * @since JDK1.1
 161      * @deprecated no replacement
 162      */
 163     @Deprecated
 164     public void write(int b)
 165     {
 166         if (b == '\n') {
 167             // synchronize on "this" first to avoid potential deadlock
 168             synchronized (this) {
 169                 synchronized (logOut) {
 170                     // construct prefix for log messages:
 171                     buffer.setLength(0);;
 172                     buffer.append(              // date/time stamp...
 173                         (new Date()).toString());
 174                     buffer.append(':');
 175                     buffer.append(name);        // ...log name...
 176                     buffer.append(':');
 177                     buffer.append(Thread.currentThread().getName());
 178                     buffer.append(':'); // ...and thread name
 179 
 180                     try {
 181                         // write prefix through to underlying byte stream
 182                         logWriter.write(buffer.toString());
 183                         logWriter.flush();
 184 
 185                         // finally, write the already converted bytes of
 186                         // the log message
 187                         bufOut.writeTo(logOut);
 188                         logOut.write(b);
 189                         logOut.flush();
 190                     } catch (IOException e) {
 191                         setError();
 192                     } finally {
 193                         bufOut.reset();
 194                     }
 195                 }
 196             }
 197         }
 198         else
 199             super.write(b);
 200     }
 201 
 202     /**
 203      * Write a subarray of bytes.  Pass each through write byte method.
 204      * @since JDK1.1
 205      * @deprecated no replacement
 206      */
 207     @Deprecated
 208     public void write(byte b[], int off, int len)
 209     {
 210         if (len < 0)
 211             throw new ArrayIndexOutOfBoundsException(len);
 212         for (int i = 0; i < len; ++ i)
 213             write(b[off + i]);
 214     }
 215 
 216     /**
 217      * Return log name as string representation.
 218      * @return log name
 219      * @since JDK1.1
 220      * @deprecated no replacement
 221      */
 222     @Deprecated
 223     public String toString()
 224     {
 225         return name;
 226     }
 227 
 228     /** log level constant (no logging). */
 229     public static final int SILENT  = 0;
 230     /** log level constant (brief logging). */
 231     public static final int BRIEF   = 10;
 232     /** log level constant (verbose logging). */
 233     public static final int VERBOSE = 20;
 234 
 235     /**
 236      * Convert a string name of a logging level to its internal
 237      * integer representation.
 238      * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
 239      * @return corresponding integer log level
 240      * @since JDK1.1
 241      * @deprecated no replacement
 242      */
 243     @Deprecated
 244     public static int parseLevel(String s)
 245     {
 246         if ((s == null) || (s.length() < 1))
 247             return -1;
 248 
 249         try {
 250             return Integer.parseInt(s);
 251         } catch (NumberFormatException e) {
 252         }
 253         if (s.length() < 1)
 254             return -1;
 255 
 256         if ("SILENT".startsWith(s.toUpperCase()))
 257             return SILENT;
 258         else if ("BRIEF".startsWith(s.toUpperCase()))
 259             return BRIEF;
 260         else if ("VERBOSE".startsWith(s.toUpperCase()))
 261             return VERBOSE;
 262 
 263         return -1;
 264     }
 265 }