1 /*
   2  * Copyright (c) 2009, 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.rmi.Remote;
  30 import java.rmi.RemoteException;
  31 import java.rmi.NoSuchObjectException;
  32 
  33 import java.util.Properties;
  34 import java.rmi.Remote;
  35 import java.rmi.RemoteException;
  36 import java.rmi.NoSuchObjectException;
  37 
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;
  40 
  41 /**
  42  * A helper class for RMI-IIOP and CORBA APIs.
  43  */
  44 
  45 public final class IIOPHelper {
  46     private IIOPHelper() { }
  47 
  48     // loads IIOPProxy implementation class if available
  49     private static final String IMPL_CLASS =
  50         "com.sun.jmx.remote.protocol.iiop.IIOPProxyImpl";
  51     private static final IIOPProxy proxy =
  52         AccessController.doPrivileged(new PrivilegedAction<IIOPProxy>() {
  53             public IIOPProxy run() {
  54                 try {
  55                     Class<?> c = Class.forName(IMPL_CLASS, true, null);
  56                     return (IIOPProxy)c.newInstance();
  57                 } catch (ClassNotFoundException cnf) {
  58                     return null;
  59                 } catch (InstantiationException e) {
  60                     throw new AssertionError(e);
  61                 } catch (IllegalAccessException e) {
  62                     throw new AssertionError(e);
  63                 }
  64             }});
  65 
  66     /**
  67      * Returns true if RMI-IIOP and CORBA is available.
  68      */
  69     public static boolean isAvailable() {
  70         return proxy != null;
  71     }
  72 
  73     private static void ensureAvailable() {
  74         if (proxy == null)
  75             throw new AssertionError("Should not here");
  76     }
  77 
  78     /**
  79      * Returns true if the given object is a Stub.
  80      */
  81     public static boolean isStub(Object obj) {
  82         return (proxy == null) ? false : proxy.isStub(obj);
  83     }
  84 
  85     /**
  86      * Returns the Delegate to which the given Stub delegates.
  87      */
  88     public static Object getDelegate(Object stub) {
  89         ensureAvailable();
  90         return proxy.getDelegate(stub);
  91     }
  92 
  93     /**
  94      * Sets the Delegate for a given Stub.
  95      */
  96     public static void setDelegate(Object stub, Object delegate) {
  97         ensureAvailable();
  98         proxy.setDelegate(stub, delegate);
  99     }
 100 
 101     /**
 102      * Returns the ORB associated with the given stub
 103      *
 104      * @throws  UnsupportedOperationException
 105      *          if the object does not support the operation that
 106      *          was invoked
 107      */
 108     public static Object getOrb(Object stub) {
 109         ensureAvailable();
 110         return proxy.getOrb(stub);
 111     }
 112 
 113     /**
 114      * Connects the Stub to the given ORB.
 115      */
 116     public static void connect(Object stub, Object orb)
 117         throws RemoteException
 118     {
 119         ensureAvailable();
 120         proxy.connect(stub, orb);
 121     }
 122 
 123     /**
 124      * Returns true if the given object is an ORB.
 125      */
 126     public static boolean isOrb(Object obj) {
 127         ensureAvailable();
 128         return proxy.isOrb(obj);
 129     }
 130 
 131     /**
 132      * Creates, and returns, a new ORB instance.
 133      */
 134     public static Object createOrb(String[] args, Properties props) {
 135         ensureAvailable();
 136         return proxy.createOrb(args, props);
 137     }
 138 
 139     /**
 140      * Converts a string, produced by the object_to_string method, back
 141      * to a CORBA object reference.
 142      */
 143     public static Object stringToObject(Object orb, String str) {
 144         ensureAvailable();
 145         return proxy.stringToObject(orb, str);
 146     }
 147 
 148     /**
 149      * Converts the given CORBA object reference to a string.
 150      */
 151     public static String objectToString(Object orb, Object obj) {
 152         ensureAvailable();
 153         return proxy.objectToString(orb, obj);
 154     }
 155 
 156     /**
 157      * Checks to ensure that an object of a remote or abstract interface
 158      * type can be cast to a desired type.
 159      */
 160     public static <T> T narrow(Object narrowFrom, Class<T> narrowTo) {
 161         ensureAvailable();
 162         return proxy.narrow(narrowFrom, narrowTo);
 163     }
 164 
 165     /**
 166      * Makes a server object ready to receive remote calls
 167      */
 168     public static void exportObject(Remote obj) throws RemoteException {
 169         ensureAvailable();
 170         proxy.exportObject(obj);
 171     }
 172 
 173     /**
 174      * Deregisters a server object from the runtime.
 175      */
 176     public static void unexportObject(Remote obj) throws NoSuchObjectException {
 177         ensureAvailable();
 178         proxy.unexportObject(obj);
 179     }
 180 
 181     /**
 182      * Returns a stub for the given server object.
 183      */
 184     public static Remote toStub(Remote obj) throws NoSuchObjectException {
 185         ensureAvailable();
 186         return proxy.toStub(obj);
 187     }
 188 }