KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > ra > inbound > MessageEndpointProxy


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

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 JavaDoc;
53 import javax.jms.MessageListener JavaDoc;
54 import javax.resource.ResourceException JavaDoc;
55 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
56 import java.lang.reflect.Method JavaDoc;
57
58 /**
59  * @author <a HREF="mailto:michael.gaffney@panacya.com">Michael Gaffney </a>
60  */

61 public class MessageEndpointProxy implements MessageListener JavaDoc, MessageEndpoint JavaDoc {
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 JavaDoc 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 JavaDoc endpoint) {
91         if (!(endpoint instanceof MessageListener JavaDoc)) {
92             throw new IllegalArgumentException JavaDoc("MessageEndpoint is not a MessageListener");
93         }
94         proxyID = getID();
95         this.endpoint = endpoint;
96     }
97
98     public void beforeDelivery(Method JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
99         state.beforeDelivery(this, method);
100     }
101
102     public void onMessage(Message JavaDoc message) {
103 // log.warn("Delivery Count: " + getNextDeliveryCount() );
104
state.onMessage(this, message);
105     }
106
107     public void afterDelivery() throws ResourceException JavaDoc {
108         state.afterDelivery(this);
109     }
110
111     public void release() {
112         state.release(this);
113     }
114
115     public String JavaDoc 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 JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
125             throw new IllegalStateException JavaDoc();
126         }
127
128         public void onMessage(MessageEndpointProxy proxy, Message JavaDoc message) {
129             throw new IllegalStateException JavaDoc();
130         }
131
132         public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException JavaDoc {
133             throw new IllegalStateException JavaDoc();
134         }
135
136         public void release(MessageEndpointProxy proxy) {
137             throw new IllegalStateException JavaDoc();
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 JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
152             try {
153                 proxy.endpoint.beforeDelivery(method);
154             } catch (NoSuchMethodException JavaDoc e) {
155                 transition(proxy, DEAD);
156                 throw e;
157             } catch (ResourceException JavaDoc e) {
158                 transition(proxy, DEAD);
159                 throw e;
160             }
161         }
162
163         public void onMessage(MessageEndpointProxy proxy, Message JavaDoc message) {
164             try {
165                 ((MessageListener JavaDoc) proxy.endpoint).onMessage(message);
166             } catch (RuntimeException JavaDoc e) {
167                 transition(proxy, GOING_TO_DIE);
168                 throw e;
169             }
170         }
171
172         public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException JavaDoc {
173             try {
174                 proxy.endpoint.afterDelivery();
175             } catch (ResourceException JavaDoc 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 JavaDoc {
189             try {
190                 proxy.endpoint.afterDelivery();
191             } catch (ResourceException JavaDoc 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 JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
211             throw new InvalidMessageEndpointException();
212         }
213
214         public void onMessage(MessageEndpointProxy proxy, Message JavaDoc message) {
215             throw new InvalidMessageEndpointException();
216         }
217
218         public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException JavaDoc {
219             throw new InvalidMessageEndpointException();
220         }
221
222         public void release(MessageEndpointProxy proxy) {
223             throw new InvalidMessageEndpointException();
224         }
225     }
226 }
227
Popular Tags