-
Notifications
You must be signed in to change notification settings - Fork 1
/
RedisSession.java
135 lines (115 loc) · 3.85 KB
/
RedisSession.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.tomcat.session.redis;
import java.io.IOException;
import java.security.Principal;
import java.util.Date;
import java.util.HashMap;
import org.apache.catalina.Manager;
import org.apache.catalina.session.StandardSession;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.Utils;
/**
* A Redis-backed session
*/
public class RedisSession extends StandardSession {
private final Log log = Utils.getLog(RedisSession.class);
protected static Boolean manualDirtyTrackingSupportEnabled = true;
public static void setManualDirtyTrackingSupportEnabled(Boolean enabled) {
manualDirtyTrackingSupportEnabled = enabled;
}
protected static String manualDirtyTrackingAttributeKey = "__changed__";
public static void setManualDirtyTrackingAttributeKey(String key) {
manualDirtyTrackingAttributeKey = key;
}
protected HashMap<String, Object> changedAttributes;
protected Boolean dirty;
public RedisSession(Manager manager) {
super(manager);
resetDirtyTracking();
}
public Boolean isDirty() {
return dirty || !changedAttributes.isEmpty();
}
public HashMap<String, Object> getChangedAttributes() {
return changedAttributes;
}
public void resetDirtyTracking() {
changedAttributes = new HashMap<>();
dirty = false;
}
@Override
public void setAttribute(String key, Object value) {
if (manualDirtyTrackingSupportEnabled && manualDirtyTrackingAttributeKey.equals(key)) {
dirty = true;
return;
}
Object oldValue = getAttribute(key);
super.setAttribute(key, value);
if ((value != null || oldValue != null)
&& (value == null && oldValue != null || oldValue == null && value != null
|| !value.getClass().isInstance(oldValue) || !value.equals(oldValue))) {
if (this.manager instanceof RedisSessionManager && ((RedisSessionManager) this.manager).getSaveOnChange()) {
try {
((RedisSessionManager) this.manager).save(this, true);
} catch (IOException ex) {
log.error("Error saving session on setAttribute (triggered by saveOnChange=true): "
+ ex.getMessage());
}
} else {
changedAttributes.put(key, value);
}
}
}
@Override
public void removeAttribute(String name) {
super.removeAttribute(name);
if (this.manager instanceof RedisSessionManager && ((RedisSessionManager) this.manager).getSaveOnChange()) {
try {
((RedisSessionManager) this.manager).save(this, true);
} catch (IOException ex) {
log.error("Error saving session on setAttribute (triggered by saveOnChange=true): " + ex.getMessage());
}
} else {
dirty = true;
}
}
@Override
public void setId(String id) {
// Specifically do not call super(): it's implementation does unexpected things
// like calling manager.remove(session.id) and manager.add(session).
this.id = id;
}
@Override
public void setPrincipal(Principal principal) {
dirty = true;
super.setPrincipal(principal);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("RedisSession[");
sb.append(id);
sb.append("] (created: ");
sb.append(new Date(creationTime));
sb.append(", accessed: ");
sb.append(new Date(lastAccessedTime));
sb.append(", expires: ");
sb.append(new Date(lastAccessedTime + maxInactiveInterval * 1000));
sb.append(")");
return (sb.toString());
}
}