提交 92c588da 编写于 作者: A avorobye

8058865: JMX Test Refactoring

Summary: JMX functional tests were ported into JTREG
Reviewed-by: jbachorik
上级 175d8287
/*
* Copyright (c) 2008, 2015, 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.
*/
import java.util.ArrayList;
import javax.management.AttributeNotFoundException;
import javax.management.BadAttributeValueExpException;
import javax.management.BadBinaryOpValueExpException;
import javax.management.BadStringOperationException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidApplicationException;
import javax.management.InvalidAttributeValueException;
import javax.management.JMException;
import javax.management.JMRuntimeException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanRegistrationException;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.OperationsException;
import javax.management.ReflectionException;
import javax.management.RuntimeErrorException;
import javax.management.RuntimeMBeanException;
import javax.management.RuntimeOperationsException;
import javax.management.ServiceNotFoundException;
import javax.management.StringValueExp;
import javax.management.modelmbean.InvalidTargetObjectTypeException;
import javax.management.modelmbean.XMLParseException;
import javax.management.monitor.MonitorSettingException;
import javax.management.openmbean.InvalidKeyException;
import javax.management.openmbean.InvalidOpenTypeException;
import javax.management.openmbean.KeyAlreadyExistsException;
import javax.management.openmbean.OpenDataException;
import javax.management.relation.InvalidRelationIdException;
import javax.management.relation.InvalidRelationServiceException;
import javax.management.relation.InvalidRelationTypeException;
import javax.management.relation.InvalidRoleInfoException;
import javax.management.relation.InvalidRoleValueException;
import javax.management.relation.RelationException;
import javax.management.relation.RelationNotFoundException;
import javax.management.relation.RelationServiceNotRegisteredException;
import javax.management.relation.RelationTypeNotFoundException;
import javax.management.relation.RoleInfoNotFoundException;
import javax.management.relation.RoleNotFoundException;
import javax.management.remote.JMXProviderException;
import javax.management.remote.JMXServerErrorException;
/**
* |----- Original Description Coming From Tonga Original Source Code -------|
* | |
* | That class creates an ArrayList and fill it with an instance of each of |
* | the Exception class of the JMX API. |
* | It's dedicated to use by ExceptionTest. |
* |-------------------------------------------------------------------------|
*/
public class ExceptionFactory {
public static final ArrayList<Exception> exceptions =
new ArrayList<Exception>();
static {
String mes = "SQE";
exceptions.add(new AttributeNotFoundException());
exceptions.add(new BadAttributeValueExpException(mes));
exceptions.add(new BadBinaryOpValueExpException(new StringValueExp(mes)));
exceptions.add(new BadStringOperationException(mes));
exceptions.add(new InstanceAlreadyExistsException());
exceptions.add(new InstanceNotFoundException());
exceptions.add(new IntrospectionException());
exceptions.add(new InvalidApplicationException(mes));
exceptions.add(new InvalidAttributeValueException());
exceptions.add(new JMException());
exceptions.add(new JMRuntimeException());
exceptions.add(new ListenerNotFoundException());
exceptions.add(new MalformedObjectNameException());
exceptions.add(new MBeanException(new Exception(mes), mes));
exceptions.add(new MBeanRegistrationException(new Exception(mes), mes));
exceptions.add(new NotCompliantMBeanException());
exceptions.add(new OperationsException());
exceptions.add(new ReflectionException(new Exception(mes), mes));
exceptions.add(new RuntimeErrorException(new Error(mes), mes));
exceptions.add(new RuntimeMBeanException(new RuntimeException(mes), mes));
exceptions.add(new RuntimeOperationsException(new RuntimeException(mes), mes));
exceptions.add(new ServiceNotFoundException());
exceptions.add(new InvalidTargetObjectTypeException());
exceptions.add(new XMLParseException());
exceptions.add(new MonitorSettingException());
exceptions.add(new InvalidKeyException());
exceptions.add(new InvalidOpenTypeException());
exceptions.add(new KeyAlreadyExistsException());
exceptions.add(new OpenDataException());
exceptions.add(new InvalidRelationIdException());
exceptions.add(new InvalidRelationServiceException());
exceptions.add(new InvalidRelationTypeException());
exceptions.add(new InvalidRoleInfoException());
exceptions.add(new InvalidRoleValueException());
exceptions.add(new RelationException());
exceptions.add(new RelationNotFoundException());
exceptions.add(new RelationServiceNotRegisteredException());
exceptions.add(new RelationTypeNotFoundException());
exceptions.add(new RoleInfoNotFoundException());
exceptions.add(new RoleNotFoundException());
exceptions.add(new JMXProviderException());
exceptions.add(new JMXServerErrorException(mes, new Error(mes)));
ExceptionTest.Utils.debug(ExceptionTest.Utils.DEBUG_STANDARD,
"DataFactory::updateMap: Initialized" +
" an ArrayList with the " +
exceptions.size() + " exceptions of the JMX API");
}
}
/*
* Copyright (c) 2008, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Checks that exceptions are correctly wired (compared to reference).
* @author Olivier Lagneau
* @modules java.management
* @run main/othervm/timeout=300 -DDEBUG_STANDARD ExceptionTest
*/
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import java.lang.reflect.Method;
import java.lang.management.ManagementFactory;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class ExceptionTest {
/*
* First Debug properties and arguments are collect in expected
* map (argName, value) format, then calls original test's run method.
*/
public static void main(String args[]) throws Exception {
System.out.println("=================================================");
// Parses parameters
Utils.parseDebugProperties();
Map<String, Object> map = Utils.parseParameters(args) ;
// Run test
ExceptionTest test = new ExceptionTest();
test.run(map);
}
public void run(Map<String, Object> args) {
System.out.println("ExceptionTest::run: Start");
int errorCount = 0;
JMXConnectorServer cs = null;
JMXConnector cc = null;
try {
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
ObjectName objName =
new ObjectName(ExceptionThrower.EXCEPTION_THROWER_NAME);
System.out.println("ExceptionTest::run: Create and register MBean " + objName);
mbsc.createMBean("ExceptionThrower", objName);
System.out.println("---- OK\n");
// ----
System.out.println("ExceptionTest::run: Ask for exception(s)");
Object[] throwExceptionParam = new Object[1];
String[] throwExceptionSig = new String[]{"int"};
for (int i = 0; i < ExceptionFactory.exceptions.size(); i++) {
throwExceptionParam[0] = new Integer(i);
Exception ex =
(Exception)mbsc.invoke(objName,
"throwException", throwExceptionParam, throwExceptionSig);
if ( ! matches(ex, ExceptionFactory.exceptions.get(i)) ) {
errorCount++;
System.out.println("ExceptionTest::run: (ERROR) Received \n["
+ ex + "]\nin place of\n["
+ ExceptionFactory.exceptions.get(i) + "]");
} else {
System.out.println("OK [" + ex + "]");
}
}
System.out.println("---- DONE\n");
} catch (Exception e) {
Utils.printThrowable(e, true);
throw new RuntimeException();
} finally {
try {
// Close JMX Connector Client
cc.close();
// Stop connertor server
cs.stop();
} catch (Exception e) {
Utils.printThrowable(e, true);
throw new RuntimeException(
"Unable to either close connector client or stop connector server");
}
}
if (errorCount == 0) {
System.out.println("ExceptionTest::run: Done without any error");
} else {
System.out.println("ExceptionTest::run: Done with " + errorCount
+ " error(s)");
throw new RuntimeException("errorCount = " + errorCount);
}
System.out.println("ExceptionTest::run: Done");
}
// Check both Exception are identical.
// That means:
// - none is null.
// - they are of the same Class.
// - if their respective messages aren't null they're equal.
// - if the message of one is null the message of the other is null too.
private boolean matches(Exception ex, Exception refex) {
if ( ex == null || refex == null ) {
System.out.println("(ERROR) Called with one or more null parameter; check "
+ ex + " against " + refex);
return false;
}
String exClass = ex.getClass().getName();
String refexClass = refex.getClass().getName();
if ( ! exClass.equals(refexClass) ) {
System.out.println("(ERROR) Class names don't match; check ["
+ exClass + "] against [" + refexClass + "]");
return false;
}
String exMes = ex.getMessage();
String refexMes = refex.getMessage();
if ( exMes != null && refexMes != null ) {
if ( ! exMes.equals(refexMes) ) {
System.out.println("(ERROR) Non null messages don't match; check ["
+ exMes + "] against [" + refexMes + "]");
return false;
}
} else if ( (exMes == null && refexMes != null)
|| (exMes != null && refexMes == null) ) {
System.out.println("(ERROR) Messages don't match; check [" + exMes
+ "] against [" + refexMes + "]");
}
return true;
}
// Utility inner class coming from JMX Tonga test suite.
// Also used by ExceptionFactory.
static class Utils {
// DEBUG is printed depending on the DEBUG and DEBUG_LEVEL JAVA property
static final String DEBUG_HEADER = "[debug] ";
// DEBUG levels
static int selectedDebugLevel = 0;
static final int DEBUG_STANDARD = 1;
static final int DEBUG_VERBOSE = 2; // Mainly used for stress tests
static final int DEBUG_ALL = DEBUG_STANDARD | DEBUG_VERBOSE;
static void parseDebugProperties() {
int level = 0;
Properties p = System.getProperties();
// get selected levels
if (p.getProperty("DEBUG_STANDARD") != null) {
level |= DEBUG_STANDARD;
}
if (p.getProperty("DEBUG_VERBOSE") != null) {
level |= DEBUG_VERBOSE;
}
if (p.getProperty("DEBUG_ALL") != null) {
level |= DEBUG_ALL;
}
selectedDebugLevel = level;
}
/**
* Reproduces the original parsing and collection of test parameters
* from the DTonga JMX test suite.
*
* Collects passed args and returns them in a map(argname, value) structure,
* which will be then propagated as necessary to various called methods.
*/
static Map<String, Object> parseParameters(String args[])
throws Exception {
debug(DEBUG_STANDARD, "TestRoot::parseParameters: Start");
HashMap<String, Object> map = new HashMap<>();
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].trim().startsWith("-") ) {
if ((i+1) < args.length && !args[i+1].startsWith("-") ) {
debug(DEBUG_STANDARD,
"TestRoot::parseParameters: added in map = " +
args[i] +
" with value " +
args[i+1]) ;
map.put(args[i].trim(), args[i+1].trim()) ;
} else if ((i+1) < args.length && args[i+1].startsWith("-") ||
(i+1) == args.length ) {
debug(DEBUG_STANDARD,
"TestRoot::parseParameters: added in map = " +
args[i] +
" with null value") ;
map.put(args[i].trim(), null) ;
} else {
System.out.println(
"TestRoot::parseParameters: (WARNING) not added in map = " +
args[i]) ;
}
}
}
debug(DEBUG_STANDARD, "TestRoot::parseParameters: Done") ;
return map ;
}
/**
* This method is to be used in all tests to print anything
* that is temporary.
* Printing is done only when debug is activated by the property DEBUG.
* Printing depends also on the DEBUG_LEVEL property.
* Here it encapsulates a System.out.println.
*/
static void debug(int level, String line) {
if ((selectedDebugLevel & level) != 0) {
System.out.println(DEBUG_HEADER + line);
}
}
/**
* Do print stack trace when withStack is true.
* Does try to call getTargetException() and getTargetError() then
* print embedded stacks in the case of an Exception wrapping
* another Exception or an Error. Recurse until no more wrapping
* is found.
*/
static void printThrowable(Throwable theThro, boolean withStack) {
try {
if (withStack) {
theThro.printStackTrace(System.out);
}
if (theThro instanceof Exception) {
Exception t = (Exception) theThro;
Method target = null;
String blank = " ";
try {
target = t.getClass().getMethod("getTargetException",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetException method could be there or not
}
System.out.println(blank + t.getClass() + "==>" + t.getMessage());
while (target != null) {
try {
t = (Exception) target.invoke(t,
(java.lang.Object[]) null);
} catch (Exception ee) {
t = null;
}
try {
if (t != null) {
blank = blank + " ";
System.out.println(blank + t.getClass() + "==>" +
t.getMessage());
try {
target =
t.getClass().getMethod("getTargetException",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetException method could be there or not }
}
} else {
target = null;
}
} catch (Exception ee) {
target = null;
}
}
// We may have exceptions wrapping an Error then it is
// getTargetError that is likely to be called
try {
target = ((Exception) theThro).getClass().getMethod("getTargetError",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetError method could be there or not
}
Throwable err = theThro;
while (target != null) {
try {
err = (Error) target.invoke(err,
(java.lang.Object[]) null);
} catch (Exception ee) {
err = null;
}
try {
if (err != null) {
blank = blank + " ";
System.out.println(blank + err.getClass() + "==>" +
err.getMessage());
if (withStack) {
err.printStackTrace(System.out);
}
try {
target = err.getClass().getMethod("getTargetError",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetError method could be there or not
}
} else {
target = null;
}
} catch (Exception ee) {
target = null;
}
}
} else {
System.out.println("Throwable is : " + theThro);
}
} catch (Throwable x) {
System.out.println("Exception : raised in printException : " + x);
}
}
}
}
/*
* Copyright (c) 2008, 2015, 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.
*/
/**
* This class defines a simple standard MBean.
*/
public class ExceptionThrower implements ExceptionThrowerMBean {
public static final String EXCEPTION_THROWER_NAME
= "sqe:type=ExceptionThrower";
public Exception throwException(int exceptionIndex) {
return ExceptionFactory.exceptions.get(exceptionIndex);
}
}
/*
* Copyright (c) 2008, 2015, 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.
*/
/**
* This interface defines a simple standard MBean.
*/
public interface ExceptionThrowerMBean {
public Exception throwException(int exceptionIndex);
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.management.Descriptor;
import javax.management.ImmutableDescriptor;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
/**
* Class Basic
* Basic Description
*/
public class Basic implements BasicMXBean, NotificationEmitter,
MBeanRegistration {
public static final String EXCEPTION_MESSAGE = "from Basic";
public static final String NOTIFICATION_MESSAGE = "from Basic";
/** Attribute : IntAtt */
private int intAtt = 0;
/** Attribute : IntegerAtt */
private Integer integerAtt = 0;
/** Attribute : BoolAtt */
private boolean boolAtt = false;
/** Attribute : BooleanAtt */
private Boolean booleanAtt = false;
/** Attribute : StringAtt */
private String stringAtt = null;
/** Attribute : DateAtt */
private Date dateAtt = null;
/** Attribute : ObjectNameAtt */
private ObjectName objectNameAtt = null;
/** Attribute : NotifDescriptorAsMapAtt */
private Map<String, String> notifDescriptorAsMapAtt = null;
/** Attribute : NotifDescriptorAtt */
private Descriptor notifDescriptorAtt = null;
/** Attribute : SqeParameter */
private SqeParameter sqeParameterAtt = null;
/* Creates a new instance of Basic */
@SqeDescriptorKey("CONSTRUCTOR Basic")
public Basic() {
}
/* Creates a new instance of Basic */
@SqeDescriptorKey("CONSTRUCTOR Basic")
public Basic(
@SqeDescriptorKey("CONSTRUCTOR PARAMETER SqeParameter") SqeParameter param) {
}
/**
* Get int attribute
*/
public int getIntAtt() {
return intAtt;
}
/**
* Set int attribute
*/
public void setIntAtt(int value) {
intAtt = value;
}
/**
* Get Integer attribute
*/
public Integer getIntegerAtt() {
return integerAtt;
}
/**
* Set Integer attribute
*/
public void setIntegerAtt(Integer value) {
integerAtt = value;
}
/**
* Get boolean attribute
*/
public boolean getBoolAtt() {
return boolAtt;
}
/**
* Set boolean attribute
*/
public void setBoolAtt(boolean value) {
boolAtt = value;
}
/**
* Get Boolean attribute
*/
public Boolean getBooleanAtt() {
return booleanAtt;
}
/**
* Set Boolean attribute
*/
public void setBooleanAtt(Boolean value) {
booleanAtt = value;
}
/**
* Get String attribute
*/
public String getStringAtt() {
return stringAtt;
}
/**
* Set String attribute
*/
public void setStringAtt(String value) {
stringAtt = value;
}
/**
* Get Date attribute
*/
public Date getDateAtt() {
return dateAtt;
}
/**
* Set Date attribute
*/
public void setDateAtt(Date value) {
dateAtt = value;
}
/**
* Get ObjectName attribute
*/
public ObjectName getObjectNameAtt() {
return objectNameAtt;
}
/**
* Set ObjectName attribute
*/
public void setObjectNameAtt(ObjectName value) {
objectNameAtt = value;
}
/**
* Get SqeParameter attribute
*/
public SqeParameter getSqeParameterAtt() throws Exception {
if (sqeParameterAtt == null) {
sqeParameterAtt = new SqeParameter();
sqeParameterAtt.setGlop("INITIALIZED");
}
return sqeParameterAtt;
}
/**
* Set SqeParameter attribute
*/
public void setSqeParameterAtt(SqeParameter value) {
sqeParameterAtt = value;
}
/**
* Get the Descriptor used to build the NotificationInfo
* of emitted notifications.
*/
public Map<String, String> getNotifDescriptorAsMapAtt() {
if (notifDescriptorAsMapAtt == null) {
initNotifDescriptorAtt();
}
return notifDescriptorAsMapAtt;
}
/**
* Set the Descriptor used to build the NotificationInfo
* of emitted notifications.
* <br>A Map<String, Object> would better fit Descriptor needs but then
* it is not convertible according the MXBean specification so the MBean
* registration fails.
* As we plan to test our custom Descriptor finds its way into
* the metadata of emitted notifications, String is good enough.
*/
public void setNotifDescriptorAsMapAtt(Map<String, String> value) {
notifDescriptorAsMapAtt = new HashMap<String, String>(value);
notifDescriptorAtt = new ImmutableDescriptor(value);
}
/**
* Do nothing
*/
public void doNothing() {
// I said NOTHING !
}
/**
* Do take SqeParameter as a parameter
*/
public void doWeird(SqeParameter param) {
}
/**
* Throw an Exception
*/
public void throwException() throws Exception {
throw new Exception(EXCEPTION_MESSAGE);
}
/**
* Throw an Error
*/
public void throwError() {
throw new InternalError(EXCEPTION_MESSAGE);
}
/**
* Reset all attributes
*/
public void reset() {
intAtt = 0;
integerAtt = 0;
boolAtt = false;
booleanAtt = Boolean.FALSE;
stringAtt = null;
dateAtt = null;
objectNameAtt = null;
}
/**
* Returns the weather for the coming days
* @param verbose <code>boolean</code> verbosity
* @throws java.lang.Exception <code>storm</code>
* @return <code>ObjectName</code>
*/
public Weather getWeather(boolean verbose)
throws java.lang.Exception {
return Weather.SUNNY;
}
// Starting here are the 4 methods of MBeanRegistration interface.
// We use that to grab the ObjectName the MBean is registered with.
//
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
// Grab a reference on the MBeanServer we're registered in.
mbs = server;
// Compute the name we're registered with.
if (name != null) {
mbeanName = name;
return name;
} else {
mbeanName =
new ObjectName("sqe:type=" + Basic.class.getName());
return mbeanName;
}
}
public void postRegister(Boolean registrationDone) {
// Do nothing
}
public void preDeregister() throws Exception {
// Do nothing
}
public void postDeregister() {
// Do nothing
}
/**
* Send one Notification of the provided notifType type.
*/
public void sendNotification(String notifType) {
Notification notification = null;
if (notifType.equals(NOTIF_TYPE_0)) {
notification = new Notification(NOTIF_TYPE_0,
mbeanName,
seqNumber,
NOTIFICATION_MESSAGE);
} else if (notifType.equals(NOTIF_TYPE_1)) {
notification = new SqeNotification(NOTIF_TYPE_1,
mbeanName,
seqNumber,
NOTIFICATION_MESSAGE);
}
seqNumber++;
broadcaster.sendNotification(notification);
}
/**
* That method starts a set of threads, each thread sends a given number of
* notifications.
* The number of threads can be set via the attribute numOfNotificationSenders.
* The number of notification sent by each thread can be set via
* the attribute numOfNotificationSenderLoops.
* Depending on the parameter customNotification we send either custom
* notification(s) or MBeanServer registration and unregistration notification(s).
* When customNotification=true the total number of notification(s) sent is
* (numOfNotificationSenders * numOfNotificationSenderLoops). They are
* sequentially of type NOTIF_TYPE_0 then NOTIF_TYPE_1 and so on.
*
* When customNotification=false the total number of notification(s) sent is
* (numOfNotificationSenders * numOfNotificationSenderLoops) registration
* notification(s)
* +
* (numOfNotificationSenders * numOfNotificationSenderLoops) unregistration
* notification(s)
*
* @throws java.lang.Exception
*/
public void sendNotificationWave(boolean customNotification) throws
Exception {
// Build the set of notification sender.
Collection<Callable<Integer>> tasks =
new HashSet<Callable<Integer>>(numOfNotificationSenders);
for (int i = 1; i <= numOfNotificationSenders; i++) {
tasks.add(new NotifSender(numOfNotificationSenderLoops,
customNotification, i));
}
// Start all notification sender in parallel.
ExecutorService execServ = null;
try {
execServ = Executors.newFixedThreadPool(numOfNotificationSenders);
List<Future<Integer>> taskHandlers = execServ.invokeAll(tasks);
checkNotifSenderThreadStatus(taskHandlers);
} finally {
if (!execServ.isShutdown()) {
execServ.shutdown();
}
}
}
public void setNumOfNotificationSenders(int value) {
numOfNotificationSenders = value;
}
public void setNumOfNotificationSenderLoops(int value) {
numOfNotificationSenderLoops = value;
}
/**
* MBean Notification support
* You shouldn't update these methods
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws IllegalArgumentException {
broadcaster.addNotificationListener(listener, filter, handback);
}
public MBeanNotificationInfo[] getNotificationInfo() {
if (notifDescriptorAtt == null) {
initNotifDescriptorAtt();
}
return new MBeanNotificationInfo[]{
new MBeanNotificationInfo(new String[]{
NOTIF_TYPE_0
},
javax.management.Notification.class.getName(),
"Standard JMX Notification",
notifDescriptorAtt),
new MBeanNotificationInfo(new String[]{
NOTIF_TYPE_1
},
SqeNotification.class.getName(),
"SQE Notification",
notifDescriptorAtt)
};
}
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
broadcaster.removeNotificationListener(listener);
}
public void removeNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws ListenerNotFoundException {
broadcaster.removeNotificationListener(listener, filter, handback);
}
// </editor-fold>
private synchronized long getNextSeqNumber() {
return seqNumber++;
}
private void initNotifDescriptorAtt() {
String key = "CRABE";
String value = "TAMBOUR";
notifDescriptorAtt =
new ImmutableDescriptor(new String[]{key + "=" + value});
notifDescriptorAsMapAtt =
new HashMap<String, String>();
notifDescriptorAsMapAtt.put(key, value);
}
private void checkNotifSenderThreadStatus(
List<Future<Integer>> taskHandlers)
throws Exception {
String msgTag = "Basic::checkNotifSenderThreadStatus: ";
// Grab back status of each notification sender.
for (Future<Integer> f : taskHandlers) {
if (f.isCancelled()) {
String message = msgTag +
"---- ERROR : One thread has been cancelled";
System.out.println(message);
throw new RuntimeException(message);
} else {
Integer effectiveNumOfLoops = f.get();
if (effectiveNumOfLoops != numOfNotificationSenderLoops) {
String message = msgTag + "---- ERROR : One thread did " +
effectiveNumOfLoops + " loops in place of " +
numOfNotificationSenderLoops;
System.out.println(message);
throw new RuntimeException(message);
}
}
}
}
//
private int numOfNotificationSenderLoops = 2;
private int numOfNotificationSenders = 13;
private class NotifSender implements Callable<Integer> {
private int cycles;
private boolean customNotification;
private int senderID;
public NotifSender(int cycles, boolean customNotification, int id) {
this.cycles = cycles;
this.customNotification = customNotification;
this.senderID = id;
}
public Integer call() throws Exception {
int callsDone = 0;
try {
for (int i = 1; i <= cycles; i++) {
if (customNotification) {
if (i % 2 == 0) {
sendNotification(NOTIF_TYPE_0);
} else {
sendNotification(NOTIF_TYPE_1);
}
} else {
ObjectName mbeanName = new ObjectName("SQE:type=" +
mbeanClassName + ",senderID=" + senderID);
mbs.createMBean(mbeanClassName, mbeanName);
mbs.unregisterMBean(mbeanName);
}
callsDone++;
}
} catch (Exception e) {
System.out.println("NotifSender::call: (ERROR) Thread [" + senderID +
"] failed after " + callsDone + " cycles");
throw e;
}
return Integer.valueOf(callsDone);
}
}
//
private long seqNumber;
private final NotificationBroadcasterSupport broadcaster =
new NotificationBroadcasterSupport();
private ObjectName mbeanName;
private MBeanServer mbs;
private String mbeanClassName = "Simple";
/**
* Notification types definitions. To use when creating JMX Notifications.
*/
public static final String NOTIF_TYPE_0 =
"sqe.notification.a.type";
public static final String NOTIF_TYPE_1 =
"sqe.notification.b.type";
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import java.util.Date;
import java.util.Map;
import javax.management.ObjectName;
/**
* Interface BasicMBean
* Basic Description
*/
@SqeDescriptorKey("INTERFACE BasicMXBean")
public interface BasicMXBean
{
/**
* Get int attribute
*/
@SqeDescriptorKey("ATTRIBUTE intAtt")
public int getIntAtt();
/**
* Set int attribute
*/
@SqeDescriptorKey("ATTRIBUTE intAtt")
public void setIntAtt(int value);
/**
* Get Integer attribute
*/
@SqeDescriptorKey("ATTRIBUTE integerAtt")
public Integer getIntegerAtt();
/**
* Set Integer attribute
*/
@SqeDescriptorKey("ATTRIBUTE integerAtt")
public void setIntegerAtt(Integer value);
/**
* Get boolean attribute
*/
@SqeDescriptorKey("ATTRIBUTE boolAtt")
public boolean getBoolAtt();
/**
* Set boolean attribute
*/
@SqeDescriptorKey("ATTRIBUTE boolAtt")
public void setBoolAtt(boolean value);
/**
* Get Boolean attribute
*/
@SqeDescriptorKey("ATTRIBUTE booleanAtt")
public Boolean getBooleanAtt();
/**
* Set Boolean attribute
*/
@SqeDescriptorKey("ATTRIBUTE booleanAtt")
public void setBooleanAtt(Boolean value);
/**
* Get String attribute
*/
@SqeDescriptorKey("ATTRIBUTE stringAtt")
public String getStringAtt();
/**
* Set String attribute
*/
@SqeDescriptorKey("ATTRIBUTE stringAtt")
public void setStringAtt(String value);
/**
* Get Date attribute
*/
@SqeDescriptorKey("ATTRIBUTE dateAtt")
public Date getDateAtt();
/**
* Set Date attribute
*/
@SqeDescriptorKey("ATTRIBUTE dateAtt")
public void setDateAtt(Date value);
/**
* Get ObjectName attribute
*/
@SqeDescriptorKey("ATTRIBUTE objectNameAtt")
public ObjectName getObjectNameAtt();
/**
* Set ObjectName attribute
*/
@SqeDescriptorKey("ATTRIBUTE objectNameAtt")
public void setObjectNameAtt(ObjectName value);
/**
* Get SqeParameter attribute
*/
@SqeDescriptorKey("ATTRIBUTE sqeParameterAtt")
public SqeParameter getSqeParameterAtt() throws Exception;
/**
* Set SqeParameter attribute
*/
@SqeDescriptorKey("ATTRIBUTE sqeParameterAtt")
public void setSqeParameterAtt(SqeParameter value);
/**
* Set NumOfNotificationSenders attribute
*/
@SqeDescriptorKey("ATTRIBUTE NumOfNotificationSenders")
public void setNumOfNotificationSenders(int value);
/**
* Set NumOfNotificationSenderLoops attribute
*/
@SqeDescriptorKey("ATTRIBUTE NumOfNotificationSenderLoops")
public void setNumOfNotificationSenderLoops(int value);
/**
* do nothing
*
*/
@SqeDescriptorKey("OPERATION doNothing")
public void doNothing();
/**
* Do take SqeParameter as a parameter
*/
@SqeDescriptorKey("OPERATION doWeird")
public void doWeird(@SqeDescriptorKey("METHOD PARAMETER")SqeParameter param);
/**
* throw an Exception
*
*/
@SqeDescriptorKey("OPERATION throwException")
public void throwException() throws Exception;
/**
* throw an Error
*
*/
@SqeDescriptorKey("OPERATION throwError")
public void throwError();
/**
* reset all attributes
*
*/
@SqeDescriptorKey("OPERATION reset")
public void reset();
/**
* returns the weather for the coming days
*
* @param verbose <code>boolean</code> verbosity
* @return <code>ObjectName</code>
*/
@SqeDescriptorKey("OPERATION getWeather")
public Weather getWeather(@SqeDescriptorKey("METHOD PARAMETER")boolean verbose)
throws java.lang.Exception;
public enum Weather {
CLOUDY, SUNNY
}
@SqeDescriptorKey("ATTRIBUTE notifDescriptorAsMapAtt")
public Map<String, String> getNotifDescriptorAsMapAtt();
@SqeDescriptorKey("ATTRIBUTE notifDescriptorAsMapAtt")
public void setNotifDescriptorAsMapAtt(Map<String, String> value);
@SqeDescriptorKey("OPERATION sendNotification")
public void sendNotification(@SqeDescriptorKey("METHOD PARAMETER")String notifType);
@SqeDescriptorKey("OPERATION sendNotificationWave")
public void sendNotificationWave(boolean customNotification) throws Exception;
}
/*
* Copyright (c) 2005, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Checks correct exception and error events from NotificationListener
* @author Olivier Lagneau
* @modules java.management
* @library /lib/testlibrary
* @compile Basic.java
* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanExceptionHandlingTest -timeForNotificationInSeconds 3
*/
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MBeanException;
import javax.management.MBeanServerDelegate;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.RuntimeErrorException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class MXBeanExceptionHandlingTest implements NotificationListener {
private static String BASIC_MXBEAN_CLASS_NAME = "Basic";
private long timeForNotificationInSeconds = 3L;
private int numOfNotifications = 2;
private BlockingQueue<Notification> notifList = null;
/*
* First Debug properties and arguments are collect in expected
* map (argName, value) format, then calls original test's run method.
*/
public static void main(String args[]) throws Exception {
System.out.println("=================================================");
// Parses parameters
Utils.parseDebugProperties();
Map<String, Object> map = Utils.parseParameters(args) ;
// Run test
MXBeanExceptionHandlingTest test = new MXBeanExceptionHandlingTest();
test.run(map);
}
protected void parseArgs(Map<String, Object> args) throws Exception {
String arg = null;
// Init timeForNotificationInSeconds
// It is the maximum time in seconds we wait for a notification.
arg = (String)args.get("-timeForNotificationInSeconds") ;
if (arg != null) {
timeForNotificationInSeconds = (new Long(arg)).longValue();
}
}
public void run(Map<String, Object> args) {
System.out.println("MXBeanExceptionHandlingTest::run: Start") ;
int errorCount = 0 ;
try {
parseArgs(args);
notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println("Add me as notification listener");
mbsc.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME,
this, null, null);
System.out.println("---- OK\n") ;
// ----
System.out.println("Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println("---- OK\n") ;
// ----
System.out.println("Call method throwException on our MXBean");
try {
mbsc.invoke(objName, "throwException", null, null);
errorCount++;
System.out.println("(ERROR) Did not get awaited MBeanException") ;
} catch (MBeanException mbe) {
System.out.println("(OK) Got awaited MBeanException") ;
Throwable cause = mbe.getCause();
if ( cause instanceof java.lang.Exception ) {
System.out.println("(OK) Cause is of the right class") ;
String mess = cause.getMessage();
if ( mess.equals(Basic.EXCEPTION_MESSAGE ) ) {
System.out.println("(OK) Cause message is fine") ;
} else {
errorCount++;
System.out.println("(ERROR) Cause has message "
+ cause.getMessage()
+ " as we expect "
+ Basic.EXCEPTION_MESSAGE) ;
}
} else {
errorCount++;
System.out.println("(ERROR) Cause is of class "
+ cause.getClass().getName()
+ " as we expect java.lang.Exception") ;
}
} catch (Exception e) {
errorCount++;
System.out.println("(ERROR) Did not get awaited MBeanException but "
+ e) ;
Utils.printThrowable(e, true);
}
System.out.println("---- DONE\n") ;
// ----
System.out.println("Call method throwError on our MXBean");
try {
mbsc.invoke(objName, "throwError", null, null);
errorCount++;
System.out.println("(ERROR) Did not get awaited RuntimeErrorException") ;
} catch (RuntimeErrorException ree) {
System.out.println("(OK) Got awaited RuntimeErrorException") ;
Throwable cause = ree.getCause();
if ( cause instanceof java.lang.InternalError ) {
System.out.println("(OK) Cause is of the right class") ;
String mess = cause.getMessage();
if ( mess.equals(Basic.EXCEPTION_MESSAGE ) ) {
System.out.println("(OK) Cause message is fine") ;
} else {
errorCount++;
System.out.println("(ERROR) Cause has message "
+ cause.getMessage()
+ " as we expect "
+ Basic.EXCEPTION_MESSAGE) ;
}
} else {
errorCount++;
System.out.println("(ERROR) Cause is of class "
+ cause.getClass().getName()
+ " as we expect java.lang.InternalError") ;
}
} catch (Exception e) {
errorCount++;
System.out.println("(ERROR) Did not get awaited RuntimeErrorException but "
+ e) ;
Utils.printThrowable(e, true);
}
System.out.println("---- DONE\n") ;
// ----
System.out.println("Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println("---- OK\n") ;
Thread.sleep(timeForNotificationInSeconds * 1000);
int numOfReceivedNotif = notifList.size();
if ( numOfReceivedNotif == numOfNotifications ) {
System.out.println("(OK) We received "
+ numOfNotifications
+ " Notifications") ;
} else {
errorCount++;
System.out.println("(ERROR) We received "
+ numOfReceivedNotif
+ " Notifications in place of "
+ numOfNotifications) ;
}
} catch(Exception e) {
Utils.printThrowable(e, true) ;
throw new RuntimeException(e);
}
if ( errorCount == 0 ) {
System.out.println("MXBeanExceptionHandlingTest::run: Done without any error") ;
} else {
System.out.println("MXBeanExceptionHandlingTest::run: Done with "
+ errorCount
+ " error(s)") ;
throw new RuntimeException("errorCount = " + errorCount);
}
}
public void handleNotification(Notification notification, Object handback) {
System.out.println("MXBeanExceptionHandlingTest::handleNotification: Received "
+ notification);
notifList.add(notification);
}
}
此差异已折叠。
/*
* Copyright (c) 2005, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Checks access to test MXBean
* @author Olivier Lagneau
* @modules java.management
* @library /lib/testlibrary
* @compile Basic.java
* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanInteropTest2
*/
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.management.Attribute;
import javax.management.JMX;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class MXBeanInteropTest2 {
private static String BASIC_MXBEAN_CLASS_NAME = "Basic";
/*
* First Debug properties and arguments are collect in expected
* map (argName, value) format, then calls original test's run method.
*/
public static void main(String args[]) throws Exception {
System.out.println("=================================================");
// Parses parameters
Utils.parseDebugProperties();
Map<String, Object> map = Utils.parseParameters(args) ;
// Run test
MXBeanInteropTest2 test = new MXBeanInteropTest2();
test.run(map);
}
public void run(Map<String, Object> args) {
System.out.println("MXBeanInteropTest2::run: Start") ;
int errorCount = 0 ;
try {
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// Prints all MBeans whatever the domain is.
printMBeans(mbsc) ;
// Call test body
errorCount += doBasicMXBeanTest(mbsc) ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
throw new RuntimeException(e);
}
if ( errorCount == 0 ) {
System.out.println("MXBeanInteropTest2::run: Done without any error") ;
} else {
System.out.println("MXBeanInteropTest2::run: Done with "
+ errorCount
+ " error(s)") ;
throw new RuntimeException("errorCount = " + errorCount);
}
}
/**
* Prints all MBeans whatever the domain is.
*/
private static void printMBeans(MBeanServerConnection mbsc) throws Exception {
Set<ObjectName> set = mbsc.queryNames(null, null);
System.out.println("---- MBeans found :");
for (Iterator<ObjectName> iter = set.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
System.out.println("\n") ;
}
private final int doBasicMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- doBasicMXBeanTest") ;
try {
ObjectName objName =
new ObjectName("sqe:type=BasicMXBean") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
printMBeanInfo(mbInfo);
System.out.println("---- OK\n") ;
System.out.println("getMBeanInfo\t\t"
+ mbInfo);
System.out.println("---- OK\n") ;
System.out.println("Check mxbean field in the MBeanInfo");
String mxbeanField =
(String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
if ( mxbeanField == null || ! mxbeanField.equals("true")) {
System.out.println("---- ERROR : Improper mxbean field value "
+ mxbeanField);
errorCount++;
}
System.out.println("---- OK\n") ;
System.out.println("Set attribute ObjectNameAtt");
Attribute att = new Attribute("ObjectNameAtt", objName);
mbsc.setAttribute(objName, att);
ObjectName value =
(ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt");
if ( ! value.equals(objName) ) {
errorCount++;
System.out.println("---- ERROR : setAttribute failed, got "
+ value
+ " while expecting "
+ objName);
}
System.out.println("---- OK\n") ;
System.out.println("Call operation doNothing");
mbsc.invoke(objName, "doNothing", null, null);
System.out.println("---- OK\n") ;
System.out.println("Call operation getWeather");
Object weather = mbsc.invoke(objName,
"getWeather",
new Object[]{Boolean.TRUE},
new String[]{"boolean"});
System.out.println("Weather is " + weather);
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private void printMBeanInfo(MBeanInfo mbInfo) {
System.out.println("Description " + mbInfo.getDescription());
for (MBeanConstructorInfo ctor : mbInfo.getConstructors()) {
System.out.println("Constructor " + ctor.getName());
}
for (MBeanAttributeInfo att : mbInfo.getAttributes()) {
System.out.println("Attribute " + att.getName()
+ " [" + att.getType() + "]");
}
for (MBeanOperationInfo oper : mbInfo.getOperations()) {
System.out.println("Operation " + oper.getName());
}
for (MBeanNotificationInfo notif : mbInfo.getNotifications()) {
System.out.println("Notification " + notif.getName());
}
}
}
/*
* Copyright (c) 2005, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Checks correct collection of MXBean's class after unregistration
* @author Olivier Lagneau
* @modules java.management
* @library /lib/testlibrary
* @run main/othervm/timeout=300 MXBeanLoadingTest1
*/
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Map;
import javax.management.Attribute;
import javax.management.JMX;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MXBean;
import javax.management.ObjectName;
import javax.management.loading.PrivateMLet;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
public class MXBeanLoadingTest1 {
public static void main(String[] args) throws Exception {
MXBeanLoadingTest1 test = new MXBeanLoadingTest1();
test.run((Map<String, Object>)null);
}
public void run(Map<String, Object> args) {
System.out.println("MXBeanLoadingTest1::run: Start") ;
try {
System.out.println("We ensure no reference is retained on MXBean class"
+ " after it is unregistered. We take time to perform"
+ " some little extra check of Descriptors, MBean*Info.");
ClassLoader myClassLoader = MXBeanLoadingTest1.class.getClassLoader();
if (!(myClassLoader instanceof URLClassLoader)) {
String message = "(ERROR) Test's class loader is not " +
"a URLClassLoader";
System.out.println(message);
throw new RuntimeException(message);
}
URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;
URL[] urls = myURLClassLoader.getURLs();
PrivateMLet mlet = new PrivateMLet(urls, null, false);
Class<?> shadowClass = mlet.loadClass(TestMXBean.class.getName());
if (shadowClass == TestMXBean.class) {
String message = "(ERROR) MLet got original TestMXBean, not shadow";
System.out.println(message);
throw new RuntimeException(message);
}
shadowClass = null;
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ObjectName mletName = new ObjectName("x:type=mlet");
mbs.registerMBean(mlet, mletName);
ObjectName testName = new ObjectName("x:type=test");
mbs.createMBean(Test.class.getName(), testName, mletName);
// That test fails because the MXBean instance is accessed via
// a delegate OpenMBean which has
ClassLoader testLoader = mbs.getClassLoaderFor(testName);
if (testLoader != mlet) {
System.out.println("MLet " + mlet);
String message = "(ERROR) MXBean's class loader is not MLet: "
+ testLoader;
System.out.println(message);
throw new RuntimeException(message);
}
testLoader = null;
// Cycle get/set/get of the attribute of type Luis.
// We check the set is effective.
CompositeData cd_B = (CompositeData)mbs.getAttribute(testName, "B");
CompositeType compType_B = cd_B.getCompositeType();
CompositeDataSupport cds_B =
new CompositeDataSupport(compType_B,
new String[]{"something"},
new Object[]{Integer.valueOf(13)});
Attribute myAtt = new Attribute("B", cds_B);
mbs.setAttribute(testName, myAtt);
CompositeData cd_B2 = (CompositeData)mbs.getAttribute(testName, "B");
if ( ((Integer)cd_B2.get("something")).intValue() != 13 ) {
String message = "(ERROR) The setAttribute of att B did not work;"
+ " expect Luis.something = 13 but got "
+ cd_B2.get("something");
System.out.println(message);
throw new RuntimeException(message);
}
MBeanInfo info = mbs.getMBeanInfo(testName);
String mxbeanField =
(String)info.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
if ( mxbeanField == null || ! mxbeanField.equals("true")) {
String message = "(ERROR) Improper mxbean field value "
+ mxbeanField;
System.out.println(message);
throw new RuntimeException(message);
}
// Check the 2 attributes.
MBeanAttributeInfo[] attrs = info.getAttributes();
if ( attrs.length == 2 ) {
for (MBeanAttributeInfo mbai : attrs) {
String originalTypeFieldValue =
(String)mbai.getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
OpenType<?> openTypeFieldValue =
(OpenType<?>)mbai.getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
if ( mbai.getName().equals("A") ) {
if ( !mbai.isReadable() || !mbai.isWritable()
|| mbai.isIs()
|| !mbai.getType().equals("int") ) {
String message = "(ERROR) Unexpected MBeanAttributeInfo for A "
+ mbai;
System.out.println(message);
throw new RuntimeException(message);
}
if ( ! originalTypeFieldValue.equals("int") ) {
String message = "(ERROR) Unexpected originalType in Descriptor for A "
+ originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
if ( ! openTypeFieldValue.equals(SimpleType.INTEGER) ) {
String message = "(ERROR) Unexpected openType in Descriptor for A "
+ originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
} else if ( mbai.getName().equals("B") ) {
if ( !mbai.isReadable() || !mbai.isWritable()
|| mbai.isIs()
|| !mbai.getType().equals("javax.management.openmbean.CompositeData") ) {
String message = "(ERROR) Unexpected MBeanAttributeInfo for B "
+ mbai;
System.out.println(message);
throw new RuntimeException(message);
}
if ( ! originalTypeFieldValue.equals(Luis.class.getName()) ) {
String message = "(ERROR) Unexpected originalType in Descriptor for B "
+ originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
if ( ! openTypeFieldValue.equals(compType_B) ) {
String message = "(ERROR) Unexpected openType in Descriptor for B "
+ compType_B;
System.out.println(message);
throw new RuntimeException(message);
}
} else {
String message = "(ERROR) Unknown attribute name";
System.out.println(message);
throw new RuntimeException(message);
}
}
} else {
String message = "(ERROR) Unexpected MBeanAttributeInfo array"
+ Arrays.deepToString(attrs);
System.out.println(message);
throw new RuntimeException(message);
}
// Check the MXBean operation.
MBeanOperationInfo[] ops = info.getOperations();
// The impact is ACTION_INFO as for a standard MBean it is UNKNOWN,
// logged 6320104.
if (ops.length != 1 || !ops[0].getName().equals("bogus")
|| ops[0].getSignature().length > 0
|| !ops[0].getReturnType().equals("void")) {
String message = "(ERROR) Unexpected MBeanOperationInfo array "
+ Arrays.deepToString(ops);
System.out.println(message);
throw new RuntimeException(message);
}
String originalTypeFieldValue =
(String)ops[0].getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
OpenType<?> openTypeFieldValue =
(OpenType<?>)ops[0].getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
if ( ! originalTypeFieldValue.equals("void") ) {
String message = "(ERROR) Unexpected originalType in Descriptor for bogus "
+ originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
if ( ! openTypeFieldValue.equals(SimpleType.VOID) ) {
String message = "(ERROR) Unexpected openType in Descriptor for bogus "
+ originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
// Check there is 2 constructors.
if (info.getConstructors().length != 2) {
String message = "(ERROR) Wrong number of constructors " +
"in introspected bean: " +
Arrays.asList(info.getConstructors());
System.out.println(message);
throw new RuntimeException(message);
}
// Check MXBean class name.
if (!info.getClassName().endsWith("Test")) {
String message = "(ERROR) Wrong info class name: " +
info.getClassName();
System.out.println(message);
throw new RuntimeException(message);
}
mbs.unregisterMBean(testName);
mbs.unregisterMBean(mletName);
WeakReference<PrivateMLet> mletRef =
new WeakReference<PrivateMLet>(mlet);
mlet = null;
System.out.println("MXBean registered and unregistered, waiting for " +
"garbage collector to collect class loader");
for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
System.gc();
Thread.sleep(1);
}
if (mletRef.get() == null)
System.out.println("(OK) class loader was GC'd");
else {
String message = "(ERROR) Class loader was not GC'd";
System.out.println(message);
throw new RuntimeException(message);
}
} catch(Exception e) {
Utils.printThrowable(e, true) ;
throw new RuntimeException(e);
}
System.out.println("MXBeanLoadingTest1::run: Done without any error") ;
}
// I agree the use of the MXBean annotation and the MXBean suffix for the
// interface name are redundant but however harmless.
//
@MXBean(true)
public static interface TestMXBean {
public void bogus();
public int getA();
public void setA(int a);
public Luis getB();
public void setB(Luis mi);
}
public static class Test implements TestMXBean {
private Luis luis = new Luis() ;
public Test() {}
public Test(int x) {}
public void bogus() {}
public int getA() {return 0;}
public void setA(int a) {}
public Luis getB() {return this.luis;}
public void setB(Luis luis) {this.luis = luis;}
}
public static class Luis {
private int something = 0;
public Luis() {}
public int getSomething() {return something;}
public void setSomething(int v) {something = v;}
public void doNothing() {}
}
}
/*
* Copyright (c) 2005, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Checks MXBean proper registration both as its implementation class and interface
* @author Olivier Lagneau
* @modules java.management
* @library /lib/testlibrary
* @compile Basic.java
* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanNotifTest -numOfNotifications 239 -timeForNotificationInSeconds 4
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.lang.management.ManagementFactory;
import javax.management.Attribute;
import javax.management.Descriptor;
import javax.management.ImmutableDescriptor;
import javax.management.MBeanServer;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;
public class MXBeanNotifTest implements NotificationListener {
private static String BASIC_MXBEAN_CLASS_NAME = "Basic";
private static String BASIC_MXBEAN_INTERFACE_NAME = "BasicMXBean";
private long timeForNotificationInSeconds = 3L;
private int numOfNotifications = 1;
private BlockingQueue<Notification> notifList = null;
private int numOfNotifDescriptorElements = 13;
/*
* First Debug properties and arguments are collect in expected
* map (argName, value) format, then calls original test's run method.
*/
public static void main(String args[]) throws Exception {
System.out.println("=================================================");
// Parses parameters
Utils.parseDebugProperties();
Map<String, Object> map = Utils.parseParameters(args) ;
// Run test
MXBeanNotifTest test = new MXBeanNotifTest();
test.run(map);
}
protected void parseArgs(Map<String, Object> args) throws Exception {
String arg = null;
// Init numOfNotifications
// It is the number of notifications we should trigger and check.
arg = (String)args.get("-numOfNotifications") ;
if (arg != null) {
numOfNotifications = (new Integer(arg)).intValue();
}
// Init timeForNotificationInSeconds
// It is the maximum time in seconds we wait for each notification.
arg = (String)args.get("-timeForEachNotificationInSeconds") ;
if (arg != null) {
timeForNotificationInSeconds = (new Long(arg)).longValue();
}
}
public void run(Map<String, Object> args) {
System.out.println("MXBeanNotifTest::run: Start") ;
int errorCount = 0 ;
try {
parseArgs(args);
notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println("MXBeanNotifTest::run: Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println("---- OK\n") ;
// ----
System.out.println("MXBeanNotifTest::run: Add me as notification listener");
mbsc.addNotificationListener(objName, this, null, null);
// ----
System.out.println("MXBeanNotifTest::run: Retrieve the Descriptor"
+ " that should be in MBeanNotificationInfo");
TabularData tabData =
(TabularData)mbsc.getAttribute(objName, "NotifDescriptorAsMapAtt");
Map<String, String> descrMap = new HashMap<>();
for (Iterator<?> it = tabData.values().iterator(); it.hasNext(); ) {
CompositeData compData = (CompositeData)it.next();
descrMap.put((String)compData.get("key"),
(String)compData.get("value"));
}
Descriptor refNotifDescriptor = new ImmutableDescriptor(descrMap);
System.out.println("---- OK\n") ;
// ----
// Because the MBean holding the targeted attribute is MXBean, we
// should use for the setAttribute a converted form for the
// attribute value as described by the MXBean mapping rules.
// This explains all that lovely stuff for creating a
// TabularDataSupport.
//
// WARNING : the MBeanInfo of the MXBean used on opposite side
// is computed when the MBean is registered.
// It means the Descriptor considered for the MBeanNotificationInfo
// is not the one we set in the lines below, it is too late.
// However, we check that set is harmless when we check
// the MBeanNotificationInfo.
//
System.out.println("MXBeanNotifTest::run: Set a Map<String, String>"
+ " attribute");
String typeName =
"java.util.Map<java.lang.String,java.lang.String>";
String[] keyValue = new String[] {"key", "value"};
OpenType<?>[] openTypes =
new OpenType<?>[] {SimpleType.STRING, SimpleType.STRING};
CompositeType rowType = new CompositeType(typeName, typeName,
keyValue, keyValue, openTypes);
TabularType tabType = new TabularType(typeName, typeName,
rowType, new String[]{"key"});
TabularDataSupport convertedDescrMap =
new TabularDataSupport(tabType);
for (int i = 0; i < numOfNotifDescriptorElements; i++) {
Object[] descrValue = {"field" + i, "value" + i};
CompositeData data =
new CompositeDataSupport(rowType, keyValue, descrValue);
convertedDescrMap.put(data);
}
Attribute descrAtt =
new Attribute("NotifDescriptorAsMapAtt", convertedDescrMap);
mbsc.setAttribute(objName, descrAtt);
System.out.println("---- OK\n") ;
// ----
System.out.println("MXBeanNotifTest::run: Compare the Descriptor from"
+ " the MBeanNotificationInfo against a reference");
MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
errorCount += checkMBeanInfo(mbInfo, refNotifDescriptor);
System.out.println("---- DONE\n") ;
// ----
System.out.println("Check isInstanceOf(Basic)");
if ( ! mbsc.isInstanceOf(objName, BASIC_MXBEAN_CLASS_NAME) ) {
errorCount++;
System.out.println("---- ERROR isInstanceOf returned false\n") ;
} else {
System.out.println("---- OK\n") ;
}
// ----
System.out.println("Check isInstanceOf(BasicMXBean)");
if ( ! mbsc.isInstanceOf(objName, BASIC_MXBEAN_INTERFACE_NAME) ) {
errorCount++;
System.out.println("---- ERROR isInstanceOf returned false\n") ;
} else {
System.out.println("---- OK\n") ;
}
// ----
System.out.println("MXBeanNotifTest::run: Ask for "
+ numOfNotifications + " notification(s)");
Object[] sendNotifParam = new Object[1];
String[] sendNotifSig = new String[]{"java.lang.String"};
for (int i = 0; i < numOfNotifications; i++) {
// Select which type of notification we ask for
if ( i % 2 == 0 ) {
sendNotifParam[0] = Basic.NOTIF_TYPE_0;
} else {
sendNotifParam[0] = Basic.NOTIF_TYPE_1;
}
// Trigger notification emission
mbsc.invoke(objName,
"sendNotification",
sendNotifParam,
sendNotifSig);
// Wait for it then check it when it comes early enough
Notification notif =
notifList.poll(timeForNotificationInSeconds,
TimeUnit.SECONDS) ;
// The very first notification is likely to come in slower than
// all the others. Because that test isn't targeting the speed
// notifications are delivered with, we prefer to secure it.
if (i == 0 && notif == null) {
System.out.println("MXBeanNotifTest::run: Wait extra "
+ timeForNotificationInSeconds + " second(s) the "
+ " very first notification");
notif = notifList.poll(timeForNotificationInSeconds,
TimeUnit.SECONDS);
}
if ( notif == null ) {
errorCount++;
System.out.println("---- ERROR No notification received"
+ " within allocated " + timeForNotificationInSeconds
+ " second(s) !");
} else {
errorCount +=
checkNotification(notif,
(String)sendNotifParam[0],
Basic.NOTIFICATION_MESSAGE,
objName);
}
}
int toc = 0;
while ( notifList.size() < 2 && toc < 10 ) {
Thread.sleep(499);
toc++;
}
System.out.println("---- DONE\n") ;
} catch(Exception e) {
Utils.printThrowable(e, true) ;
throw new RuntimeException(e);
}
if ( errorCount == 0 ) {
System.out.println("MXBeanNotifTest::run: Done without any error") ;
} else {
System.out.println("MXBeanNotifTest::run: Done with "
+ errorCount
+ " error(s)") ;
throw new RuntimeException("errorCount = " + errorCount);
}
}
private int checkMBeanInfo(MBeanInfo mbi, Descriptor refDescr) {
MBeanNotificationInfo[] notifsInfo = mbi.getNotifications();
int res = 0;
for (MBeanNotificationInfo mbni : notifsInfo) {
if ( mbni.getDescriptor().equals(refDescr) ) {
System.out.println("(OK)");
} else {
System.out.println("(ERROR) Descriptor of the notification is "
+ mbni.getDescriptor()
+ " as we expect "
+ refDescr);
res++;
}
}
return res;
}
private int checkNotification(Notification notif,
String refType,
String refMessage,
ObjectName refSource) {
int res = 0;
Utils.debug(Utils.DEBUG_VERBOSE,
"\t getSource " + notif.getSource());
Utils.debug(Utils.DEBUG_VERBOSE,
"\t getMessage " + notif.getMessage());
Utils.debug(Utils.DEBUG_VERBOSE,
"\t getSequenceNumber " + notif.getSequenceNumber());
Utils.debug(Utils.DEBUG_VERBOSE,
"\t getTimeStamp " + notif.getTimeStamp());
Utils.debug(Utils.DEBUG_VERBOSE,
"\t getType " + notif.getType());
Utils.debug(Utils.DEBUG_VERBOSE,
"\t getUserData " + notif.getUserData());
if ( ! notif.getType().equals(refType) ) {
res++;
System.out.println("(ERROR) Type is not "
+ refType + " in notification\n" + notif);
} else {
if ( notif.getType().equals(Basic.NOTIF_TYPE_0)
&& ! (notif instanceof javax.management.Notification) ) {
res++;
System.out.println("(ERROR) Notification is not instance of "
+ " javax.management.Notification but rather "
+ notif.getClass().getName());
} else if ( notif.getType().equals(Basic.NOTIF_TYPE_1)
&& ! (notif instanceof SqeNotification) ) {
res++;
System.out.println("(ERROR) Notification is not instance of "
+ " javasoft.sqe.jmx.share.SqeNotification but rather "
+ notif.getClass().getName());
}
}
if ( ! notif.getMessage().equals(refMessage) ) {
res++;
System.out.println("(ERROR) Message is not "
+ refMessage + " in notification\n" + notif);
}
if ( ! notif.getSource().equals(refSource) ) {
res++;
System.out.println("(ERROR) Source is not "
+ refSource + " in notification\n" + notif);
}
return res;
}
public void handleNotification(Notification notification, Object handback) {
Utils.debug(Utils.DEBUG_VERBOSE,
"MXBeanNotifTest::handleNotification: Received "
+ notification);
notifList.add(notification);
}
}
/*
* Copyright (c) 2005, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Checks that a serialized instance is not transmitted from an MXBean.
* All the communication should be done via Open Types
* @author Olivier Lagneau
* @modules java.management
* @library /lib/testlibrary
* @compile Basic.java
* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanWeirdParamTest
*/
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Process;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;
import jdk.testlibrary.ProcessTools;
import jdk.testlibrary.JDKToolFinder;
public class MXBeanWeirdParamTest {
private static String BASIC_MXBEAN_CLASS_NAME = "Basic";
private static final String CLIENT_CLASS_MAIN =
"MXBeanWeirdParamTest$ClientSide";
private JMXConnectorServer cs;
/*
* First Debug properties and arguments are collect in expected
* map (argName, value) format, then calls original test's run method.
*/
public static void main(String args[]) throws Exception {
System.out.println("=================================================");
// Parses parameters
Utils.parseDebugProperties();
Map<String, Object> map = Utils.parseParameters(args) ;
// Run test
MXBeanWeirdParamTest test = new MXBeanWeirdParamTest();
test.run(map);
}
/*
* Create the MBeansServe side of the test and returns its address
*/
private JMXServiceURL createServerSide() throws Exception {
final int NINETY_SECONDS = 90;
// We will use the platform mbean server
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
Utils.waitReady(cs, NINETY_SECONDS);
JMXServiceURL addr = cs.getAddress();
return addr;
}
/*
* Creating command-line for running subprocess JVM:
*
* JVM command line is like:
* {test_jdk}/bin/java {defaultopts} -cp {test.class.path} {testopts} main
*
* {defaultopts} are the default java options set by the framework.
*
*/
private List<String> buildCommandLine() {
List<String> opts = new ArrayList<>();
opts.add(JDKToolFinder.getJDKTool("java"));
opts.addAll(Arrays.asList(jdk.testlibrary.Utils.getTestJavaOpts()));
// We need to set WEIRD_PARAM propertty on the client-side
opts.add("-DWEIRD_PARAM");
opts.add("-cp");
opts.add(System.getProperty("test.class.path", "test.class.path"));
opts.add(CLIENT_CLASS_MAIN);
return opts;
}
/**
* Runs MXBeanWeirdParamTest$ClientSide with the passed options and redirects
* subprocess standard I/O to the current (parent) process. This provides a
* trace of what happens in the subprocess while it is runnning (and before
* it terminates).
*
* @param serviceUrlStr string representing the JMX service Url to connect to.
*/
private int runClientSide(String serviceUrlStr) throws Exception {
// Building command-line
List<String> opts = buildCommandLine();
opts.add(serviceUrlStr);
// Launch separate JVM subprocess
int exitCode = 0;
String[] optsArray = opts.toArray(new String[0]);
ProcessBuilder pb = new ProcessBuilder(optsArray);
Process p = ProcessTools.startProcess("MXBeanWeirdParamTest$ClientSide", pb);
// Handling end of subprocess
try {
exitCode = p.waitFor();
if (exitCode != 0) {
System.out.println(
"Subprocess unexpected exit value of [" + exitCode +
"]. Expected 0.\n");
}
} catch (InterruptedException e) {
System.out.println("Parent process interrupted with exception : \n " + e + " :" );
// Parent thread unknown state, killing subprocess.
p.destroyForcibly();
throw new RuntimeException(
"Parent process interrupted with exception : \n " + e + " :" );
} finally {
return exitCode;
}
}
public void run(Map<String, Object> args) throws Exception {
System.out.println("MXBeanWeirdParamTest::run: Start") ;
int errorCount = 0;
try {
// Initialise the server side
JMXServiceURL urlToUse = createServerSide();
// Run client side
errorCount = runClientSide(urlToUse.toString());
if ( errorCount == 0 ) {
System.out.println("MXBeanWeirdParamTest::run: Done without any error") ;
} else {
System.out.println("MXBeanWeirdParamTest::run: Done with "
+ errorCount
+ " error(s)") ;
throw new RuntimeException("errorCount = " + errorCount);
}
cs.stop();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
private static class ClientSide {
public static void main(String args[]) throws Exception {
int errorCount = 0 ;
String msgTag = "ClientSide::main: ";
try {
// Get a connection to remote mbean server
JMXServiceURL addr = new JMXServiceURL(args[0]);
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println(msgTag + "Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
System.out.println(msgTag +"(OK) Got result of class "
+ result.getClass().getName());
System.out.println(msgTag +"Received CompositeData is " + result);
System.out.println(msgTag +"---- OK\n") ;
// ----
// We use the value returned by getAttribute to perform the invoke.
System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
mbsc.invoke(objName, "doWeird",
new Object[]{result},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
// We build the CompositeData ourselves that time.
System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
String typeName = "SqeParameter";
String[] itemNames = new String[] {"glop"};
OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
CompositeType rowType = new CompositeType(typeName, typeName,
itemNames, itemNames, openTypes);
Object[] itemValues = {"HECTOR"};
CompositeData data =
new CompositeDataSupport(rowType, itemNames, itemValues);
TabularType tabType = new TabularType(typeName, typeName,
rowType, new String[]{"glop"});
TabularDataSupport tds = new TabularDataSupport(tabType);
tds.put(data);
System.out.println(msgTag +"Source CompositeData is " + data);
mbsc.invoke(objName, "doWeird",
new Object[]{data},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println(msgTag +"---- OK\n") ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
throw new RuntimeException(e);
} finally {
System.exit(errorCount);
}
}
}
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.management.DescriptorKey;
/**
* That annotation is usable everywhere DescriptorKey is (and even more).
* It is for use to test that you can retrieve the SqeDescriptorKey into the
* appropriate Descriptor instances as built by the JMX runtime.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface SqeDescriptorKey {
@DescriptorKey("sqeDescriptorKey")
String value();
// List descriptor fields that may be added or may be updated
// when retrieving an MBeanInfo using a JMXWS connection compared to the
// MBeanInfo returned by a local MBeanServer.
// The annotation format is :
// <descriptorFieldName>=<descriptorFieldValue>
// The values actually handled by the test suite are :
// openType=SimpleType.VOID
@DescriptorKey("descriptorFields")
String[] descriptorFields() default {};
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import javax.management.Notification;
/**
* Could hold someday a specific semantic.
* For now it is used to have a Notification which of another class, no more.
*/
public class SqeNotification extends Notification {
/** Creates a new instance of SqeNotification */
public SqeNotification(String type, Object source, long sequenceNumber) {
super(type, source, sequenceNumber);
}
/** Creates a new instance of SqeNotification */
public SqeNotification(String type, Object source, long sequenceNumber,
long timeStamp) {
super(type, source, sequenceNumber, timeStamp);
}
/** Creates a new instance of SqeNotification */
public SqeNotification(String type, Object source, long sequenceNumber,
long timeStamp, String message) {
super(type, source, sequenceNumber, timeStamp, message);
}
/** Creates a new instance of SqeNotification */
public SqeNotification(String type, Object source, long sequenceNumber,
String message) {
super(type, source, sequenceNumber, message);
}
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import java.io.Serializable;
/**
* That class is to use as an MBean operation parameter or returned value.
* The property Glop with its public getter + setter is only there to be
* reconstructible following MXBean specification, so that SqeParameter can be
* used for what it is designed to.
*/
public class SqeParameter implements Serializable {
private static boolean weird;
private String glop;
static {
if ( System.getProperty("WEIRD_PARAM") != null ) {
weird = true;
}
}
/**
* Creates a new instance of SqeParameter.
* <br>When the Java property WEIRD_PARAM is set, that constructor
* throws an exception.
* <br>That can be used to ensure the class is instantiated on server side
* but never on client side.
*/
public SqeParameter() throws Exception {
if ( weird ) {
throw new Exception();
}
}
public String getGlop() {
return glop;
}
public void setGlop(String value) {
glop = value;
}
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import java.lang.reflect.Method;
import javax.management.remote.JMXConnectorServerMBean;
// utility class for MXBean* tests coming from JMX Tonga test suite
class Utils {
// DEBUG is printed depending on the DEBUG and DEBUG_LEVEL JAVA property
private static final String DEBUG_HEADER = "[debug] ";
// DEBUG levels
private static int selectedDebugLevel = 0;
static final int DEBUG_STANDARD = 1;
static final int DEBUG_VERBOSE = 2; // Mainly used for stress tests
static final int DEBUG_ALL = DEBUG_STANDARD | DEBUG_VERBOSE;
static void parseDebugProperties() {
int level = 0;
Properties p = System.getProperties();
// get selected levels
if (p.getProperty("DEBUG_STANDARD") != null) {
level |= DEBUG_STANDARD;
}
if (p.getProperty("DEBUG_VERBOSE") != null) {
level |= DEBUG_VERBOSE;
}
if (p.getProperty("DEBUG_ALL") != null) {
level |= DEBUG_ALL;
}
selectedDebugLevel = level;
}
/**
* Reproduces the original parsing and collection of test parameters
* from the DTonga JMX test suite.
*
* Collects passed args and returns them in a map(argname, value) structure,
* which will be then propagated as necessary to various called methods.
*/
static Map<String, Object> parseParameters(String args[])
throws Exception {
Utils.debug(DEBUG_STANDARD, "TestRoot::parseParameters: Start");
HashMap<String, Object> map = new HashMap<>();
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].trim().startsWith("-") ) {
if ((i+1) < args.length && !args[i+1].startsWith("-") ) {
Utils.debug(DEBUG_STANDARD,
"TestRoot::parseParameters: added in map = " +
args[i] +
" with value " +
args[i+1]) ;
map.put(args[i].trim(), args[i+1].trim()) ;
} else if ((i+1) < args.length && args[i+1].startsWith("-") ||
(i+1) == args.length ) {
Utils.debug(DEBUG_STANDARD,
"TestRoot::parseParameters: added in map = " +
args[i] +
" with null value") ;
map.put(args[i].trim(), null) ;
} else {
System.out.println(
"TestRoot::parseParameters: (WARNING) not added in map = " +
args[i]) ;
}
}
}
Utils.debug(DEBUG_STANDARD, "TestRoot::parseParameters: Done") ;
return map ;
}
/**
* This method is to be used in all tests to print anything
* that is temporary.
* Printing is done only when debug is activated by the property DEBUG.
* Printing depends also on the DEBUG_LEVEL property.
* Here it encapsulates a System.out.println.
*/
public static void debug(int level, String line) {
if ((selectedDebugLevel & level) != 0) {
System.out.println(DEBUG_HEADER + line);
}
}
/**
* Do print stack trace when withStack is true.
* Does try to call getTargetException() and getTargetError() then
* print embedded stacks in the case of an Exception wrapping
* another Exception or an Error. Recurse until no more wrapping
* is found.
*/
public static void printThrowable(Throwable theThro, boolean withStack) {
try {
if (withStack) {
theThro.printStackTrace(System.out);
}
if (theThro instanceof Exception) {
Exception t = (Exception) theThro;
Method target = null;
String blank = " ";
try {
target = t.getClass().getMethod("getTargetException",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetException method could be there or not
}
System.out.println(blank + t.getClass() + "==>" + t.getMessage());
while (target != null) {
try {
t = (Exception) target.invoke(t,
(java.lang.Object[]) null);
} catch (Exception ee) {
t = null;
}
try {
if (t != null) {
blank = blank + " ";
System.out.println(blank + t.getClass() + "==>" +
t.getMessage());
try {
target =
t.getClass().getMethod("getTargetException",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetException method could be there or not }
}
} else {
target = null;
}
} catch (Exception ee) {
target = null;
}
}
// We may have exceptions wrapping an Error then it is
// getTargetError that is likely to be called
try {
target = ((Exception) theThro).getClass().getMethod("getTargetError",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetError method could be there or not
}
Throwable err = theThro;
while (target != null) {
try {
err = (Error) target.invoke(err,
(java.lang.Object[]) null);
} catch (Exception ee) {
err = null;
}
try {
if (err != null) {
blank = blank + " ";
System.out.println(blank + err.getClass() + "==>" +
err.getMessage());
if (withStack) {
err.printStackTrace(System.out);
}
try {
target = err.getClass().getMethod("getTargetError",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetError method could be there or not
}
} else {
target = null;
}
} catch (Exception ee) {
target = null;
}
}
} else {
System.out.println("Throwable is : " + theThro);
}
} catch (Throwable x) {
System.out.println("Exception : raised in printException : " + x);
}
}
/**
* Wait up to maxTimeInSeconds second(s) the given JMX connector server
* comes up (which means isActive returns true).
* If it fails to do so we throw a RunTime exception.
*/
public static void waitReady(JMXConnectorServerMBean server,
int maxTimeInSeconds) throws Exception {
int elapsed = 0;
while (!server.isActive() && elapsed < maxTimeInSeconds) {
Thread.sleep(1000);
elapsed++;
}
if (server.isActive()) {
String message = "Utils::waitReady: JMX connector server came up";
if ( elapsed == 0) {
message += " immediately";
} else {
message += " after " + elapsed + " seconds";
}
message += " [" + server.getAddress() + "]";
Utils.debug(DEBUG_STANDARD, message);
} else {
String message = "Utils::waitReady: (ERROR) JMX connector" +
" server didn't come up after " + elapsed + " seconds [" +
server.getAddress() + "]";
System.out.println(message);
throw new RuntimeException(message);
}
}
}
/*
* Copyright (c) 2006, 2015, 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.
*/
public abstract class QueryData {
protected int intValue = 9;
protected long longValue = 9L;
protected Integer integerValue = Integer.valueOf(9);
protected boolean booleanValue = true;
protected double doubleValue = 9D;
protected float floatValue = 9.0F;
protected String stringValue = "9";
}
/*
* Copyright (c) 2006, 2015, 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.
*/
import java.util.ArrayList;
import javax.management.Query;
import javax.management.QueryExp;
import javax.management.ValueExp;
/**
* Class used for building QueryExp instances of all every possible type
* in terms of JMX API members; note that several JMX classes are private
* and appears in the JDK API only by their serial form.
* Comments in each case of the big switch in method getQuery() details which
* API member we cover with a given query.
*/
public class QueryFactory extends QueryData {
private String mbeanClassName = "";
private String primitiveIntAttName = "IntAtt";
private String primitiveLongAttName = "LongAtt";
private String integerAttName = "IntegerAtt";
private String primitiveBooleanAttName = "BooleanAtt";
private String primitiveDoubleAttName = "DoubleAtt";
private String primitiveFloatAttName = "FloatAtt";
private String stringAttName = "StringAtt";
private ArrayList<QueryExp> queries = new ArrayList<QueryExp>();
/**
* Creates a new instance of QueryFactory.
* The name is the fully qualified class name of an MBean.
* There is severe constraints on that MBean that must:
* <ul>
* <li>extend QueryData in order to inherit attribute values.
* <li>define a RW attribute IntAtt of type int
* initialized to QueryData.longValue
* <li>define a RW attribute LongAtt of type long
* initialized to QueryData.intValue
* <li>define a RW attribute IntegerAtt of type Integer
* initialized to QueryData.integerValue
* <li>define a RW attribute BooleanAtt of type boolean
* initialized to QueryData.booleanValue
* <li>define a RW attribute DoubleAtt of type double
* initialized to QueryData.doubleValue
* <li>define a RW attribute FloatAtt of type float
* initialized to QueryData.floatValue
* <li>define a RW attribute StringAtt of type String
* initialized to QueryData.stringValue
* </ul>
*/
public QueryFactory(String name) {
this.mbeanClassName = name;
}
/**
* Returns the highest index value the method getQuery supports.
* WARNING : returns 0 if buildQueries haven't been called first !
*/
public int getSize() {
return queries.size();
}
/**
* Populates an ArrayList of QueryExp.
* Lowest index is 1.
* Highest index is returned by getSize().
* <br>The queries numbered 1 to 23 allow to cover all the underlying
* Java classes of the JMX API used to build queries.
*/
public void buildQueries() {
if ( queries.size() == 0 ) {
int smallerIntValue = intValue - 1;
int biggerIntValue = intValue + 1;
// case 1:
// True if the MBean is of class mbeanClassName
// We cover javax.management.InstanceOfQueryExp
queries.add(Query.isInstanceOf(Query.value(mbeanClassName)));
// case 2:
// True if the MBean is of class mbeanClassName
// We cover javax.management.MatchQueryExp and
// javax.management.ClassAttributeValueExp
queries.add(Query.match(Query.classattr(),
Query.value(mbeanClassName)));
// case 3:
// True if an attribute named primitiveIntAttName of type int has
// the value intValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.NumericValueExp
queries.add(Query.eq(Query.attr(primitiveIntAttName),
Query.value(intValue)));
// case 4:
// True if an attribute named primitiveLongAttName of type long has
// the value longValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.NumericValueExp
queries.add(Query.eq(Query.attr(primitiveLongAttName),
Query.value(longValue)));
// case 5:
// True if an attribute named primitiveDoubleAttName of type double
// has the value doubleValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.NumericValueExp
queries.add(Query.eq(Query.attr(primitiveDoubleAttName),
Query.value(doubleValue)));
// case 6:
// True if an attribute named primitiveFloatAttName of type float
// has the value floatValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.NumericValueExp
queries.add(Query.eq(Query.attr(primitiveFloatAttName),
Query.value(floatValue)));
// case 7:
// True if an attribute named primitiveIntAttName of type int is
// hold by an MBean of class mbeanClassName and has
// the value intValue
// We cover javax.management.QualifiedAttributeValueExp
queries.add(Query.eq(Query.attr(mbeanClassName, primitiveIntAttName),
Query.value(intValue)));
// case 8:
// True if an attribute named stringAttName of type String has
// the value stringValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.StringValueExp
queries.add(Query.eq(Query.attr(stringAttName),
Query.value(stringValue)));
// case 9:
// True if an attribute named integerAttName of type Integer has
// the value integerValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.NumericValueExp
queries.add(Query.eq(Query.attr(integerAttName),
Query.value(integerValue)));
// case 10:
// True if an attribute named primitiveBooleanAttName of type boolean
// has the value booleanValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to EQ and javax.management.BooleanValueExp
queries.add(Query.eq(Query.attr(primitiveBooleanAttName),
Query.value(booleanValue)));
// case 11:
// True if an attribute named primitiveIntAttName of type int has
// not the value smallerIntValue
// We cover javax.management.NotQueryExp
queries.add(Query.not(Query.eq(Query.attr(primitiveIntAttName),
Query.value(smallerIntValue))));
// case 12:
// True if either
// an attribute named primitiveIntAttName of type int has
// the value intValue
// or
// an attribute named primitiveLongAttName of type long has
// the value longValue
// We cover javax.management.OrQueryExp
queries.add(Query.or(
Query.eq(Query.attr(primitiveIntAttName),
Query.value(intValue)),
Query.eq(Query.attr(primitiveLongAttName),
Query.value(longValue))));
// case 13:
// True if
// an attribute named primitiveIntAttName of type int has
// the value intValue
// and
// an attribute named primitiveLongAttName of type long has
// the value longValue
// We cover javax.management.AndQueryExp
queries.add(Query.and(
Query.eq(Query.attr(primitiveIntAttName),
Query.value(intValue)),
Query.eq(Query.attr(primitiveLongAttName),
Query.value(longValue))));
// case 14:
// True if an attribute named primitiveIntAttName of type int has
// the value intValue
// We cover javax.management.InQueryExp
ValueExp[] inArray = {Query.value(intValue)};
queries.add(Query.in(Query.attr(primitiveIntAttName), inArray));
// case 15:
// True if an attribute named primitiveIntAttName of type int has
// its value in between smallerIntValue and biggerIntValue
// We cover javax.management.BetweenRelQueryExp
queries.add(Query.between(Query.attr(primitiveIntAttName),
Query.value(smallerIntValue),
Query.value(biggerIntValue)));
// case 16:
// True if an attribute named primitiveIntAttName of type int has
// a value greater than smallerIntValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to GT
queries.add(Query.gt(Query.attr(primitiveIntAttName),
Query.value(smallerIntValue)));
// case 17:
// True if an attribute named primitiveIntAttName of type int has
// a value greater or equal to smallerIntValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to GE
queries.add(Query.geq(Query.attr(primitiveIntAttName),
Query.value(smallerIntValue)));
// case 18:
// True if an attribute named primitiveIntAttName of type int has
// a value smaller than biggerIntValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to LT
queries.add(Query.lt(Query.attr(primitiveIntAttName),
Query.value(biggerIntValue)));
// case 19:
// True if an attribute named primitiveIntAttName of type int has
// a value smaller or equal to biggerIntValue
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to LE
queries.add(Query.leq(Query.attr(primitiveIntAttName),
Query.value(biggerIntValue)));
// case 20:
// True if an attribute named primitiveIntAttName of type int has
// a value equal to intValue minus zero
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to MINUS
queries.add(Query.eq(Query.attr(primitiveIntAttName),
Query.minus(Query.value(intValue), Query.value(0))));
// case 21:
// True if an attribute named primitiveIntAttName of type int has
// a value equal to intValue plus zero
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to PLUS
queries.add(Query.eq(Query.attr(primitiveIntAttName),
Query.plus(Query.value(intValue), Query.value(0))));
// case 22:
// True if an attribute named primitiveIntAttName of type int has
// a value equal to intValue divided by one
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to DIV
queries.add(Query.eq(Query.attr(primitiveIntAttName),
Query.div(Query.value(intValue), Query.value(1))));
// case 23:
// True if an attribute named primitiveIntAttName of type int has
// a value equal to intValue multiplicated by one
// We cover javax.management.BinaryRelQueryExp with
// a relOp equal to TIMES
queries.add(Query.eq(Query.attr(primitiveIntAttName),
Query.times(Query.value(intValue), Query.value(1))));
// case 24:
// That query is a complex one that combines within a big AND
// queries with index 2 to 23 inclusive. But because a List is
// zero based, we must decrement all indexes by 1 when retrieving
// any previously stored query.
QueryExp q2_3 = Query.and(queries.get(2-1), queries.get(3-1));
QueryExp q4_5 = Query.and(queries.get(4-1), queries.get(5-1));
QueryExp q6_7 = Query.and(queries.get(6-1), queries.get(7-1));
QueryExp q8_9 = Query.and(queries.get(8-1), queries.get(9-1));
QueryExp q10_11 = Query.and(queries.get(10-1), queries.get(11-1));
QueryExp q12_13 = Query.and(queries.get(12-1), queries.get(13-1));
QueryExp q14_15 = Query.and(queries.get(14-1), queries.get(15-1));
QueryExp q16_17 = Query.and(queries.get(16-1), queries.get(17-1));
QueryExp q18_19 = Query.and(queries.get(18-1), queries.get(19-1));
QueryExp q20_21 = Query.and(queries.get(20-1), queries.get(21-1));
QueryExp q22_23 = Query.and(queries.get(22-1), queries.get(23-1));
QueryExp q2_5 = Query.and(q2_3, q4_5);
QueryExp q6_9 = Query.and(q6_7, q8_9);
QueryExp q10_13 = Query.and(q10_11, q12_13);
QueryExp q14_17 = Query.and(q14_15, q16_17);
QueryExp q18_21 = Query.and(q18_19, q20_21);
QueryExp q2_9 = Query.and(q2_5, q6_9);
QueryExp q10_17 = Query.and(q10_13, q14_17);
QueryExp q18_23 = Query.and(q18_21, q22_23);
QueryExp q2_17 = Query.and(q2_9, q10_17);
queries.add(Query.and(q2_17, q18_23));
// case 25:
// Complex query mixing AND and OR.
queries.add(Query.or(q6_9, q18_23));
}
}
/**
* Returns a QueryExp taken is the ArrayList populated by buildQueries().
* Lowest index is 1.
* Highest index is returned by getSize().
* <br>The queries numbered 1 to 23 allow to cover all the underlying
* Java classes of the JMX API used to build queries.
*/
public QueryExp getQuery(int index) {
return queries.get(index - 1);
}
}
/*
* Copyright (c) 2004, 2015, 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.
*/
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.management.remote.JMXServiceURL ;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.StandardMBean;
/**
* This class defines an MBean that can be registered and used on client side
* to handle informations or properties of the remote server.
*
* For example, this MBean can store IOR addresses
* of RMI/IIOP connector(s) used in a test.
*
* That MBean might not be used for testing purpose itself.
*/
public class ServerDelegate implements ServerDelegateMBean, MBeanRegistration {
private MBeanServer mbeanServer = null;
private List<JMXServiceURL> addresses = null;
private String port;
private static String javaVersion = System.getProperty("java.version");
private int sqeJmxwsCredentialsProviderCallCount = 0;
private String jmxwsCredentialsProviderUrl = null;
private int testJMXAuthenticatorCallCount = 0;
private Principal testJMXAuthenticatorPrincipal = null;
@SqeDescriptorKey("NO PARAMETER CONSTRUCTOR ServerDelegate")
public ServerDelegate() {
addresses = new ArrayList<JMXServiceURL>();
}
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
// Initialize MBeanServer attribute
mbeanServer = server;
return name;
}
public void postRegister(Boolean registrationDone) {
}
public void preDeregister() throws Exception {
}
public void postDeregister() {
}
public void addAddress(JMXServiceURL url) {
addresses.add(url) ;
}
public List<JMXServiceURL> getAddresses() {
return addresses ;
}
public void setPort(String p) {
port = p ;
}
public String getPort() {
return port ;
}
public String getJavaVersion() {
return javaVersion;
}
public void sqeJmxwsCredentialsProviderCalled() {
sqeJmxwsCredentialsProviderCallCount++;
}
public int getSqeJmxwsCredentialsProviderCallCount() {
return sqeJmxwsCredentialsProviderCallCount;
}
public void setJmxwsCredentialsProviderUrl(String url) {
jmxwsCredentialsProviderUrl = url;
}
public String getJmxwsCredentialsProviderUrl() {
return jmxwsCredentialsProviderUrl;
}
public void testJMXAuthenticatorCalled() {
testJMXAuthenticatorCallCount++;
}
public int getTestJMXAuthenticatorCallCount() {
return testJMXAuthenticatorCallCount;
}
public void setTestJMXAuthenticatorPrincipal(Principal principal) {
testJMXAuthenticatorPrincipal = principal;
}
public String getTestJMXAuthenticatorPrincipalString() {
if ( testJMXAuthenticatorPrincipal != null ) {
return testJMXAuthenticatorPrincipal.toString();
}
return null;
}
/**
* Instantiates and registers a StandardMBean in the MBean server.
*
* @param implementationClassName
* The implementation class name of the MBean.
* @param interfaceClassName
* The management interface class name of the MBean.
* @param isMXBean
* If true, the resultant MBean is an MXBean.
* @param name
* The object name of the StandardMBean.
*/
@SuppressWarnings("unchecked")
public void createStandardMBean(
String implementationClassName,
String interfaceClassName,
boolean isMXBean,
ObjectName name)
throws Exception {
Object implementation =
Class.forName(implementationClassName).newInstance();
Class<Object> interfaceClass = interfaceClassName == null ? null :
(Class<Object>)Class.forName(interfaceClassName);
// Create the StandardMBean
StandardMBean standardMBean = new StandardMBean(
implementation,
interfaceClass,
isMXBean);
// Register the StandardMBean
mbeanServer.registerMBean(standardMBean, name);
}
/**
* Instantiates and registers a StandardMBean in the MBean server.
* The object will use standard JMX design pattern to determine
* the management interface associated with the given implementation.
*/
@SuppressWarnings("unchecked")
public void createStandardMBean(
String implementationClassName,
boolean isMXBean,
ObjectName name)
throws Exception {
createStandardMBean(implementationClassName, null, isMXBean, name);
}
}
/*
* Copyright (c) 2004, 2015, 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.
*/
import java.security.Principal;
import java.util.List;
import javax.management.remote.JMXServiceURL ;
import javax.management.ObjectName;
@SqeDescriptorKey("INTERFACE ServerDelegateMBean")
public interface ServerDelegateMBean {
@SqeDescriptorKey("ATTRIBUTE Address")
public void addAddress(JMXServiceURL url);
@SqeDescriptorKey("ATTRIBUTE Address")
public List<JMXServiceURL> getAddresses();
public String getPort();
public void setPort(String p);
public String getJavaVersion();
public void sqeJmxwsCredentialsProviderCalled();
public int getSqeJmxwsCredentialsProviderCallCount();
public void setJmxwsCredentialsProviderUrl(String url);
public String getJmxwsCredentialsProviderUrl();
public void testJMXAuthenticatorCalled();
public int getTestJMXAuthenticatorCallCount();
public void setTestJMXAuthenticatorPrincipal(Principal principal);
public String getTestJMXAuthenticatorPrincipalString();
public void createStandardMBean(
String implementationClassName,
String interfaceClassName,
boolean isMXBean,
ObjectName name)
throws Exception;
public void createStandardMBean(
String implementationClassName,
boolean isMXBean,
ObjectName name)
throws Exception;
}
/*
* Copyright (c) 2005, 2015, 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.
*/
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.management.DescriptorKey;
/**
* That annotation is usable everywhere DescriptorKey is (and even more).
* It is for use to test that you can retrieve the SqeDescriptorKey into the
* appropriate Descriptor instances as built by the JMX runtime.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface SqeDescriptorKey {
@DescriptorKey("sqeDescriptorKey")
String value();
// List descriptor fields that may be added or may be updated
// when retrieving an MBeanInfo using a JMXWS connection compared to the
// MBeanInfo returned by a local MBeanServer.
// The annotation format is :
// <descriptorFieldName>=<descriptorFieldValue>
// The values actually handled by the test suite are :
// openType=SimpleType.VOID
@DescriptorKey("descriptorFields")
String[] descriptorFields() default {};
}
/*
* Copyright (c) 2006, 2015, 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.
*/
/*
* @test
* @bug 8058865
* @summary Tests most of the existing query types.
* @author Olivier Lagneau
* @modules java.management
* @compile TestQuery.java
* @run main/othervm/timeout=300 -DDEBUG_STANDARD SupportedQueryTypesTest -mbeanClassName TestQuery
*/
import java.util.Map ;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.lang.reflect.Method;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName ;
import javax.management.QueryExp;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class SupportedQueryTypesTest {
protected String mbeanClassName = null;
private MBeanServerConnection mbsc = null;
/*
* First Debug properties and arguments are collect in expected
* map (argName, value) format, then calls original test's run method.
*/
public static void main(String args[]) throws Exception {
System.out.println("=================================================");
// Parses parameters
Utils.parseDebugProperties();
Map<String, Object> map = Utils.parseParameters(args) ;
// Run test
SupportedQueryTypesTest test = new SupportedQueryTypesTest();
test.run(map);
}
public void run(Map<String, Object> args) {
int errorCount = 0;
ObjectName on = null;
ObjectName serverDelegateObjectName = null;
JMXConnectorServer cs = null;
JMXConnector cc = null;
System.out.println("SupportedQueryTypesTest::run: Start") ;
try {
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
cc = JMXConnectorFactory.connect(addr);
mbsc = cc.getMBeanServerConnection();
// Create and register the ServerDelegate MBean on the remote MBeanServer
String serverDelegateClassName = ServerDelegate.class.getName();
serverDelegateObjectName =
new ObjectName("defaultDomain:class=" + serverDelegateClassName);
mbsc.createMBean(serverDelegateClassName, serverDelegateObjectName);
// Retrieve the MBean class name
mbeanClassName = (String) args.get("-mbeanClassName") ;
on = new ObjectName("defaultDomain:class=" + mbeanClassName);
// Create and register the MBean on the remote MBeanServer
System.out.println("SupportedQueryTypesTest::run: CREATE " +
mbeanClassName + " on the remote MBeanServer with name "
+ on);
mbsc.createMBean(mbeanClassName, on);
// Create a QueryFactory and setup which query we'll use.
QueryFactory queries = new QueryFactory(mbeanClassName);
queries.buildQueries();
int maxIndex = queries.getSize();
int minIndex = 1;
// Create a reference Set<ObjectName> to check later on
// the queryNames() results
Set<ObjectName> referenceNameSet = new HashSet<ObjectName>();
referenceNameSet.add(on);
// Create a reference Set<ObjectInstance> to check later on
// the queryMBeans() results
ObjectInstance oi = new ObjectInstance(on, mbeanClassName);
Set<ObjectInstance> referenceInstanceSet =
new HashSet<ObjectInstance>();
referenceInstanceSet.add(oi);
// Perform the queryNames and queryMBeans requests
for (int i = minIndex; i <= maxIndex; i++ ) {
QueryExp query = queries.getQuery(i);
System.out.println("----");
System.out.println("SupportedQueryTypesTest::run: Query # " + i);
System.out.println("query " + query);
errorCount +=
doQueryNames(query, referenceNameSet);
errorCount +=
doQueryMBeans(query, referenceInstanceSet);
}
} catch(Exception e) {
Utils.printThrowable(e, true);
errorCount++;
} finally {
// Do unregister the MBean
try {
if (mbsc.isRegistered(on)) {
mbsc.unregisterMBean(on);
}
if (mbsc.isRegistered(serverDelegateObjectName)) {
mbsc.unregisterMBean(serverDelegateObjectName);
}
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
}
try {
// Close JMX Connector Client
cc.close();
// Stop connertor server
cs.stop();
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
}
}
System.out.println("");
System.out.println("SupportedQueryTypesTest::run: Done") ;
// Handle result
if (errorCount == 0) {
System.out.println("SupportedQueryTypesTest::run: (OK)");
} else {
String message = "SupportedQueryTypesTest::run: (ERROR) Got " +
+ errorCount + " error(s)";
System.out.println(message);
throw new RuntimeException(message);
}
}
private int doQueryNames(QueryExp query, Set<ObjectName> referenceSet) {
int errorCount = 0;
System.out.println(" <*> Perform queryNames call ");
try {
// Call queryNames on the remote MBeanServer
Set<ObjectName> remoteSet = mbsc.queryNames(null, query);
// Compare the 2 Set<ObjectName>
errorCount += checkSet(remoteSet, referenceSet);
// Cleaning
remoteSet.clear();
} catch (Exception e) {
Utils.printThrowable(e, true);
errorCount++;
}
if ( errorCount == 0 ) {
System.out.println("\t(OK)");
} else {
System.out.println("\t(ERROR) Query failed");
}
return errorCount;
}
private int doQueryMBeans(QueryExp query, Set<ObjectInstance> referenceSet) {
int errorCount = 0;
System.out.println(" <*> Perform queryMBeans call ");
try {
// Call queryMBeans on the remote MBeanServer
Set<ObjectInstance> remoteSet = mbsc.queryMBeans(null, query);
// Compare the 2 Set<ObjectInstance>
errorCount += checkSet(remoteSet, referenceSet);
// Cleaning
remoteSet.clear();
} catch (Exception e) {
Utils.printThrowable(e, true);
errorCount++;
}
if ( errorCount == 0 ) {
System.out.println("\t(OK)");
} else {
System.out.println("\t(ERROR) Query failed");
}
return errorCount;
}
/**
* Pretty print of a Set content.
* When the Set isn't empty, toString() is called on each element.
* <br>The variable's name used to hold that Set is given via the setName
* parameter and used in the output.
*/
private static void printSet(Set<?> printableSet, String setName) {
if ( printableSet.size() == 0 ) {
System.out.println("The Set " + setName + " is empty");
} else {
System.out.println("The Set " + setName + " contains :");
for (Iterator<?> it = printableSet.iterator(); it.hasNext();) {
Object elem = it.next();
System.out.println("\t" + elem.toString());
}
}
}
/**
* This method check the Set remoteSet is equal to
* the reference Set referenceSet,
* which means same size and content (order doesn't matter).
* <br>It returns 0 when the check is fine, otherwise 1.
*/
private int checkSet(Set<?> remoteSet, Set<?> referenceSet) {
if ( ! remoteSet.equals(referenceSet) ) {
System.out.println("SupportedQueryTypesTest::checkSet:"
+ " (ERROR) Set aren't as expected");
printSet(remoteSet, "remoteSet");
printSet(referenceSet, "referenceSet");
return 1;
} else {
return 0;
}
}
// Utility inner class coming from JMX Tonga test suite.
private static class Utils {
// DEBUG is printed depending on the DEBUG and DEBUG_LEVEL JAVA property
static final String DEBUG_HEADER = "[debug] ";
// DEBUG levels
static int selectedDebugLevel = 0;
static final int DEBUG_STANDARD = 1;
static final int DEBUG_VERBOSE = 2; // Mainly used for stress tests
static final int DEBUG_ALL = DEBUG_STANDARD | DEBUG_VERBOSE;
static void parseDebugProperties() {
int level = 0;
Properties p = System.getProperties();
// get selected levels
if (p.getProperty("DEBUG_STANDARD") != null) {
level |= DEBUG_STANDARD;
}
if (p.getProperty("DEBUG_VERBOSE") != null) {
level |= DEBUG_VERBOSE;
}
if (p.getProperty("DEBUG_ALL") != null) {
level |= DEBUG_ALL;
}
selectedDebugLevel = level;
}
/**
* Reproduces the original parsing and collection of test parameters
* from the DTonga JMX test suite.
*
* Collects passed args and returns them in a map(argname, value) structure,
* which will be then propagated as necessary to various called methods.
*/
static Map<String, Object> parseParameters(String args[])
throws Exception {
debug(DEBUG_STANDARD, "TestRoot::parseParameters: Start");
HashMap<String, Object> map = new HashMap<>();
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].trim().startsWith("-") ) {
if ((i+1) < args.length && !args[i+1].startsWith("-") ) {
debug(DEBUG_STANDARD,
"TestRoot::parseParameters: added in map = " +
args[i] +
" with value " +
args[i+1]) ;
map.put(args[i].trim(), args[i+1].trim()) ;
} else if ((i+1) < args.length && args[i+1].startsWith("-") ||
(i+1) == args.length ) {
debug(DEBUG_STANDARD,
"TestRoot::parseParameters: added in map = " +
args[i] +
" with null value") ;
map.put(args[i].trim(), null) ;
} else {
System.out.println(
"TestRoot::parseParameters: (WARNING) not added in map = " +
args[i]) ;
}
}
}
debug(DEBUG_STANDARD, "TestRoot::parseParameters: Done") ;
return map ;
}
/**
* This method is to be used in all tests to print anything
* that is temporary.
* Printing is done only when debug is activated by the property DEBUG.
* Printing depends also on the DEBUG_LEVEL property.
* Here it encapsulates a System.out.println.
*/
static void debug(int level, String line) {
if ((selectedDebugLevel & level) != 0) {
System.out.println(DEBUG_HEADER + line);
}
}
/**
* Do print stack trace when withStack is true.
* Does try to call getTargetException() and getTargetError() then
* print embedded stacks in the case of an Exception wrapping
* another Exception or an Error. Recurse until no more wrapping
* is found.
*/
static void printThrowable(Throwable theThro, boolean withStack) {
try {
if (withStack) {
theThro.printStackTrace(System.out);
}
if (theThro instanceof Exception) {
Exception t = (Exception) theThro;
Method target = null;
String blank = " ";
try {
target = t.getClass().getMethod("getTargetException",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetException method could be there or not
}
System.out.println(blank + t.getClass() + "==>" + t.getMessage());
while (target != null) {
try {
t = (Exception) target.invoke(t,
(java.lang.Object[]) null);
} catch (Exception ee) {
t = null;
}
try {
if (t != null) {
blank = blank + " ";
System.out.println(blank + t.getClass() + "==>" +
t.getMessage());
try {
target =
t.getClass().getMethod("getTargetException",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetException method could be there or not }
}
} else {
target = null;
}
} catch (Exception ee) {
target = null;
}
}
// We may have exceptions wrapping an Error then it is
// getTargetError that is likely to be called
try {
target = ((Exception) theThro).getClass().getMethod("getTargetError",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetError method could be there or not
}
Throwable err = theThro;
while (target != null) {
try {
err = (Error) target.invoke(err,
(java.lang.Object[]) null);
} catch (Exception ee) {
err = null;
}
try {
if (err != null) {
blank = blank + " ";
System.out.println(blank + err.getClass() + "==>" +
err.getMessage());
if (withStack) {
err.printStackTrace(System.out);
}
try {
target = err.getClass().getMethod("getTargetError",
(java.lang.Class<?>[]) null);
} catch (Exception ee) {
// OK: getTargetError method could be there or not
}
} else {
target = null;
}
} catch (Exception ee) {
target = null;
}
}
} else {
System.out.println("Throwable is : " + theThro);
}
} catch (Throwable x) {
System.out.println("Exception : raised in printException : " + x);
}
}
}
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
/*
* Copyright (c) 2003, 2015, 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.
*/
import java.io.Serializable ;
/**
* That class is used to modelize a parameter to be used as MBean property
* value or MBean operation parameter or returned value.
*/
public class RjmxMBeanParameter implements Serializable {
public String name = "unset" ;
public RjmxMBeanParameter() {
}
public RjmxMBeanParameter(String name) {
this.name = name ;
}
public boolean equals(Object obj) {
if ( this.name.equals(((RjmxMBeanParameter)obj).name) ) {
return true ;
} else {
return false ;
}
}
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
# Access control file for SQE tests.
# Default username
SQE_username readwrite create Simple
# Functional authorization tests
username1 readwrite create Simple
username2 readonly
username3 readonly
username4 readwrite create Simple
username5 readwrite create Simple
PasswordFileAuthentication {
com.sun.jmx.remote.security.FileLoginModule required
passwordFile="${password.file}";
};
SampleLoginModule {
TestSampleLoginModule required;
};
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册