1 22 package info.monitorenter.reflection; 23 24 import info.monitorenter.util.TimeStampedValue; 25 import info.monitorenter.util.collections.IRingBuffer; 26 import info.monitorenter.util.collections.RingBufferArrayFast; 27 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.util.LinkedList ; 32 33 import javax.naming.directory.NoSuchAttributeException ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 import javax.swing.event.EventListenerList ; 37 38 39 63 public class ObjectRecorder extends Thread { 64 65 74 public final class ObjectInspection { 75 76 protected long m_time; 77 78 79 private LinkedList m_values; 80 81 86 private ObjectInspection() { 87 this.m_time = new java.util.Date ().getTime(); 88 this.m_values = new LinkedList (); 89 } 90 91 98 private void add(final Object value) { 99 this.m_values.add(value); 100 } 101 102 113 public Object get(final int index) { 114 return this.m_values.get(index); 115 } 116 117 123 public long getTime() { 124 return this.m_time; 125 } 126 127 138 protected void remove(final Object value) { 139 this.m_values.remove(value); 140 } 141 142 150 public String toString() { 151 StringBuffer ret = new StringBuffer ("\nObjectInspection:\n"); 152 ret.append("-----------------\n"); 153 ret.append("Inspected: ").append(ObjectRecorder.this.getInspected().toString()).append("\n"); 154 ret.append("time: ").append(this.m_time).append("\n"); 155 for (int i = ObjectRecorder.this.m_fields.length - 1; i >= 0; i--) { 156 ret.append(ObjectRecorder.this.m_fields[i].getName()).append(": ").append( 157 this.m_values.get(i).toString()).append("\n"); 158 } 159 return ret.toString(); 160 } 161 } 162 163 164 protected static final boolean VERBOSE = false; 165 166 167 protected IRingBuffer m_buffer = new RingBufferArrayFast(100); 168 169 170 protected EventListenerList m_changeListeners = new EventListenerList (); 171 172 173 protected Field [] m_fields; 174 175 178 protected long m_interval; 179 180 181 protected Object m_toinspect; 182 183 194 public ObjectRecorder(final Object toinspect, final long interval) { 195 this.m_interval = interval; 196 this.m_toinspect = toinspect; 197 this.m_fields = toinspect.getClass().getDeclaredFields(); 199 this.start(); 200 } 201 202 211 public void addChangeListener(final ChangeListener x) { 212 this.m_changeListeners.add(ChangeListener .class, x); 213 } 216 217 222 protected void fireChange() { 223 ChangeEvent ce = new ChangeEvent (this); 224 Object [] listeners = this.m_changeListeners.getListenerList(); 225 for (int i = listeners.length - 1; i >= 0; i -= 2) { 226 ChangeListener cl = (ChangeListener ) listeners[i]; 227 cl.stateChanged(ce); 228 } 229 } 230 231 248 public TimeStampedValue[] getAttributeHistory(final String attributeName) 249 throws NoSuchAttributeException { 250 int attribindex = -1; 252 for (int i = this.m_fields.length - 1; i >= 0; i--) { 253 if (this.m_fields[i].getName().equals(attributeName)) { 254 attribindex = i; 255 break; 256 } 257 } 258 if (attribindex == -1) { 259 throw new NoSuchAttributeException ("The Attribute with the name: " + attributeName 260 + " does not exist in " + this.m_toinspect.getClass().getName()); 261 } 262 int stop = this.m_buffer.size(); 263 TimeStampedValue[] ret = new TimeStampedValue[stop]; 264 ObjectInspection tmp; 265 synchronized (this.m_buffer) { 266 java.util.Iterator it = this.m_buffer.iteratorF2L(); 267 int i = 0; 268 while (it.hasNext()) { 269 tmp = (ObjectInspection) it.next(); 270 ret[i++] = new TimeStampedValue(tmp.getTime(), tmp.get(attribindex)); 271 } 272 } 273 return ret; 274 } 275 276 282 public String [] getAttributeNames() { 283 String [] ret = new String [this.m_fields.length]; 284 for (int i = 0; i < this.m_fields.length; i++) { 285 ret[i] = this.m_fields[i].getName(); 286 } 287 return ret; 288 289 } 290 291 297 public Object getInspected() { 298 return this.m_toinspect; 299 } 300 301 316 public TimeStampedValue getLastValue(final String fieldname) throws NoSuchAttributeException { 317 int attribindex = -1; 319 for (int i = this.m_fields.length - 1; i >= 0; i--) { 320 if (this.m_fields[i].getName().equals(fieldname)) { 321 attribindex = i; 322 break; 323 } 324 } 325 if (attribindex == -1) { 326 throw new NoSuchAttributeException ("The Attribute with the name: " + fieldname 327 + " does not exist in " + this.m_toinspect.getClass().getName()); 328 } 329 ObjectInspection tmp = (ObjectInspection) this.m_buffer.getYoungest(); 330 return new TimeStampedValue(tmp.getTime(), tmp.get(attribindex)); 331 } 332 333 342 public IRingBuffer getRingBuffer() { 343 return this.m_buffer; 344 } 345 346 355 public void inspect() { 356 ObjectInspection newentry = new ObjectInspection(); 357 for (int i = 0; i < this.m_fields.length; i++) { 358 if (ObjectRecorder.VERBOSE) { 359 System.out.println(this.getClass().getName() + " inpspecting " + this.m_fields[i].getName() 360 + " of " + this.m_toinspect.getClass().getName() + "."); 361 } 362 try { 363 this.m_fields[i].setAccessible(true); 364 newentry.add(this.m_fields[i].get(this.m_toinspect)); 365 } catch (IllegalAccessException e) { 366 if (ObjectRecorder.VERBOSE) { 367 System.err.println(this.getClass().getName() + ".inspect(): No public access to " 368 + this.m_fields[i].getName() + " of " + this.m_toinspect.getClass().getName()); 369 } 370 String fieldname = this.m_fields[i].getName(); 372 char[] fieldnm = fieldname.toCharArray(); 373 fieldnm[0] = Character.toUpperCase(fieldnm[0]); 374 fieldname = new String (fieldnm); 375 String methodname = new StringBuffer ("get").append(fieldname).toString(); 376 try { 378 Method toinvoke = this.m_toinspect.getClass().getDeclaredMethod(methodname, 379 new Class [] {}); 380 newentry.add(toinvoke.invoke(this.m_toinspect, new Object [] {})); 381 382 } catch (NoSuchMethodException f) { 383 if (ObjectRecorder.VERBOSE) { 384 System.err.println(this.getClass().getName() + ".inspect(): Failure at getting field " 385 + this.m_fields[i].getName() + " by trying to invoke a method: " + methodname); 386 } 387 } catch (SecurityException g) { 388 g.printStackTrace(); 389 } catch (IllegalAccessException h) { 390 h.printStackTrace(); 391 } catch (InvocationTargetException l) { 392 l.printStackTrace(); 393 } 394 } 395 } 396 this.m_buffer.add(newentry); 397 this.fireChange(); 398 } 399 400 407 public void removeChangeListener(final ChangeListener x) { 408 this.m_changeListeners.remove(ChangeListener .class, x); 409 } 410 411 415 public void run() { 416 while (true) { 417 try { 418 sleep(this.m_interval); 419 } catch (InterruptedException e) { 420 } 422 this.inspect(); 423 } 424 } 425 426 438 public void setHistoryLength(final int length) { 439 this.m_buffer.setBufferSize(length); 440 } 441 442 451 public void setInterval(final long sleeptime) { 452 this.m_interval = sleeptime; 453 } 454 455 458 public String toString() { 459 return this.m_buffer.toString(); 460 } 461 } 462 | Popular Tags |