File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\server\web\WebSession.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.server.web;
8
9 import java.sql.Connection;
10 import java.sql.DatabaseMetaData;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import java.sql.Timestamp;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Locale;
18
19 import org.h2.bnf.Bnf;
20 import org.h2.message.TraceSystem;
21 import org.h2.util.New;
22
23 /**
24 * The web session keeps all data of a user session.
25 * This class is used by the H2 Console.
26 */
27 class WebSession {
28
29 private static final int MAX_HISTORY = 1000;
30
31 /**
32 * The last time this client sent a request.
33 */
34 long lastAccess;
35
36 /**
37 * The session attribute map.
38 */
39 final HashMap<String, Object> map = New.hashMap();
40
41 /**
42 * The current locale.
43 */
44 Locale locale;
45
46 /**
47 * The currently executing statement.
48 */
49 Statement executingStatement;
50
51 /**
52 * The current updatable result set.
53 */
54 ResultSet result;
55
56 private final WebServer server;
57
58 private final ArrayList<String> commandHistory = New.arrayList();
59
60 private Connection conn;
61 private DatabaseMetaData meta;
62 private DbContents contents = new DbContents();
63 private Bnf bnf;
64 private boolean shutdownServerOnDisconnect;
65
66 WebSession(WebServer server) {
67 this.server = server;
68 }
69
70 /**
71 * Put an attribute value in the map.
72 *
73 * @param key the key
74 * @param value the new value
75 */
76 void put(String key, Object value) {
77 map.put(key, value);
78 }
79
80 /**
81 * Get the value for the given key.
82 *
83 * @param key the key
84 * @return the value
85 */
86 Object get(String key) {
87 if ("sessions".equals(key)) {
88 return server.getSessions();
89 }
90 return map.get(key);
91 }
92
93 /**
94 * Remove a session attribute from the map.
95 *
96 * @param key the key
97 */
98 void remove(String key) {
99 map.remove(key);
100 }
101
102 /**
103 * Get the BNF object.
104 *
105 * @return the BNF object
106 */
107 Bnf getBnf() {
108 return bnf;
109 }
110
111 /**
112 * Load the SQL grammar BNF.
113 */
114 void loadBnf() {
115 try {
116 Bnf newBnf = Bnf.getInstance(null);
117 DbContextRule columnRule = new DbContextRule(contents, DbContextRule.COLUMN);
118 DbContextRule newAliasRule = new DbContextRule(contents, DbContextRule.NEW_TABLE_ALIAS);
119 DbContextRule aliasRule = new DbContextRule(contents, DbContextRule.TABLE_ALIAS);
120 DbContextRule tableRule = new DbContextRule(contents, DbContextRule.TABLE);
121 DbContextRule schemaRule = new DbContextRule(contents, DbContextRule.SCHEMA);
122 DbContextRule columnAliasRule = new DbContextRule(contents, DbContextRule.COLUMN_ALIAS);
123 newBnf.updateTopic("column_name", columnRule);
124 newBnf.updateTopic("new_table_alias", newAliasRule);
125 newBnf.updateTopic("table_alias", aliasRule);
126 newBnf.updateTopic("column_alias", columnAliasRule);
127 newBnf.updateTopic("table_name", tableRule);
128 newBnf.updateTopic("schema_name", schemaRule);
129 newBnf.linkStatements();
130 bnf = newBnf;
131 } catch (Exception e) {
132 // ok we don't have the bnf
133 server.traceError(e);
134 }
135 }
136
137 /**
138 * Get the SQL statement from history.
139 *
140 * @param id the history id
141 * @return the SQL statement
142 */
143 String getCommand(int id) {
144 return commandHistory.get(id);
145 }
146
147 /**
148 * Add a SQL statement to the history.
149 *
150 * @param sql the SQL statement
151 */
152 void addCommand(String sql) {
153 if (sql == null) {
154 return;
155 }
156 sql = sql.trim();
157 if (sql.length() == 0) {
158 return;
159 }
160 if (commandHistory.size() > MAX_HISTORY) {
161 commandHistory.remove(0);
162 }
163 int idx = commandHistory.indexOf(sql);
164 if (idx >= 0) {
165 commandHistory.remove(idx);
166 }
167 commandHistory.add(sql);
168 }
169
170 /**
171 * Get the list of SQL statements in the history.
172 *
173 * @return the commands
174 */
175 ArrayList<String> getCommands() {
176 return commandHistory;
177 }
178
179 /**
180 * Update session meta data information and get the information in a map.
181 *
182 * @return a map containing the session meta data
183 */
184 HashMap<String, Object> getInfo() {
185 HashMap<String, Object> m = New.hashMap();
186 m.putAll(map);
187 m.put("lastAccess", new Timestamp(lastAccess).toString());
188 try {
189 m.put("url", conn == null ? "${text.admin.notConnected}" : conn.getMetaData().getURL());
190 m.put("user", conn == null ? "-" : conn.getMetaData().getUserName());
191 m.put("lastQuery", commandHistory.size() == 0 ? "" : commandHistory.get(0));
192 m.put("executing", executingStatement == null ? "${text.admin.no}" : "${text.admin.yes}");
193 } catch (SQLException e) {
194 TraceSystem.traceThrowable(e);
195 }
196 return m;
197 }
198
199 void setConnection(Connection conn) throws SQLException {
200 this.conn = conn;
201 if (conn == null) {
202 meta = null;
203 } else {
204 meta = conn.getMetaData();
205 }
206 contents = new DbContents();
207 }
208
209 DatabaseMetaData getMetaData() {
210 return meta;
211 }
212
213 Connection getConnection() {
214 return conn;
215 }
216
217 DbContents getContents() {
218 return contents;
219 }
220
221 /**
222 * Shutdown the server when disconnecting.
223 */
224 void setShutdownServerOnDisconnect() {
225 this.shutdownServerOnDisconnect = true;
226 }
227
228 boolean getShutdownServerOnDisconnect() {
229 return shutdownServerOnDisconnect;
230 }
231
232 /**
233 * Close the connection and stop the statement if one is currently
234 * executing.
235 */
236 void close() {
237 if (executingStatement != null) {
238 try {
239 executingStatement.cancel();
240 } catch (Exception e) {
241 // ignore
242 }
243 }
244 if (conn != null) {
245 try {
246 conn.close();
247 } catch (Exception e) {
248 // ignore
249 }
250 }
251
252 }
253
254 }
255