Coverage Report - net.admin4j.jdbc.driver.Admin4jJdbcJmxMBean
 
Classes in this File Line Coverage Branch Coverage Complexity
Admin4jJdbcJmxMBean
76%
19/25
70%
7/10
1.346
Admin4jJdbcJmxMBean$ConnectionInfo
19%
4/21
N/A
1.346
Admin4jJdbcJmxMBean$StatementInfo
44%
8/18
0%
0/4
1.346
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.jdbc.driver;
 15  
 
 16  
 import java.lang.management.ManagementFactory;
 17  
 import java.sql.DriverManager;
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.management.MBeanServer;
 22  
 import javax.management.NotCompliantMBeanException;
 23  
 import javax.management.ObjectName;
 24  
 import javax.management.StandardMBean;
 25  
 
 26  
 import net.admin4j.deps.commons.lang3.SystemUtils;
 27  
 import net.admin4j.jdbc.driver.sql.ConnectionWrapper30Base;
 28  
 import net.admin4j.util.annotate.PackageRestrictions;
 29  
 
 30  
 /**
 31  
  * Exposes Driver info via the JMX Console
 32  
  * @author D. Ashmore
 33  
  * @since 1.0
 34  
  */
 35  
 @PackageRestrictions({"net.admin4j","java","javax"})
 36  
 public class Admin4jJdbcJmxMBean extends StandardMBean implements Admin4jJdbcJmx {
 37  
         
 38  
         private ConnectionInfo[] connections;
 39  
         
 40  
         public Admin4jJdbcJmxMBean() throws NotCompliantMBeanException {
 41  24
                 super(Admin4jJdbcJmx.class);
 42  24
                 refreshConnectionList();
 43  24
         }
 44  
 
 45  
         private void refreshConnectionList() {
 46  24
                 List<ConnectionInfo> list = new ArrayList<ConnectionInfo>();
 47  
                 ConnectionInfo cInfo;
 48  24
                 for (ConnectionWrapper30Base conn: ConnectionRegistry.getCurrentConnectionSet()) {
 49  2
                         cInfo = new ConnectionInfo();
 50  2
                         cInfo.setSqlStatementHistory(conn.getSqlStatementHistory().toArray(new StatementInfo[0]));
 51  
 //                        cInfo.setDatabaseName(conn.getDatabaseName());
 52  
 //                        cInfo.setInstanceName(conn.getInstanceName());
 53  
 //                        cInfo.setUserName(conn.getUser());
 54  
 //                        cInfo.setServerName(conn.getServerName());
 55  2
                         if (cInfo.getSqlStatementHistory() != null && cInfo.getSqlStatementHistory().length > 0) {
 56  0
                                 cInfo.setLastSqlStatement(cInfo.getSqlStatementHistory()[0]);
 57  
                         }
 58  2
                         list.add(cInfo);
 59  
                 }
 60  
                 
 61  24
                 connections = list.toArray(new ConnectionInfo[0]);
 62  24
         }
 63  
         
 64  
         public static void registerMBean()  {
 65  
                 try {
 66  24
                         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 67  24
                         ObjectName name = new ObjectName("Jdbc:type=Admin4j");
 68  24
                         Object mbean = new Admin4jJdbcJmxMBean();
 69  24
                         if (!mbs.isRegistered(name) ) {
 70  6
                                 mbs.registerMBean(mbean, name);
 71  
                         }
 72  
                         else {
 73  18
                             if (DriverManager.getLogWriter() != null) {
 74  0
                                 DriverManager.getLogWriter().println("Admin4jJdbcJmxMBean already registered.");
 75  
                             }
 76  
                         }
 77  0
                 } catch (Exception e) {
 78  0
                         throw new RuntimeException("Admin4jJdbcJmxMBean registration error", e);
 79  24
                 } 
 80  
 
 81  24
         }
 82  
         
 83  2
         public static class ConnectionInfo {
 84  
                 private StatementInfo lastSqlStatement;
 85  
                 private StatementInfo[] sqlStatementHistory;
 86  
                 private String userName;
 87  
                 private String databaseName;
 88  
                 private String serverName;
 89  
                 private String instanceName;
 90  
                 public StatementInfo getLastSqlStatement() {
 91  0
                         return lastSqlStatement;
 92  
                 }
 93  
                 public void setLastSqlStatement(StatementInfo lastSqlStatement) {
 94  0
                         this.lastSqlStatement = lastSqlStatement;
 95  0
                 }
 96  
                 public StatementInfo[] getSqlStatementHistory() {
 97  4
                         return sqlStatementHistory;
 98  
                 }
 99  
                 public void setSqlStatementHistory(StatementInfo[] sqlStatementHistory) {
 100  2
                         this.sqlStatementHistory = sqlStatementHistory;
 101  2
                 }
 102  
                 
 103  
                 public String getLastSqlStatementStr() {
 104  0
                         return this.lastSqlStatement.toString();
 105  
                 }
 106  
                 
 107  
                 @Override
 108  
                 public String toString() {
 109  0
                         return lastSqlStatement.toString();
 110  
                 }
 111  
                 public String getUserName() {
 112  0
                         return userName;
 113  
                 }
 114  
                 public void setUserName(String userName) {
 115  0
                         this.userName = userName;
 116  0
                 }
 117  
                 public String getDatabaseName() {
 118  0
                         return databaseName;
 119  
                 }
 120  
                 public void setDatabaseName(String databaseName) {
 121  0
                         this.databaseName = databaseName;
 122  0
                 }
 123  
                 public String getServerName() {
 124  0
                         return serverName;
 125  
                 }
 126  
                 public void setServerName(String serverName) {
 127  0
                         this.serverName = serverName;
 128  0
                 }
 129  
                 public String getInstanceName() {
 130  0
                         return instanceName;
 131  
                 }
 132  
                 public void setInstanceName(String instanceName) {
 133  0
                         this.instanceName = instanceName;
 134  0
                 }
 135  
                 
 136  
         }
 137  
         
 138  
         public static class StatementInfo {
 139  
                 public String sqlText;
 140  
                 public StackTraceElement[] executionStack;
 141  
                 
 142  0
                 public StatementInfo() {}
 143  180132
                 public StatementInfo(String sqlText, StackTraceElement[] executionStack) {
 144  180132
                         this.setSqlText(sqlText);
 145  180132
                         this.setExecutionStack(executionStack);
 146  180132
                 }
 147  
                 
 148  
                 public String getSqlText() {
 149  0
                         return sqlText;
 150  
                 }
 151  
                 public void setSqlText(String sqlText) {
 152  180132
                         this.sqlText = sqlText;
 153  180132
                 }
 154  
                 public StackTraceElement[] getExecutionStack() {
 155  0
                         return executionStack;
 156  
                 }
 157  
                 public void setExecutionStack(StackTraceElement[] executionStack) {
 158  180132
                         this.executionStack = executionStack;
 159  180132
                 }
 160  
                 
 161  
                 public String getExecutionStackString() {
 162  0
                         StringBuffer buffer = new StringBuffer();
 163  0
                         for (StackTraceElement element: this.executionStack) {
 164  0
                                 if (buffer.length() > 0) {
 165  0
                                         buffer.append(SystemUtils.LINE_SEPARATOR);
 166  
                                 }
 167  0
                                 buffer.append(element);
 168  
                         }
 169  
                         
 170  0
                         return buffer.toString();
 171  
                 }
 172  
                 
 173  
                 @Override
 174  
                 public String toString() {
 175  0
                         return sqlText;
 176  
                 }
 177  
                 
 178  
         }
 179  
 
 180  
         /* (non-Javadoc)
 181  
          * @see net.sourceforge.jtds.jdbc.JtdsJmx#getConnections()
 182  
          */
 183  
         public ConnectionInfo[] getConnections() {
 184  0
                 refreshConnectionList();
 185  0
                 return connections;
 186  
         }
 187  
 
 188  
 }