Logging improvements for WeakObserverList.

- De-static checkedApply(). It should be overrideable and know
  other things about the ObserverList that it belongs to.
- Created an overrideable observerForLog().
- Have the DerefOp include the original Op's toString() in its own.
This commit is contained in:
Ray J. Greenwell
2015-05-04 17:48:00 -07:00
parent 3212061b2b
commit 0436d95cea
2 changed files with 34 additions and 17 deletions
@@ -191,6 +191,30 @@ public abstract class ObserverList<T>
return size() == 0; return size() == 0;
} }
/**
* Applies the operation to the observer, catching and logging any exceptions thrown in the
* process.
*/
protected boolean checkedApply (ObserverOp<T> obop, T obs)
{
try {
return obop.apply(obs);
} catch (Throwable thrown) {
log.warning("ObserverOp choked during notification",
"op", obop, "obs", observerForLog(obs), thrown);
// if they booched it, definitely don't remove them
return true;
}
}
/**
* Get the object that should be logged as the observer.
*/
protected Object observerForLog (T observer)
{
return observer;
}
protected static class Impl<T> extends ObserverList<T> { protected static class Impl<T> extends ObserverList<T> {
protected Impl (Policy notifyPolicy) { protected Impl (Policy notifyPolicy) {
_policy = notifyPolicy; _policy = notifyPolicy;
@@ -273,8 +297,8 @@ public abstract class ObserverList<T>
protected boolean isDuplicate (T obs) { protected boolean isDuplicate (T obs) {
// make sure we're not violating the list constraints // make sure we're not violating the list constraints
if (indexOf(obs) >= 0) { if (indexOf(obs) >= 0) {
log.warning("Observer attempted to observe list it's already observing!", "obs", obs, log.warning("Observer attempted to observe list it's already observing!",
new Exception()); "obs", obs, new Exception());
return true; return true;
} }
return false; return false;
@@ -284,19 +308,4 @@ public abstract class ObserverList<T>
protected List<T> _list; protected List<T> _list;
protected boolean _checkDups = true; protected boolean _checkDups = true;
} }
/**
* Applies the operation to the observer, catching and logging any exceptions thrown in the
* process.
*/
protected static <T> boolean checkedApply (ObserverOp<T> obop, T obs)
{
try {
return obop.apply(obs);
} catch (Throwable thrown) {
log.warning("ObserverOp choked during notification", "op", obop, "obs", obs, thrown);
// if they booched it, definitely don't remove them
return true;
}
}
} }
@@ -108,6 +108,10 @@ public class WeakObserverList<T> extends ObserverList<T>
return observer != null && _op.apply(observer); return observer != null && _op.apply(observer);
} }
@Override public String toString () {
return "DerefOp:" + _op;
}
/** The wrapped op. */ /** The wrapped op. */
protected ObserverOp<T> _op; protected ObserverOp<T> _op;
} }
@@ -128,6 +132,10 @@ public class WeakObserverList<T> extends ObserverList<T>
} }
return -1; return -1;
} }
@Override protected Object observerForLog (WeakReference<T> ref) {
return ref.get();
}
} }
/** A delegate list that contains weak reference wrapped elements. */ /** A delegate list that contains weak reference wrapped elements. */