44import fi .helsinki .cs .tmc .data .Exercise ;
55import fi .helsinki .cs .tmc .events .TmcEvent ;
66import 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 ;
127import java .util .List ;
13- import java .util .logging .Level ;
148import java .util .logging .Logger ;
15- import org .netbeans .api .annotations .common .NullAllowed ;
169
1710public 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}
0 commit comments