1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.lang.management.ManagementFactory;
  26 import java.net.ServerSocket;
  27 import java.rmi.registry.LocateRegistry;
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 import java.util.logging.Level;
  31 import java.util.logging.Logger;
  32 
  33 import javax.management.Attribute;
  34 import javax.management.AttributeList;
  35 import javax.management.AttributeNotFoundException;
  36 import javax.management.InstanceAlreadyExistsException;
  37 import javax.management.InstanceNotFoundException;
  38 import javax.management.InvalidAttributeValueException;
  39 import javax.management.MBeanException;
  40 import javax.management.MBeanRegistrationException;
  41 import javax.management.MBeanServer;
  42 import javax.management.MBeanServerConnection;
  43 import javax.management.MalformedObjectNameException;
  44 import javax.management.NotCompliantMBeanException;
  45 import javax.management.ObjectName;
  46 import javax.management.ReflectionException;
  47 import javax.management.remote.JMXConnector;
  48 import javax.management.remote.JMXConnectorFactory;
  49 import javax.management.remote.JMXConnectorServer;
  50 import javax.management.remote.JMXConnectorServerFactory;
  51 import javax.management.remote.JMXServiceURL;
  52 
  53 /**
  54  * @test
  55  * @bug 8147857
  56  * @summary Tests whether RMIConnector logs attribute names correctly.
  57  * @author Severin Gehwolf
  58  */
  59 public class RMIConnectorLogAttributesTest {
  60 
  61     private static final String ILLEGAL = ", FirstName[LastName]";
  62     private static final Logger logger = Logger.getLogger("javax.management.remote.rmi");
  63     private static final String ANY_NAME = "foo";
  64     private static final TestLogHandler handler;
  65     static {
  66         handler = new TestLogHandler(ILLEGAL);
  67         handler.setLevel(Level.FINEST);
  68         logger.setLevel(Level.ALL);
  69         logger.addHandler(handler);
  70     }
  71 
  72     private JMXConnectorServer startServer(int rmiPort) throws Exception {
  73         System.out.println("DEBUG: Create RMI registry on port " + rmiPort);
  74         LocateRegistry.createRegistry(rmiPort);
  75 
  76         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  77 
  78         HashMap<String,Object> env = new HashMap<String,Object>();
  79 
  80         JMXServiceURL url =
  81                 new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:" + rmiPort + "/jmxrmi");
  82         JMXConnectorServer cs =
  83                 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
  84 
  85         cs.start();
  86         System.out.println("DEBUG: Started the RMI connector server");
  87         return cs;
  88     }
  89 
  90     private int findPort() {
  91         for (int i = 13333; i < 13333 + 100; i++) {
  92             try {
  93                 ServerSocket socket = new ServerSocket(i);
  94                 socket.close();
  95                 return i;
  96             } catch (IOException e) {
  97                 continue;
  98             }
  99         }
 100         return -1;
 101     }
 102 
 103     private void runTest() {
 104         int rmiPort = findPort();
 105         if (rmiPort == -1) {
 106             throw new RuntimeException("Test failed. No available port");
 107         }
 108         JMXConnectorServer server = null;
 109         try {
 110             server = startServer(rmiPort);
 111             JMXConnector connector = connectToServer(server);
 112             doTest(connector);
 113         } catch (Exception e) {
 114             throw new RuntimeException("Test failed unexpectedly", e);
 115         } finally {
 116             if (server != null) {
 117                 try {
 118                     server.stop();
 119                 } catch (IOException e) {
 120                     // ignore
 121                 }
 122             }
 123         }
 124     }
 125 
 126     private JMXConnector connectToServer(JMXConnectorServer server) throws IOException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, ReflectionException, MBeanException {
 127         JMXServiceURL url = server.getAddress();
 128         Map<String, Object> env = new HashMap<String, Object>();
 129         JMXConnector connector = JMXConnectorFactory.connect(url, env);
 130 
 131         System.out.println("DEBUG: Client connected to RMI at: " + url);
 132 
 133         return connector;
 134     }
 135 
 136     private void doTest(JMXConnector connector) throws IOException,
 137     MalformedObjectNameException, ReflectionException,
 138     InstanceAlreadyExistsException, MBeanRegistrationException,
 139     MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
 140         MBeanServerConnection  mbsc = connector.getMBeanServerConnection();
 141 
 142 
 143         ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
 144         System.out.println("DEBUG: Calling createMBean");
 145         mbsc.createMBean(Name.class.getName(), objName);
 146 
 147         System.out.println("DEBUG: Calling setAttributes");
 148         AttributeList attList = new AttributeList();
 149         attList.add(new Attribute("FirstName", ANY_NAME));
 150         attList.add(new Attribute("LastName", ANY_NAME));
 151         mbsc.setAttributes(objName, attList);
 152     }
 153 
 154     public static void main(String[] args) throws Exception {
 155         RMIConnectorLogAttributesTest test = new RMIConnectorLogAttributesTest();
 156         test.runTest();
 157         if (handler.testFailed()) {
 158             throw new RuntimeException("Test failed. Logged incorrect: '" + ILLEGAL + "'");
 159         }
 160         System.out.println("Test passed!");
 161     }
 162 
 163 }