Skip to content

Commit 0109670

Browse files
author
André Behrens
committed
Merge pull request OfficeDev#439 from candrews/closeQuietly
Use IOUtils.closeQuietly instead of reimplementing that method in each location
2 parents 84d5aa2 + a4ce021 commit 0109670

6 files changed

Lines changed: 21 additions & 43 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<commons-logging.version>1.2</commons-logging.version>
101101
<joda-time.version>2.8</joda-time.version>
102102
<commons-lang3.version>3.4</commons-lang3.version>
103+
<commons-io.version>2.4</commons-io.version>
103104
<!-- Dependencies [TEST]: -->
104105
<junit.version>4.12</junit.version>
105106
<hamcrest-all.version>1.3</hamcrest-all.version>
@@ -223,6 +224,12 @@
223224
<version>${httpcore.version}</version>
224225
</dependency>
225226

227+
<dependency>
228+
<groupId>commons-io</groupId>
229+
<artifactId>commons-io</artifactId>
230+
<version>${commons-io.version}</version>
231+
</dependency>
232+
226233
<dependency>
227234
<groupId>commons-logging</groupId>
228235
<artifactId>commons-logging</artifactId>

src/main/java/microsoft/exchange/webservices/data/core/ExchangeServiceBase.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import microsoft.exchange.webservices.data.misc.EwsTraceListener;
5757
import microsoft.exchange.webservices.data.misc.ITraceListener;
5858

59+
import org.apache.commons.io.IOUtils;
5960
import org.apache.commons.logging.Log;
6061
import org.apache.commons.logging.LogFactory;
6162
import org.apache.http.client.AuthenticationStrategy;
@@ -264,19 +265,8 @@ private void initializeHttpContext() {
264265

265266
@Override
266267
public void close() {
267-
try {
268-
httpClient.close();
269-
} catch (IOException e) {
270-
LOG.debug(e);
271-
}
272-
273-
if (httpPoolingClient != null) {
274-
try {
275-
httpPoolingClient.close();
276-
} catch (IOException e) {
277-
LOG.debug(e);
278-
}
279-
}
268+
IOUtils.closeQuietly(httpClient);
269+
IOUtils.closeQuietly(httpPoolingClient);
280270
}
281271

282272
// Event handlers

src/main/java/microsoft/exchange/webservices/data/core/request/HangingServiceRequestBase.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import microsoft.exchange.webservices.data.core.exception.xml.XmlException;
3737
import microsoft.exchange.webservices.data.misc.HangingTraceStream;
3838
import microsoft.exchange.webservices.data.security.XmlNodeType;
39+
import org.apache.commons.io.IOUtils;
3940
import org.apache.commons.logging.Log;
4041
import org.apache.commons.logging.LogFactory;
4142

@@ -241,14 +242,7 @@ private void parseResponses() {
241242
// Stream is closed, so disconnect.
242243
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
243244
} finally {
244-
if (responseCopy != null) {
245-
try {
246-
responseCopy.close();
247-
responseCopy = null;
248-
} catch (Exception ex) {
249-
LOG.error(ex);
250-
}
251-
}
245+
IOUtils.closeQuietly(responseCopy);
252246
}
253247
}
254248

@@ -272,11 +266,7 @@ private void setIsConnected(boolean value) {
272266
*/
273267
public void disconnect() {
274268
synchronized (this) {
275-
try {
276-
this.response.close();
277-
} catch (IOException e) {
278-
// Ignore exception on disconnection
279-
}
269+
IOUtils.closeQuietly(this.response);
280270
this.disconnect(HangingRequestDisconnectReason.UserInitiated, null);
281271
}
282272
}
@@ -289,11 +279,7 @@ public void disconnect() {
289279
*/
290280
public void disconnect(HangingRequestDisconnectReason reason, Exception exception) {
291281
if (this.isConnected()) {
292-
try {
293-
this.response.close();
294-
} catch (IOException e) {
295-
// Ignore exception on disconnection
296-
}
282+
IOUtils.closeQuietly(this.response);
297283
this.internalOnDisconnect(reason, exception);
298284
}
299285
}

src/main/java/microsoft/exchange/webservices/data/core/request/HttpWebRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import microsoft.exchange.webservices.data.core.WebProxy;
2828
import microsoft.exchange.webservices.data.core.exception.http.EWSHttpException;
2929

30+
import java.io.Closeable;
3031
import java.io.IOException;
3132
import java.io.InputStream;
3233
import java.io.OutputStream;
@@ -36,7 +37,7 @@
3637
/**
3738
* The Class HttpWebRequest.
3839
*/
39-
public abstract class HttpWebRequest {
40+
public abstract class HttpWebRequest implements Closeable {
4041

4142
/**
4243
* The url.

src/main/java/microsoft/exchange/webservices/data/core/request/ServiceRequestBase.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import microsoft.exchange.webservices.data.core.exception.xml.XmlException;
4747
import microsoft.exchange.webservices.data.misc.SoapFaultDetails;
4848
import microsoft.exchange.webservices.data.security.XmlNodeType;
49+
import org.apache.commons.io.IOUtils;
4950
import org.apache.commons.logging.Log;
5051
import org.apache.commons.logging.LogFactory;
5152

@@ -644,12 +645,7 @@ protected HttpWebRequest validateAndEmitRequest() throws Exception {
644645
throw new ServiceRequestException(String.format("The request failed. %s", e.getMessage()), e);
645646
}
646647
} catch (Exception e) {
647-
try {
648-
request.close();
649-
} catch (Exception e2) {
650-
// Ignore exception while closing the request.
651-
}
652-
648+
IOUtils.closeQuietly(request);
653649
throw e;
654650
}
655651
}

src/main/java/microsoft/exchange/webservices/data/property/complex/FileAttachment.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
3434
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
3535

36+
import org.apache.commons.io.IOUtils;
37+
3638
import java.io.File;
3739
import java.io.FileInputStream;
3840
import java.io.FileOutputStream;
@@ -234,11 +236,7 @@ public void load(String fileName) throws Exception {
234236
this.load();
235237
this.loadToStream.flush();
236238
} finally {
237-
try {
238-
this.loadToStream.close();
239-
} catch(Exception e) {
240-
//ignore exception on close
241-
}
239+
IOUtils.closeQuietly(this.loadToStream);
242240
this.loadToStream = null;
243241
}
244242

0 commit comments

Comments
 (0)