summaryrefslogtreecommitdiff
path: root/management/core/src/org/apache/qpid/management/jmx/CMLMBean.java
blob: 2d1dafb9f08298a27fba8a44b9489f7e77eca1ac (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed 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.management.jmx;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
import org.apache.qpid.management.ManagementConnection;
import org.apache.qpid.management.messaging.CMLMessageFactory;
import org.apache.qpid.schema.cml.CmlDocument;
import org.apache.qpid.schema.cml.FieldType;
import org.apache.qpid.schema.cml.InspectReplyType;
import org.apache.qpid.schema.cml.MethodReplyType;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.management.*;
import javax.management.openmbean.OpenMBeanAttributeInfo;
import javax.management.openmbean.OpenMBeanInfoSupport;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import java.util.Hashtable;

public class CMLMBean implements DynamicMBean
{
    private static final Logger _log = Logger.getLogger(CMLMBean.class);

    /**
     * The number of milliseconds after which data values are considered "stale" and will be
     * refreshed by querying the broker. This is a way of ensure that reading attributes
     * repeatedly does not hit the broker heavily.
     */
    private static final long REFRESH_IN_MILLIS = 2000;

    /**
     * Name of the attribute for the parent MBean
     */
    public static final String PARENT_ATTRIBUTE = "__parent";

    private OpenMBeanInfoSupport _mbeanInfo;

    private AMQMBeanInfo _extraMbeanInfo;

    /**
     * The cached inspect reply. This is used to read attribute values and is refreshed automatically
     * if a request for an attribute is made after the time interval specified in REFRESH_IN_MILLIS
     */
    private InspectReplyType _inspectReply;

    private CMLMBean _parent;

    private ObjectName _objectName;

    private ManagementConnection _connection;

    private int _objectId;

    private long _lastRefreshTime = System.currentTimeMillis();

    public CMLMBean(CMLMBean parent, OpenMBeanInfoSupport mbeanInfo, AMQMBeanInfo extraMbeanInfo,
                    InspectReplyType inspectReply, ManagementConnection connection, int objectId)
    {
        _mbeanInfo = mbeanInfo;
        _extraMbeanInfo = extraMbeanInfo;
        _inspectReply = inspectReply;
        _parent = parent;
        _connection = connection;
        _objectId = objectId;
    }

    /**
     * Utility method that populates all the type infos up to the root. Used when
     * constructing the ObjectName.
     * We end up with properties of the form "className", "objectId" in the map.
     * @param leaf the child node. Must not be null. Note that the child types are not populated since the
     * convention is different for the child where instead of "className" the word "type" is
     * used. See the JMX Best Practices document on the Sun JMX website for details.
     * @param properties
     */
    public static void populateAllTypeInfo(Hashtable<String, String> properties, CMLMBean leaf)
    {
        CMLMBean current = leaf.getParent();
        while (current != null)
        {
            properties.put(current.getType(), Integer.toString(current.getObjectId()));
            current = current.getParent();
        }
    }

    public String getType()
    {
        return _inspectReply.getClass1();
    }

    public int getObjectId()
    {
        return _inspectReply.getObject2();
    }

    public InspectReplyType getInspectReply()
    {
        return _inspectReply;
    }

    public CMLMBean getParent()
    {
        return _parent;
    }

    public ObjectName getObjectName()
    {
        return _objectName;
    }

    public void setObjectName(ObjectName objectName)
    {
        _objectName = objectName;
    }

    public Object getAttribute(String attribute)
            throws AttributeNotFoundException, MBeanException, ReflectionException
    {
        if (PARENT_ATTRIBUTE.equals(attribute))
        {
            if (_parent == null)
            {
                return null;
            }
            else
            {
                return _parent.getObjectName();
            }
        }
        if (needRefresh())
        {            
            refreshValues();
        }
        String nsDecl = "declare namespace cml='http://www.amqp.org/schema/cml';";
        FieldType[] fields = (FieldType[]) _inspectReply.selectPath(nsDecl + "$this/cml:field[@name='" +
                                                                    attribute + "']");
        if (fields == null || fields.length == 0)
        {
            throw new AttributeNotFoundException("Attribute " + attribute + " not found");
        }
        else
        {
            OpenMBeanAttributeInfo attrInfo = _extraMbeanInfo.getAttributeInfo(attribute);
            OpenType openType = attrInfo.getOpenType();
            String value = fields[0].getStringValue();
            try
            {
                return createAttributeValue(openType, value, attrInfo.getName());
            }
            catch (MalformedObjectNameException e)
            {
                throw new MBeanException(e);
            }
        }
    }

    private boolean needRefresh()
    {
        return ((System.currentTimeMillis() - _lastRefreshTime) > REFRESH_IN_MILLIS);
    }

    private void refreshValues() throws MBeanException
    {
        _log.debug("Refreshing values...");
        try
        {
            TextMessage response = _connection.sendRequest(CMLMessageFactory.createInspectRequest(_objectId));

            CmlDocument cmlDoc = CmlDocument.Factory.parse(response.getText());
            _inspectReply = cmlDoc.getCml().getInspectReply();
            _lastRefreshTime = System.currentTimeMillis();
        }
        catch (Exception e)
        {
            throw new MBeanException(e);
        }
    }

    private Object createAttributeValue(OpenType openType, String value, String mbeanType)
            throws MalformedObjectNameException
    {
        if (openType.equals(SimpleType.STRING))
        {
            return value;
        }
        else if (openType.equals(SimpleType.BOOLEAN))
        {
            return Boolean.valueOf(value);
        }
        else if (openType.equals(SimpleType.INTEGER))
        {
            return Integer.valueOf(value);
        }
        else if (openType.equals(SimpleType.DOUBLE))
        {
            return Double.valueOf(value);
        }
        else if (openType.equals(SimpleType.OBJECTNAME))
        {
            Hashtable<String, String> props = new Hashtable<String, String>();
            props.put("objectid", value);
            props.put("type", mbeanType);
            // this populates all type info for parents
            populateAllTypeInfo(props, this);
            // add in type info for this level. This information is available from the inspect reply xml fragment
            props.put(_inspectReply.getClass1(), Integer.toString(_inspectReply.getObject2()));
            return new ObjectName(JmxConstants.JMX_DOMAIN, props);
        }
        else
        {
            _log.warn("Unsupported open type: " + openType + " - returning string value");
            return value;
        }
    }

    public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException,
                                                         MBeanException, ReflectionException
    {

    }

    public AttributeList getAttributes(String[] attributes)
    {
        AttributeList al = new AttributeList(attributes.length);
        for (String name : attributes)
        {
            try
            {
                Object value = getAttribute(name);
                final Attribute attr = new Attribute(name, value);
                al.add(attr);
            }
            catch (Exception e)
            {
                _log.error("Unable to get value for attribute: " + name, e);
            }
        }
        return al;
    }

    public AttributeList setAttributes(AttributeList attributes)
    {
        return null;
    }

    public Object invoke(String actionName, Object params[], String signature[]) throws MBeanException,
                                                                                        ReflectionException
    {
        _log.debug("Invoke called on action " + actionName);
        try
        {
            TextMessage response = _connection.sendRequest(CMLMessageFactory.createMethodRequest(_objectId, actionName));
            CmlDocument cmlDoc = CmlDocument.Factory.parse(response.getText());
            CmlDocument.Cml cml = cmlDoc.getCml();
            MethodReplyType methodReply = cml.getMethodReply();
            if (methodReply.getStatus() != MethodReplyType.Status.OK)
            {
                throw new MBeanException(new Exception("Response code from method: " + methodReply.getStatus()));
            }
            return null;
        }
        catch (AMQException e)
        {
            throw new MBeanException(e);
        }
        catch (JMSException e)
        {
            throw new MBeanException(e);
        }
        catch (org.apache.xmlbeans.XmlException e)
        {
            throw new MBeanException(e);
        }
    }

    public MBeanInfo getMBeanInfo()
    {
        return _mbeanInfo;
    }
}