1 18 package org.apache.activemq.ra; 19 20 import java.lang.reflect.Method ; 21 22 import javax.jms.Message ; 23 import javax.jms.MessageListener ; 24 import javax.resource.ResourceException ; 25 import javax.resource.spi.endpoint.MessageEndpoint ; 26 27 30 public class MessageEndpointProxy implements MessageListener , MessageEndpoint { 31 32 private static final MessageEndpointState ALIVE = new MessageEndpointAlive(); 33 private static final MessageEndpointState DEAD = new MessageEndpointDead(); 34 35 36 private static int proxyCount = 0; 37 private final int proxyID; 38 39 private final MessageEndpoint endpoint; 40 private final MessageListener messageListener; 41 private MessageEndpointState state = ALIVE; 42 43 private static int getID() { 44 return ++proxyCount; 45 } 46 47 public MessageEndpointProxy(MessageEndpoint endpoint) { 48 if (!(endpoint instanceof MessageListener )) { 49 throw new IllegalArgumentException ("MessageEndpoint is not a MessageListener"); 50 } 51 messageListener = (MessageListener ) endpoint; 52 proxyID = getID(); 53 this.endpoint = endpoint; 54 } 55 56 public void beforeDelivery(Method method) throws NoSuchMethodException , ResourceException { 57 state.beforeDelivery(this, method); 58 } 59 60 public void onMessage(Message message) { 61 state.onMessage(this, message); 62 } 63 64 public void afterDelivery() throws ResourceException { 65 state.afterDelivery(this); 66 } 67 68 public void release() { 69 state.release(this); 70 } 71 72 public String toString() { 73 return "MessageEndpointProxy{ " + 74 "proxyID: " + proxyID + 75 ", endpoint: " + endpoint + 76 " }"; 77 } 78 79 private abstract static class MessageEndpointState { 80 81 public void beforeDelivery(MessageEndpointProxy proxy, Method method) throws NoSuchMethodException , ResourceException { 82 throw new IllegalStateException (); 83 } 84 85 public void onMessage(MessageEndpointProxy proxy, Message message) { 86 throw new IllegalStateException (); 87 } 88 89 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 90 throw new IllegalStateException (); 91 } 92 93 public void release(MessageEndpointProxy proxy) { 94 throw new IllegalStateException (); 95 } 96 97 protected final void transition(MessageEndpointProxy proxy, MessageEndpointState nextState) { 98 proxy.state = nextState; 99 nextState.enter(proxy); 100 } 101 102 protected void enter(MessageEndpointProxy proxy) { 103 } 104 } 105 106 private static class MessageEndpointAlive extends MessageEndpointState { 107 108 public void beforeDelivery(MessageEndpointProxy proxy, Method method) throws NoSuchMethodException , ResourceException { 109 try { 110 proxy.endpoint.beforeDelivery(method); 111 } catch (NoSuchMethodException e) { 112 transition(proxy, DEAD); 113 throw e; 114 } catch (ResourceException e) { 115 transition(proxy, DEAD); 116 throw e; 117 } 118 } 119 120 public void onMessage(MessageEndpointProxy proxy, Message message) { 121 proxy.messageListener.onMessage(message); 122 } 123 124 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 125 try { 126 proxy.endpoint.afterDelivery(); 127 } catch (ResourceException e) { 128 transition(proxy, DEAD); 129 throw e; 130 } 131 } 132 133 public void release(MessageEndpointProxy proxy) { 134 transition(proxy, DEAD); 135 } 136 } 137 138 private static class MessageEndpointDead extends MessageEndpointState { 139 140 protected void enter(MessageEndpointProxy proxy) { 141 proxy.endpoint.release(); 142 } 143 144 public void beforeDelivery(MessageEndpointProxy proxy, Method method) throws NoSuchMethodException , ResourceException { 145 throw new InvalidMessageEndpointException(); 146 } 147 148 public void onMessage(MessageEndpointProxy proxy, Message message) { 149 throw new InvalidMessageEndpointException(); 150 } 151 152 public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException { 153 throw new InvalidMessageEndpointException(); 154 } 155 156 public void release(MessageEndpointProxy proxy) { 157 throw new InvalidMessageEndpointException(); 158 } 159 } 160 } 161 | Popular Tags |