Skip to content

Commit 39f697b

Browse files
committed
Made LoggableEvent metadata editable for tagging EventReceivers to add fields more nicely
1 parent 1937c85 commit 39f697b

File tree

4 files changed

+55
-76
lines changed

4 files changed

+55
-76
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/LoggableEvent.java

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@
44
import fi.helsinki.cs.tmc.data.Exercise;
55
import fi.helsinki.cs.tmc.events.TmcEvent;
66
import fi.helsinki.cs.tmc.utilities.JsonMaker;
7-
import java.net.NetworkInterface;
8-
import java.net.SocketException;
9-
import java.net.UnknownHostException;
10-
import java.util.ArrayList;
11-
import java.util.Enumeration;
127
import java.util.List;
13-
import java.util.logging.Level;
148
import java.util.logging.Logger;
15-
import org.netbeans.api.annotations.common.NullAllowed;
169

1710
public class LoggableEvent implements TmcEvent {
1811

@@ -21,12 +14,11 @@ public class LoggableEvent implements TmcEvent {
2114
private String courseName;
2215
private String exerciseName;
2316
private String eventType;
24-
private int hostId;
2517

2618
private byte[] data;
2719

28-
@NullAllowed
29-
private String metadata;
20+
private JsonMaker metadata;
21+
3022
private long happenedAt; // millis from epoch
3123
private long systemNanotime;
3224
private transient String key;
@@ -43,20 +35,20 @@ public LoggableEvent(Course course, String eventType, byte[] data) {
4335
this(course.getName(), "", eventType, data, null);
4436
}
4537

46-
public LoggableEvent(Exercise exercise, String eventType, byte[] data, String metadata) {
38+
public LoggableEvent(Exercise exercise, String eventType, byte[] data, JsonMaker metadata) {
4739
this(exercise.getCourseName(), exercise.getName(), eventType, data, metadata);
4840
}
4941

5042
public LoggableEvent(String courseName, String exerciseName, String eventType, byte[] data) {
5143
this(courseName, exerciseName, eventType, data, null);
5244
}
5345

54-
public LoggableEvent(String courseName, String exerciseName, String eventType, byte[] data, String metadata) {
46+
public LoggableEvent(String courseName, String exerciseName, String eventType, byte[] data, JsonMaker metadata) {
5547
this.courseName = courseName;
5648
this.exerciseName = exerciseName;
5749
this.eventType = eventType;
5850
this.data = data;
59-
this.metadata = metadata;
51+
this.metadata = JsonMaker.create().merge(metadata);
6052
this.happenedAt = System.currentTimeMillis();
6153
this.systemNanotime = System.nanoTime();
6254

@@ -79,19 +71,36 @@ public byte[] getData() {
7971
return data;
8072
}
8173

82-
public int getHostId() {
83-
return hostId;
84-
}
85-
86-
public void setHostId(int hostId) {
87-
this.hostId = hostId;
88-
}
89-
9074
/**
9175
* Optional JSON metadata.
9276
*/
9377
public String getMetadata() {
94-
return metadata;
78+
return metadata.toString();
79+
}
80+
81+
public LoggableEvent addMetadata(String name, String value) {
82+
metadata.add(name, value);
83+
return this;
84+
}
85+
86+
public LoggableEvent addMetadata(String name, long value) {
87+
metadata.add(name, value);
88+
return this;
89+
}
90+
91+
public LoggableEvent addMetadata(String name, boolean value) {
92+
metadata.add(name, value);
93+
return this;
94+
}
95+
96+
public LoggableEvent addMetadata(String name, List<String> values) {
97+
metadata.add(name, values);
98+
return this;
99+
}
100+
101+
public LoggableEvent addMetadata(JsonMaker metadata) {
102+
this.metadata.merge(metadata);
103+
return this;
95104
}
96105

97106
/**
@@ -113,51 +122,8 @@ public long getSystemNanotime() {
113122
return systemNanotime;
114123
}
115124

116-
117-
118-
119125
@Override
120126
public String toString() {
121127
return "LoggableEvent{" + "courseName=" + courseName + ", exerciseName=" + exerciseName + ", eventType=" + eventType + ", happenedAt=" + happenedAt + ", systemNanotime=" + systemNanotime + ", key=" + key + ", metadata=" + metadata + ", data=" + new String(data) + "}";
122128
}
123-
124-
/**
125-
* Generates information which should mostly be static throughout netbeans
126-
* session. However, the ip address sure could change.
127-
*/
128-
private static String getStaticHostInformation() {
129-
JsonMaker builder = JsonMaker.create();
130-
131-
try {
132-
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
133-
builder.add("hostAddress", localMachine.getHostAddress());
134-
builder.add("hostName", localMachine.getHostName());
135-
} catch (UnknownHostException ex) {
136-
log.log(Level.WARNING, "Exception while getting host name information: {0}", ex);
137-
}
138-
139-
try {
140-
Enumeration<NetworkInterface> iterator = NetworkInterface.getNetworkInterfaces();
141-
List<String> macs = new ArrayList<String>(2);
142-
while (iterator.hasMoreElements()) {
143-
NetworkInterface networkInterface = iterator.nextElement();
144-
if (networkInterface.isUp() && !networkInterface.isLoopback()) {
145-
byte[] mac = networkInterface.getHardwareAddress();
146-
StringBuilder sb = new StringBuilder();
147-
for (int i = 0; i < mac.length; i++) {
148-
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
149-
}
150-
macs.add(sb.toString());
151-
}
152-
153-
}
154-
builder.add("mac", macs);
155-
156-
} catch (SocketException ex) {
157-
log.log(Level.WARNING, "Exception while getting host mac information: {0}", ex);
158-
}
159-
160-
builder.add("hostUsername", System.getProperty("user.name"));
161-
return builder.toString();
162-
}
163129
}

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/SpywareFacade.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public TaggingEventReceiver(EventReceiver nextReceiver, int hostId) {
7979

8080
@Override
8181
public void receiveEvent(LoggableEvent event) {
82-
event.setHostId(hostId);
82+
event.addMetadata("host_id", hostId);
8383
nextReceiver.receiveEvent(event);
8484
}
8585

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/eventsources/SourceSnapshotEventSource.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ private void reactToChange(final ChangeType changeType, final FileObject fileObj
101101
return;
102102
}
103103

104-
String metadata = JsonMaker.create()
104+
JsonMaker metadata = JsonMaker.create()
105105
.add("cause", changeType.name().toLowerCase())
106-
.add("file", filePath)
107-
.toString();
106+
.add("file", filePath);
108107
invokeSnapshotThreadViaEdt(fileObject, metadata);
109108
}
110109

@@ -114,17 +113,16 @@ private void reactToRename(final ChangeType changeType, final FileRenameEvent re
114113
return;
115114
}
116115

117-
String metadata = JsonMaker.create()
116+
JsonMaker metadata = JsonMaker.create()
118117
.add("cause", changeType.name().toLowerCase())
119118
.add("file", filePath)
120-
.add("previous_name", renameEvent.getName() + "." + renameEvent.getExt())
121-
.toString();
119+
.add("previous_name", renameEvent.getName() + "." + renameEvent.getExt());
122120
invokeSnapshotThreadViaEdt(renameEvent.getFile(), metadata);
123121
}
124122

125123
// I have no idea what thread FileUtil callbacks are made in,
126124
// so I'll go to the EDT to safely read the global state.
127-
private void invokeSnapshotThreadViaEdt(final FileObject fileObject, final String metadata) {
125+
private void invokeSnapshotThreadViaEdt(final FileObject fileObject, final JsonMaker metadata) {
128126
SwingUtilities.invokeLater(new Runnable() {
129127
@Override
130128
public void run() {
@@ -141,7 +139,7 @@ public void run() {
141139
});
142140
}
143141

144-
private void startSnapshotThread(FileObject changedFile, String metadata) {
142+
private void startSnapshotThread(FileObject changedFile, JsonMaker metadata) {
145143
if (!settings.isSpywareEnabled()) {
146144
return;
147145
}
@@ -171,9 +169,9 @@ private static class SnapshotThread extends Thread {
171169
private final EventReceiver receiver;
172170
private final Exercise exercise;
173171
private final TmcProjectInfo projectInfo;
174-
private final String metadata;
172+
private final JsonMaker metadata;
175173

176-
private SnapshotThread(EventReceiver receiver, Exercise exercise, TmcProjectInfo projectInfo, String metadata) {
174+
private SnapshotThread(EventReceiver receiver, Exercise exercise, TmcProjectInfo projectInfo, JsonMaker metadata) {
177175
super("Source snapshot");
178176
this.receiver = receiver;
179177
this.exercise = exercise;

tmc-plugin/src/fi/helsinki/cs/tmc/utilities/JsonMaker.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonObject;
66
import com.google.gson.JsonPrimitive;
77
import java.util.List;
8+
import java.util.Map;
89

910
/**
1011
* A convenient way to build ad-hoc JSON objects.
@@ -57,4 +58,18 @@ public JsonMaker add(String name, List<String> values) {
5758
public String toString() {
5859
return toplevel.toString();
5960
}
61+
62+
private JsonObject asJsonObject() {
63+
return toplevel;
64+
}
65+
public JsonMaker merge(JsonMaker merge) {
66+
if (merge == null) {
67+
return this;
68+
}
69+
for (Map.Entry<String, JsonElement> element : merge.asJsonObject().entrySet()) {
70+
toplevel.add(element.getKey(), element.getValue());
71+
}
72+
return this;
73+
}
74+
6075
}

0 commit comments

Comments
 (0)