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.util.MissingResourceException;
33 import java.util.logging.Logger;
34 import javax.jbi.JBIException;
35 import javax.jbi.component.ComponentContext;
36 import javax.jbi.messaging.DeliveryChannel;
37 import javax.jbi.messaging.MessageExchange;
38 import javax.jbi.messaging.MessagingException;
39
40
41
42
43
44
45
46
47
48
49
50 public class RuntimeContext {
51
52 private static RuntimeContext sRuntimeContext;
53
54
55 private RuntimeContext() {
56 }
57
58 public static RuntimeContext getInstance() {
59 if ( sRuntimeContext == null ) {
60 synchronized (RuntimeContext.class) {
61 if ( sRuntimeContext == null ) {
62 sRuntimeContext = new RuntimeContext();
63 }
64 }
65 }
66 return sRuntimeContext;
67 }
68
69
70
71
72 private ComponentContext mComponentContext;
73
74
75
76
77
78
79 public ComponentContext getComponentContext() {
80 return this.mComponentContext;
81 }
82
83
84
85
86
87
88 public void setComponentContext(ComponentContext componentContext) {
89 this.mComponentContext = componentContext;
90 this.mDeliveryChannel = null;
91 }
92
93
94
95
96 private DeliveryChannel mDeliveryChannel;
97
98
99
100
101
102
103 public DeliveryChannel getDeliveryChannel() {
104 return this.mDeliveryChannel;
105 }
106
107
108
109
110
111 public void openDeliveryChannel() {
112 try {
113
114 ComponentContext compCtx = getComponentContext();
115 this.mDeliveryChannel = compCtx.getDeliveryChannel();
116 } catch (MessagingException ex) {
117 RuntimeHelper.logDebug(ex);
118 } catch ( Exception ex) {
119 RuntimeHelper.logDebug(ex);
120 }
121 }
122
123
124
125 public void closeDeliveryChannel() {
126 try {
127
128 if ( this.mDeliveryChannel != null ) {
129 this.mDeliveryChannel.close();
130 }
131 } catch (MessagingException ex) {
132 RuntimeHelper.logDebug(ex);
133 } finally {
134
135 this.mDeliveryChannel = null;
136 }
137 }
138
139
140 private Logger mDefLogger;
141
142
143
144 private Logger getDefaultLogger() {
145 if ( mDefLogger == null ) {
146 this.mDefLogger = Logger.getLogger(RuntimeContext.class.getName(), null);
147 }
148 return this.mDefLogger;
149 }
150
151
152
153
154 private Logger mLogger;
155
156
157
158
159
160
161
162 public void setLogger(String name, String resourceBundle) {
163
164 if (this.mComponentContext != null) {
165
166 try {
167 this.mLogger = this.mComponentContext.getLogger(name, resourceBundle);
168 } catch (MissingResourceException ex) {
169 ex.printStackTrace();
170 } catch (JBIException ex) {
171 ex.printStackTrace();
172 }
173 } else {
174 this.mLogger = Logger.getLogger(name, resourceBundle);
175 }
176 }
177
178
179
180
181
182
183 public Logger getLogger() {
184
185 if (this.mLogger == null) {
186
187
188 return getDefaultLogger();
189 }
190 return this.mLogger;
191 }
192
193
194
195
196
197 public String getComponentName() {
198 String componentName = null;
199
200 if (this.mComponentContext != null) {
201 componentName = this.mComponentContext.getComponentName();
202 }
203 return componentName;
204 }
205
206
207
208
209 private MessageExchangeHandlerFactory mMEHandlerFactory;
210
211
212
213
214
215
216 public MessageExchangeHandlerFactory getMessageExchangeHandlerFactory() {
217 return this.mMEHandlerFactory;
218 }
219
220
221
222
223
224
225 public void setMessageExchangeHandlerFactory(MessageExchangeHandlerFactory factory) {
226 this.mMEHandlerFactory = factory;
227 }
228
229
230
231
232 public MessageExchangeHandler newMessageExchangeHandler(MessageExchange msgExchange) {
233 MessageExchangeHandlerFactory factory = getMessageExchangeHandlerFactory();
234 if ( factory == null ) {
235 return null;
236 }
237 return factory.newHandler(msgExchange);
238 }
239 }