Skip to content

Commit 9f742b3

Browse files
plukasewern
authored andcommitted
SAK-40560: SiteStats allows mutation of cached event registry data (sakaiproject#5959)
* SAK-40560: SiteStats allows mutation of cached event registry data * SAK-40560: implement Cloneable using copy constructor
1 parent 101c5ad commit 9f742b3

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

sitestats/sitestats-api/src/java/org/sakaiproject/sitestats/api/event/EventInfo.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import java.io.Serializable;
2222

23-
public class EventInfo implements Serializable {
23+
public class EventInfo implements Serializable, Cloneable {
2424
private static final long serialVersionUID = 1L;
2525
private String eventId;
2626
private boolean selected;
@@ -30,6 +30,17 @@ public EventInfo(String eventId) {
3030
this.eventId = eventId.trim();
3131
}
3232

33+
public EventInfo(EventInfo info) {
34+
eventId = info.eventId;
35+
selected = info.selected;
36+
anonymous = info.anonymous;
37+
}
38+
39+
@Override
40+
public EventInfo clone() {
41+
return new EventInfo(this);
42+
}
43+
3344
public String getEventId() {
3445
return eventId;
3546
}

sitestats/sitestats-api/src/java/org/sakaiproject/sitestats/api/event/ToolInfo.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.sakaiproject.sitestats.api.parser.EventParserTip;
2727

2828

29-
public class ToolInfo implements Serializable {
29+
public class ToolInfo implements Serializable, Cloneable {
3030
private static final long serialVersionUID = 1L;
3131
private String toolId;
3232
private List<String> additionalToolIds;
@@ -44,6 +44,22 @@ public ToolInfo(String toolId, List<String> additionalToolIds) {
4444
eventInfos = new ArrayList<EventInfo>();
4545
}
4646

47+
public ToolInfo(ToolInfo tool) {
48+
toolId = tool.toolId;
49+
additionalToolIds = tool.additionalToolIds != null ? new ArrayList<>(tool.additionalToolIds) : null;
50+
eventInfos = new ArrayList<>(tool.eventInfos.size());
51+
for (EventInfo info : tool.eventInfos) {
52+
eventInfos.add(info.clone());
53+
}
54+
selected = tool.selected;
55+
eventParserTip = tool.eventParserTip != null ? tool.eventParserTip.clone() : null;
56+
}
57+
58+
@Override
59+
public ToolInfo clone() {
60+
return new ToolInfo(this);
61+
}
62+
4763
public List<EventInfo> getEvents() {
4864
return eventInfos;
4965
}
@@ -132,5 +148,4 @@ public String toString() {
132148
}
133149
return buff.toString();
134150
}
135-
136151
}

sitestats/sitestats-api/src/java/org/sakaiproject/sitestats/api/parser/EventParserTip.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import java.io.Serializable;
2222

23-
public class EventParserTip implements Serializable {
23+
public class EventParserTip implements Serializable, Cloneable {
2424
private static final long serialVersionUID = 1L;
2525
private String forWhat;
2626
private String separator;
@@ -37,6 +37,17 @@ public EventParserTip(String forWhat, String separator, String index) {
3737
this.index = index;
3838
}
3939

40+
public EventParserTip(EventParserTip tip) {
41+
forWhat = tip.forWhat;
42+
separator = tip.separator;
43+
index = tip.index;
44+
}
45+
46+
@Override
47+
public EventParserTip clone() {
48+
return new EventParserTip(this);
49+
}
50+
4051
public String getFor() {
4152
return forWhat;
4253
}

sitestats/sitestats-impl/src/java/org/sakaiproject/sitestats/impl/event/EventRegistryServiceImpl.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,17 @@ private List<ToolInfo> getMergedEventRegistry() {
402402
}
403403
// STAT-380 ensure we do not return a null from this method
404404
if (eventRegistry == null) {
405-
eventRegistry = new ArrayList<ToolInfo>(0);
405+
return new ArrayList<>(0);
406406
}
407-
return eventRegistry;
407+
408+
// defensively deep copy the event registry before returning it, this prevents outside code from
409+
// mutating the cached registry entries
410+
List<ToolInfo> cloneRegistry = new ArrayList<>(eventRegistry.size());
411+
for (ToolInfo t : eventRegistry) {
412+
cloneRegistry.add(t.clone());
413+
}
414+
415+
return cloneRegistry;
408416
}
409417

410418
/** Process event registry expired notifications */

0 commit comments

Comments
 (0)