|
| 1 | +package org.scijava.event.bushe; |
| 2 | + |
| 3 | +import java.lang.ref.WeakReference; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.lang.reflect.AccessibleObject; |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | + |
| 8 | +/** |
| 9 | + * Common base class for EventService Proxies. |
| 10 | + * <p> |
| 11 | + * Implementing Prioritized even when Priority is not used is always OK. The default |
| 12 | + * value of 0 retains the FIFO order. |
| 13 | + */ |
| 14 | +public abstract class AbstractProxySubscriber implements ProxySubscriber, Prioritized { |
| 15 | + private Object proxiedSubscriber; |
| 16 | + private Method subscriptionMethod; |
| 17 | + private ReferenceStrength referenceStrength; |
| 18 | + private EventService eventService; |
| 19 | + private int priority; |
| 20 | + protected boolean veto; |
| 21 | + |
| 22 | + protected AbstractProxySubscriber(Object proxiedSubscriber, Method subscriptionMethod, |
| 23 | + ReferenceStrength referenceStrength, EventService es, boolean veto) { |
| 24 | + this(proxiedSubscriber, subscriptionMethod, referenceStrength, 0, es, veto); |
| 25 | + } |
| 26 | + |
| 27 | + protected AbstractProxySubscriber(Object proxiedSubscriber, Method subscriptionMethod, |
| 28 | + ReferenceStrength referenceStrength, int priority, EventService es, boolean veto) { |
| 29 | + this.referenceStrength = referenceStrength; |
| 30 | + this.priority = priority; |
| 31 | + eventService = es; |
| 32 | + this.veto = veto; |
| 33 | + if (proxiedSubscriber == null) { |
| 34 | + throw new IllegalArgumentException("The realSubscriber cannot be null when constructing a proxy subscriber."); |
| 35 | + } |
| 36 | + if (subscriptionMethod == null) { |
| 37 | + throw new IllegalArgumentException("The subscriptionMethod cannot be null when constructing a proxy subscriber."); |
| 38 | + } |
| 39 | + Class<?> returnType = subscriptionMethod.getReturnType(); |
| 40 | + if (veto && returnType != Boolean.TYPE) { |
| 41 | + throw new IllegalArgumentException("The subscriptionMethod must have the two parameters, the first one must be a String and the second a non-primitive (Object or derivative)."); |
| 42 | + } |
| 43 | + if (ReferenceStrength.WEAK.equals(referenceStrength)) { |
| 44 | + this.proxiedSubscriber = new WeakReference(proxiedSubscriber); |
| 45 | + } else { |
| 46 | + this.proxiedSubscriber = proxiedSubscriber; |
| 47 | + } |
| 48 | + this.subscriptionMethod = subscriptionMethod; |
| 49 | + } |
| 50 | + |
| 51 | + /** @return the object this proxy is subscribed on behalf of */ |
| 52 | + public Object getProxiedSubscriber() { |
| 53 | + if (proxiedSubscriber instanceof WeakReference) { |
| 54 | + return ((WeakReference)proxiedSubscriber).get(); |
| 55 | + } |
| 56 | + return proxiedSubscriber; |
| 57 | + } |
| 58 | + |
| 59 | + /** @return the subscriptionMethod passed in the constructor */ |
| 60 | + public Method getSubscriptionMethod() { |
| 61 | + return subscriptionMethod; |
| 62 | + } |
| 63 | + |
| 64 | + /** @return the EventService passed in the constructor */ |
| 65 | + public EventService getEventService() { |
| 66 | + return eventService; |
| 67 | + } |
| 68 | + |
| 69 | + /** @return the ReferenceStrength passed in the constructor */ |
| 70 | + public ReferenceStrength getReferenceStrength() { |
| 71 | + return referenceStrength; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return the priority, no effect if priority is 0 (the default value) |
| 76 | + */ |
| 77 | + public int getPriority() { |
| 78 | + return priority; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Called by EventServices to inform the proxy that it is unsubscribed. |
| 83 | + * The ProxySubscriber should perform any necessary cleanup. |
| 84 | + * <p> |
| 85 | + * <b>Overriding classes must call super.proxyUnsubscribed() or risk |
| 86 | + * things not being cleanup up properly.</b> |
| 87 | + */ |
| 88 | + public void proxyUnsubscribed() { |
| 89 | + proxiedSubscriber = null; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public final int hashCode() { |
| 94 | + throw new RuntimeException("Proxy subscribers are not allowed in Hash " + |
| 95 | + "Maps, since the underlying values use Weak References that" + |
| 96 | + "may disappear, the calculations may not be the same in" + |
| 97 | + "successive calls as required by hashCode."); |
| 98 | + } |
| 99 | + |
| 100 | + protected boolean retryReflectiveCallUsingAccessibleObject(Object[] args, Method subscriptionMethod, Object obj, |
| 101 | + IllegalAccessException e, String message) { |
| 102 | + boolean accessibleTriedAndFailed = false; |
| 103 | + if (subscriptionMethod != null) { |
| 104 | + AccessibleObject[] accessibleMethod = {subscriptionMethod}; |
| 105 | + try { |
| 106 | + AccessibleObject.setAccessible(accessibleMethod, true); |
| 107 | + Object returnValue = subscriptionMethod.invoke(obj, args); |
| 108 | + return Boolean.valueOf(returnValue+""); |
| 109 | + } catch (SecurityException ex) { |
| 110 | + accessibleTriedAndFailed = true; |
| 111 | + } catch (InvocationTargetException e1) { |
| 112 | + throw new RuntimeException(message, e); |
| 113 | + } catch (IllegalAccessException e1) { |
| 114 | + throw new RuntimeException(message, e); |
| 115 | + } |
| 116 | + } |
| 117 | + if (accessibleTriedAndFailed) { |
| 118 | + message = message + ". An attempt was made to make the method accessible, but the SecurityManager denied the attempt."; |
| 119 | + } |
| 120 | + throw new RuntimeException(message, e); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean equals(Object obj) { |
| 125 | + if (obj instanceof AbstractProxySubscriber) { |
| 126 | + AbstractProxySubscriber bps = (AbstractProxySubscriber) obj; |
| 127 | + if (referenceStrength != bps.referenceStrength) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + if (subscriptionMethod != bps.subscriptionMethod) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + if (ReferenceStrength.WEAK == referenceStrength) { |
| 134 | + if (((WeakReference)proxiedSubscriber).get() != ((WeakReference)bps.proxiedSubscriber).get()) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + } else { |
| 138 | + if (proxiedSubscriber != bps.proxiedSubscriber) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + if (veto != bps.veto) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + if (eventService != bps.eventService) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + return true; |
| 149 | + } else { |
| 150 | + return false; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String toString() { |
| 156 | + return "AbstractProxySubscriber{" + |
| 157 | + "realSubscriber=" + (proxiedSubscriber instanceof WeakReference? |
| 158 | + ((WeakReference)proxiedSubscriber).get():proxiedSubscriber) + |
| 159 | + ", subscriptionMethod=" + subscriptionMethod + |
| 160 | + ", veto=" + veto + |
| 161 | + ", referenceStrength=" + referenceStrength + |
| 162 | + ", eventService=" + eventService + |
| 163 | + '}'; |
| 164 | + } |
| 165 | +} |
0 commit comments