summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/utils/JMXTestUtils.java
blob: 81f7f079368ff92ac703588333bded8d6d893817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.test.utils;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.qpid.commands.objects.AllObjects;
import org.apache.qpid.management.common.JMXConnnectionFactory;
import org.apache.qpid.management.common.mbeans.ManagedBroker;
import org.apache.qpid.management.common.mbeans.ManagedConnection;
import org.apache.qpid.management.common.mbeans.ManagedExchange;
import org.apache.qpid.management.common.mbeans.ServerInformation;

import javax.management.JMException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

/**
 * 
 */
public class JMXTestUtils
{
    QpidTestCase _test;
    MBeanServerConnection _mbsc;
    JMXConnector _jmxc;

    private String USER;
    private String PASSWORD;

    public JMXTestUtils(QpidTestCase test, String user, String password)
    {
        _test = test;
        USER = user;
        PASSWORD = password;
    }

    public void setUp() throws IOException, ConfigurationException, Exception
    {
        _test.setConfigurationProperty("management.enabled", "true");       
        _test.setSystemProperty("qpid.management.disableCustomSocketFactory", "true");
    }

    public void open() throws Exception
    {
        _jmxc = JMXConnnectionFactory.getJMXConnection(
                5000, "127.0.0.1",
                _test.getManagementPort(_test.getPort()), USER, PASSWORD);

        _mbsc = _jmxc.getMBeanServerConnection();
    }

    public void close() throws IOException
    {
        _jmxc.close();
    }

    /**
     * Create a non-durable test exchange with the current test name
     *
     * @throws javax.management.JMException - is thrown if a exchange with this testName already exists
     * @throws java.io.IOException          - if there is a problem with the JMX Connection
     * @throws javax.management.MBeanException
     *                                      - if there is another problem creating the exchange
     */
    public void createExchange(String virtualHostName, String name, String type, boolean durable)
            throws JMException, IOException, MBeanException
    {
        ManagedBroker managedBroker = getManagedBroker(virtualHostName);

        managedBroker.createNewExchange(name, type, durable);
    }

    /**
     * Create a non-durable queue (with no owner) that is named after the
     * creating test.
     *
     * @throws JMException - is thrown if a queue with this testName already exists
     * @throws IOException - if there is a problem with the JMX Connection
     */
    public void createQueue(String virtualHostName, String name, String owner, boolean durable)
            throws JMException, IOException
    {
        ManagedBroker managedBroker = getManagedBroker(virtualHostName);

        managedBroker.createNewQueue(name, owner, durable);
    }

    /**
     * Retrive the ObjectName for the test Virtualhost.
     *
     * This is then use to create aproxy to the ManagedBroker MBean.
     *
     * @return the ObjectName for the 'test' VirtualHost.
     */
    public ObjectName getVirtualHostManagerObjectName(String vhostName)
    {
        // Get the name of the test manager
        AllObjects allObject = new AllObjects(_mbsc);
        allObject.querystring = "org.apache.qpid:type=VirtualHost.VirtualHostManager,VirtualHost=" + vhostName + ",*";

        Set<ObjectName> objectNames = allObject.returnObjects();

        _test.assertNotNull("Null ObjectName Set returned", objectNames);
        _test.assertEquals("Incorrect number test vhosts returned", 1, objectNames.size());

        // We have verified we have only one value in objectNames so return it
        return objectNames.iterator().next();
    }

    /**
     * Retrive the ObjectName for the given Exchange on the test Virtualhost.
     *
     * This is then use to create aproxy to the ManagedExchange MBean.
     *
     * @param queue The exchange to retireve e.g. 'direct'
     *
     * @return the ObjectName for the given exchange on the test VirtualHost.
     */
    public ObjectName getQueueObjectName(String virtualHostName, String queue)
    {
        // Get the name of the test manager
        AllObjects allObject = new AllObjects(_mbsc);
        allObject.querystring = "org.apache.qpid:type=VirtualHost.Queue,VirtualHost=" + virtualHostName + ",name=" + queue + ",*";

        Set<ObjectName> objectNames = allObject.returnObjects();

        _test.assertNotNull("Null ObjectName Set returned", objectNames);
        _test.assertEquals("Incorrect number of queues with name '" + allObject.querystring +
                           "' returned", 1, objectNames.size());

        // We have verified we have only one value in objectNames so return it
        return objectNames.iterator().next();
    }

