All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.addc.jndi.CorbaUtils Maven / Gradle / Ivy

package com.addc.jndi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.Remote;
import java.util.Hashtable;
import java.util.Properties;

import javax.naming.ConfigurationException;

import org.omg.CORBA.ORB;
import org.omg.CORBA.SystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.addc.commons.annotation.CoberturaIgnore;
import com.addc.commons.iiop.OrbAlreadyExistsException;
import com.addc.commons.iiop.OrbHolder;
import com.addc.naming.seshat.ObjectBinding;

/**
 * The CorbaUtils class supplies helper methods for resolving the ORB,
 * converting rmi.Remote objects to CORBA objects, serializing Java Objects
 * ready to bind and getting them back from ObjectBinding instances returned by
 * the Seshat Naming Service.
 * 

* These utilities are used by the {@link com.addc.jndi.ecn.ECNStateFactory} and * {@link com.addc.jndi.ecn.ECNObjectFactory} classes. * */ @SuppressWarnings({ "PMD.LooseCoupling", "PMD.IdenticalCatchBranches" }) public final class CorbaUtils { private static final CorbaUtils INSTANCE= new CorbaUtils(); private static final Logger LOGGER= LoggerFactory.getLogger(CorbaUtils.class); private Method toStubMethod; private Method connectMethod; private Class corbaStubClass; /** * Get the singleton instance * * @return the singleton instance */ public static CorbaUtils getInstance() { return INSTANCE; } /** * Get the ORB for the JNDI client * * @param env * The environment to lookup * @param create * If true and no ORB is present create one. * @return The ORB from either the environment, the {@link OrbHolder} or a * new one. */ public ORB getOrb(Hashtable env, boolean create) { ORB orb= (ORB) env.get(JndiKeys.KEY_ORB); if (orb == null) { orb= OrbHolder.getInstance().getOrb(); } if ((orb == null) && create) { orb= ORB.init(new String[0], new Properties()); try { OrbHolder.getInstance().setOrb(orb); } catch (OrbAlreadyExistsException e) { LOGGER.error("Unexpected failure", e); } } return orb; } /** * Make a deep clone of the given environment map * * @param original * The map to clone * @return The clone */ public Hashtable copyEnvironment(Hashtable original) { Hashtable environment= new Hashtable<>(); if (original != null) { for (Object key : original.keySet()) { Object value= original.get(key); environment.put(key, value); } } return environment; } /** * Convert a remote rmi object to its CORBA equivalent. * * @param remote * The remote object to convert. * @param orb * The ORB to use for conversion. * @return The CORBA object reference that can be bound/re-bound. * @exception ClassNotFoundException * If the class cannot be found. * @exception ConfigurationException * If the remote Object cannot be converted. */ @SuppressWarnings("PMD.PreserveStackTrace") @CoberturaIgnore public org.omg.CORBA.Object remoteToCorba(Remote remote, ORB orb) throws ClassNotFoundException, ConfigurationException { if (toStubMethod == null) { initMethodHandles(); } Object obj; try { obj= toStubMethod.invoke(null, new Object[] { remote }); } catch (InvocationTargetException e) { LOGGER.error("Problem with PortableRemoteObject.toStub(); object not exported or stub not found", e); throw new ConfigurationException( "Problem with PortableRemoteObject.toStub(); object not exported or stub not found"); } catch (IllegalAccessException e) { LOGGER.error("Cannot invoke javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)", e); throw new ConfigurationException("Cannot invoke javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)"); } if (!corbaStubClass.isInstance(obj)) { return null; } try { connectMethod.invoke(obj, new Object[] { orb }); } catch (InvocationTargetException e) { LOGGER.error("Problem invoking javax.rmi.CORBA.Stub.connect()", e); throw new ConfigurationException("Problem invoking javax.rmi.CORBA.Stub.connect()"); } catch (IllegalAccessException e) { LOGGER.error("Cannot invoke javax.rmi.CORBA.Stub.connect()", e); throw new ConfigurationException("Cannot invoke javax.rmi.CORBA.Stub.connect()"); } return (org.omg.CORBA.Object) obj; } /** * Get a Java object from an ObjectBinding. * * @param ob * The ObjectBinding instance to use. * @return A Java object. * @exception org.omg.CORBA.SystemException * If there was a problem in the CORBA layer. * @exception IOException * If the object couldn't be de-serialized. * @exception ClassNotFoundException * If the object class is not on the classpath. */ public Serializable corbaToJava(ObjectBinding ob) throws SystemException, IOException, ClassNotFoundException { Serializable obj= null; ByteArrayInputStream bais= new ByteArrayInputStream(ob.data()); ObjectInputStream ois= new ObjectInputStream(bais); obj= (Serializable) ois.readObject(); ob._release(); return obj; } /** * Initialize the method reference variables. */ @CoberturaIgnore private void initMethodHandles() throws ClassNotFoundException { corbaStubClass= Class.forName("javax.rmi.CORBA.Stub"); try { connectMethod= corbaStubClass.getMethod("connect", new Class[] { ORB.class }); } catch (NoSuchMethodException e) { LOGGER.error("No method definition for javax.rmi.CORBA.Stub.connect(org.omg.CORBA.ORB)", e); throw new IllegalStateException("No method definition for javax.rmi.CORBA.Stub.connect(org.omg.CORBA.ORB)", e); } Class class1= Class.forName("javax.rmi.PortableRemoteObject"); try { toStubMethod= class1.getMethod("toStub", new Class[] { Remote.class }); } catch (NoSuchMethodException e) { LOGGER.error("No method definition for javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)", e); throw new IllegalStateException( "No method definition for javax.rmi.PortableRemoteObject.toStub(java.rmi.Remote)", e); } } /** * Serialise an object into a byte array ready for storing in an binding to * the Seshat Naming Service. * * @param object * The object to serialise. * @return A serialised object as a byte array * @exception NotSerializableException * If the object isn't serializable. * @exception IOException * If the object could not be streamed. */ public byte[] serializeObject(Object object) throws NotSerializableException, IOException { byte[] result= null; ByteArrayOutputStream baos= new ByteArrayOutputStream(); ObjectOutputStream out= null; try { out= new ObjectOutputStream(baos); out.writeObject(object); result= baos.toByteArray(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { LOGGER.debug(e.getMessage(), e); } } } return result; } /** * Ensure that CorbaUtils instances cannot be created. */ private CorbaUtils() { } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy