001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.server.web;
007:
008: import java.sql.Connection;
009: import java.sql.DatabaseMetaData;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013: import java.sql.Timestamp;
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.Locale;
017:
018: import org.h2.bnf.Bnf;
019: import org.h2.message.TraceSystem;
020:
021: /**
022: * The web session keeps all data of a user session.
023: * This class is used by the H2 Console.
024: */
025: public class WebSession {
026: long lastAccess;
027: HashMap map = new HashMap();
028: Locale locale;
029: WebServer server;
030:
031: private static final int MAX_HISTORY = 1000;
032: private ArrayList commandHistory = new ArrayList();
033:
034: private Connection conn;
035: private DatabaseMetaData meta;
036: private DbContents contents = new DbContents();
037: private DbContextRule columnRule;
038: private DbContextRule newAliasRule;
039: private DbContextRule tableRule;
040: private DbContextRule aliasRule;
041: private DbContextRule columnAliasRule;
042: private Bnf bnf;
043: Statement executingStatement;
044: ResultSet result;
045:
046: WebSession(WebServer server) {
047: this .server = server;
048: }
049:
050: public void put(String key, Object value) {
051: map.put(key, value);
052: }
053:
054: public Object get(String key) {
055: if ("sessions".equals(key)) {
056: return server.getSessions();
057: }
058: return map.get(key);
059: }
060:
061: public void remove(String key) {
062: map.remove(key);
063: }
064:
065: public Bnf getBnf() {
066: return bnf;
067: }
068:
069: void loadBnf() {
070: try {
071: Bnf newBnf = Bnf.getInstance(null);
072: columnRule = new DbContextRule(contents,
073: DbContextRule.COLUMN);
074: newAliasRule = new DbContextRule(contents,
075: DbContextRule.NEW_TABLE_ALIAS);
076: aliasRule = new DbContextRule(contents,
077: DbContextRule.TABLE_ALIAS);
078: tableRule = new DbContextRule(contents, DbContextRule.TABLE);
079: columnAliasRule = new DbContextRule(contents,
080: DbContextRule.COLUMN_ALIAS);
081: // bnf.updateTopic("newTableName", new String[]{"TEST"});
082: // String[] schemas;
083: // if(contents.isMySQL) {
084: // schemas = new String[0];
085: // } else {
086: // schemas = new String[contents.schemas.length];
087: // for(int i=0; i<contents.schemas.length; i++) {
088: // schemas[i] = contents.schemas[i].quotedName + ".";
089: // }
090: // }
091: // bnf.updateTopic("schemaName", schemas);
092: newBnf.updateTopic("columnName", columnRule);
093: newBnf.updateTopic("newTableAlias", newAliasRule);
094: newBnf.updateTopic("tableAlias", aliasRule);
095: newBnf.updateTopic("columnAlias", columnAliasRule);
096: newBnf.updateTopic("tableName", tableRule);
097: // bnf.updateTopic("name", new String[]{""});
098: newBnf.linkStatements();
099: bnf = newBnf;
100: } catch (Exception e) {
101: // ok we don't have the bnf
102: e.printStackTrace();
103: }
104: }
105:
106: String getCommand(int id) {
107: return (String) commandHistory.get(id);
108: }
109:
110: void addCommand(String sql) {
111: if (sql == null) {
112: return;
113: }
114: sql = sql.trim();
115: if (sql.length() == 0) {
116: return;
117: }
118: if (commandHistory.size() > MAX_HISTORY) {
119: commandHistory.remove(0);
120: }
121: int idx = commandHistory.indexOf(sql);
122: if (idx >= 0) {
123: commandHistory.remove(idx);
124: }
125: commandHistory.add(sql);
126: }
127:
128: ArrayList getCommands() {
129: return commandHistory;
130: }
131:
132: public HashMap getInfo() {
133: HashMap m = new HashMap();
134: m.putAll(map);
135: m.put("lastAccess", new Timestamp(lastAccess).toString());
136: try {
137: m.put("url", conn == null ? "not connected" : conn
138: .getMetaData().getURL());
139: m.put("user", conn == null ? "-" : conn.getMetaData()
140: .getUserName());
141: m.put("lastQuery", commandHistory.size() == 0 ? ""
142: : commandHistory.get(0));
143: m.put("executing", executingStatement == null ? "no"
144: : "yes");
145: } catch (SQLException e) {
146: TraceSystem.traceThrowable(e);
147: }
148: return m;
149: }
150:
151: void setConnection(Connection conn) throws SQLException {
152: this .conn = conn;
153: if (conn == null) {
154: meta = null;
155: } else {
156: meta = conn.getMetaData();
157: }
158: contents = new DbContents();
159: }
160:
161: DatabaseMetaData getMetaData() {
162: return meta;
163: }
164:
165: Connection getConnection() {
166: return conn;
167: }
168:
169: public DbContents getContents() {
170: return contents;
171: }
172:
173: }
|