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 java.io.StringReader;
33 import javax.jbi.messaging.DeliveryChannel;
34 import javax.jbi.messaging.ExchangeStatus;
35 import javax.jbi.messaging.Fault;
36 import javax.jbi.messaging.MessageExchange;
37 import javax.jbi.messaging.MessagingException;
38
39
40
41
42
43
44
45
46 public abstract class AbstractMessageExchangeHandler implements MessageExchangeHandler {
47
48 public static String IN_MESSAGE = "in";
49 public static String OUT_MESSAGE = "out";
50
51 private MessageExchange mMessageExchange;
52
53
54 protected AbstractMessageExchangeHandler() {
55 this.mMessageExchange = null;
56 }
57
58 protected final DeliveryChannel getDeliveryChannel() {
59 return RuntimeHelper.getDeliveryChannel();
60 }
61
62 public final MessageExchange getMessageExchange() {
63 return this.mMessageExchange;
64 }
65
66 public final void setMessageExchange(MessageExchange msgExchange) {
67 this.mMessageExchange = msgExchange;
68 }
69
70 protected final void send() throws MessagingException {
71 this.getDeliveryChannel().send(this.mMessageExchange);
72 }
73
74 protected final void sendDone() throws MessagingException {
75 this.mMessageExchange.setStatus(ExchangeStatus.DONE);
76 this.getDeliveryChannel().send(this.mMessageExchange);
77 }
78
79 protected final void sendFault() throws MessagingException {
80 Fault fault = this.mMessageExchange.createFault();
81 sendFault(fault);
82 }
83
84 protected final void sendFault(Exception ex) throws MessagingException {
85 Fault fault = this.mMessageExchange.createFault();
86 if ( ex != null ) {
87 String xmlText = RuntimeHelper.getExceptionAsXmlText(ex);
88 fault.setContent(RuntimeHelper.createDOMSource(new StringReader(xmlText)));
89 }
90 sendFault(fault);
91 }
92
93 protected void sendFault(Fault fault) throws MessagingException {
94 this.mMessageExchange.setFault(fault);
95 this.getDeliveryChannel().send(this.mMessageExchange);
96 }
97
98
99 protected final void sendError(Exception ex) {
100 try {
101 this.mMessageExchange.setError(ex);
102 this.getDeliveryChannel().send(this.mMessageExchange);
103 } catch (MessagingException msgEx) {
104 msgEx.printStackTrace();
105 }
106 }
107
108 protected abstract void validateMessageExchange() throws MessagingException;
109 protected abstract void processError(Exception ex);
110 protected abstract void processDone();
111 protected abstract void processMessage();
112 protected abstract void processFault(Fault fault);
113
114 private void processActive() {
115 Fault fault = this.getMessageExchange().getFault();
116 if ( fault != null ) {
117 processFault(fault);
118 } else {
119 processMessage();
120 }
121 }
122
123 public final void processMessageExchange() {
124 try {
125 validateMessageExchange();
126 } catch (MessagingException ex) {
127 ex.printStackTrace();
128 if ( this.getMessageExchange() != null ) {
129 sendError(ex);
130 }
131 return;
132 }
133
134 MessageExchange msgExchange = this.getMessageExchange();
135 ExchangeStatus status = msgExchange.getStatus();
136
137 if (ExchangeStatus.ACTIVE.equals(status) ) {
138 processActive();
139 } else if (ExchangeStatus.DONE.equals(status) ) {
140 processDone();
141 } else if (ExchangeStatus.ERROR.equals(status) ) {
142 processError(msgExchange.getError());
143 }
144 }
145
146 public final void run() {
147 try {
148 this.processMessageExchange();
149 } catch (Exception ex) {
150 ex.printStackTrace();
151 }
152 }
153
154 }