Skip to content

Commit a7d1e9f

Browse files
authored
Merge pull request #270 from DevFactory/release/exception-handlers-fix-1
Code quality fix - Exception handlers should preserve the original exception
2 parents 4c0bc5e + f99d543 commit a7d1e9f

File tree

6 files changed

+39
-20
lines changed

6 files changed

+39
-20
lines changed

src/main/java/com/notnoop/apns/EnhancedApnsNotification.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@
3434
import java.util.concurrent.atomic.AtomicInteger;
3535
import com.notnoop.apns.internal.Utilities;
3636
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
39+
import java.io.UnsupportedEncodingException;
3740

3841
/**
3942
* Represents an APNS notification to be sent to Apple service.
4043
*/
4144
public class EnhancedApnsNotification implements ApnsNotification {
42-
45+
46+
private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedApnsNotification.class);
4347
private final static byte COMMAND = 1;
4448
private static AtomicInteger nextId = new AtomicInteger(0);
4549
private final int identifier;
@@ -168,7 +172,8 @@ public String toString() {
168172
String payloadString;
169173
try {
170174
payloadString = new String(payload, "UTF-8");
171-
} catch (Exception ex) {
175+
} catch (UnsupportedEncodingException ex) {
176+
LOGGER.debug("UTF-8 charset not found on the JRE", ex);
172177
payloadString = "???";
173178
}
174179
return "Message(Id="+identifier+"; Token="+Utilities.encodeHex(deviceToken)+"; Payload="+payloadString+")";

src/main/java/com/notnoop/apns/SimpleApnsNotification.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
import com.notnoop.apns.internal.Utilities;
3636
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37-
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
39+
import java.io.UnsupportedEncodingException;
3840

3941
/**
4042
* Represents an APNS notification to be sent to Apple service. This is for legacy use only
@@ -52,7 +54,8 @@
5254
@SuppressWarnings("deprecation")
5355
@Deprecated
5456
public class SimpleApnsNotification implements ApnsNotification {
55-
57+
58+
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleApnsNotification.class);
5659
private final static byte COMMAND = 0;
5760
private final byte[] deviceToken;
5861
private final byte[] payload;
@@ -155,7 +158,8 @@ public String toString() {
155158
String payloadString;
156159
try {
157160
payloadString = new String(payload, "UTF-8");
158-
} catch (Exception ex) {
161+
} catch (UnsupportedEncodingException ex) {
162+
LOGGER.debug("UTF-8 charset not found on the JRE", ex);
159163
payloadString = "???";
160164
}
161165
return "Message(Token="+Utilities.encodeHex(deviceToken)+"; Payload="+payloadString+")";

src/main/java/com/notnoop/apns/internal/ApnsConnectionImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public void run() {
147147
try {
148148
in = socket.getInputStream();
149149
} catch (IOException ioe) {
150+
logger.warn("The value of socket is null", ioe);
150151
in = null;
151152
}
152153

src/test/java/com/notnoop/apns/utils/ApnsServerStub.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.nio.ByteBuffer;
4242
import java.util.concurrent.Semaphore;
4343
import java.util.concurrent.atomic.AtomicInteger;
44+
import org.slf4j.Logger;
45+
import org.slf4j.LoggerFactory;
4446

4547
public class ApnsServerStub {
4648

@@ -70,7 +72,8 @@ public static ApnsServerStub prepareAndStartServer() {
7072
server.start();
7173
return server;
7274
}
73-
75+
76+
private static final Logger LOGGER = LoggerFactory.getLogger(ApnsServerStub.class);
7477
private final AtomicInteger toWaitBeforeSend = new AtomicInteger(0);
7578
private final ByteArrayOutputStream received;
7679
private final ByteArrayOutputStream toSend;
@@ -118,14 +121,14 @@ public void stop() {
118121
gatewaySocket.close();
119122
}
120123
} catch (IOException e) {
121-
e.printStackTrace();
124+
LOGGER.warn("Can not close gatewaySocket properly", e);
122125
}
123126
try {
124127
if (feedbackSocket != null) {
125128
feedbackSocket.close();
126129
}
127130
} catch (IOException e) {
128-
e.printStackTrace();
131+
LOGGER.warn("Can not close feedbackSocket properly", e);
129132
}
130133

131134
if (gatewayThread != null) {
@@ -146,7 +149,7 @@ public void sendError(int err, int id) {
146149
gatewayOutputStream.write(buf.array());
147150
gatewayOutputStream.flush();
148151
} catch (Exception ex) {
149-
ex.printStackTrace();
152+
LOGGER.warn("An error occured with accessing gateway", ex);
150153
}
151154
}
152155

@@ -227,14 +230,14 @@ public void run() {
227230
in.close();
228231
}
229232
} catch (IOException ioex) {
230-
System.err.println(ioex.toString());
233+
LOGGER.warn("Can not close socket properly", ioex);
231234
}
232235
try {
233236
if (gatewayOutputStream != null) {
234237
gatewayOutputStream.close();
235238
}
236239
} catch (IOException ioex) {
237-
System.err.println(ioex.toString());
240+
LOGGER.warn("Can not close gatewayOutputStream properly", ioex);
238241
}
239242
messages.release();
240243
}

src/test/java/com/notnoop/apns/utils/Simulator/ApnsServerSimulator.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public void stop() {
8585
gatewaySocket.close();
8686
}
8787
} catch (IOException e) {
88-
e.printStackTrace();
88+
logger.warn("Can not close gatewaySocket properly", e);
8989
}
9090
try {
9191
if (feedbackSocket != null) {
9292
feedbackSocket.close();
9393
}
9494
} catch (IOException e) {
95-
e.printStackTrace();
95+
logger.warn("Can not close feedbackSocket properly", e);
9696
}
9797

9898
if (gatewayThread != null) {
@@ -141,9 +141,10 @@ public void run() {
141141
try {
142142
handleGatewayConnection(new InputOutputSocket(gatewaySocket.accept()));
143143
} catch (SocketException ex) {
144+
logger.warn("Interruption while handling gateway connection", ex);
144145
interrupt();
145146
} catch (IOException ioe) {
146-
ioe.printStackTrace();
147+
logger.warn("An error occured while handling gateway connection", ioe);
147148
}
148149
}
149150
} finally {
@@ -186,6 +187,7 @@ private void parseNotifications(final InputOutputSocket inputOutputSocket) {
186187
break;
187188
}
188189
} catch (IOException ioe) {
190+
logger.warn("An error occured while reading notification", ioe);
189191
Thread.currentThread().interrupt();
190192
}
191193
}
@@ -255,7 +257,7 @@ public void interrupt() {
255257
try {
256258
gatewaySocket.close();
257259
} catch (IOException e) {
258-
e.printStackTrace();
260+
logger.warn("Can not close gatewaySocket properly", e);
259261
}
260262
}
261263
}
@@ -310,9 +312,10 @@ public void run() {
310312
try {
311313
handleFeedbackConnection(new InputOutputSocket(feedbackSocket.accept()));
312314
} catch (SocketException ex) {
315+
logger.warn("Interruption while handling feedback connection", ex);
313316
interrupt();
314317
} catch (IOException ioe) {
315-
ioe.printStackTrace();
318+
logger.warn("An error occured while handling feedback connection", ioe);
316319
}
317320
}
318321
} finally {
@@ -331,7 +334,7 @@ public void run() {
331334
sendFeedback(inputOutputSocket);
332335
} catch (IOException ioe) {
333336
// An exception is unexpected here. Close the current connection and bail out.
334-
ioe.printStackTrace();
337+
logger.warn("An error occured while sending feedback", ioe);
335338
} finally {
336339
inputOutputSocket.close();
337340
}

src/test/java/com/notnoop/apns/utils/Simulator/InputOutputSocket.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
import java.io.DataOutputStream;
3434
import java.io.IOException;
3535
import java.net.Socket;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
3638

3739
/**
3840
* Wrap some of the boilerplate code using socket, enable passing around a socket together with its streams.
3941
*/
4042
public class InputOutputSocket {
43+
private static final Logger LOGGER = LoggerFactory.getLogger(InputOutputSocket.class);
4144
private final Socket socket;
4245
private final ApnsInputStream inputStream;
4346
private final DataOutputStream outputStream;
@@ -75,19 +78,19 @@ public synchronized void close() {
7578
try {
7679
inputStream.close();
7780
} catch (IOException e) {
78-
e.printStackTrace();
81+
LOGGER.warn("Can not close inputStream properly", e);
7982
}
8083

8184
try {
8285
outputStream.close();
8386
} catch (IOException e) {
84-
e.printStackTrace();
87+
LOGGER.warn("Can not close outputStream properly", e);
8588
}
8689

8790
try {
8891
socket.close();
8992
} catch (IOException e) {
90-
e.printStackTrace();
93+
LOGGER.warn("Can not close socket properly", e);
9194
}
9295
}
9396

0 commit comments

Comments
 (0)