001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.monitoring.console.util;
017:
018: import java.sql.Connection;
019: import java.sql.DatabaseMetaData;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023:
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.naming.NamingException;
027: import javax.sql.DataSource;
028:
029: public class DBManager {
030: private static Connection con = null;
031: private static boolean initialized = false;
032:
033: public DBManager() {
034: con = createConnection();
035: if (!initialized)
036: if (this .initializeDB())
037: initialized = true;
038: }
039:
040: public static Connection createConnection() {
041:
042: try {
043: Context context = new InitialContext();
044: DataSource ds = (DataSource) context
045: .lookup("java:comp/env/MonitoringClientDS");
046: con = ds.getConnection();
047: } catch (NamingException e) {
048: e.printStackTrace();
049: } catch (SQLException e) {
050: System.err.println("SQL state: " + e.getSQLState());
051: System.err.println("SQL error: " + e.getErrorCode());
052: e.printStackTrace();
053: }
054: return con;
055: }
056:
057: public Connection getConnection() {
058: return con;
059: }
060:
061: public boolean isInitialized() {
062: return initialized;
063: }
064:
065: private boolean checkTables() {
066: try {
067: DatabaseMetaData metadata = null;
068: metadata = con.getMetaData();
069: String[] names = { "TABLE" };
070: ResultSet tableNames = metadata.getTables(null, null, null,
071: names);
072:
073: if (tableNames.next()) {
074: return true;
075: } else {
076: return false;
077: }
078: } catch (SQLException e) {
079: System.err.println("SQL state: " + e.getSQLState());
080: System.err.println("SQL error: " + e.getErrorCode());
081: e.printStackTrace();
082: }
083: return false;
084: }
085:
086: private boolean initializeDB() {
087: boolean success = false;
088: if (checkTables())
089: return true;
090: try {
091: PreparedStatement pStmt = con
092: .prepareStatement("CREATE TABLE servers("
093: + "server_id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1),"
094: + "enabled SMALLINT DEFAULT 1 NOT NULL,"
095: + "name VARCHAR(128) DEFAULT NULL,"
096: + "ip VARCHAR(128) UNIQUE NOT NULL,"
097: + "port INTEGER NOT NULL,"
098: + "protocol INTEGER DEFAULT 1 NOT NULL,"
099: + "username VARCHAR(128) NOT NULL,"
100: + "password VARCHAR(1024) NOT NULL,"
101: + "added TIMESTAMP NOT NULL,"
102: + "modified TIMESTAMP NOT NULL,"
103: + "last_seen TIMESTAMP NOT NULL" + ")");
104: pStmt.executeUpdate();
105: pStmt = con
106: .prepareStatement("CREATE TABLE graphs("
107: + "graph_id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1),"
108: + "enabled SMALLINT NOT NULL DEFAULT 1,"
109: + "server_id INTEGER NOT NULL DEFAULT 0,"
110: + "name VARCHAR(128) UNIQUE NOT NULL,"
111: + "description LONG VARCHAR DEFAULT NULL,"
112: + "timeframe INTEGER NOT NULL DEFAULT 60,"
113: + "mbean VARCHAR(512) NOT NULL,"
114: + "data1operation CHAR DEFAULT NULL,"
115: + "dataname1 VARCHAR(128) NOT NULL,"
116: + "operation VARCHAR(128) DEFAULT NULL,"
117: + "data2operation CHAR DEFAULT NULL,"
118: + "dataname2 VARCHAR(128) DEFAULT NULL,"
119: + "xlabel VARCHAR(128) DEFAULT NULL,"
120: + "ylabel VARCHAR(128) DEFAULT NULL,"
121: + "warninglevel1 FLOAT DEFAULT NULL,"
122: + "warninglevel2 FLOAT DEFAULT NULL,"
123: + "color VARCHAR(6) NOT NULL DEFAULT '1176c2',"
124: + "last_js LONG VARCHAR DEFAULT NULL,"
125: + "added TIMESTAMP NOT NULL,"
126: + "modified TIMESTAMP NOT NULL,"
127: + "archive SMALLINT NOT NULL DEFAULT 0,"
128: + "last_seen TIMESTAMP NOT NULL" + ")");
129: pStmt.executeUpdate();
130: pStmt = con
131: .prepareStatement("CREATE TABLE views("
132: + "view_id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1),"
133: + "enabled SMALLINT NOT NULL DEFAULT 1,"
134: + "name VARCHAR(128) NOT NULL,"
135: + "description LONG VARCHAR DEFAULT NULL,"
136: + "graph_count INTEGER NOT NULL DEFAULT 0,"
137: + "added TIMESTAMP NOT NULL,"
138: + "modified TIMESTAMP NOT NULL)");
139: pStmt.executeUpdate();
140: pStmt = con.prepareStatement("CREATE TABLE views_graphs("
141: + "view_id INTEGER NOT NULL,"
142: + "graph_id INTEGER NOT NULL)");
143: pStmt.executeUpdate();
144: success = true;
145: } catch (SQLException e) {
146: System.err.println("SQL state: " + e.getSQLState());
147: System.err.println("SQL error: " + e.getErrorCode());
148: e.printStackTrace();
149: }
150: return success;
151: }
152: }
|