1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/*
* Copyright (c) 2016, Red Hat Inc.
* 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.IOException;
import java.lang.management.ManagementFactory;
import java.net.ServerSocket;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
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;
/**
* @test
* @bug 8147857
* @summary Tests whether RMIConnector logs attribute names correctly.
* @author Severin Gehwolf
*
* @modules java.logging
* java.management.rmi
*/
public class RMIConnectorLogAttributesTest {
private static final String ILLEGAL = ", FirstName[LastName]";
private static final Logger logger = Logger.getLogger("javax.management.remote.rmi");
private static final String ANY_NAME = "foo";
private static final TestLogHandler handler;
static {
handler = new TestLogHandler(ILLEGAL);
handler.setLevel(Level.FINEST);
logger.setLevel(Level.ALL);
logger.addHandler(handler);
}
private JMXConnectorServer startServer(int rmiPort) throws Exception {
System.out.println("DEBUG: Create RMI registry on port " + rmiPort);
LocateRegistry.createRegistry(rmiPort);
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
HashMap<String,Object> env = new HashMap<String,Object>();
JMXServiceURL url =
new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:" + rmiPort + "/jmxrmi");
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
cs.start();
System.out.println("DEBUG: Started the RMI connector server");
return cs;
}
private int findPort() {
for (int i = 13333; i < 13333 + 100; i++) {
try {
ServerSocket socket = new ServerSocket(i);
socket.close();
return i;
} catch (IOException e) {
continue;
}
}
return -1;
}
private void runTest() {
int rmiPort = findPort();
if (rmiPort == -1) {
throw new RuntimeException("Test failed. No available port");
}
JMXConnectorServer server = null;
try {
server = startServer(rmiPort);
JMXConnector connector = connectToServer(server);
doTest(connector);
} catch (Exception e) {
throw new RuntimeException("Test failed unexpectedly", e);
} finally {
if (server != null) {
try {
server.stop();
} catch (IOException e) {
// ignore
}
}
}
}
private JMXConnector connectToServer(JMXConnectorServer server) throws IOException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, ReflectionException, MBeanException {
JMXServiceURL url = server.getAddress();
Map<String, Object> env = new HashMap<String, Object>();
JMXConnector connector = JMXConnectorFactory.connect(url, env);
System.out.println("DEBUG: Client connected to RMI at: " + url);
return connector;
}
private void doTest(JMXConnector connector) throws IOException,
MalformedObjectNameException, ReflectionException,
InstanceAlreadyExistsException, MBeanRegistrationException,
MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
System.out.println("DEBUG: Calling createMBean");
mbsc.createMBean(Name.class.getName(), objName);
System.out.println("DEBUG: Calling setAttributes");
AttributeList attList = new AttributeList();
attList.add(new Attribute("FirstName", ANY_NAME));
attList.add(new Attribute("LastName", ANY_NAME));
mbsc.setAttributes(objName, attList);
}
public static void main(String[] args) throws Exception {
RMIConnectorLogAttributesTest test = new RMIConnectorLogAttributesTest();
test.runTest();
if (handler.testFailed()) {
throw new RuntimeException("Test failed. Logged incorrect: '" + ILLEGAL + "'");
}
System.out.println("Test passed!");
}
}
|