1 /* 2 * Copyright (c) 2003, 2012, 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 ProviderTest.java 26 * @summary Tests jar services provider are called 27 * @run clean ProviderTest provider.JMXConnectorProviderImpl provider.JMXConnectorServerProviderImpl 28 * @run build ProviderTest provider.JMXConnectorProviderImpl provider.JMXConnectorServerProviderImpl 29 * @run main ProviderTest 30 */ 31 32 import java.net.MalformedURLException; 33 34 import javax.management.remote.JMXConnectorFactory; 35 import javax.management.remote.JMXConnectorServerFactory; 36 import javax.management.remote.JMXConnector; 37 import javax.management.remote.JMXConnectorServer; 38 import javax.management.remote.JMXServiceURL; 39 import javax.management.remote.JMXProviderException; 40 41 42 import javax.management.MBeanServerConnection; 43 import javax.management.MBeanServerFactory; 44 import javax.management.MBeanServer; 45 46 /* 47 * Tests jar services provider are called 48 */ 49 import provider.JMXConnectorProviderImpl; 50 import provider.JMXConnectorServerProviderImpl; 51 public class ProviderTest { 52 53 public static void main(String[] args) throws Exception { 54 System.out.println("Starting ProviderTest"); 55 MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 56 57 // First do the test with a protocol handled by Service providers 58 JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://"); 59 dotest(url, mbs); 60 61 boolean clientCalled = provider.JMXConnectorProviderImpl.called(); 62 boolean serverCalled = provider.JMXConnectorServerProviderImpl.called(); 63 boolean ok = clientCalled && serverCalled; 64 if (!ok) { 65 if (!clientCalled) 66 System.out.println("Client provider not called"); 67 if (!serverCalled) 68 System.out.println("Server provider not called"); 69 throw new RuntimeException("Test failed - see log for details"); 70 } 71 72 // The Service Provider doesn't handle IIOP. Default providers MUST 73 // be called, which may or may not support IIOP. 74 url = new JMXServiceURL("service:jmx:iiop://"); 75 try { 76 dotest(url, mbs); 77 } catch (MalformedURLException e) { 78 try { 79 Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie"); 80 e.printStackTrace(System.out); 81 throw new RuntimeException("MalformedURLException throw but IIOP appears to be supported"); 82 } catch (ClassNotFoundException expected) { } 83 System.out.println("MalformedURLException thrown, IIOP transport not supported"); 84 } 85 86 // Unsupported protocol. 87 JMXConnectorServer server = null; 88 JMXConnector client = null; 89 url = 90 new JMXServiceURL("service:jmx:unknown-protocol://"); 91 try { 92 server = 93 JMXConnectorServerFactory.newJMXConnectorServer(url, 94 null, 95 mbs); 96 throw new RuntimeException("Exception not thrown."); 97 } catch (MalformedURLException e) { 98 System.out.println("Expected MalformedURLException thrown."); 99 } 100 101 try { 102 client = 103 JMXConnectorFactory.newJMXConnector(url, 104 null); 105 throw new RuntimeException("Exception not thrown."); 106 } catch (MalformedURLException e) { 107 System.out.println("Expected MalformedURLException thrown."); 108 } 109 110 //JMXConnectorProviderException 111 url = 112 new JMXServiceURL("service:jmx:throw-provider-exception://"); 113 try { 114 server = 115 JMXConnectorServerFactory.newJMXConnectorServer(url, 116 null, 117 mbs); 118 throw new RuntimeException("Exception not thrown."); 119 } catch(JMXProviderException e) { 120 System.out.println("Expected JMXProviderException thrown."); 121 } 122 123 try { 124 client = 125 JMXConnectorFactory.newJMXConnector(url, 126 null); 127 throw new RuntimeException("Exception not thrown."); 128 }catch(JMXProviderException e) { 129 System.out.println("Expected JMXProviderException thrown."); 130 } 131 132 System.out.println("Test OK"); 133 } 134 135 private static void dotest(JMXServiceURL url, MBeanServer mbs) 136 throws Exception { 137 JMXConnectorServer server = null; 138 JMXConnector client = null; 139 140 server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); 141 server.start(); 142 JMXServiceURL outputAddr = server.getAddress(); 143 System.out.println("Server started ["+ outputAddr+ "]"); 144 145 client = JMXConnectorFactory.newJMXConnector(outputAddr, null); 146 147 client.connect(); 148 System.out.println("Client connected"); 149 150 MBeanServerConnection connection 151 = client.getMBeanServerConnection(); 152 153 System.out.println(connection.getDefaultDomain()); 154 } 155 }