/* * 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.tomcat.util.modeler;
privatefinal ReadWriteLock mBeanInfoLock = new ReentrantReadWriteLock(); /** * The <code>ModelMBeanInfo</code> object that corresponds * to this <code>ManagedBean</code> instance.
*/ privatetransientvolatile MBeanInfo info = null;
private Map<String,AttributeInfo> attributes = new HashMap<>();
private Map<String,OperationInfo> operations = new HashMap<>();
private NotificationInfo notifications[] = new NotificationInfo[0]; protected String type = null;
/** * Constructor. Will add default attributes.
*/ public ManagedBean() {
AttributeInfo ai=new AttributeInfo();
ai.setName("modelerType");
ai.setDescription("Type of the modeled resource. Can be set only once");
ai.setType("java.lang.String");
ai.setWriteable(false);
addAttribute(ai);
}
/** * @return the collection of attributes for this MBean.
*/ public AttributeInfo[] getAttributes() { return attributes.values().toArray(new AttributeInfo[0]);
}
/** * The fully qualified name of the Java class of the MBean * described by this descriptor. If not specified, the standard JMX * class (<code>javax.management.modelmbean.RequiredModeLMBean</code>) * will be utilized. * @return the class name
*/ public String getClassName() { returnthis.className;
}
/** * @return the (optional) <code>ObjectName</code> domain in which * this MBean should be registered in the MBeanServer.
*/ public String getDomain() { returnthis.domain;
}
/** * @return the name of this managed bean, which must be unique * among all MBeans managed by a particular MBeans server.
*/ public String getName() { returnthis.name;
}
/** * @return the collection of notifications for this MBean.
*/ public NotificationInfo[] getNotifications() { returnthis.notifications;
}
/** * @return the collection of operations for this MBean.
*/ public OperationInfo[] getOperations() { return operations.values().toArray(new OperationInfo[0]);
}
/** * @return the fully qualified name of the Java class of the resource * implementation class described by the managed bean described * by this descriptor.
*/ public String getType() { returnthis.type;
}
// --------------------------------------------------------- Public Methods
/** * Add a new attribute to the set of attributes for this MBean. * * @param attribute The new attribute descriptor
*/ publicvoid addAttribute(AttributeInfo attribute) {
attributes.put(attribute.getName(), attribute);
}
/** * Add a new notification to the set of notifications for this MBean. * * @param notification The new notification descriptor
*/ publicvoid addNotification(NotificationInfo notification) {
mBeanInfoLock.writeLock().lock(); try {
NotificationInfo results[] = new NotificationInfo[notifications.length + 1];
System.arraycopy(notifications, 0, results, 0,
notifications.length);
results[notifications.length] = notification;
notifications = results; this.info = null;
} finally {
mBeanInfoLock.writeLock().unlock();
}
}
/** * Add a new operation to the set of operations for this MBean. * * @param operation The new operation descriptor
*/ publicvoid addOperation(OperationInfo operation) {
operations.put(createOperationKey(operation), operation);
}
/** * Create and return a <code>ModelMBean</code> that has been * preconfigured with the <code>ModelMBeanInfo</code> information * for this managed bean, and is associated with the specified * managed object instance. The returned <code>ModelMBean</code> * will <strong>NOT</strong> have been registered with our * <code>MBeanServer</code>. * * @param instance Instanced of the managed object, or <code>null</code> * for no associated instance * @return the MBean * @exception InstanceNotFoundException if the managed resource * object cannot be found * @exception MBeanException if a problem occurs instantiating the * <code>ModelMBean</code> instance * @exception RuntimeOperationsException if a JMX runtime error occurs
*/ public DynamicMBean createMBean(Object instance) throws InstanceNotFoundException,
MBeanException, RuntimeOperationsException {
BaseModelMBean mbean = null;
// Load the ModelMBean implementation class if(getClassName().equals(BASE_MBEAN)) { // Skip introspection
mbean = new BaseModelMBean();
} else { Class<?> clazz = null;
Exception ex = null; try {
clazz = Class.forName(getClassName());
} catch (Exception e) {
}
// Set the managed resource (if any) try { if (instance != null) {
mbean.setManagedResource(instance, "ObjectReference");
}
} catch (InstanceNotFoundException e) { throw e;
}
return mbean;
}
/** * Create and return a <code>ModelMBeanInfo</code> object that * describes this entire managed bean. * @return the MBean info
*/
MBeanInfo getMBeanInfo() {
mBeanInfoLock.writeLock().lock(); try { if (info == null) { // Create subordinate information descriptors as required
AttributeInfo attrs[] = getAttributes();
MBeanAttributeInfo attributes[] = new MBeanAttributeInfo[attrs.length]; for (int i = 0; i < attrs.length; i++) {
attributes[i] = attrs[i].createAttributeInfo();
}
OperationInfo opers[] = getOperations();
MBeanOperationInfo operations[] = new MBeanOperationInfo[opers.length]; for (int i = 0; i < opers.length; i++) {
operations[i] = opers[i].createOperationInfo();
}
NotificationInfo notifs[] = getNotifications();
MBeanNotificationInfo notifications[] = new MBeanNotificationInfo[notifs.length]; for (int i = 0; i < notifs.length; i++) {
notifications[i] = notifs[i].createNotificationInfo();
}
// Construct and return a new ModelMBeanInfo object
info = new MBeanInfo(getClassName(),
getDescription(),
attributes, new MBeanConstructorInfo[] {},
operations,
notifications);
}
AttributeInfo attrInfo = attributes.get(aname); // Look up the actual operation to be used if (attrInfo == null) { thrownew AttributeNotFoundException(sm.getString("managedMBean.noAttribute", aname, resource));
}
if (params == null) {
params = new Object[0];
} if (signature == null) {
signature = new String[0];
} if (params.length != signature.length) { thrownew RuntimeOperationsException( new IllegalArgumentException(sm.getString("managedMBean.inconsistentArguments")),
sm.getString("managedMBean.inconsistentArguments"));
}
// Acquire the ModelMBeanOperationInfo information for // the requested operation
OperationInfo opInfo =
operations.get(createOperationKey(aname, signature)); if (opInfo == null) { thrownew MBeanException(new ServiceNotFoundException(sm.getString("managedMBean.noOperation", aname)),
sm.getString("managedMBean.noOperation", aname));
}
// Prepare the signature required by Java reflection APIs // FIXME - should we use the signature from opInfo? Class<?> types[] = newClass[signature.length]; for (int i = 0; i < signature.length; i++) {
types[i] = BaseModelMBean.getAttributeClass(signature[i]);
}
// Locate the method to be invoked, either in this MBean itself // or in the corresponding managed resource // FIXME - Accessible methods in superinterfaces?
Object object = null;
Exception exception = null; try {
object = bean;
method = object.getClass().getMethod(aname, types);
} catch (NoSuchMethodException e) {
exception = e;
} try { if ((method == null) && (resource != null)) {
object = resource;
method = object.getClass().getMethod(aname, types);
}
} catch (NoSuchMethodException e) {
exception = e;
} if (method == null) { thrownew ReflectionException(exception, sm.getString("managedMBean.noMethod", aname));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.