src/share/classes/java/rmi/server/LogStream.java

Print this page
rev 10048 : 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
Reviewed-by:


  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         SecurityManager sm = System.getSecurityManager();
 124 
 125         if (sm != null) {
 126             sm.checkPermission(
 127                 new java.util.logging.LoggingPermission("control", null));
 128         }
 129 
 130         defaultStream = newDefault;
 131     }
 132 
 133     /**
 134      * Return the current stream to which output from this log is sent.
 135      * @return output stream for this log
 136      * @see #setOutputStream
 137      * @since JDK1.1
 138      * @deprecated no replacement
 139      */
 140     @Deprecated
 141     public synchronized OutputStream getOutputStream()
 142     {
 143         return logOut;
 144     }
 145 
 146     /**
 147      * Set the stream to which output from this log is sent.
 148      * @param out new output stream for this log
 149      * @see #getOutputStream
 150      * @since JDK1.1
 151      * @deprecated no replacement
 152      */
 153     @Deprecated
 154     public synchronized void setOutputStream(OutputStream out)
 155     {
 156         logOut = out;
 157         // Maintain an OutputStreamWriter with default CharToByteConvertor
 158         // (just like new PrintStream) for writing log message prefixes.
 159         logWriter = new OutputStreamWriter(logOut);
 160     }
 161 
 162     /**
 163      * Write a byte of data to the stream.  If it is not a newline, then
 164      * the byte is appended to the internal buffer.  If it is a newline,
 165      * then the currently buffered line is sent to the log's output
 166      * stream, prefixed with the appropriate logging information.
 167      * @since JDK1.1
 168      * @deprecated no replacement
 169      */
 170     @Deprecated
 171     public void write(int b)
 172     {
 173         if (b == '\n') {
 174             // synchronize on "this" first to avoid potential deadlock
 175             synchronized (this) {
 176                 synchronized (logOut) {
 177                     // construct prefix for log messages:
 178                     buffer.setLength(0);;
 179                     buffer.append(              // date/time stamp...
 180                         (new Date()).toString());
 181                     buffer.append(':');
 182                     buffer.append(name);        // ...log name...
 183                     buffer.append(':');
 184                     buffer.append(Thread.currentThread().getName());
 185                     buffer.append(':'); // ...and thread name
 186 
 187                     try {


 191 
 192                         // finally, write the already converted bytes of
 193                         // the log message
 194                         bufOut.writeTo(logOut);
 195                         logOut.write(b);
 196                         logOut.flush();
 197                     } catch (IOException e) {
 198                         setError();
 199                     } finally {
 200                         bufOut.reset();
 201                     }
 202                 }
 203             }
 204         }
 205         else
 206             super.write(b);
 207     }
 208 
 209     /**
 210      * Write a subarray of bytes.  Pass each through write byte method.
 211      * @since JDK1.1
 212      * @deprecated no replacement
 213      */
 214     @Deprecated
 215     public void write(byte b[], int off, int len)
 216     {
 217         if (len < 0)
 218             throw new ArrayIndexOutOfBoundsException(len);
 219         for (int i = 0; i < len; ++ i)
 220             write(b[off + i]);
 221     }
 222 
 223     /**
 224      * Return log name as string representation.
 225      * @return log name
 226      * @since JDK1.1
 227      * @deprecated no replacement
 228      */
 229     @Deprecated
 230     public String toString()
 231     {
 232         return name;
 233     }
 234 
 235     /** log level constant (no logging). */
 236     public static final int SILENT  = 0;
 237     /** log level constant (brief logging). */
 238     public static final int BRIEF   = 10;
 239     /** log level constant (verbose logging). */
 240     public static final int VERBOSE = 20;
 241 
 242     /**
 243      * Convert a string name of a logging level to its internal
 244      * integer representation.
 245      * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
 246      * @return corresponding integer log level
 247      * @since JDK1.1
 248      * @deprecated no replacement
 249      */
 250     @Deprecated
 251     public static int parseLevel(String s)
 252     {
 253         if ((s == null) || (s.length() < 1))
 254             return -1;
 255 
 256         try {
 257             return Integer.parseInt(s);
 258         } catch (NumberFormatException e) {
 259         }
 260         if (s.length() < 1)
 261             return -1;
 262 
 263         if ("SILENT".startsWith(s.toUpperCase()))
 264             return SILENT;
 265         else if ("BRIEF".startsWith(s.toUpperCase()))
 266             return BRIEF;
 267         else if ("VERBOSE".startsWith(s.toUpperCase()))


  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   1.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 1.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 1.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 1.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 1.1
 119      * @deprecated no replacement
 120      */
 121     @Deprecated
 122     public static synchronized void setDefaultStream(PrintStream newDefault) {
 123         SecurityManager sm = System.getSecurityManager();
 124 
 125         if (sm != null) {
 126             sm.checkPermission(
 127                 new java.util.logging.LoggingPermission("control", null));
 128         }
 129 
 130         defaultStream = newDefault;
 131     }
 132 
 133     /**
 134      * Return the current stream to which output from this log is sent.
 135      * @return output stream for this log
 136      * @see #setOutputStream
 137      * @since 1.1
 138      * @deprecated no replacement
 139      */
 140     @Deprecated
 141     public synchronized OutputStream getOutputStream()
 142     {
 143         return logOut;
 144     }
 145 
 146     /**
 147      * Set the stream to which output from this log is sent.
 148      * @param out new output stream for this log
 149      * @see #getOutputStream
 150      * @since 1.1
 151      * @deprecated no replacement
 152      */
 153     @Deprecated
 154     public synchronized void setOutputStream(OutputStream out)
 155     {
 156         logOut = out;
 157         // Maintain an OutputStreamWriter with default CharToByteConvertor
 158         // (just like new PrintStream) for writing log message prefixes.
 159         logWriter = new OutputStreamWriter(logOut);
 160     }
 161 
 162     /**
 163      * Write a byte of data to the stream.  If it is not a newline, then
 164      * the byte is appended to the internal buffer.  If it is a newline,
 165      * then the currently buffered line is sent to the log's output
 166      * stream, prefixed with the appropriate logging information.
 167      * @since 1.1
 168      * @deprecated no replacement
 169      */
 170     @Deprecated
 171     public void write(int b)
 172     {
 173         if (b == '\n') {
 174             // synchronize on "this" first to avoid potential deadlock
 175             synchronized (this) {
 176                 synchronized (logOut) {
 177                     // construct prefix for log messages:
 178                     buffer.setLength(0);;
 179                     buffer.append(              // date/time stamp...
 180                         (new Date()).toString());
 181                     buffer.append(':');
 182                     buffer.append(name);        // ...log name...
 183                     buffer.append(':');
 184                     buffer.append(Thread.currentThread().getName());
 185                     buffer.append(':'); // ...and thread name
 186 
 187                     try {


 191 
 192                         // finally, write the already converted bytes of
 193                         // the log message
 194                         bufOut.writeTo(logOut);
 195                         logOut.write(b);
 196                         logOut.flush();
 197                     } catch (IOException e) {
 198                         setError();
 199                     } finally {
 200                         bufOut.reset();
 201                     }
 202                 }
 203             }
 204         }
 205         else
 206             super.write(b);
 207     }
 208 
 209     /**
 210      * Write a subarray of bytes.  Pass each through write byte method.
 211      * @since 1.1
 212      * @deprecated no replacement
 213      */
 214     @Deprecated
 215     public void write(byte b[], int off, int len)
 216     {
 217         if (len < 0)
 218             throw new ArrayIndexOutOfBoundsException(len);
 219         for (int i = 0; i < len; ++ i)
 220             write(b[off + i]);
 221     }
 222 
 223     /**
 224      * Return log name as string representation.
 225      * @return log name
 226      * @since 1.1
 227      * @deprecated no replacement
 228      */
 229     @Deprecated
 230     public String toString()
 231     {
 232         return name;
 233     }
 234 
 235     /** log level constant (no logging). */
 236     public static final int SILENT  = 0;
 237     /** log level constant (brief logging). */
 238     public static final int BRIEF   = 10;
 239     /** log level constant (verbose logging). */
 240     public static final int VERBOSE = 20;
 241 
 242     /**
 243      * Convert a string name of a logging level to its internal
 244      * integer representation.
 245      * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
 246      * @return corresponding integer log level
 247      * @since 1.1
 248      * @deprecated no replacement
 249      */
 250     @Deprecated
 251     public static int parseLevel(String s)
 252     {
 253         if ((s == null) || (s.length() < 1))
 254             return -1;
 255 
 256         try {
 257             return Integer.parseInt(s);
 258         } catch (NumberFormatException e) {
 259         }
 260         if (s.length() < 1)
 261             return -1;
 262 
 263         if ("SILENT".startsWith(s.toUpperCase()))
 264             return SILENT;
 265         else if ("BRIEF".startsWith(s.toUpperCase()))
 266             return BRIEF;
 267         else if ("VERBOSE".startsWith(s.toUpperCase()))