File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jmx\DatabaseInfo.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.jmx;
8
9 import java.lang.management.ManagementFactory;
10
11 import java.sql.Timestamp;
12 import java.util.Hashtable;
13 import java.util.Map;
14 import java.util.TreeMap;
15 import javax.management.JMException;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18 import org.h2.command.Command;
19 import org.h2.engine.ConnectionInfo;
20 import org.h2.engine.Constants;
21 import org.h2.engine.Database;
22 import org.h2.engine.Session;
23 import org.h2.table.Table;
24 import org.h2.util.New;
25
26 /**
27 * The MBean implementation.
28 *
29 * @author Eric Dong
30 * @author Thomas Mueller
31 */
32 public class DatabaseInfo implements DatabaseInfoMBean {
33
34 private static final Map<String, ObjectName> MBEANS = New.hashMap();
35
36 /** Database. */
37 private final Database database;
38
39 private DatabaseInfo(Database database) {
40 if (database == null) {
41 throw new IllegalArgumentException("Argument 'database' must not be null");
42 }
43 this.database = database;
44 }
45
46 /**
47 * Returns a JMX new ObjectName instance.
48 *
49 * @param name name of the MBean
50 * @param path the path
51 * @return a new ObjectName instance
52 * @throws JMException if the ObjectName could not be created
53 */
54 private static ObjectName getObjectName(String name, String path) throws JMException {
55 name = name.replace(':', '_');
56 path = path.replace(':', '_');
57 Hashtable<String, String> map = new Hashtable<String, String>();
58 map.put("name", name);
59 map.put("path", path);
60 return new ObjectName("org.h2", map);
61 }
62
63 /**
64 * Registers an MBean for the database.
65 *
66 * @param connectionInfo connection info
67 * @param database database
68 */
69 public static void registerMBean(ConnectionInfo connectionInfo, Database database) throws JMException {
70 String path = connectionInfo.getName();
71 if (!MBEANS.containsKey(path)) {
72 MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
73 String name = database.getShortName();
74 ObjectName mbeanObjectName = getObjectName(name, path);
75 MBEANS.put(path, mbeanObjectName);
76 DatabaseInfo info = new DatabaseInfo(database);
77 Object mbean = new DocumentedMBean(info, DatabaseInfoMBean.class);
78 mbeanServer.registerMBean(mbean, mbeanObjectName);
79 }
80 }
81
82 /**
83 * Unregisters the MBean for the database if one is registered.
84 *
85 * @param name database name
86 */
87 public static void unregisterMBean(String name) throws Exception {
88 ObjectName mbeanObjectName = MBEANS.remove(name);
89 if (mbeanObjectName != null) {
90 MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
91 mbeanServer.unregisterMBean(mbeanObjectName);
92 }
93 }
94
95 public boolean isExclusive() {
96 return database.getExclusiveSession() != null;
97 }
98
99 public boolean isReadOnly() {
100 return database.isReadOnly();
101 }
102
103 public String getMode() {
104 return database.getMode().getName();
105 }
106
107 public boolean isMultiThreaded() {
108 return database.isMultiThreaded();
109 }
110
111 public boolean isMvcc() {
112 return database.isMultiVersion();
113 }
114
115 public int getLogMode() {
116 return database.getLogMode();
117 }
118
119 public void setLogMode(int value) {
120 database.setLogMode(value);
121 }
122
123 public int getTraceLevel() {
124 return database.getTraceSystem().getLevelFile();
125 }
126
127 public void setTraceLevel(int level) {
128 database.getTraceSystem().setLevelFile(level);
129 }
130
131 public long getFileWriteCountTotal() {
132 return database.isPersistent() ? database.getPageStore().getWriteCountTotal() : 0L;
133 }
134
135 public long getFileWriteCount() {
136 return database.isPersistent() ? database.getPageStore().getWriteCount() : 0L;
137 }
138
139 public long getFileReadCount() {
140 return database.isPersistent() ? database.getPageStore().getReadCount() : 0L;
141 }
142
143 public long getFileSize() {
144 return database.isPersistent() ?
145 (database.getPageStore().getPageCount() * database.getPageStore().getPageSize() / 1024) : 0;
146 }
147
148 public int getCacheSizeMax() {
149 return database.isPersistent() ? database.getPageStore().getCache().getMaxMemory() : 0;
150 }
151
152 public void setCacheSizeMax(int kb) {
153 if (database.isPersistent()) {
154 database.getPageStore().getCache().setMaxMemory(kb);
155 }
156 }
157
158 public int getCacheSize() {
159 return database.isPersistent() ? database.getPageStore().getCache().getMemory() : 0;
160 }
161
162 public String getVersion() {
163 return Constants.getFullVersion();
164 }
165
166 public String listSettings() {
167 StringBuilder buff = new StringBuilder();
168 for (Map.Entry<String, String> e : new TreeMap<String, String>(database.getSettings().getSettings()).entrySet()) {
169 buff.append(e.getKey()).append(" = ").append(e.getValue()).append('\n');
170 }
171 return buff.toString();
172 }
173
174 public String listSessions() {
175 StringBuilder buff = new StringBuilder();
176 for (Session session : database.getSessions(false)) {
177 buff.append("session id: ").append(session.getId());
178 buff.append(" user: ").append(session.getUser().getName()).append('\n');
179 buff.append("connected: ").append(new Timestamp(session.getSessionStart())).append('\n');
180 Command command = session.getCurrentCommand();
181 if (command != null) {
182 buff.append("statement: ").append(session.getCurrentCommand()).append('\n');
183 long commandStart = session.getCurrentCommandStart();
184 if (commandStart != 0) {
185 buff.append("started: ").append(new Timestamp(commandStart)).append('\n');
186 }
187 }
188 Table[] t = session.getLocks();
189 if (t.length > 0) {
190 for (Table table : session.getLocks()) {
191 if (table.isLockedExclusivelyBy(session)) {
192 buff.append("write lock on ");
193 } else {
194 buff.append("read lock on ");
195 }
196 buff.append(table.getSchema().getName()).append('.').append(table.getName()).append('\n');
197 }
198 }
199 buff.append('\n');
200 }
201 return buff.toString();
202 }
203
204 }
205