Skip to content

Commit 939236f

Browse files
committed
Cleaning up the loggers and using container-managed client sessions instead of user-managed
1 parent 34233c3 commit 939236f

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

websocket/whiteboard/src/main/java/org/javaee7/websocket/whiteboard/FigureDecoder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import java.io.StringReader;
4343
import java.lang.invoke.MethodHandles;
44+
import java.util.logging.Level;
4445
import java.util.logging.Logger;
4546

4647
import javax.json.Json;
@@ -55,11 +56,11 @@
5556
*/
5657
public class FigureDecoder implements Decoder.Text<Figure> {
5758

58-
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
59+
private static final Logger LOGGER = Logger.getLogger(FigureDecoder.class.getName());
5960

6061
@Override
6162
public Figure decode(String string) throws DecodeException {
62-
LOGGER.info("decoding: " + string);
63+
LOGGER.log(Level.INFO, "decoding: {0}", string);
6364
JsonObject jsonObject = Json.createReader(new StringReader(string)).readObject();
6465
return new Figure(jsonObject);
6566
}
@@ -70,18 +71,16 @@ public boolean willDecode(String string) {
7071
Json.createReader(new StringReader(string)).readObject();
7172
return true;
7273
} catch (JsonException ex) {
73-
ex.printStackTrace();
74+
LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
7475
return false;
7576
}
7677
}
7778

7879
@Override
7980
public void init(EndpointConfig ec) {
80-
LOGGER.info("init");
8181
}
8282

8383
@Override
8484
public void destroy() {
85-
LOGGER.info("destroy");
8685
}
8786
}

websocket/whiteboard/src/main/java/org/javaee7/websocket/whiteboard/FigureEncoder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
package org.javaee7.websocket.whiteboard;
4141

4242
import java.lang.invoke.MethodHandles;
43+
import java.util.logging.Level;
4344
import java.util.logging.Logger;
4445

4546
import javax.websocket.EncodeException;
@@ -51,20 +52,19 @@
5152
*/
5253
public class FigureEncoder implements Encoder.Text<Figure> {
5354

54-
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
55+
private static final Logger LOGGER = Logger.getLogger(FigureDecoder.class.getName());
5556

5657
@Override
5758
public String encode(Figure figure) throws EncodeException {
59+
LOGGER.log(Level.INFO, "encoding: {0}", figure);
5860
return figure.getJson().toString();
5961
}
6062

6163
@Override
6264
public void init(EndpointConfig ec) {
63-
LOGGER.info("init");
6465
}
6566

6667
@Override
6768
public void destroy() {
68-
LOGGER.info("destroy");
6969
}
7070
}

websocket/whiteboard/src/main/java/org/javaee7/websocket/whiteboard/Whiteboard.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.nio.ByteBuffer;
4545
import java.util.concurrent.ConcurrentHashMap;
4646
import java.util.concurrent.ConcurrentMap;
47+
import java.util.logging.Level;
4748
import java.util.logging.Logger;
4849

4950
import javax.websocket.EncodeException;
@@ -61,7 +62,7 @@
6162
decoders = {FigureDecoder.class})
6263
public class Whiteboard {
6364

64-
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
65+
private static final Logger LOGGER = Logger.getLogger(Whiteboard.class.getName());
6566

6667
private static final Object PRESENT = new Object();
6768

@@ -79,8 +80,9 @@ public void onClose(Session peer) {
7980

8081
@OnMessage
8182
public void broadcastFigure(Figure figure, Session session) throws IOException, EncodeException {
82-
LOGGER.info("boradcastFigure: " + figure);
83-
for (Session peer : peers.keySet()) {
83+
LOGGER.log(Level.INFO, "boradcastFigure: {0}", figure);
84+
for (Session peer : session.getOpenSessions()) {
85+
// for (Session peer : peers.keySet()) {
8486
if (!peer.equals(session)) {
8587
peer.getBasicRemote().sendObject(figure);
8688
}
@@ -89,8 +91,9 @@ public void broadcastFigure(Figure figure, Session session) throws IOException,
8991

9092
@OnMessage
9193
public void broadcastSnapshot(ByteBuffer data, Session session) throws IOException {
92-
LOGGER.info("broadcastBinary: " + data);
93-
for (Session peer : peers.keySet()) {
94+
LOGGER.log(Level.INFO, "broadcastBinary: {0}", data);
95+
for (Session peer : session.getOpenSessions()) {
96+
// for (Session peer : peers.keySet()) {
9497
if (!peer.equals(session)) {
9598
peer.getBasicRemote().sendBinary(data);
9699
}

0 commit comments

Comments
 (0)