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