1 21 22 package org.apache.derby.impl.services.stream; 23 24 import org.apache.derby.iapi.services.stream.InfoStreams; 25 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 26 import org.apache.derby.iapi.services.stream.PrintWriterGetHeader; 27 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 import org.apache.derby.iapi.services.monitor.ModuleControl; 30 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 31 import org.apache.derby.iapi.services.monitor.Monitor; 32 33 import org.apache.derby.iapi.reference.Property; 34 import org.apache.derby.iapi.services.property.PropertyUtil; 35 36 import java.io.BufferedOutputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.IOException ; 39 import java.io.OutputStream ; 40 import java.io.File ; 41 import java.io.Writer ; 42 43 import java.util.Properties ; 44 45 import java.lang.reflect.Method ; 46 import java.lang.reflect.Field ; 47 import java.lang.reflect.Modifier ; 48 import java.lang.reflect.Member ; 49 import java.lang.reflect.InvocationTargetException ; 50 51 72 public final class SingleStream 73 implements InfoStreams, ModuleControl, java.security.PrivilegedAction 74 { 75 76 79 private HeaderPrintWriter theStream; 80 81 82 85 public SingleStream() { 86 } 87 88 91 public void boot(boolean create, Properties properties) { 92 theStream = makeStream(); 93 } 94 95 96 99 public void stop() { 100 ((BasicHeaderPrintWriter) theStream).complete(); 101 } 102 103 106 107 110 public HeaderPrintWriter stream() { 111 return theStream; 112 } 113 114 118 123 private HeaderPrintWriter makeStream() { 124 125 PrintWriterGetHeader header = makeHeader(); 127 HeaderPrintWriter hpw = makeHPW(header); 128 129 if (hpw == null) 132 hpw = createDefaultStream(header); 133 return hpw; 134 } 135 136 139 private PrintWriterGetHeader makeHeader() { 140 141 return new BasicGetLogHeader(true, true, (String ) null); 142 } 143 144 148 private HeaderPrintWriter makeHPW(PrintWriterGetHeader header) { 149 150 153 String target = PropertyUtil. 154 getSystemProperty(Property.ERRORLOG_FILE_PROPERTY); 155 if (target!=null) 156 return makeFileHPW(target, header); 157 158 target = PropertyUtil. 159 getSystemProperty(Property.ERRORLOG_METHOD_PROPERTY); 160 if (target!=null) 161 return makeMethodHPW(target, header); 162 163 target = PropertyUtil. 164 getSystemProperty(Property.ERRORLOG_FIELD_PROPERTY); 165 if (target!=null) 166 return makeFieldHPW(target, header); 167 168 return null; 169 } 170 171 177 private HeaderPrintWriter PBmakeFileHPW(String fileName, 178 PrintWriterGetHeader header) { 179 180 boolean appendInfoLog = PropertyUtil.getSystemBoolean(Property.LOG_FILE_APPEND); 181 182 File streamFile = new File (fileName); 183 184 if (!streamFile.isAbsolute()) { 186 Object monitorEnv = Monitor.getMonitor().getEnvironment(); 187 if (monitorEnv instanceof File ) 188 streamFile = new File ((File ) monitorEnv, fileName); 189 } 190 191 FileOutputStream fos; 192 193 try { 194 195 if (streamFile.exists() && appendInfoLog) 196 fos = new FileOutputStream (streamFile.getPath(), true); 197 else 198 fos = new FileOutputStream (streamFile); 199 } catch (IOException ioe) { 200 return useDefaultStream(header, ioe); 201 } catch (SecurityException se) { 202 return useDefaultStream(header, se); 203 } 204 205 return new BasicHeaderPrintWriter(new BufferedOutputStream (fos), header, 206 true, streamFile.getPath()); 207 } 208 209 private HeaderPrintWriter makeMethodHPW(String methodInvocation, 210 PrintWriterGetHeader header) { 211 212 int lastDot = methodInvocation.lastIndexOf('.'); 213 String className = methodInvocation.substring(0, lastDot); 214 String methodName = methodInvocation.substring(lastDot+1); 215 216 Throwable t; 217 try { 218 Class theClass = Class.forName(className); 219 220 try { 221 Method theMethod = theClass.getMethod(methodName, new Class [0]); 222 223 if (!Modifier.isStatic(theMethod.getModifiers())) { 224 HeaderPrintWriter hpw = useDefaultStream(header); 225 hpw.printlnWithHeader(theMethod.toString() + " is not static"); 226 return hpw; 227 } 228 229 try { 230 return makeValueHPW(theMethod, theMethod.invoke((Object ) null, 231 new Object [0]), header, methodInvocation); 232 } catch (IllegalAccessException iae) { 233 t = iae; 234 } catch (IllegalArgumentException iarge) { 235 t = iarge; 236 } catch (InvocationTargetException ite) { 237 t = ite.getTargetException(); 238 } 239 240 } catch (NoSuchMethodException nsme) { 241 t = nsme; 242 } 243 } catch (ClassNotFoundException cnfe) { 244 t = cnfe; 245 } catch (SecurityException se) { 246 t = se; 247 248 } 249 return useDefaultStream(header, t); 250 251 } 252 253 254 private HeaderPrintWriter makeFieldHPW(String fieldAccess, 255 PrintWriterGetHeader header) { 256 257 int lastDot = fieldAccess.lastIndexOf('.'); 258 String className = fieldAccess.substring(0, lastDot); 259 String fieldName = fieldAccess.substring(lastDot+1, 260 fieldAccess.length()); 261 262 Throwable t; 263 try { 264 Class theClass = Class.forName(className); 265 266 try { 267 Field theField = theClass.getField(fieldName); 268 269 if (!Modifier.isStatic(theField.getModifiers())) { 270 HeaderPrintWriter hpw = useDefaultStream(header); 271 hpw.printlnWithHeader(theField.toString() + " is not static"); 272 return hpw; 273 } 274 275 try { 276 return makeValueHPW(theField, theField.get((Object ) null), 277 header, fieldAccess); 278 } catch (IllegalAccessException iae) { 279 t = iae; 280 } catch (IllegalArgumentException iarge) { 281 t = iarge; 282 } 283 284 } catch (NoSuchFieldException nsfe) { 285 t = nsfe; 286 } 287 } catch (ClassNotFoundException cnfe) { 288 t = cnfe; 289 } catch (SecurityException se) { 290 t = se; 291 } 292 return useDefaultStream(header, t); 293 294 305 } 306 307 private HeaderPrintWriter makeValueHPW(Member whereFrom, Object value, 308 PrintWriterGetHeader header, String name) { 309 310 if (value instanceof OutputStream ) 311 return new BasicHeaderPrintWriter((OutputStream ) value, header, false, name); 312 else if (value instanceof Writer) 313 return new BasicHeaderPrintWriter((Writer) value, header, false, name); 314 315 HeaderPrintWriter hpw = useDefaultStream(header); 316 317 if (value == null) 318 hpw.printlnWithHeader(whereFrom.toString() + "=null"); 319 else 320 hpw.printlnWithHeader(whereFrom.toString() + " instanceof " + value.getClass().getName()); 321 322 return hpw; 323 } 324 325 326 329 private HeaderPrintWriter createDefaultStream(PrintWriterGetHeader header) { 330 return makeFileHPW("derby.log", header); 331 } 332 333 336 private HeaderPrintWriter useDefaultStream(PrintWriterGetHeader header) { 337 338 return new BasicHeaderPrintWriter(System.err, header, false, "System.err"); 339 } 340 341 private HeaderPrintWriter useDefaultStream(PrintWriterGetHeader header, Throwable t) { 342 343 HeaderPrintWriter hpw = useDefaultStream(header); 344 hpw.printlnWithHeader(t.toString()); 345 return hpw; 346 } 347 348 351 352 private String PBfileName; 353 private PrintWriterGetHeader PBheader; 354 355 private HeaderPrintWriter makeFileHPW(String fileName, PrintWriterGetHeader header) 356 { 357 this.PBfileName = fileName; 358 this.PBheader = header; 359 return (HeaderPrintWriter) java.security.AccessController.doPrivileged(this); 360 } 361 362 363 public final Object run() 364 { 365 return PBmakeFileHPW(PBfileName, PBheader); 367 } 368 } 369 370 | Popular Tags |