/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.monitoring.share;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.lang.management.*;
import javax.management.*;
import javax.management.loading.*;
import nsk.share.*;
/**
* The CustomMBeanServer
implemenets the
* {@link javax.management.MBeanServer MBeanServer} interface to provide
* minimal funcionality for JSR-174 testing.
*
Insignificant methods that are not used just throw {@link TestBug TestBug}
* with "not implemented" message. If this exception is caught during test
* execution, the corresponding method must be implemented.
*/
public class CustomMBeanServer implements MBeanServer {
// Name of the system property, which specifies class name of MBean server
final static String SERVER_BUILDER_PROPERTY
= "javax.management.builder.initial";
// Class name of MBeanServer builder that creates CustomMBeanServer
final static String CUSTOM_SERVER_BUILDER
= CustomMBeanServerBuilder.class.getCanonicalName();
// Default MBeanServer builder
final static String DEFAULT_SERVER_BUILDER = "";
// Internal trace level
private final static int TRACE_ALL = 10;
// Prefix to print while logging
private final static String LOG_PREFIX = "CustomMBeanServer> ";
private final static String BROADCASTER_ITNF_NAME =
"javax.management.NotificationBroadcaster";
private final static String DYNAMICMBEAN_ITNF_NAME =
"javax.management.DynamicMBean";
// Private variables
private String defaultDomain;
private CustomMBeanRegistration register = new CustomMBeanRegistration();
private Log.Logger log;
private HashtableCustomMBeanServer
object.
*
* @param defaultDomain default domain of the new MBeanServer
*/
public CustomMBeanServer(String defaultDomain) {
this.defaultDomain = defaultDomain;
}
/**
* Instantiates and registers an MBean in the MBean server.
*
* @see javax.management.MBeanServer#createMBean(String, ObjectName)
*/
public ObjectInstance createMBean(String className, ObjectName name)
throws ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
throw new TestBug("not implemented");
}
/**
* Instantiates and registers an MBean in the MBean server.
*
* @see javax.management.MBeanServer#createMBean(String, ObjectName,
* Object[], String[])
*/
public ObjectInstance createMBean(String className, ObjectName name,
Object[] params, String[] signature)
throws ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException {
throw new TestBug("not implemented");
}
/**
* Instantiates and registers an MBean in the MBean server.
*
* @see javax.management.MBeanServer#createMBean(String, ObjectName,
* ObjectName)
*/
public ObjectInstance createMBean(String className, ObjectName name,
ObjectName loaderName)
throws ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException,
InstanceNotFoundException {
throw new TestBug("not implemented");
}
/**
* Instantiates and registers an MBean in the MBean server.
*
* @see javax.management.MBeanServer#createMBean(String, ObjectName,
* ObjectName, Object[], String[])
*/
public ObjectInstance createMBean(String className, ObjectName name,
ObjectName loaderName, Object[] params,
String[] signature)
throws ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException,
InstanceNotFoundException {
throw new TestBug("not implemented");
}
/**
* Registers a pre-existing object as an MBean with the MBean server
*
* @see javax.management.MBeanServer#registerMBean(Object, ObjectName)
*/
public ObjectInstance registerMBean(Object object, ObjectName name)
throws InstanceAlreadyExistsException,
MBeanRegistrationException,
NotCompliantMBeanException {
ObjectName newName = null;
try {
newName = register.preRegister(this, name);
} catch (Exception e) {
register.postRegister(Boolean.valueOf(false));
throw new MBeanRegistrationException(e);
}
// The log object may not be initialized by that time, so try
// to check it
if (log != null)
log.trace(TRACE_ALL, "[registerMBean] " + newName);
if (isRegistered(newName)) {
register.postRegister(Boolean.valueOf(false));
throw new InstanceAlreadyExistsException("already registered");
}
ObjectInstance instance = null;
try {
instance = new ObjectInstance(newName, object.getClass().getName());
} catch (IllegalArgumentException e) {
throw new RuntimeOperationsException(e);
}
registeredMBeans.put(newName, new ObjectKeeper(instance, object));
register.postRegister(Boolean.valueOf(true));
return instance;
}
/**
* Unregisters an MBean from the MBean server.
*
* @see javax.management.MBeanServer#unregisterMBean(ObjectName)
*/
public void unregisterMBean(ObjectName name)
throws InstanceNotFoundException,
MBeanRegistrationException {
throw new TestBug("not implemented");
}
/**
* Gets the ObjectInstance
for a given MBean registered with
* the MBean server.
*
* @see javax.management.MBeanServer#getObjectInstance(ObjectName)
*/
public ObjectInstance getObjectInstance(ObjectName name)
throws InstanceNotFoundException {
throw new TestBug("not implemented");
}
/**
* Gets MBeans controlled by the MBean server.
*
* @see javax.management.MBeanServer#queryMBeans(ObjectName, QueryExp)
*/
public SetLog
} object.
*
* @param log a new Log
object.
*/
public void setLog(Log log) {
this.log = new Log.Logger(log, LOG_PREFIX + "> ");
}
// **********************************************************************
//
// Private methods
//
// **********************************************************************
/**
* Gets the object reference for a given MBean registered with the MBean
* server.
*
* @param name The object name of the MBean.
*
* @return The MBean object, specified by name
.
*
* @throws InstanceNotFoundException The MBean specified is not registered
* in the MBean server.
*/
private DynamicMBean getObject(ObjectName name) throws InstanceNotFoundException {
DynamicMBean mbean = registeredMBeansStd.get(name);
if (mbean == null) {
ObjectKeeper objKeeper = registeredMBeans.get(name);
if (objKeeper == null)
throw new InstanceNotFoundException();
Object object = objKeeper.object;
if (object instanceof DynamicMBean)
mbean = (DynamicMBean) object;
else
mbean = new StandardMBean(object, getMBeanInterface(object), true);
registeredMBeansStd.put(name, mbean);
}
return mbean;
/*
ObjectKeeper objKeeper = (ObjectKeeper) registeredMBeans.get(name);
if (objKeeper == null)
throw new InstanceNotFoundException();
Class superOfMBeans = null;
try {
superOfMBeans = Class.forName(DYNAMICMBEAN_ITNF_NAME);
} catch (ClassNotFoundException e) {
throw new InstanceNotFoundException();
}
if (superOfMBeans.isAssignableFrom(objKeeper.object.getClass())) {
return (DynamicMBean )objKeeper.object;
}
return null;
*/
}
/**
* Obtain NotificationBroadcaster for given MBean registered with the MBean
* server.
*
* @param name The object name of the MBean.
*
* @return The MBean object, specified by name
.
*
* @throws InstanceNotFoundException if MBean specified is not registered
* in the MBean server.
*/
private NotificationBroadcaster getNotificationBroadcaster(ObjectName name) throws InstanceNotFoundException {
ObjectKeeper objKeeper = (ObjectKeeper) registeredMBeans.get(name);
if (objKeeper == null)
throw new InstanceNotFoundException();
Object mbean = objKeeper.object;
if (mbean instanceof NotificationBroadcaster)
return (NotificationBroadcaster) mbean;
throw new InstanceNotFoundException();
}
/**
* Invoke the method
*/
private Object invokeObjectMethod(ObjectName name, String methodName,
Object[] params, String[] signature) throws InstanceNotFoundException,
MBeanException,
ReflectionException {
if (log != null)
log.trace(TRACE_ALL, "[invokeObjectMethod] " + name + "> "
+ methodName);
DynamicMBean mbean = getObject(name);
return mbean.invoke(methodName, params, signature);
}
private Class getInterface(Class cl, String prefix) {
Class[] interfaces = cl.getInterfaces();
if (interfaces == null || interfaces.length == 0)
return null;
for (Class c : interfaces) {
if (c.getName().startsWith(prefix))
return c;
c = getInterface(c, prefix);
if (c != null)
return c;
}
return null;
}
/**
* Discover MBean interface of the bean.
*
* Note: this is very specialized for java.lang.management
* and java.util.logging tests.
* It is generally not correct for any MBean.
*
* @param object the bean
* @return interface class
*/
private Class getMBeanInterface(Object object) throws InstanceNotFoundException {
String className = object.getClass().getName();
Class> iface = null;
if (className.startsWith("java.lang.management"))
iface = getInterface(object.getClass(), "java.lang.management");
else if (className.startsWith("java.util.logging"))
iface = getInterface(object.getClass(), "java.util.logging");
else if (className.startsWith("sun.management"))
iface = getInterface(object.getClass(), "java.lang.management");
if (iface != null)
return iface;
Class>[] interfaces = object.getClass().getInterfaces();
System.out.println(object);
System.out.println(object.getClass());
System.out.println(interfaces.length);
for (Class> c : interfaces) {
System.out.println(c.getName());
}
throw new TestBug("No suitable implemented interface found for: " + object + " class: " + object.getClass());
}
}