1
2
3
4
5
6
7
8 package net.kwfgrid.gwes;
9
10 import net.kwfgrid.gwes.exception.DatabaseException;
11 import org.apache.log4j.Logger;
12
13
14
15
16
17
18
19
20 final class XMLDBSessionManager extends Thread {
21
22
23
24
25 private final static int SESSION_TIMEOUT = 1000 * 60;
26
27
28
29
30 static final Logger logger = Logger.getLogger(XMLDBSessionManager.class);
31
32
33
34
35 public final Object adminLock = new Object();
36
37
38
39
40 public final Object queryLock = new Object();
41
42
43
44
45 private String adminSessionId;
46
47
48
49
50 private String querySessionId;
51
52
53
54
55 private long adminTimestamp;
56
57
58
59
60 private long queryTimestamp;
61
62 private XMLDB xmldb;
63
64 static int threadcount = 0;
65
66
67
68
69 public XMLDBSessionManager(XMLDB xmldb) throws DatabaseException {
70 super("XMLDBSessionManager#" + (++threadcount));
71 this.xmldb = xmldb;
72 start();
73 }
74
75
76
77
78 public final void run() {
79 logger.info(this + " started ...");
80 while (!isInterrupted()) {
81 long now = System.currentTimeMillis();
82 try {
83 logger.debug("Searching for old sessions");
84
85 synchronized (adminLock) {
86 if (adminSessionId != null && now - adminTimestamp > SESSION_TIMEOUT) {
87 xmldb.disconnectAdmin(adminSessionId);
88 adminSessionId = null;
89 logger.debug("Admin session disconnected");
90 }
91 }
92 } catch (DatabaseException e) {
93 logger.error("exception: " + e, e);
94 synchronized (adminLock) {
95 adminSessionId = null;
96 logger.debug("Admin session destroyed");
97 }
98 }
99 try {
100
101 synchronized (queryLock) {
102 if (querySessionId != null && now - queryTimestamp > SESSION_TIMEOUT) {
103 xmldb.disconnectQuery(querySessionId);
104 querySessionId = null;
105 logger.debug("Query session disconnected");
106 }
107 }
108 } catch (DatabaseException e) {
109 logger.error("exception: " + e, e);
110 synchronized (queryLock) {
111 querySessionId = null;
112 logger.debug("Query session destroyed");
113 }
114 }
115 try {
116 Thread.sleep(SESSION_TIMEOUT);
117 } catch (InterruptedException e1) {
118 interrupt();
119 }
120 }
121 }
122
123
124
125
126
127
128
129
130
131 public String getAdminSessionId() throws DatabaseException {
132 if (adminSessionId == null) {
133 adminSessionId = xmldb.connectAdmin();
134 }
135 adminTimestamp = System.currentTimeMillis();
136 return adminSessionId;
137 }
138
139
140
141
142
143
144
145
146
147 public String getQuerySessionId() throws DatabaseException {
148 if (querySessionId == null) {
149 querySessionId = xmldb.connectQuery();
150 }
151 queryTimestamp = System.currentTimeMillis();
152 return querySessionId;
153 }
154
155
156
157
158 public void resetSession() {
159
160 try {
161 synchronized (adminLock) {
162 if (adminSessionId != null) {
163 xmldb.disconnectAdmin(adminSessionId);
164 logger.debug("Admin session disconnected");
165 adminSessionId = null;
166 }
167 }
168 } catch (DatabaseException e) {
169 adminSessionId = null;
170 }
171
172
173 try {
174 synchronized (queryLock) {
175 if (querySessionId != null) {
176 xmldb.disconnectQuery(querySessionId);
177 logger.debug("Query session disconnected");
178 querySessionId = null;
179 }
180 }
181 } catch (DatabaseException e) {
182 querySessionId = null;
183 }
184
185 }
186
187 }