    /**
     * Retrive the ObjectName for the given Exchange on the test Virtualhost.
     *
     * This is then use to create aproxy to the ManagedExchange MBean.
     *
     * @param virtualHostName
     * @param exchange        The exchange to retireve e.g. 'direct'
     *
     * @return the ObjectName for the given exchange on the test VirtualHost.
     */
    public ObjectName getExchangeObjectName(String virtualHostName, String exchange)
    {
        // Get the name of the test manager
        AllObjects allObject = new AllObjects(_mbsc);
        allObject.querystring = "org.apache.qpid:type=VirtualHost.Exchange,VirtualHost=" + virtualHostName + ",name=" + exchange + ",*";

        Set<ObjectName> objectNames = allObject.returnObjects();

        _test.assertNotNull("Null ObjectName Set returned", objectNames);
        _test.assertEquals("Incorrect number of exchange with name '" + exchange +
                           "' returned", 1, objectNames.size());

        // We have verified we have only one value in objectNames so return it
        return objectNames.iterator().next();
    }

    public <T> T getManagedObject(Class<T> managedClass, String queryString)
    {
        AllObjects allObject = new AllObjects(_mbsc);
        allObject.querystring = queryString;

        Set<ObjectName> objectNames = allObject.returnObjects();

        _test.assertNotNull("Null ObjectName Set returned", objectNames);
        _test.assertEquals("More than one " + managedClass + " returned", 1, objectNames.size());

        ObjectName objectName = objectNames.iterator().next();

        return getManagedObject(managedClass, objectName);
    }

    public <T> T getManagedObject(Class<T> managedClass, ObjectName objectName)
    {
        return MBeanServerInvocationHandler.
                newProxyInstance(_mbsc, objectName, managedClass, false);
    }

    public ManagedBroker getManagedBroker(String virtualHost)
    {
        return getManagedObject(ManagedBroker.class, getVirtualHostManagerObjectName(virtualHost).toString());
    }

    public ManagedExchange getManagedExchange(String exchangeName)
    {
        return MBeanServerInvocationHandler.
                newProxyInstance(_mbsc, getExchangeObjectName("test", exchangeName),
                                 ManagedExchange.class, false);
    }

    /**
     * Retrive {@link ServerInformation} JMX MBean.
     */
    public ServerInformation getServerInformation()
    {
        // Get the name of the test manager
        AllObjects allObject = new AllObjects(_mbsc);
        allObject.querystring = "org.apache.qpid:type=ServerInformation,name=ServerInformation,*";

        Set<ObjectName> objectNames = allObject.returnObjects();

        TestCase.assertNotNull("Null ObjectName Set returned", objectNames);
        TestCase.assertEquals("Incorrect number of objects returned", 1, objectNames.size());

        // We have verified we have only one value in objectNames so return it
        return getManagedObject(ServerInformation.class, objectNames.iterator().next());
    }

    /**
     * Retrive all {@link ManagedConnection} objects.
     */
    public List<ManagedConnection> getManagedConnections(String vhost)
    {
        // Get the name of the test manager
        AllObjects allObject = new AllObjects(_mbsc);
        allObject.querystring = "org.apache.qpid:type=VirtualHost.Connection,VirtualHost=" + vhost + ",name=*";

        Set<ObjectName> objectNames = allObject.returnObjects();

        TestCase.assertNotNull("Null ObjectName Set returned", objectNames);

        // Collect all the connection objects
        List<ManagedConnection> connections = new ArrayList<ManagedConnection>();
        for (ObjectName name : objectNames)
        {
            connections.add(getManagedObject(ManagedConnection.class, name));
        }
        return connections; 
    }
}