29
29
*/
30
30
public abstract class AbstractWebServiceConnection implements WebServiceConnection {
31
31
32
+ private TransportInputStream tis ;
33
+
34
+ private TransportOutputStream tos ;
35
+
32
36
public final void send (WebServiceMessage message ) throws IOException {
33
37
onSendBeforeWrite (message );
34
- TransportOutputStream tos = createTransportOutputStream ();
35
- try {
36
- message .writeTo (tos );
37
- tos .flush ();
38
- }
39
- finally {
40
- tos .close ();
38
+ tos = createTransportOutputStream ();
39
+ if (tos == null ) {
40
+ return ;
41
41
}
42
+ message .writeTo (tos );
43
+ tos .flush ();
42
44
onSendAfterWrite (message );
43
45
}
44
46
45
- public final WebServiceMessage receive (WebServiceMessageFactory messageFactory ) throws IOException {
46
- onReceiveBeforeRead ();
47
- TransportInputStream tis = createTransportInputStream ();
48
- if (tis == null ) {
49
- return null ;
50
- }
51
- WebServiceMessage message = null ;
52
- try {
53
- message = messageFactory .createWebServiceMessage (tis );
54
- }
55
- finally {
56
- tis .close ();
57
- }
58
- onReceiveAfterRead (message );
59
- return message ;
47
+ /**
48
+ * Called before the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
49
+ * #send(WebServiceMessage)}.
50
+ * <p/>
51
+ * Default implementation does nothing.
52
+ *
53
+ * @param message the message
54
+ * @throws IOException when an I/O exception occurs
55
+ */
56
+ protected void onSendBeforeWrite (WebServiceMessage message ) throws IOException {
60
57
}
61
58
62
59
/**
@@ -69,27 +66,37 @@ public final WebServiceMessage receive(WebServiceMessageFactory messageFactory)
69
66
protected abstract TransportOutputStream createTransportOutputStream () throws IOException ;
70
67
71
68
/**
72
- * Called before the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
69
+ * Called after the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
73
70
* #send(WebServiceMessage)}.
74
71
* <p/>
75
72
* Default implementation does nothing.
76
73
*
77
74
* @param message the message
78
75
* @throws IOException when an I/O exception occurs
79
76
*/
80
- protected void onSendBeforeWrite (WebServiceMessage message ) throws IOException {
77
+ protected void onSendAfterWrite (WebServiceMessage message ) throws IOException {
78
+ }
79
+
80
+ public final WebServiceMessage receive (WebServiceMessageFactory messageFactory ) throws IOException {
81
+ onReceiveBeforeRead ();
82
+ tis = createTransportInputStream ();
83
+ if (tis == null ) {
84
+ return null ;
85
+ }
86
+ WebServiceMessage message = messageFactory .createWebServiceMessage (tis );
87
+ onReceiveAfterRead (message );
88
+ return message ;
81
89
}
82
90
83
91
/**
84
- * Called after the given message has been written to the <code>TransportOutputStream </code>. Called from {@link
85
- * #send(WebServiceMessage )}.
92
+ * Called before a message has been read from the <code>TransportInputStream </code>. Called from {@link
93
+ * #receive(WebServiceMessageFactory )}.
86
94
* <p/>
87
95
* Default implementation does nothing.
88
96
*
89
- * @param message the message
90
97
* @throws IOException when an I/O exception occurs
91
98
*/
92
- protected void onSendAfterWrite ( WebServiceMessage message ) throws IOException {
99
+ protected void onReceiveBeforeRead ( ) throws IOException {
93
100
}
94
101
95
102
/**
@@ -101,26 +108,47 @@ protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
101
108
protected abstract TransportInputStream createTransportInputStream () throws IOException ;
102
109
103
110
/**
104
- * Called before a message has been read from the <code>TransportInputStream</code>. Called from {@link
111
+ * Called when the given message has been read from the <code>TransportInputStream</code>. Called from {@link
105
112
* #receive(WebServiceMessageFactory)}.
106
113
* <p/>
107
114
* Default implementation does nothing.
108
115
*
116
+ * @param message the message
109
117
* @throws IOException when an I/O exception occurs
110
118
*/
111
- protected void onReceiveBeforeRead () throws IOException {
119
+ protected void onReceiveAfterRead (WebServiceMessage message ) throws IOException {
120
+ }
121
+
122
+ public final void close () throws IOException {
123
+ IOException ioex = null ;
124
+ if (tis != null ) {
125
+ try {
126
+ tis .close ();
127
+ }
128
+ catch (IOException ex ) {
129
+ ioex = ex ;
130
+ }
131
+ }
132
+ if (tos != null ) {
133
+ try {
134
+ tos .close ();
135
+ }
136
+ catch (IOException ex ) {
137
+ ioex = ex ;
138
+ }
139
+ }
140
+ onClose ();
141
+ if (ioex != null ) {
142
+ throw ioex ;
143
+ }
112
144
}
113
145
114
146
/**
115
- * Called when the given message has been read from the <code>TransportInputStream</code>. Called from {@link
116
- * #receive(WebServiceMessageFactory)}.
117
- * <p/>
118
- * Default implementation does nothing.
147
+ * Template method invoked from {@link #close()}. Default implementation is empty.
119
148
*
120
- * @param message the message
121
- * @throws IOException when an I/O exception occurs
149
+ * @throws IOException if an I/O error occurs when closing this connection
122
150
*/
123
- protected void onReceiveAfterRead ( WebServiceMessage message ) throws IOException {
151
+ protected void onClose ( ) throws IOException {
124
152
}
125
153
126
154
}
0 commit comments