| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.jmx; |
| 7 |
|
| 8 |
import java.lang.management.ManagementFactory; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.Hashtable; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.Map.Entry; |
| 13 |
import javax.management.JMException; |
| 14 |
import javax.management.MBeanServer; |
| 15 |
import javax.management.ObjectName; |
| 16 |
import org.h2.command.Command; |
| 17 |
import org.h2.engine.ConnectionInfo; |
| 18 |
import org.h2.engine.Constants; |
| 19 |
import org.h2.engine.Database; |
| 20 |
import org.h2.engine.SessionLocal; |
| 21 |
import org.h2.table.Table; |
| 22 |
import org.h2.util.NetworkConnectionInfo; |
| 23 |
|
| 24 |
/** |
| 25 |
* The MBean implementation. |
| 26 |
* |
| 27 |
* @author Eric Dong |
| 28 |
* @author Thomas Mueller |
| 29 |
*/ |
| 30 |
public class DatabaseInfo implements DatabaseInfoMBean { |
| 31 |
|
| 32 |
private static final Map<String, ObjectName> MBEANS = new HashMap<>(); |
| 33 |
|
| 34 |
/** Database. */ |
| 35 |
private final Database database; |
| 36 |
|
| 37 |
private DatabaseInfo(Database database) { |
| 38 |
if (database == null) { |
| 39 |
throw new IllegalArgumentException("Argument 'database' must not be null"); |
| 40 |
} |
| 41 |
this.database = database; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns a JMX new ObjectName instance. |
| 46 |
* |
| 47 |
* @param name name of the MBean |
| 48 |
* @param path the path |
| 49 |
* @return a new ObjectName instance |
| 50 |
* @throws JMException if the ObjectName could not be created |
| 51 |
*/ |
| 52 |
private static ObjectName getObjectName(String name, String path) |
| 53 |
throws JMException { |
| 54 |
name = name.replace(':', '_'); |
| 55 |
path = path.replace(':', '_'); |
| 56 |
Hashtable<String, String> map = new Hashtable<>(); |
| 57 |
map.put("name", name); |
| 58 |
map.put("path", path); |
| 59 |
return new ObjectName("org.h2", map); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers an MBean for the database. |
| 64 |
* |
| 65 |
* @param connectionInfo connection info |
| 66 |
* @param database database |
| 67 |
* @throws JMException on failure |
| 68 |
*/ |
| 69 |
public static void registerMBean(ConnectionInfo connectionInfo, |
| 70 |
Database database) throws JMException { |
| 71 |
String path = connectionInfo.getName(); |
| 72 |
if (!MBEANS.containsKey(path)) { |
| 73 |
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); |
| 74 |
String name = database.getShortName(); |
| 75 |
ObjectName mbeanObjectName = getObjectName(name, path); |
| 76 |
MBEANS.put(path, mbeanObjectName); |
| 77 |
DatabaseInfo info = new DatabaseInfo(database); |
| 78 |
Object mbean = new DocumentedMBean(info, DatabaseInfoMBean.class); |
| 79 |
mbeanServer.registerMBean(mbean, mbeanObjectName); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Unregisters the MBean for the database if one is registered. |
| 85 |
* |
| 86 |
* @param name database name |
| 87 |
* @throws JMException on failure |
| 88 |
*/ |
| 89 |
public static void unregisterMBean(String name) throws Exception { |
| 90 |
ObjectName mbeanObjectName = MBEANS.remove(name); |
| 91 |
if (mbeanObjectName != null) { |
| 92 |
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); |
| 93 |
mbeanServer.unregisterMBean(mbeanObjectName); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public boolean isExclusive() { |
| 99 |
return database.getExclusiveSession() != null; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public boolean isReadOnly() { |
| 104 |
return database.isReadOnly(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public String getMode() { |
| 109 |
return database.getMode().getName(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public int getTraceLevel() { |
| 114 |
return database.getTraceSystem().getLevelFile(); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void setTraceLevel(int level) { |
| 119 |
database.getTraceSystem().setLevelFile(level); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public long getFileWriteCount() { |
| 124 |
if (database.isPersistent()) { |
| 125 |
return database.getStore().getMvStore().getFileStore().getWriteCount(); |
| 126 |
} |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public long getFileReadCount() { |
| 132 |
if (database.isPersistent()) { |
| 133 |
return database.getStore().getMvStore().getFileStore().getReadCount(); |
| 134 |
} |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public long getFileSize() { |
| 140 |
long size = 0; |
| 141 |
if (database.isPersistent()) { |
| 142 |
size = database.getStore().getMvStore().getFileStore().size(); |
| 143 |
} |
| 144 |
return size / 1024; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public int getCacheSizeMax() { |
| 149 |
if (database.isPersistent()) { |
| 150 |
return database.getStore().getMvStore().getCacheSize() * 1024; |
| 151 |
} |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void setCacheSizeMax(int kb) { |
| 157 |
if (database.isPersistent()) { |
| 158 |
database.setCacheSize(kb); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public int getCacheSize() { |
| 164 |
if (database.isPersistent()) { |
| 165 |
return database.getStore().getMvStore().getCacheSizeUsed() * 1024; |
| 166 |
} |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public String getVersion() { |
| 172 |
return Constants.FULL_VERSION; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public String listSettings() { |
| 177 |
StringBuilder builder = new StringBuilder(); |
| 178 |
for (Entry<String, String> e : database.getSettings().getSortedSettings()) { |
| 179 |
builder.append(e.getKey()).append(" = ").append(e.getValue()).append('\n'); |
| 180 |
} |
| 181 |
return builder.toString(); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public String listSessions() { |
| 186 |
StringBuilder buff = new StringBuilder(); |
| 187 |
for (SessionLocal session : database.getSessions(false)) { |
| 188 |
buff.append("session id: ").append(session.getId()); |
| 189 |
buff.append(" user: "). |
| 190 |
append(session.getUser().getName()). |
| 191 |
append('\n'); |
| 192 |
NetworkConnectionInfo networkConnectionInfo = session.getNetworkConnectionInfo(); |
| 193 |
if (networkConnectionInfo != null) { |
| 194 |
buff.append("server: ").append(networkConnectionInfo.getServer()).append('\n') // |
| 195 |
.append("clientAddr: ").append(networkConnectionInfo.getClient()).append('\n'); |
| 196 |
String clientInfo = networkConnectionInfo.getClientInfo(); |
| 197 |
if (clientInfo != null) { |
| 198 |
buff.append("clientInfo: ").append(clientInfo).append('\n'); |
| 199 |
} |
| 200 |
} |
| 201 |
buff.append("connected: "). |
| 202 |
append(session.getSessionStart().getString()). |
| 203 |
append('\n'); |
| 204 |
Command command = session.getCurrentCommand(); |
| 205 |
if (command != null) { |
| 206 |
buff.append("statement: ") |
| 207 |
.append(command) |
| 208 |
.append('\n') |
| 209 |
.append("started: ") |
| 210 |
.append(session.getCommandStartOrEnd().getString()) |
| 211 |
.append('\n'); |
| 212 |
} |
| 213 |
for (Table table : session.getLocks()) { |
| 214 |
if (table.isLockedExclusivelyBy(session)) { |
| 215 |
buff.append("write lock on "); |
| 216 |
} else { |
| 217 |
buff.append("read lock on "); |
| 218 |
} |
| 219 |
buff.append(table.getSchema().getName()). |
| 220 |
append('.').append(table.getName()). |
| 221 |
append('\n'); |
| 222 |
} |
| 223 |
buff.append('\n'); |
| 224 |
} |
| 225 |
return buff.toString(); |
| 226 |
} |
| 227 |
|
| 228 |
} |