1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package it.imolinfo.jbi4ejb.jbi.component.runtime;
31
32 import it.imolinfo.jbi4ejb.Logger;
33 import it.imolinfo.jbi4ejb.LoggerFactory;
34 import it.imolinfo.jbi4ejb.jbi.Messages;
35
36 import javax.jbi.messaging.Fault;
37 import javax.jbi.messaging.InOut;
38 import javax.jbi.messaging.MessageExchange;
39 import javax.jbi.messaging.MessagingException;
40 import javax.jbi.messaging.NormalizedMessage;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public abstract class InOutProviderMessageExchangeHandler extends AbstractMessageExchangeHandler {
57
58 public static final long SEND_SYNC_TIMEOUT = 60000;
59
60
61 private static final Logger LOG = LoggerFactory.getLogger(InOutProviderMessageExchangeHandler.class);
62 private static final Messages MESSAGES = Messages.getMessages(InOutProviderMessageExchangeHandler.class);
63
64
65 public InOutProviderMessageExchangeHandler() {
66 super();
67 }
68
69 protected void validateMessageExchange() throws MessagingException {
70
71 MessageExchange msgExchange = this.getMessageExchange();
72
73 if ( this.getMessageExchange() == null ) {
74 String msg=MESSAGES.getString("EJB000215_MessageExchange_Object_null_in_DefaultMessageExchageHandler");
75 LOG.error(msg);
76 throw new MessagingException(msg);
77 }
78
79 if ( MessageExchange.Role.CONSUMER.equals(msgExchange.getRole()) ) {
80
81 String msg=MESSAGES.getString("EJB000217_MessageExchange_Handler_can_not_have_MessageExchange_with_CONSUMER_Role");
82 LOG.error(msg);
83 throw new MessagingException(msg);
84 }
85
86 if (!(msgExchange instanceof InOut) ) {
87
88 String msg=MESSAGES.getString("EJB000218_InOut_Message_Exchange_Handler_MessageExchange_object_should_be_instance");
89 LOG.error(msg);
90 throw new MessagingException(msg);
91 }
92 }
93
94 protected void processFault(Fault fault) {
95
96 LOG.error("EJB000219_processFault");
97 }
98
99 protected void processDone() {
100 RuntimeHelper.logVerbose("InOut Message Exchange Provider handler received DONE : END of service invocation");
101 }
102
103 protected void processError(Exception ex) {
104
105 LOG.error("EJB000220_InOut_Message_Exchange_Provider_handler_received_Error");
106
107 LOG.error("EJB000221_processError", new Object[] {ex});
108 }
109
110 protected final void processMessage() {
111
112 InOut inOutMX = (InOut) this.getMessageExchange();
113
114 NormalizedMessage inMsg = inOutMX.getInMessage();
115 if ( inMsg == null ) {
116 this.sendError(new MessagingException("InOut Provider MessageExchange received null In Message"));
117 return;
118 }
119
120 handleInMessage(inMsg);
121 }
122
123 private void handleInMessage(NormalizedMessage inMsg) {
124
125 Fault fault = null;
126 Exception processingEx = null;
127 NormalizedMessage outMsg = null;
128 boolean result = false;
129
130 InOut inOutExchange = (InOut) this.getMessageExchange();
131 try {
132
133 outMsg = inOutExchange.createMessage();
134 fault = inOutExchange.createFault();
135
136 result = invokeOperation(inMsg, outMsg, fault);
137 } catch (Exception ex) {
138 processingEx = ex;
139 ex.printStackTrace();
140 } finally {
141 try {
142 if ( result == true ) {
143
144 this.sendOutMessage(outMsg);
145 } else {
146
147 if ( processingEx != null ) {
148
149 this.sendFault(processingEx);
150 } else {
151
152 this.sendFault(fault);
153 }
154 }
155 } catch (MessagingException ex) {
156 ex.printStackTrace();
157 this.sendError(ex);
158 return;
159 }
160 }
161
162
163 this.processMessageExchange();
164 }
165
166 public void sendFault(Fault fault) throws MessagingException {
167 InOut inOutMX = (InOut) this.getMessageExchange();
168 inOutMX.setFault(fault);
169 boolean sent = false;
170 try {
171 sent = this.getDeliveryChannel().sendSync(inOutMX, SEND_SYNC_TIMEOUT);
172 if (!sent) {
173 inOutMX.setError(new MessagingException("InOutProvider ME Handler unable to send out message"));
174 }
175 } catch (Exception ex) {
176
177 LOG.error("EJB000222_sendFault");
178 inOutMX.setError(ex);
179 }
180 }
181
182 private void sendOutMessage(NormalizedMessage outMsg) throws MessagingException {
183
184 InOut inOutMX = (InOut) this.getMessageExchange();
185 inOutMX.setOutMessage(outMsg);
186 boolean sent = false;
187 try {
188 sent = this.getDeliveryChannel().sendSync(inOutMX, SEND_SYNC_TIMEOUT);
189 if (!sent) {
190 inOutMX.setError(new MessagingException("InOutProvider ME Handler unable to send out message"));
191 }
192 } catch (Exception ex) {
193 LOG.error("EJB000223_sendOutMessage");
194 inOutMX.setError(ex);
195 }
196 }
197
198 protected abstract boolean invokeOperation(NormalizedMessage inMsg, NormalizedMessage outMsg, Fault fault) throws MessagingException;
199
200 }