KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > MessageEndpointProxy


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import java.lang.reflect.Method JavaDoc;
21
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageListener JavaDoc;
24 import javax.resource.ResourceException JavaDoc;
25 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:michael.gaffney@panacya.com">Michael Gaffney </a>
29  */

30 public class MessageEndpointProxy implements MessageListener JavaDoc, MessageEndpoint JavaDoc {
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 JavaDoc endpoint;
40     private final MessageListener JavaDoc messageListener;
41     private MessageEndpointState state = ALIVE;
42
43     private static int getID() {
44         return ++proxyCount;
45     }
46     
47     public MessageEndpointProxy(MessageEndpoint JavaDoc endpoint) {
48         if (!(endpoint instanceof MessageListener JavaDoc)) {
49             throw new IllegalArgumentException JavaDoc("MessageEndpoint is not a MessageListener");
50         }
51         messageListener = (MessageListener JavaDoc) endpoint;
52         proxyID = getID();
53         this.endpoint = endpoint;
54     }
55
56     public void beforeDelivery(Method JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
57         state.beforeDelivery(this, method);
58     }
59
60     public void onMessage(Message JavaDoc message) {
61         state.onMessage(this, message);
62     }
63
64     public void afterDelivery() throws ResourceException JavaDoc {
65         state.afterDelivery(this);
66     }
67
68     public void release() {
69         state.release(this);
70     }
71
72     public String JavaDoc 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 JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
82             throw new IllegalStateException JavaDoc();
83         }
84
85         public void onMessage(MessageEndpointProxy proxy, Message JavaDoc message) {
86             throw new IllegalStateException JavaDoc();
87         }
88
89         public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException JavaDoc {
90             throw new IllegalStateException JavaDoc();
91         }
92
93         public void release(MessageEndpointProxy proxy) {
94             throw new IllegalStateException JavaDoc();
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 JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
109             try {
110                 proxy.endpoint.beforeDelivery(method);
111             } catch (NoSuchMethodException JavaDoc e) {
112                 transition(proxy, DEAD);
113                 throw e;
114             } catch (ResourceException JavaDoc e) {
115                 transition(proxy, DEAD);
116                 throw e;
117             }
118         }
119
120         public void onMessage(MessageEndpointProxy proxy, Message JavaDoc message) {
121             proxy.messageListener.onMessage(message);
122         }
123
124         public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException JavaDoc {
125             try {
126                 proxy.endpoint.afterDelivery();
127             } catch (ResourceException JavaDoc 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 JavaDoc method) throws NoSuchMethodException JavaDoc, ResourceException JavaDoc {
145             throw new InvalidMessageEndpointException();
146         }
147
148         public void onMessage(MessageEndpointProxy proxy, Message JavaDoc message) {
149             throw new InvalidMessageEndpointException();
150         }
151
152         public void afterDelivery(MessageEndpointProxy proxy) throws ResourceException JavaDoc {
153             throw new InvalidMessageEndpointException();
154         }
155
156         public void release(MessageEndpointProxy proxy) {
157             throw new InvalidMessageEndpointException();
158         }
159     }
160 }
161
Popular Tags