1 package org.jboss.logging.jdk.handlers; 2 3 import java.io.BufferedWriter ; 4 import java.io.IOException ; 5 import java.io.OutputStream ; 6 import java.io.OutputStreamWriter ; 7 import java.io.UnsupportedEncodingException ; 8 import java.io.Writer ; 9 import java.util.logging.ErrorManager ; 10 import java.util.logging.Formatter ; 11 import java.util.logging.LogRecord ; 12 13 19 public class WriterHandler extends HandlerSkeleton 20 { 21 33 protected boolean immediateFlush = true; 34 37 protected boolean bufferedIO = false; 38 41 protected int bufferSize = 8 * 1024; 42 43 private OutputStream msgOutput; 44 private Writer msgWriter; 45 48 private boolean wroteHeader; 49 50 public WriterHandler() 51 { 52 super(); 53 } 54 55 public WriterHandler(OutputStream output, Formatter formatter) 56 { 57 setFormatter(formatter); 58 setOutputStream(output); 59 } 60 61 75 public void setImmediateFlush(boolean value) 76 { 77 immediateFlush = value; 78 } 79 80 83 public boolean getImmediateFlush() 84 { 85 return immediateFlush; 86 } 87 88 public boolean isBufferedIO() 89 { 90 return bufferedIO; 91 } 92 93 102 public void setBufferedIO(boolean bufferedIO) 103 { 104 this.bufferedIO = bufferedIO; 105 if (bufferedIO) 106 { 107 immediateFlush = false; 108 } 109 } 110 111 public int getBufferSize() 112 { 113 return bufferSize; 114 } 115 116 119 public void setBufferSize(int bufferSize) 120 { 121 this.bufferSize = bufferSize; 122 } 123 124 public void setEncoding(String encoding) 125 throws SecurityException , UnsupportedEncodingException 126 { 127 super.setEncoding(encoding); 128 if (msgOutput == null) 129 { 130 return; 131 } 132 flush(); 134 if (encoding == null) 135 { 136 msgWriter = new OutputStreamWriter (msgOutput); 137 } 138 else 139 { 140 msgWriter = new OutputStreamWriter (msgOutput, encoding); 141 } 142 } 143 144 public synchronized void flush() 145 { 146 if (msgWriter != null) 147 { 148 try 149 { 150 msgWriter.flush(); 151 } 152 catch (IOException e) 153 { 154 reportError("Failed to flush writer", e, ErrorManager.FLUSH_FAILURE); 155 } 156 } 157 } 158 159 public synchronized void close() 160 { 161 if (msgWriter != null) 162 { 163 try 164 { 165 if (!wroteHeader) 166 { 167 msgWriter.write(getFormatter().getHead(this)); 168 wroteHeader = true; 169 } 170 msgWriter.write(getFormatter().getTail(this)); 171 msgWriter.flush(); 172 msgWriter.close(); 173 } 174 catch (Exception ex) 175 { 176 reportError(null, ex, ErrorManager.CLOSE_FAILURE); 179 } 180 msgWriter = null; 181 msgOutput = null; 182 } 183 184 } 185 186 public void publish(LogRecord record) 187 { 188 if(checkEntryConditions(record) == false) 189 { 190 return; 191 } 192 subPublish(record); 193 } 194 195 protected boolean checkEntryConditions(LogRecord record) 196 { 197 boolean canWrite = super.isLoggable(record); 198 if( canWrite ) 199 { 200 canWrite = msgWriter != null; 201 } 202 return canWrite; 203 } 204 205 210 protected void subPublish(LogRecord record) 211 { 212 Formatter fmt = getFormatter(); 213 String msg = fmt.format(record); 214 synchronized (this) 215 { 216 try 217 { 218 msgWriter.write(msg); 219 } 220 catch (IOException e) 221 { 222 reportError("Failed to publish recored", e, ErrorManager.WRITE_FAILURE); 223 } 224 if (this.immediateFlush) 225 { 226 flush(); 227 } 228 } 229 } 230 231 242 protected synchronized void setOutputStream(OutputStream out) 243 { 244 if (out == null) 245 { 246 throw new NullPointerException ("The out argument cannot be null"); 247 } 248 close(); 249 msgOutput = out; 250 wroteHeader = false; 251 String encoding = getEncoding(); 252 if (encoding == null) 253 { 254 msgWriter = new OutputStreamWriter (msgOutput); 255 } 256 else 257 { 258 try 259 { 260 msgWriter = new OutputStreamWriter (msgOutput, encoding); 261 } 262 catch (UnsupportedEncodingException ex) 263 { 264 throw new Error ("Unexpected exception " + ex); 267 } 268 } 269 if (bufferedIO) 270 { 271 msgWriter = new BufferedWriter (msgWriter, bufferSize); 272 } 273 } 274 275 276 } 277 | Popular Tags |