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 java.util.logging; 28 29 import java.io.*; 30 31 /** 32 * Stream based logging <tt>Handler</tt>. 33 * <p> 34 * This is primarily intended as a base class or support class to 35 * be used in implementing other logging <tt>Handlers</tt>. 36 * <p> 37 * <tt>LogRecords</tt> are published to a given <tt>java.io.OutputStream</tt>. 38 * <p> 39 * <b>Configuration:</b> 40 * By default each <tt>StreamHandler</tt> is initialized using the following 41 * <tt>LogManager</tt> configuration properties where <tt><handler-name></tt> 42 * refers to the fully-qualified class name of the handler. 43 * If properties are not defined 44 * (or have invalid values) then the specified default values are used. 45 * <ul> 46 * <li> <handler-name>.level 47 * specifies the default level for the <tt>Handler</tt> 48 * (defaults to <tt>Level.INFO</tt>). </li> 49 * <li> <handler-name>.filter 60 * For example, the properties for {@code StreamHandler} would be: 61 * <ul> 62 * <li> java.util.logging.StreamHandler.level=INFO </li> 63 * <li> java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li> 64 * </ul> 65 * <p> 66 * For a custom handler, e.g. com.foo.MyHandler, the properties would be: 67 * <ul> 68 * <li> com.foo.MyHandler.level=INFO </li> 69 * <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li> 70 * </ul> 71 * <p> 72 * @since 1.4 73 */ 74 75 public class StreamHandler extends Handler { 76 private OutputStream output; 77 private boolean doneHeader; 78 private volatile Writer writer; 79 80 // Private method to configure a StreamHandler from LogManager 81 // properties and/or default values as specified in the class 82 // javadoc. 83 private void configure() { 84 LogManager manager = LogManager.getLogManager(); 85 String cname = getClass().getName(); 86 87 setLevel(manager.getLevelProperty(cname +".level", Level.INFO)); 88 setFilter(manager.getFilterProperty(cname +".filter", null)); 89 setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter())); 90 try { 91 setEncoding(manager.getStringProperty(cname +".encoding", null)); 92 } catch (Exception ex) { 93 try { 94 setEncoding(null); 95 } catch (Exception ex2) { 96 // doing a setEncoding with null should always work. 97 // assert false; 98 } 99 } 100 } 101 102 /** 103 * Create a <tt>StreamHandler</tt>, with no current output stream. 104 */ 105 public StreamHandler() { 106 sealed = false; 107 configure(); 108 sealed = true; 109 } 110 111 /** 112 * Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt> 113 * and output stream. 114 * <p> 115 * @param out the target output stream 116 * @param formatter Formatter to be used to format output 117 */ 118 public StreamHandler(OutputStream out, Formatter formatter) { 119 sealed = false; 120 configure(); 121 setFormatter(formatter); 122 setOutputStream(out); 123 sealed = true; 124 } 125 126 /** 127 * Change the output stream. 128 * <P> 129 * If there is a current output stream then the <tt>Formatter</tt>'s 130 * tail string is written and the stream is flushed and closed. 131 * Then the output stream is replaced with the new output stream. 132 * 133 * @param out New output stream. May not be null. 134 * @exception SecurityException if a security manager exists and if 135 * the caller does not have <tt>LoggingPermission("control")</tt>. 136 */ 137 protected synchronized void setOutputStream(OutputStream out) throws SecurityException { 138 if (out == null) { 139 throw new NullPointerException(); 140 } 141 flushAndClose(); 142 output = out; 143 doneHeader = false; | 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 java.util.logging; 28 29 import java.io.*; 30 import java.security.AccessController; 31 import java.security.PrivilegedAction; 32 33 /** 34 * Stream based logging <tt>Handler</tt>. 35 * <p> 36 * This is primarily intended as a base class or support class to 37 * be used in implementing other logging <tt>Handlers</tt>. 38 * <p> 39 * <tt>LogRecords</tt> are published to a given <tt>java.io.OutputStream</tt>. 40 * <p> 41 * <b>Configuration:</b> 42 * By default each <tt>StreamHandler</tt> is initialized using the following 43 * <tt>LogManager</tt> configuration properties where <tt><handler-name></tt> 44 * refers to the fully-qualified class name of the handler. 45 * If properties are not defined 46 * (or have invalid values) then the specified default values are used. 47 * <ul> 48 * <li> <handler-name>.level 49 * specifies the default level for the <tt>Handler</tt> 50 * (defaults to <tt>Level.INFO</tt>). </li> 51 * <li> <handler-name>.filter 62 * For example, the properties for {@code StreamHandler} would be: 63 * <ul> 64 * <li> java.util.logging.StreamHandler.level=INFO </li> 65 * <li> java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li> 66 * </ul> 67 * <p> 68 * For a custom handler, e.g. com.foo.MyHandler, the properties would be: 69 * <ul> 70 * <li> com.foo.MyHandler.level=INFO </li> 71 * <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li> 72 * </ul> 73 * <p> 74 * @since 1.4 75 */ 76 77 public class StreamHandler extends Handler { 78 private OutputStream output; 79 private boolean doneHeader; 80 private volatile Writer writer; 81 82 // Private PrivilegedAction to configure a StreamHandler from constructor parameters, 83 // LogManager properties and/or default values as specified in the class 84 // javadoc. 85 private class ConfigureAction implements PrivilegedAction<Void> { 86 private final OutputStream out; 87 private final Formatter formatter; 88 89 ConfigureAction() { 90 this.out = null; 91 this.formatter = null; 92 } 93 94 ConfigureAction(OutputStream out, Formatter formatter) { 95 if (out == null || formatter == null) { 96 throw new NullPointerException(); 97 } 98 this.out = out; 99 this.formatter = formatter; 100 } 101 102 @Override 103 public Void run() { 104 LogManager manager = LogManager.getLogManager(); 105 String cname = StreamHandler.this.getClass().getName(); 106 107 setLevel(manager.getLevelProperty(cname +".level", Level.INFO)); 108 setFilter(manager.getFilterProperty(cname +".filter", null)); 109 setFormatter(formatter == null // use configured formatter if null 110 ? manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()) 111 : formatter); 112 try { 113 setEncoding(manager.getStringProperty(cname +".encoding", null)); 114 } catch (Exception ex) { 115 try { 116 setEncoding(null); 117 } catch (Exception ex2) { 118 // doing a setEncoding with null should always work. 119 // assert false; 120 } 121 } 122 if (out != null) { // don't set output stream if null 123 setOutputStream(out); 124 } 125 return null; 126 } 127 } 128 129 /** 130 * Create a <tt>StreamHandler</tt>, with no current output stream. 131 */ 132 public StreamHandler() { 133 AccessController.doPrivileged(new ConfigureAction(), 134 null, LogManager.controlPermission); 135 } 136 137 /** 138 * Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt> 139 * and output stream. 140 * <p> 141 * @param out the target output stream 142 * @param formatter Formatter to be used to format output 143 */ 144 public StreamHandler(OutputStream out, Formatter formatter) { 145 AccessController.doPrivileged(new ConfigureAction(out, formatter), 146 null, LogManager.controlPermission); 147 } 148 149 /** 150 * Change the output stream. 151 * <P> 152 * If there is a current output stream then the <tt>Formatter</tt>'s 153 * tail string is written and the stream is flushed and closed. 154 * Then the output stream is replaced with the new output stream. 155 * 156 * @param out New output stream. May not be null. 157 * @exception SecurityException if a security manager exists and if 158 * the caller does not have <tt>LoggingPermission("control")</tt>. 159 */ 160 protected synchronized void setOutputStream(OutputStream out) throws SecurityException { 161 if (out == null) { 162 throw new NullPointerException(); 163 } 164 flushAndClose(); 165 output = out; 166 doneHeader = false; |