| 1 | /* |
| 2 | Copyright (C) 2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License version 2 as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | |
| 17 | */ |
| 18 | package com.mysql.management.jmx; |
| 19 | |
| 20 | import java.sql.SQLException; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.Properties; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import javax.management.AttributeNotFoundException; |
| 26 | import javax.management.InstanceNotFoundException; |
| 27 | import javax.management.MBeanException; |
| 28 | import javax.management.MBeanServer; |
| 29 | import javax.management.MBeanServerFactory; |
| 30 | import javax.management.ObjectInstance; |
| 31 | import javax.management.ObjectName; |
| 32 | import javax.management.ReflectionException; |
| 33 | |
| 34 | import com.mysql.jdbc.ConnectionPropertiesTransform; |
| 35 | import com.mysql.jdbc.NonRegisteringDriver; |
| 36 | import com.mysql.management.MysqldResourceI; |
| 37 | import com.mysql.management.jmx.jboss.JBossMysqldDynamicMBean; |
| 38 | import com.mysql.management.util.Exceptions; |
| 39 | |
| 40 | /** |
| 41 | * This class is final simply as a hint to the compiler, it may be un-finalized |
| 42 | * safely. |
| 43 | * |
| 44 | * @author Eric Herman <eric@mysql.com> |
| 45 | * @version $Id: ConnectorMXJPropertiesTransform.java,v 1.1 2005/02/16 21:46:11 |
| 46 | * eherman Exp $ |
| 47 | */ |
| 48 | public final class ConnectorMXJPropertiesTransform implements |
| 49 | ConnectionPropertiesTransform { |
| 50 | |
| 51 | private static Class[] mbeanClasses = new Class[] { |
| 52 | MysqldDynamicMBean.class, SimpleMysqldDynamicMBean.class, |
| 53 | JBossMysqldDynamicMBean.class }; |
| 54 | |
| 55 | private MBeanServer mbeanServer; |
| 56 | |
| 57 | public ConnectorMXJPropertiesTransform(MBeanServer mbeanServer) { |
| 58 | this.mbeanServer = mbeanServer; |
| 59 | } |
| 60 | |
| 61 | public ConnectorMXJPropertiesTransform() { |
| 62 | this((MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0)); |
| 63 | } |
| 64 | |
| 65 | MBeanServer getMBeanServer() { |
| 66 | return mbeanServer; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * replaces the host and port and parameters with values for the MBean |
| 71 | */ |
| 72 | public Properties transformProperties(Properties props) throws SQLException { |
| 73 | String host = getHost(); |
| 74 | String port = getPort(); |
| 75 | if (!port.equals("3306")) { |
| 76 | host = host + ":" + port; |
| 77 | } |
| 78 | props.put(NonRegisteringDriver.HOST_PROPERTY_KEY, host); |
| 79 | props.put(NonRegisteringDriver.PORT_PROPERTY_KEY, port); |
| 80 | return props; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return "localhost" |
| 85 | */ |
| 86 | String getHost() { |
| 87 | return "localhost"; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return the port of the MBean managed MySQL server |
| 92 | * @throws SQLException |
| 93 | */ |
| 94 | String getPort() throws SQLException { |
| 95 | Exceptions.SQLBlock block = new Exceptions.SQLBlock(System.err) { |
| 96 | public Object inner() throws Exception { |
| 97 | return getPortInner(); |
| 98 | } |
| 99 | }; |
| 100 | return (String) block.exec(); |
| 101 | } |
| 102 | |
| 103 | private Object getPortInner() throws InstanceNotFoundException, |
| 104 | MBeanException, AttributeNotFoundException, ReflectionException { |
| 105 | |
| 106 | ObjectName objName = getMysqldObjectName(); |
| 107 | String port = ((String) getMBeanServer().getAttribute(objName, |
| 108 | MysqldResourceI.PORT)); |
| 109 | return port; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return the MysqldDynamicMBean ObjectName |
| 114 | * @throws InstanceNotFoundException |
| 115 | */ |
| 116 | ObjectName getMysqldObjectName() throws InstanceNotFoundException { |
| 117 | Set objectNames = getMBeanServer().queryNames(null, null); |
| 118 | StringBuffer error = errorMsgHeader(); |
| 119 | for (Iterator iter = objectNames.iterator(); iter.hasNext();) { |
| 120 | ObjectName objectName = (ObjectName) iter.next(); |
| 121 | ObjectInstance objInst = getMBeanServer().getObjectInstance( |
| 122 | objectName); |
| 123 | String className = objInst.getClassName(); |
| 124 | if (classNameMatch(className)) { |
| 125 | return objectName; |
| 126 | } |
| 127 | appendItem(error, objectName, className); |
| 128 | } |
| 129 | |
| 130 | throw new IllegalStateException(error.toString()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param error |
| 135 | * @param objectName |
| 136 | * @param className |
| 137 | */ |
| 138 | private void appendItem(StringBuffer error, ObjectName objectName, |
| 139 | String className) { |
| 140 | error.append("["); |
| 141 | error.append(className); |
| 142 | error.append("("); |
| 143 | error.append(objectName.getCanonicalName()); |
| 144 | error.append(")]"); |
| 145 | } |
| 146 | |
| 147 | private StringBuffer errorMsgHeader() { |
| 148 | StringBuffer error = new StringBuffer(); |
| 149 | |
| 150 | error.append("MySQL MBean ("); |
| 151 | |
| 152 | for (int i = 0; i < mbeanClasses.length; i++) { |
| 153 | error.append(mbeanClasses[i].getName()); |
| 154 | if (i < (mbeanClasses.length - 1)) { |
| 155 | error.append(", "); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | error.append(") Not Found in: "); |
| 160 | return error; |
| 161 | } |
| 162 | |
| 163 | boolean classNameMatch(String className) { |
| 164 | for (int i = 0; i < mbeanClasses.length; i++) { |
| 165 | if (mbeanClasses[i].getName().equals(className)) { |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | } |