4
4
import fi .helsinki .cs .tmc .data .Exercise ;
5
5
import fi .helsinki .cs .tmc .events .TmcEvent ;
6
6
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 ;
12
7
import java .util .List ;
13
- import java .util .logging .Level ;
14
8
import java .util .logging .Logger ;
15
- import org .netbeans .api .annotations .common .NullAllowed ;
16
9
17
10
public class LoggableEvent implements TmcEvent {
18
11
@@ -21,12 +14,11 @@ public class LoggableEvent implements TmcEvent {
21
14
private String courseName ;
22
15
private String exerciseName ;
23
16
private String eventType ;
24
- private int hostId ;
25
17
26
18
private byte [] data ;
27
19
28
- @ NullAllowed
29
- private String metadata ;
20
+ private JsonMaker metadata ;
21
+
30
22
private long happenedAt ; // millis from epoch
31
23
private long systemNanotime ;
32
24
private transient String key ;
@@ -43,20 +35,20 @@ public LoggableEvent(Course course, String eventType, byte[] data) {
43
35
this (course .getName (), "" , eventType , data , null );
44
36
}
45
37
46
- public LoggableEvent (Exercise exercise , String eventType , byte [] data , String metadata ) {
38
+ public LoggableEvent (Exercise exercise , String eventType , byte [] data , JsonMaker metadata ) {
47
39
this (exercise .getCourseName (), exercise .getName (), eventType , data , metadata );
48
40
}
49
41
50
42
public LoggableEvent (String courseName , String exerciseName , String eventType , byte [] data ) {
51
43
this (courseName , exerciseName , eventType , data , null );
52
44
}
53
45
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 ) {
55
47
this .courseName = courseName ;
56
48
this .exerciseName = exerciseName ;
57
49
this .eventType = eventType ;
58
50
this .data = data ;
59
- this .metadata = metadata ;
51
+ this .metadata = JsonMaker . create (). merge ( metadata ) ;
60
52
this .happenedAt = System .currentTimeMillis ();
61
53
this .systemNanotime = System .nanoTime ();
62
54
@@ -79,19 +71,36 @@ public byte[] getData() {
79
71
return data ;
80
72
}
81
73
82
- public int getHostId () {
83
- return hostId ;
84
- }
85
-
86
- public void setHostId (int hostId ) {
87
- this .hostId = hostId ;
88
- }
89
-
90
74
/**
91
75
* Optional JSON metadata.
92
76
*/
93
77
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 ;
95
104
}
96
105
97
106
/**
@@ -113,51 +122,8 @@ public long getSystemNanotime() {
113
122
return systemNanotime ;
114
123
}
115
124
116
-
117
-
118
-
119
125
@ Override
120
126
public String toString () {
121
127
return "LoggableEvent{" + "courseName=" + courseName + ", exerciseName=" + exerciseName + ", eventType=" + eventType + ", happenedAt=" + happenedAt + ", systemNanotime=" + systemNanotime + ", key=" + key + ", metadata=" + metadata + ", data=" + new String (data ) + "}" ;
122
128
}
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
- }
163
129
}
0 commit comments