1 7 8 9 package java.util.logging; 10 11 import java.io.*; 12 13 43 44 public class StreamHandler extends Handler { 45 private LogManager manager = LogManager.getLogManager(); 46 private OutputStream output; 47 private boolean doneHeader; 48 private Writer writer; 49 50 private void configure() { 54 LogManager manager = LogManager.getLogManager(); 55 String cname = getClass().getName(); 56 57 setLevel(manager.getLevelProperty(cname +".level", Level.INFO)); 58 setFilter(manager.getFilterProperty(cname +".filter", null)); 59 setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter ())); 60 try { 61 setEncoding(manager.getStringProperty(cname +".encoding", null)); 62 } catch (Exception ex) { 63 try { 64 setEncoding(null); 65 } catch (Exception ex2) { 66 } 69 } 70 } 71 72 75 public StreamHandler() { 76 sealed = false; 77 configure(); 78 sealed = true; 79 } 80 81 88 public StreamHandler(OutputStream out, Formatter formatter) { 89 sealed = false; 90 configure(); 91 setFormatter(formatter); 92 setOutputStream(out); 93 sealed = true; 94 } 95 96 107 protected synchronized void setOutputStream(OutputStream out) throws SecurityException { 108 if (out == null) { 109 throw new NullPointerException (); 110 } 111 flushAndClose(); 112 output = out; 113 doneHeader = false; 114 String encoding = getEncoding(); 115 if (encoding == null) { 116 writer = new OutputStreamWriter(output); 117 } else { 118 try { 119 writer = new OutputStreamWriter(output, encoding); 120 } catch (UnsupportedEncodingException ex) { 121 throw new Error ("Unexpected exception " + ex); 124 } 125 } 126 } 127 128 141 public void setEncoding(String encoding) 142 throws SecurityException , java.io.UnsupportedEncodingException { 143 super.setEncoding(encoding); 144 if (output == null) { 145 return; 146 } 147 flush(); 149 if (encoding == null) { 150 writer = new OutputStreamWriter(output); 151 } else { 152 writer = new OutputStreamWriter(output, encoding); 153 } 154 } 155 156 173 public synchronized void publish(LogRecord record) { 174 if (!isLoggable(record)) { 175 return; 176 } 177 String msg; 178 try { 179 msg = getFormatter().format(record); 180 } catch (Exception ex) { 181 reportError(null, ex, ErrorManager.FORMAT_FAILURE); 184 return; 185 } 186 187 try { 188 if (!doneHeader) { 189 writer.write(getFormatter().getHead(this)); 190 doneHeader = true; 191 } 192 writer.write(msg); 193 } catch (Exception ex) { 194 reportError(null, ex, ErrorManager.WRITE_FAILURE); 197 } 198 } 199 200 201 212 public boolean isLoggable(LogRecord record) { 213 if (writer == null || record == null) { 214 return false; 215 } 216 return super.isLoggable(record); 217 } 218 219 222 public synchronized void flush() { 223 if (writer != null) { 224 try { 225 writer.flush(); 226 } catch (Exception ex) { 227 reportError(null, ex, ErrorManager.FLUSH_FAILURE); 230 } 231 } 232 } 233 234 private synchronized void flushAndClose() throws SecurityException { 235 checkAccess(); 236 if (writer != null) { 237 try { 238 if (!doneHeader) { 239 writer.write(getFormatter().getHead(this)); 240 doneHeader = true; 241 } 242 writer.write(getFormatter().getTail(this)); 243 writer.flush(); 244 writer.close(); 245 } catch (Exception ex) { 246 reportError(null, ex, ErrorManager.CLOSE_FAILURE); 249 } 250 writer = null; 251 output = null; 252 } 253 } 254 255 268 public synchronized void close() throws SecurityException { 269 flushAndClose(); 270 } 271 } 272 | Popular Tags |