📄 orbutility.java
字号:
/* * @(#)ORBUtility.java 1.53 06/12/11 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.corba.se.impl.orbutil;import java.lang.Character;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.rmi.NoSuchObjectException;import java.security.AccessController;import java.security.PermissionCollection;import java.security.Policy;import java.security.PrivilegedAction;import java.security.ProtectionDomain;import java.util.ArrayList;import java.util.Arrays;import java.util.Map;import java.util.List;import java.util.ListIterator;import java.util.Set;import java.util.Map.Entry;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.Enumeration;import java.util.Properties;import java.util.IdentityHashMap;import java.util.StringTokenizer;import java.util.NoSuchElementException;import javax.rmi.CORBA.ValueHandler;import javax.rmi.CORBA.ValueHandlerMultiFormat;import javax.rmi.CORBA.Util;import org.omg.CORBA.StructMember ;import org.omg.CORBA.TypeCode ;import org.omg.CORBA.Any ;import org.omg.CORBA.TCKind ;import org.omg.CORBA.SystemException ;import org.omg.CORBA.CompletionStatus ;import org.omg.CORBA.DATA_CONVERSION ;import org.omg.CORBA.BAD_PARAM ;import org.omg.CORBA.BAD_OPERATION ;import org.omg.CORBA.INTERNAL ;import org.omg.CORBA.TypeCodePackage.BadKind ;import org.omg.CORBA.portable.OutputStream ;import org.omg.CORBA.portable.InputStream ;import com.sun.corba.se.pept.transport.ContactInfoList ;import com.sun.corba.se.spi.ior.IOR ;import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;import com.sun.corba.se.spi.orb.ORB ;import com.sun.corba.se.spi.orb.ORBVersion ;import com.sun.corba.se.spi.orb.ORBVersionFactory ;import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;import com.sun.corba.se.spi.protocol.CorbaMessageMediator;import com.sun.corba.se.spi.transport.CorbaContactInfoList ;import com.sun.corba.se.spi.logging.CORBALogDomains ;import com.sun.corba.se.spi.ior.iiop.IIOPProfile;import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate;import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;import com.sun.corba.se.impl.corba.CORBAObjectImpl ;import com.sun.corba.se.impl.logging.ORBUtilSystemException ;import com.sun.corba.se.impl.logging.OMGSystemException ;import com.sun.corba.se.impl.ior.iiop.JavaSerializationComponent;/** * Handy class full of static functions that don't belong in util.Utility for pure ORB reasons. */public final class ORBUtility { private ORBUtility() {} private static ORBUtilSystemException wrapper = ORBUtilSystemException.get( CORBALogDomains.UTIL ) ; private static OMGSystemException omgWrapper = OMGSystemException.get( CORBALogDomains.UTIL ) ; private static StructMember[] members = null; private static StructMember[] systemExceptionMembers (ORB orb) { if (members == null) { members = new StructMember[3]; members[0] = new StructMember("id", orb.create_string_tc(0), null); members[1] = new StructMember("minor", orb.get_primitive_tc(TCKind.tk_long), null); members[2] = new StructMember("completed", orb.get_primitive_tc(TCKind.tk_long), null); } return members; } private static TypeCode getSystemExceptionTypeCode(ORB orb, String repID, String name) { synchronized (TypeCode.class) { return orb.create_exception_tc(repID, name, systemExceptionMembers(orb)); } } private static boolean isSystemExceptionTypeCode(TypeCode type, ORB orb) { StructMember[] systemExceptionMembers = systemExceptionMembers(orb); try { return (type.kind().value() == TCKind._tk_except && type.member_count() == 3 && type.member_type(0).equal(systemExceptionMembers[0].type) && type.member_type(1).equal(systemExceptionMembers[1].type) && type.member_type(2).equal(systemExceptionMembers[2].type)); } catch (BadKind ex) { return false; } catch (org.omg.CORBA.TypeCodePackage.Bounds ex) { return false; } } /** * Static method for writing a CORBA standard exception to an Any. * @param any The Any to write the SystemException into. */ public static void insertSystemException(SystemException ex, Any any) { OutputStream out = any.create_output_stream(); ORB orb = (ORB)(out.orb()); String name = ex.getClass().getName(); String repID = ORBUtility.repositoryIdOf(name); out.write_string(repID); out.write_long(ex.minor); out.write_long(ex.completed.value()); any.read_value(out.create_input_stream(), getSystemExceptionTypeCode(orb, repID, name)); } public static SystemException extractSystemException(Any any) { InputStream in = any.create_input_stream(); ORB orb = (ORB)(in.orb()); if ( ! isSystemExceptionTypeCode(any.type(), orb)) { throw wrapper.unknownDsiSysex(CompletionStatus.COMPLETED_MAYBE); } return ORBUtility.readSystemException(in); } /** * Creates the correct ValueHandler for the given ORB, * querying ORBVersion information. If the ORB or * ORBVersion is null, gets the ValueHandler from * Util.createValueHandler. */ public static ValueHandler createValueHandler(ORB orb) { if (orb == null) return Util.createValueHandler(); ORBVersion version = orb.getORBVersion(); if (version == null) return Util.createValueHandler(); if (version.equals(ORBVersionFactory.getOLD())) return new ValueHandlerImpl_1_3(); if (version.equals(ORBVersionFactory.getNEW())) return new ValueHandlerImpl_1_3_1(); return Util.createValueHandler(); } /** * Returns true if the given ORB could accurately be determined to be a * Kestrel or earlier ORB. Note: If passed the ORBSingleton, this will return * false. */ public static boolean isLegacyORB(ORB orb) { try { ORBVersion currentORB = orb.getORBVersion(); return currentORB.equals( ORBVersionFactory.getOLD() ) ; } catch (SecurityException se) { return false; } } /** * Returns true if it was accurately determined that the remote ORB is * a foreign (non-JavaSoft) ORB. Note: If passed the ORBSingleton, this * will return false. */ public static boolean isForeignORB(ORB orb) { if (orb == null) return false; try { return orb.getORBVersion().equals(ORBVersionFactory.getFOREIGN()); } catch (SecurityException se) { return false; } } /** Unmarshal a byte array to an integer. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte. @param array The array of bytes. @param offset The offset from which to start unmarshalling. */ public static int bytesToInt(byte[] array, int offset) { int b1, b2, b3, b4; b1 = (array[offset++] << 24) & 0xFF000000; b2 = (array[offset++] << 16) & 0x00FF0000; b3 = (array[offset++] << 8) & 0x0000FF00; b4 = (array[offset++] << 0) & 0x000000FF; return (b1 | b2 | b3 | b4); } /** Marshal an integer to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte. @param array The array of bytes. @param offset The offset from which to start marshalling. */ public static void intToBytes(int value, byte[] array, int offset) { array[offset++] = (byte)((value >>> 24) & 0xFF); array[offset++] = (byte)((value >>> 16) & 0xFF); array[offset++] = (byte)((value >>> 8) & 0xFF); array[offset++] = (byte)((value >>> 0) & 0xFF); } /** Converts an Ascii Character into Hexadecimal digit */ public static int hexOf( char x ) { int val; val = x - '0'; if (val >=0 && val <= 9) return val; val = (x - 'a') + 10; if (val >= 10 && val <= 15) return val; val = (x - 'A') + 10; if (val >= 10 && val <= 15) return val; throw wrapper.badHexDigit() ; } // method moved from util.Utility /** * Static method for writing a CORBA standard exception to a stream. * @param strm The OutputStream to use for marshaling. */ public static void writeSystemException(SystemException ex, OutputStream strm) { String s; s = repositoryIdOf(ex.getClass().getName()); strm.write_string(s); strm.write_long(ex.minor); strm.write_long(ex.completed.value()); } /** * Static method for reading a CORBA standard exception from a stream. * @param strm The InputStream to use for unmarshaling. */ public static SystemException readSystemException(InputStream strm) { try { String name = classNameOf(strm.read_string()); SystemException ex = (SystemException)ORBClassLoader.loadClass(name).newInstance(); ex.minor = strm.read_long(); ex.completed = CompletionStatus.from_int(strm.read_long()); return ex; } catch ( Exception ex ) { throw wrapper.unknownSysex( CompletionStatus.COMPLETED_MAYBE, ex ); } } /** * Get the class name corresponding to a particular repository Id. * This is used by the system to unmarshal (instantiate) the * appropriate exception class for an marshaled as the value of * its repository Id. * @param repositoryId The repository Id for which we want a class name. */ public static String classNameOf(String repositoryId) { String className=null; className = (String) exceptionClassNames.get(repositoryId); if (className == null) className = "org.omg.CORBA.UNKNOWN"; return className; } /** * Return true if this repositoryId is a SystemException. * @param repositoryId The repository Id to check. */ public static boolean isSystemException(String repositoryId) { String className=null; className = (String) exceptionClassNames.get(repositoryId); if (className == null) return false; else return true; } /** * @return the Java serialization encoding version. */ public static byte getEncodingVersion(ORB orb, IOR ior) { // Is Java serialization enabled? // Check the JavaSerializationComponent (tagged component) // in the IIOPProfile. If present, the peer ORB's GIOP is capable // of using Java serialization instead of CDR serialization. // In such a case, use Java serialization, iff the java serialization // versions match. if (orb.getORBData().isJavaSerializationEnabled()) { IIOPProfile prof = ior.getProfile(); IIOPProfileTemplate profTemp = (IIOPProfileTemplate) prof.getTaggedProfileTemplate(); java.util.Iterator iter = profTemp.iteratorById( ORBConstants.TAG_JAVA_SERIALIZATION_ID); if (iter.hasNext()) { JavaSerializationComponent jc = (JavaSerializationComponent) iter.next(); byte jcVersion = jc.javaSerializationVersion(); if (jcVersion >= Message.JAVA_ENC_VERSION) { return Message.JAVA_ENC_VERSION; } else if (jcVersion > Message.CDR_ENC_VERSION) { return jc.javaSerializationVersion(); } else { // throw error? // Since encodingVersion is <= 0 (CDR_ENC_VERSION). } } } return Message.CDR_ENC_VERSION; // default } /** * Get the repository id corresponding to a particular class. * This is used by the system to write the * appropriate repository id for a system exception. * @param name The class name of the system exception. */ public static String repositoryIdOf(String name) { String id; id = (String) exceptionRepositoryIds.get(name); if (id == null) id = "IDL:omg.org/CORBA/UNKNOWN:1.0"; return id; } private static final Hashtable exceptionClassNames = new Hashtable(); private static final Hashtable exceptionRepositoryIds = new Hashtable(); static { // // construct repositoryId -> className hashtable // exceptionClassNames.put("IDL:omg.org/CORBA/BAD_CONTEXT:1.0", "org.omg.CORBA.BAD_CONTEXT"); exceptionClassNames.put("IDL:omg.org/CORBA/BAD_INV_ORDER:1.0", "org.omg.CORBA.BAD_INV_ORDER"); exceptionClassNames.put("IDL:omg.org/CORBA/BAD_OPERATION:1.0", "org.omg.CORBA.BAD_OPERATION"); exceptionClassNames.put("IDL:omg.org/CORBA/BAD_PARAM:1.0", "org.omg.CORBA.BAD_PARAM"); exceptionClassNames.put("IDL:omg.org/CORBA/BAD_TYPECODE:1.0", "org.omg.CORBA.BAD_TYPECODE"); exceptionClassNames.put("IDL:omg.org/CORBA/COMM_FAILURE:1.0", "org.omg.CORBA.COMM_FAILURE"); exceptionClassNames.put("IDL:omg.org/CORBA/DATA_CONVERSION:1.0", "org.omg.CORBA.DATA_CONVERSION"); exceptionClassNames.put("IDL:omg.org/CORBA/IMP_LIMIT:1.0", "org.omg.CORBA.IMP_LIMIT"); exceptionClassNames.put("IDL:omg.org/CORBA/INTF_REPOS:1.0", "org.omg.CORBA.INTF_REPOS"); exceptionClassNames.put("IDL:omg.org/CORBA/INTERNAL:1.0", "org.omg.CORBA.INTERNAL"); exceptionClassNames.put("IDL:omg.org/CORBA/INV_FLAG:1.0", "org.omg.CORBA.INV_FLAG"); exceptionClassNames.put("IDL:omg.org/CORBA/INV_IDENT:1.0", "org.omg.CORBA.INV_IDENT"); exceptionClassNames.put("IDL:omg.org/CORBA/INV_OBJREF:1.0", "org.omg.CORBA.INV_OBJREF"); exceptionClassNames.put("IDL:omg.org/CORBA/MARSHAL:1.0", "org.omg.CORBA.MARSHAL"); exceptionClassNames.put("IDL:omg.org/CORBA/NO_MEMORY:1.0", "org.omg.CORBA.NO_MEMORY"); exceptionClassNames.put("IDL:omg.org/CORBA/FREE_MEM:1.0", "org.omg.CORBA.FREE_MEM"); exceptionClassNames.put("IDL:omg.org/CORBA/NO_IMPLEMENT:1.0", "org.omg.CORBA.NO_IMPLEMENT"); exceptionClassNames.put("IDL:omg.org/CORBA/NO_PERMISSION:1.0", "org.omg.CORBA.NO_PERMISSION"); exceptionClassNames.put("IDL:omg.org/CORBA/NO_RESOURCES:1.0", "org.omg.CORBA.NO_RESOURCES"); exceptionClassNames.put("IDL:omg.org/CORBA/NO_RESPONSE:1.0", "org.omg.CORBA.NO_RESPONSE"); exceptionClassNames.put("IDL:omg.org/CORBA/OBJ_ADAPTER:1.0", "org.omg.CORBA.OBJ_ADAPTER"); exceptionClassNames.put("IDL:omg.org/CORBA/INITIALIZE:1.0",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -