001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.server.management;
022
023 import javax.management.JMException;
024 import javax.management.MalformedObjectNameException;
025 import javax.management.NotCompliantMBeanException;
026 import javax.management.ObjectName;
027 import javax.management.StandardMBean;
028
029 import org.apache.qpid.AMQException;
030 import org.apache.qpid.server.registry.ApplicationRegistry;
031
032 /**
033 * Provides implementation of the boilerplate ManagedObject interface. Most managed objects should find it useful
034 * to extend this class rather than implementing ManagedObject from scratch.
035 *
036 */
037 public abstract class DefaultManagedObject extends StandardMBean implements ManagedObject
038 {
039 private Class<?> _managementInterface;
040
041 private String _typeName;
042
043 protected DefaultManagedObject(Class<?> managementInterface, String typeName)
044 throws NotCompliantMBeanException
045 {
046 super(managementInterface);
047 _managementInterface = managementInterface;
048 _typeName = typeName;
049 }
050
051 public String getType()
052 {
053 return _typeName;
054 }
055
056 public Class<?> getManagementInterface()
057 {
058 return _managementInterface;
059 }
060
061 public ManagedObject getParentObject()
062 {
063 return null;
064 }
065
066 public void register() throws AMQException
067 {
068 try
069 {
070 getManagedObjectRegistry().registerObject(this);
071 }
072 catch (JMException e)
073 {
074 throw new AMQException("Error registering managed object " + this + ": " + e, e);
075 }
076 }
077
078 protected ManagedObjectRegistry getManagedObjectRegistry()
079 {
080 return ApplicationRegistry.getInstance().getManagedObjectRegistry();
081 }
082
083 public void unregister() throws AMQException
084 {
085 try
086 {
087 getManagedObjectRegistry().unregisterObject(this);
088 }
089 catch (JMException e)
090 {
091 throw new AMQException("Error unregistering managed object: " + this + ": " + e, e);
092 }
093 }
094
095 public String toString()
096 {
097 return getObjectInstanceName() + "[" + getType() + "]";
098 }
099
100
101 /**
102 * Created the ObjectName as per the JMX Specs
103 * @return ObjectName
104 * @throws MalformedObjectNameException
105 */
106 public ObjectName getObjectName() throws MalformedObjectNameException
107 {
108 String name = getObjectInstanceName();
109 StringBuffer objectName = new StringBuffer(ManagedObject.DOMAIN);
110
111 objectName.append(":type=");
112 objectName.append(getHierarchicalType(this));
113
114 objectName.append(",");
115 objectName.append(getHierarchicalName(this));
116 objectName.append("name=").append(name);
117
118 return new ObjectName(objectName.toString());
119 }
120
121 protected ObjectName getObjectNameForSingleInstanceMBean() throws MalformedObjectNameException
122 {
123 StringBuffer objectName = new StringBuffer(ManagedObject.DOMAIN);
124
125 objectName.append(":type=");
126 objectName.append(getHierarchicalType(this));
127
128 String hierarchyName = getHierarchicalName(this);
129 if (hierarchyName != null)
130 {
131 objectName.append(",");
132 objectName.append(hierarchyName.substring(0, hierarchyName.lastIndexOf(",")));
133 }
134
135 return new ObjectName(objectName.toString());
136 }
137
138 protected String getHierarchicalType(ManagedObject obj)
139 {
140 if (obj.getParentObject() != null)
141 {
142 String parentType = getHierarchicalType(obj.getParentObject()).toString();
143 return parentType + "." + obj.getType();
144 }
145 else
146 return obj.getType();
147 }
148
149 protected String getHierarchicalName(ManagedObject obj)
150 {
151 if (obj.getParentObject() != null)
152 {
153 String parentName = obj.getParentObject().getType() + "=" +
154 obj.getParentObject().getObjectInstanceName() + ","+
155 getHierarchicalName(obj.getParentObject());
156
157 return parentName;
158 }
159 else
160 return "";
161 }
162
163 protected static StringBuffer jmxEncode(StringBuffer jmxName, int attrPos)
164 {
165 for (int i = attrPos; i < jmxName.length(); i++)
166 {
167 if (jmxName.charAt(i) == ',')
168 {
169 jmxName.setCharAt(i, ';');
170 }
171 else if (jmxName.charAt(i) == ':')
172 {
173 jmxName.setCharAt(i, '-');
174 }
175 else if (jmxName.charAt(i) == '?' ||
176 jmxName.charAt(i) == '*' ||
177 jmxName.charAt(i) == '\\')
178 {
179 jmxName.insert(i, '\\');
180 i++;
181 }
182 else if (jmxName.charAt(i) == '\n')
183 {
184 jmxName.insert(i, '\\');
185 i++;
186 jmxName.setCharAt(i, 'n');
187 }
188 }
189 return jmxName;
190 }
191 }
|