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