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