jdk/src/java.management/share/classes/com/sun/jmx/remote/internal/IIOPHelper.java
changeset 32751 38184f604d96
parent 32750 e90079907456
parent 32712 f61a63b7d1e5
child 32752 43c458023730
child 32827 b00f765af244
equal deleted inserted replaced
32750:e90079907456 32751:38184f604d96
     1 /*
       
     2  * Copyright (c) 2009, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.jmx.remote.internal;
       
    27 
       
    28 import java.util.Properties;
       
    29 import java.io.IOException;
       
    30 import java.rmi.Remote;
       
    31 import java.rmi.NoSuchObjectException;
       
    32 
       
    33 import java.security.AccessController;
       
    34 import java.security.PrivilegedAction;
       
    35 
       
    36 /**
       
    37  * A helper class for RMI-IIOP and CORBA APIs.
       
    38  */
       
    39 
       
    40 public final class IIOPHelper {
       
    41     private IIOPHelper() { }
       
    42 
       
    43     // loads IIOPProxy implementation class if available
       
    44     private static final String IMPL_CLASS =
       
    45         "com.sun.jmx.remote.protocol.iiop.IIOPProxyImpl";
       
    46     private static final IIOPProxy proxy =
       
    47         AccessController.doPrivileged(new PrivilegedAction<IIOPProxy>() {
       
    48             public IIOPProxy run() {
       
    49                 try {
       
    50                     Class<?> c = Class.forName(IMPL_CLASS, true,
       
    51                                                IIOPHelper.class.getClassLoader());
       
    52                     return (IIOPProxy)c.newInstance();
       
    53                 } catch (ClassNotFoundException cnf) {
       
    54                     return null;
       
    55                 } catch (InstantiationException e) {
       
    56                     throw new AssertionError(e);
       
    57                 } catch (IllegalAccessException e) {
       
    58                     throw new AssertionError(e);
       
    59                 }
       
    60             }});
       
    61 
       
    62     /**
       
    63      * Returns true if RMI-IIOP and CORBA is available.
       
    64      */
       
    65     public static boolean isAvailable() {
       
    66         return proxy != null;
       
    67     }
       
    68 
       
    69     private static void ensureAvailable() {
       
    70         if (proxy == null)
       
    71             throw new AssertionError("Should not here");
       
    72     }
       
    73 
       
    74     /**
       
    75      * Returns true if the given object is a Stub.
       
    76      */
       
    77     public static boolean isStub(Object obj) {
       
    78         return (proxy == null) ? false : proxy.isStub(obj);
       
    79     }
       
    80 
       
    81     /**
       
    82      * Returns the Delegate to which the given Stub delegates.
       
    83      */
       
    84     public static Object getDelegate(Object stub) {
       
    85         ensureAvailable();
       
    86         return proxy.getDelegate(stub);
       
    87     }
       
    88 
       
    89     /**
       
    90      * Sets the Delegate for a given Stub.
       
    91      */
       
    92     public static void setDelegate(Object stub, Object delegate) {
       
    93         ensureAvailable();
       
    94         proxy.setDelegate(stub, delegate);
       
    95     }
       
    96 
       
    97     /**
       
    98      * Returns the ORB associated with the given stub
       
    99      *
       
   100      * @throws  UnsupportedOperationException
       
   101      *          if the object does not support the operation that
       
   102      *          was invoked
       
   103      */
       
   104     public static Object getOrb(Object stub) {
       
   105         ensureAvailable();
       
   106         return proxy.getOrb(stub);
       
   107     }
       
   108 
       
   109     /**
       
   110      * Connects the Stub to the given ORB.
       
   111      */
       
   112     public static void connect(Object stub, Object orb)
       
   113         throws IOException
       
   114     {
       
   115         if (proxy == null)
       
   116             throw new IOException("Connection to ORB failed, RMI/IIOP not available");
       
   117         proxy.connect(stub, orb);
       
   118     }
       
   119 
       
   120     /**
       
   121      * Returns true if the given object is an ORB.
       
   122      */
       
   123     public static boolean isOrb(Object obj) {
       
   124         return (proxy == null) ? false : proxy.isOrb(obj);
       
   125     }
       
   126 
       
   127     /**
       
   128      * Creates, and returns, a new ORB instance.
       
   129      */
       
   130     public static Object createOrb(String[] args, Properties props)
       
   131         throws IOException
       
   132     {
       
   133         if (proxy == null)
       
   134             throw new IOException("ORB initialization failed, RMI/IIOP not available");
       
   135         return proxy.createOrb(args, props);
       
   136     }
       
   137 
       
   138     /**
       
   139      * Converts a string, produced by the object_to_string method, back
       
   140      * to a CORBA object reference.
       
   141      */
       
   142     public static Object stringToObject(Object orb, String str) {
       
   143         ensureAvailable();
       
   144         return proxy.stringToObject(orb, str);
       
   145     }
       
   146 
       
   147     /**
       
   148      * Converts the given CORBA object reference to a string.
       
   149      */
       
   150     public static String objectToString(Object orb, Object obj) {
       
   151         ensureAvailable();
       
   152         return proxy.objectToString(orb, obj);
       
   153     }
       
   154 
       
   155     /**
       
   156      * Checks to ensure that an object of a remote or abstract interface
       
   157      * type can be cast to a desired type.
       
   158      */
       
   159     public static <T> T narrow(Object narrowFrom, Class<T> narrowTo) {
       
   160         ensureAvailable();
       
   161         return proxy.narrow(narrowFrom, narrowTo);
       
   162     }
       
   163 
       
   164     /**
       
   165      * Makes a server object ready to receive remote calls
       
   166      */
       
   167     public static void exportObject(Remote obj) throws IOException {
       
   168         if (proxy == null)
       
   169             throw new IOException("RMI object cannot be exported, RMI/IIOP not available");
       
   170         proxy.exportObject(obj);
       
   171     }
       
   172 
       
   173     /**
       
   174      * Deregisters a server object from the runtime.
       
   175      */
       
   176     public static void unexportObject(Remote obj) throws IOException {
       
   177         if (proxy == null)
       
   178             throw new NoSuchObjectException("Object not exported");
       
   179         proxy.unexportObject(obj);
       
   180     }
       
   181 
       
   182     /**
       
   183      * Returns a stub for the given server object.
       
   184      */
       
   185     public static Remote toStub(Remote obj) throws IOException {
       
   186         if (proxy == null)
       
   187             throw new NoSuchObjectException("Object not exported");
       
   188         return proxy.toStub(obj);
       
   189     }
       
   190 }