EMMA Coverage Report (generated Tue May 16 15:34:38 CDT 2006)
[all classes][com.mysql.management.jmx]

COVERAGE SUMMARY FOR SOURCE FILE [ConnectorMXJPropertiesTransform.java]

nameclass, %method, %block, %line, %
ConnectorMXJPropertiesTransform.java100% (2/2)100% (14/14)99%  (244/247)100% (49.9/50)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ConnectorMXJPropertiesTransform100% (1/1)100% (12/12)99%  (233/236)100% (47.9/48)
<static initializer> 100% (1/1)92%  (34/37)91%  (0.9/1)
ConnectorMXJPropertiesTransform (): void 100% (1/1)100% (8/8)100% (2/2)
ConnectorMXJPropertiesTransform (MBeanServer): void 100% (1/1)100% (6/6)100% (3/3)
appendItem (StringBuffer, ObjectName, String): void 100% (1/1)100% (22/22)100% (6/6)
classNameMatch (String): boolean 100% (1/1)100% (19/19)100% (4/4)
errorMsgHeader (): StringBuffer 100% (1/1)100% (39/39)100% (8/8)
getHost (): String 100% (1/1)100% (2/2)100% (1/1)
getMBeanServer (): MBeanServer 100% (1/1)100% (3/3)100% (1/1)
getMysqldObjectName (): ObjectName 100% (1/1)100% (45/45)100% (10/10)
getPort (): String 100% (1/1)100% (10/10)100% (2/2)
getPortInner (): Object 100% (1/1)100% (12/12)100% (3/3)
transformProperties (Properties): Properties 100% (1/1)100% (33/33)100% (7/7)
     
class ConnectorMXJPropertiesTransform$1100% (1/1)100% (2/2)100% (11/11)100% (2/2)
ConnectorMXJPropertiesTransform$1 (ConnectorMXJPropertiesTransform, PrintStre... 100% (1/1)100% (7/7)100% (1/1)
inner (): Object 100% (1/1)100% (4/4)100% (1/1)

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 */
18package com.mysql.management.jmx;
19 
20import java.sql.SQLException;
21import java.util.Iterator;
22import java.util.Properties;
23import java.util.Set;
24 
25import javax.management.AttributeNotFoundException;
26import javax.management.InstanceNotFoundException;
27import javax.management.MBeanException;
28import javax.management.MBeanServer;
29import javax.management.MBeanServerFactory;
30import javax.management.ObjectInstance;
31import javax.management.ObjectName;
32import javax.management.ReflectionException;
33 
34import com.mysql.jdbc.ConnectionPropertiesTransform;
35import com.mysql.jdbc.NonRegisteringDriver;
36import com.mysql.management.MysqldResourceI;
37import com.mysql.management.jmx.jboss.JBossMysqldDynamicMBean;
38import 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 */
48public 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}

[all classes][com.mysql.management.jmx]
EMMA 2.0.5312 (C) Vladimir Roubtsov