| 1 46 47 package org.mr.ra.inbound; 48 49 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 52 import javax.jms.Message ; 53 import javax.jms.MessageListener ; 54 import javax.resource.ResourceException ; 55 import javax.resource.spi.endpoint.MessageEndpoint ; 56 import java.lang.reflect.Method ; 57 58 61 public class MessageEndpointProxy implements MessageListener , MessageEndpoint { 62 63 private static final MessageEndpointState ALIVE = new MessageEndpointAlive(); 64 private static final MessageEndpointState GOING_TO_DIE = new MessageEndpointInTheElectricChair(); 65 private static final MessageEndpointState DEAD = new MessageEndpointDead(); 66 67 private static final Log log = LogFactory.getLog(MessageEndpointProxy.class); 68 69 private static int proxyCount = 0; 70 71 private static int deliveryCount = 0; 72 73 private final int proxyID; 74 75 private MessageEndpoint endpoint; 76 77 private boolean alive = true; 78 79 private MessageEndpointState state = ALIVE; 80 81 private static int getID() { 82 return ++proxyCount; 83 } 84 85 private static int getNextDeliveryCount() { 86 return ++deliveryCount; 87 } 88 89 90 public MessageEndpointProxy(MessageEndpoint endpoint) { 91 if (!(endpoint instanceof MessageListener )) { 92 throw new IllegalArgumentException ("MessageEndpoint is not a MessageListener"); 93 } 94 proxyID = getID(); 95 this.endpoint = endpoint; 96 } 97 98 public void beforeDelivery(Method method) throws NoSuchMethodException , ResourceException { 99 state.beforeDelivery(this, method); 100 } 101 102 public void onMessage(Message message) { 103 state.onMessage(this, message); 105 } 106 107 public void afterDelivery() throws ResourceException { 108 state.afterDelivery(this); 109 } 110 111 public void release() { 112 state.release(this); 113 } 114 115 public String toString() { 116 return "MessageEndpointProxy{ " + 117 "proxyID: " + proxyID + 118 ", endpoint: " + endpoint + 119 " }"; 120 } 121 122 private abstract static class MessageEndpointState { 123 124 public void beforeDelivery(MessageEndpointProxy proxy, Method method) throws NoSuchMethodException , ResourceException { 125 throw new IllegalStateException (); 126 } 127 128 public void onMessage(MessageEndpointProxy proxy, Message message) { 129 throw new IllegalStateException (); 130 } 131 132 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 133 throw new IllegalStateException (); 134 } 135 136 public void release(MessageEndpointProxy proxy) { 137 throw new IllegalStateException (); 138 } 139 140 protected final void transition(MessageEndpointProxy proxy, MessageEndpointState nextState) { 141 proxy.state = nextState; 142 nextState.enter(proxy); 143 } 144 145 protected void enter(MessageEndpointProxy proxy) { 146 } 147 } 148 149 private static class MessageEndpointAlive extends MessageEndpointState { 150 151 public void beforeDelivery(MessageEndpointProxy proxy, Method method) throws NoSuchMethodException , ResourceException { 152 try { 153 proxy.endpoint.beforeDelivery(method); 154 } catch (NoSuchMethodException e) { 155 transition(proxy, DEAD); 156 throw e; 157 } catch (ResourceException e) { 158 transition(proxy, DEAD); 159 throw e; 160 } 161 } 162 163 public void onMessage(MessageEndpointProxy proxy, Message message) { 164 try { 165 ((MessageListener ) proxy.endpoint).onMessage(message); 166 } catch (RuntimeException e) { 167 transition(proxy, GOING_TO_DIE); 168 throw e; 169 } 170 } 171 172 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 173 try { 174 proxy.endpoint.afterDelivery(); 175 } catch (ResourceException e) { 176 transition(proxy, DEAD); 177 throw e; 178 } 179 } 180 181 public void release(MessageEndpointProxy proxy) { 182 transition(proxy, DEAD); 183 } 184 } 185 186 private static class MessageEndpointInTheElectricChair extends MessageEndpointState { 187 188 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 189 try { 190 proxy.endpoint.afterDelivery(); 191 } catch (ResourceException e) { 192 throw e; 193 } finally { 194 transition(proxy, DEAD); 195 } 196 } 197 198 public void release(MessageEndpointProxy proxy) { 199 transition(proxy, DEAD); 200 } 201 } 202 203 private static class MessageEndpointDead extends MessageEndpointState { 204 205 protected void enter(MessageEndpointProxy proxy) { 206 proxy.endpoint.release(); 207 proxy.endpoint = null; 208 } 209 210 public void beforeDelivery(MessageEndpointProxy proxy, Method method) throws NoSuchMethodException , ResourceException { 211 throw new InvalidMessageEndpointException(); 212 } 213 214 public void onMessage(MessageEndpointProxy proxy, Message message) { 215 throw new InvalidMessageEndpointException(); 216 } 217 218 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 219 throw new InvalidMessageEndpointException(); 220 } 221 222 public void release(MessageEndpointProxy proxy) { 223 throw new InvalidMessageEndpointException(); 224 } 225 } 226 } 227 | Popular Tags |