1 /* 2 * Copyright (c) 2003, 2015, 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 /* 25 * @test 26 * @bug 4944680 27 * @summary Tests that IllegalArgumentException is thrown when 28 * MBeanServerForwrder is null. 29 * @author Daniel Fuchs 30 * @modules java.management.rmi 31 * java.management/com.sun.jmx.remote.security 32 * @run clean SetMBeanServerForwarder 33 * @run build SetMBeanServerForwarder 34 * @run main SetMBeanServerForwarder 35 */ 36 import javax.management.*; 37 import javax.management.remote.*; 38 import javax.management.remote.rmi.*; 39 import java.util.Map; 40 41 import com.sun.jmx.remote.security.MBeanServerAccessController; 42 43 public class SetMBeanServerForwarder { 44 45 static boolean isPresent(String cn) { 46 try { 47 Class.forName(cn); 48 return true; 49 } catch (ClassNotFoundException x) { 50 return false; 51 } 52 } 53 54 public static int test(String... urls) { 55 int errorCount = 0; 56 for (int i=0;i<urls.length;i++) { 57 try { 58 final MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 59 final JMXConnectorServer cs1,cs2; 60 final JMXServiceURL url; 61 62 System.out.println("*** -----------------------------------"); 63 System.out.println("*** JMXConnectorServer("+urls[i]+")"); 64 System.out.println("*** -----------------------------------"); 65 66 try { 67 url = new JMXServiceURL(urls[i]); 68 cs1 = JMXConnectorServerFactory 69 .newJMXConnectorServer(url,(Map)null,mbs); 70 cs2 = JMXConnectorServerFactory 71 .newJMXConnectorServer(url,(Map)null,null); 72 } catch (Throwable thr) { 73 System.out.println("Failed to create ConnectorServer "+ 74 "from [" + urls[i] +"]: " + thr); 75 thr.printStackTrace(); 76 errorCount++; 77 continue; 78 } 79 80 // Test using a JMXConnectorServer already connected to an 81 // MBeanServer 82 // 83 84 // Set null MBeanServerForwarder - expect exception 85 // 86 try { 87 cs1.setMBeanServerForwarder(null); 88 errorCount++; 89 System.out.println("Expected IllegalArgumentException "+ 90 " not thrown (null forwarder) for " + 91 url); 92 System.out.println("\t\t[connected to MBeanServer]"); 93 } catch (IllegalArgumentException iae) { 94 System.out.println("Received expected exception: " + 95 iae); 96 } 97 98 // Now try with a real MBSF - should not throw exception 99 // 100 try { 101 final MBeanServerForwarder fwd = new 102 MBeanServerAccessController() { 103 protected void checkRead() {} 104 protected void checkWrite() {} 105 }; 106 cs1.setMBeanServerForwarder(fwd); 107 108 // Verify that the MBSF was correctly set. 109 // 110 if (cs1.getMBeanServer() != fwd) { 111 System.out.println("MBeanServerForwarder not set "+ 112 "for " + url); 113 System.out.println("\t\t[connected to MBeanServer]"); 114 throw new AssertionError("cs1.getMBeanServer()!=fwd"); 115 } 116 117 // Verify that the MBS was correctly forwarded to the MBSF 118 // 119 if (fwd.getMBeanServer() != mbs) { 120 System.out.println("MBeanServer not set in Forwarder"+ 121 " for " + url); 122 System.out.println("\t\t[connected to MBeanServer]"); 123 throw new AssertionError("fwd.getMBeanServer()!=mbs"); 124 } 125 System.out.println("MBeanServerForwarder successfully "+ 126 "set for " + url); 127 System.out.println("\t\t[connected to MBeanServer]"); 128 } catch (Throwable x) { 129 errorCount++; 130 System.out.println("Failed to set forwarder for " + 131 url); 132 System.out.println("\t\t[connected to MBeanServer]"); 133 System.out.println("Unexpected exception: " + 134 x); 135 x.printStackTrace(); 136 } 137 138 // Test using a JMXConnectorServer not connected to any 139 // MBeanServer 140 // 141 142 // Set null MBeanServerForwarder - expect exception 143 // 144 try { 145 cs2.setMBeanServerForwarder(null); 146 errorCount++; 147 System.out.println("Expected IllegalArgumentException "+ 148 " not thrown (null forwarder) for " + 149 url); 150 System.out.println("\t\t[not connected to MBeanServer]"); 151 } catch (IllegalArgumentException iae) { 152 System.out.println("Received expected exception: " + 153 iae); 154 } 155 156 // Now try with a real MBSF - should not throw exception 157 // 158 try { 159 final MBeanServerForwarder fwd = new 160 MBeanServerAccessController() { 161 protected void checkRead() {} 162 protected void checkWrite() {} 163 }; 164 cs2.setMBeanServerForwarder(fwd); 165 166 // Verify that the MBSF was correctly set. 167 // 168 if (cs2.getMBeanServer() != fwd) { 169 System.out.println("MBeanServerForwarder not set "+ 170 "for " + url); 171 System.out.println("\t\t[not connected to MBeanServer]"); 172 throw new AssertionError("cs2.getMBeanServer()!=fwd"); 173 } 174 175 // Now register the connector 176 // 177 final ObjectName name = 178 new ObjectName(":type="+cs2.getClass().getName()+ 179 ",url="+ObjectName.quote(urls[i])); 180 mbs.registerMBean(cs2,name); 181 try { 182 183 // Verify that the MBSF was not disconnected. 184 // 185 if (cs2.getMBeanServer() != fwd) { 186 System.out. 187 println("MBeanServerForwarder changed "+ 188 "for " + url); 189 System.out. 190 println("\t\t[registerMBean]"); 191 throw new 192 AssertionError("cs2.getMBeanServer()!=fwd"); 193 } 194 195 // Verify that the MBS was not forwarded to the MBSF 196 // 197 if (fwd.getMBeanServer() != null) { 198 System.out. 199 println("MBeanServer changed in Forwarder"+ 200 " for " + url); 201 System.out.println("\t\t[registerMBean]"); 202 throw new 203 AssertionError("fwd.getMBeanServer()!=null"); 204 } 205 206 } finally { 207 mbs.unregisterMBean(name); 208 } 209 210 System.out.println("MBeanServerForwarder successfully "+ 211 "set for " + url); 212 System.out.println("\t\t[not connected to MBeanServer]"); 213 } catch (Throwable x) { 214 errorCount++; 215 System.out.println("Failed to set forwarder for " + 216 url); 217 System.out.println("\t\t[not connected to MBeanServer]"); 218 System.out.println("Unexpected exception: " + 219 x); 220 x.printStackTrace(); 221 } 222 223 } catch (Exception x) { 224 System.err.println("Unexpected exception for " + 225 urls[i] + ": " + x); 226 x.printStackTrace(); 227 errorCount++; 228 } 229 } 230 return errorCount; 231 } 232 233 public static void main(String args[]) { 234 int errCount = 0; 235 236 // mandatory 237 errCount += test("service:jmx:rmi://"); 238 239 // optional 240 if (isPresent("javax.management.remote.rmi._RMIConnectionImpl_Tie")) 241 errCount += test("service:jmx:iiop://"); 242 if (isPresent("javax.management.remote.generic.GenericConnector")) 243 errCount += test("service:jmx:jmxmp://"); 244 245 if (errCount > 0) { 246 throw new RuntimeException("SetMBeanServerForwarder failed: " + 247 errCount + " error(s) reported."); 248 } 249 System.out.println("SetMBeanServerForwarder passed."); 250 } 251 }