1. /*
  2. * @(#)ORBUtility.java 1.51 04/06/21
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.orbutil;
  8. import java.lang.Character;
  9. import java.lang.reflect.Field;
  10. import java.lang.reflect.Method;
  11. import java.rmi.NoSuchObjectException;
  12. import java.security.AccessController;
  13. import java.security.PermissionCollection;
  14. import java.security.Policy;
  15. import java.security.PrivilegedAction;
  16. import java.security.ProtectionDomain;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Map;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import java.util.Set;
  23. import java.util.Map.Entry;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.Hashtable;
  28. import java.util.Iterator;
  29. import java.util.Enumeration;
  30. import java.util.Properties;
  31. import java.util.IdentityHashMap;
  32. import java.util.StringTokenizer;
  33. import java.util.NoSuchElementException;
  34. import javax.rmi.CORBA.ValueHandler;
  35. import javax.rmi.CORBA.ValueHandlerMultiFormat;
  36. import javax.rmi.CORBA.Util;
  37. import org.omg.CORBA.StructMember ;
  38. import org.omg.CORBA.TypeCode ;
  39. import org.omg.CORBA.Any ;
  40. import org.omg.CORBA.TCKind ;
  41. import org.omg.CORBA.SystemException ;
  42. import org.omg.CORBA.CompletionStatus ;
  43. import org.omg.CORBA.DATA_CONVERSION ;
  44. import org.omg.CORBA.BAD_PARAM ;
  45. import org.omg.CORBA.BAD_OPERATION ;
  46. import org.omg.CORBA.INTERNAL ;
  47. import org.omg.CORBA.TypeCodePackage.BadKind ;
  48. import org.omg.CORBA.portable.OutputStream ;
  49. import org.omg.CORBA.portable.InputStream ;
  50. import com.sun.corba.se.pept.transport.ContactInfoList ;
  51. import com.sun.corba.se.spi.ior.IOR ;
  52. import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
  53. import com.sun.corba.se.spi.orb.ORB ;
  54. import com.sun.corba.se.spi.orb.ORBVersion ;
  55. import com.sun.corba.se.spi.orb.ORBVersionFactory ;
  56. import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;
  57. import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
  58. import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
  59. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  60. import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
  61. import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate;
  62. import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  63. import com.sun.corba.se.impl.corba.CORBAObjectImpl ;
  64. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  65. import com.sun.corba.se.impl.logging.OMGSystemException ;
  66. import com.sun.corba.se.impl.ior.iiop.JavaSerializationComponent;
  67. /**
  68. * Handy class full of static functions that don't belong in util.Utility for pure ORB reasons.
  69. */
  70. public final class ORBUtility {
  71. private ORBUtility() {}
  72. private static ORBUtilSystemException wrapper = ORBUtilSystemException.get(
  73. CORBALogDomains.UTIL ) ;
  74. private static OMGSystemException omgWrapper = OMGSystemException.get(
  75. CORBALogDomains.UTIL ) ;
  76. private static StructMember[] members = null;
  77. private static StructMember[] systemExceptionMembers (ORB orb) {
  78. if (members == null) {
  79. members = new StructMember[3];
  80. members[0] = new StructMember("id", orb.create_string_tc(0), null);
  81. members[1] = new StructMember("minor", orb.get_primitive_tc(TCKind.tk_long), null);
  82. members[2] = new StructMember("completed", orb.get_primitive_tc(TCKind.tk_long), null);
  83. }
  84. return members;
  85. }
  86. private static TypeCode getSystemExceptionTypeCode(ORB orb, String repID, String name) {
  87. synchronized (TypeCode.class) {
  88. return orb.create_exception_tc(repID, name, systemExceptionMembers(orb));
  89. }
  90. }
  91. private static boolean isSystemExceptionTypeCode(TypeCode type, ORB orb) {
  92. StructMember[] systemExceptionMembers = systemExceptionMembers(orb);
  93. try {
  94. return (type.kind().value() == TCKind._tk_except &&
  95. type.member_count() == 3 &&
  96. type.member_type(0).equal(systemExceptionMembers[0].type) &&
  97. type.member_type(1).equal(systemExceptionMembers[1].type) &&
  98. type.member_type(2).equal(systemExceptionMembers[2].type));
  99. } catch (BadKind ex) {
  100. return false;
  101. } catch (org.omg.CORBA.TypeCodePackage.Bounds ex) {
  102. return false;
  103. }
  104. }
  105. /**
  106. * Static method for writing a CORBA standard exception to an Any.
  107. * @param any The Any to write the SystemException into.
  108. */
  109. public static void insertSystemException(SystemException ex, Any any) {
  110. OutputStream out = any.create_output_stream();
  111. ORB orb = (ORB)(out.orb());
  112. String name = ex.getClass().getName();
  113. String repID = ORBUtility.repositoryIdOf(name);
  114. out.write_string(repID);
  115. out.write_long(ex.minor);
  116. out.write_long(ex.completed.value());
  117. any.read_value(out.create_input_stream(),
  118. getSystemExceptionTypeCode(orb, repID, name));
  119. }
  120. public static SystemException extractSystemException(Any any) {
  121. InputStream in = any.create_input_stream();
  122. ORB orb = (ORB)(in.orb());
  123. if ( ! isSystemExceptionTypeCode(any.type(), orb)) {
  124. throw wrapper.unknownDsiSysex(CompletionStatus.COMPLETED_MAYBE);
  125. }
  126. return ORBUtility.readSystemException(in);
  127. }
  128. /**
  129. * Creates the correct ValueHandler for the given ORB,
  130. * querying ORBVersion information. If the ORB or
  131. * ORBVersion is null, gets the ValueHandler from
  132. * Util.createValueHandler.
  133. */
  134. public static ValueHandler createValueHandler(ORB orb) {
  135. if (orb == null)
  136. return Util.createValueHandler();
  137. ORBVersion version = orb.getORBVersion();
  138. if (version == null)
  139. return Util.createValueHandler();
  140. if (version.equals(ORBVersionFactory.getOLD()))
  141. return new ValueHandlerImpl_1_3();
  142. if (version.equals(ORBVersionFactory.getNEW()))
  143. return new ValueHandlerImpl_1_3_1();
  144. return Util.createValueHandler();
  145. }
  146. /**
  147. * Returns true if the given ORB could accurately be determined to be a
  148. * Kestrel or earlier ORB. Note: If passed the ORBSingleton, this will return
  149. * false.
  150. */
  151. public static boolean isLegacyORB(ORB orb)
  152. {
  153. try {
  154. ORBVersion currentORB = orb.getORBVersion();
  155. return currentORB.equals( ORBVersionFactory.getOLD() ) ;
  156. } catch (SecurityException se) {
  157. return false;
  158. }
  159. }
  160. /**
  161. * Returns true if it was accurately determined that the remote ORB is
  162. * a foreign (non-JavaSoft) ORB. Note: If passed the ORBSingleton, this
  163. * will return false.
  164. */
  165. public static boolean isForeignORB(ORB orb)
  166. {
  167. if (orb == null)
  168. return false;
  169. try {
  170. return orb.getORBVersion().equals(ORBVersionFactory.getFOREIGN());
  171. } catch (SecurityException se) {
  172. return false;
  173. }
  174. }
  175. /** Unmarshal a byte array to an integer.
  176. Assume the bytes are in BIGENDIAN order.
  177. i.e. array[offset] is the most-significant-byte
  178. and array[offset+3] is the least-significant-byte.
  179. @param array The array of bytes.
  180. @param offset The offset from which to start unmarshalling.
  181. */
  182. public static int bytesToInt(byte[] array, int offset)
  183. {
  184. int b1, b2, b3, b4;
  185. b1 = (array[offset++] << 24) & 0xFF000000;
  186. b2 = (array[offset++] << 16) & 0x00FF0000;
  187. b3 = (array[offset++] << 8) & 0x0000FF00;
  188. b4 = (array[offset++] << 0) & 0x000000FF;
  189. return (b1 | b2 | b3 | b4);
  190. }
  191. /** Marshal an integer to a byte array.
  192. The bytes are in BIGENDIAN order.
  193. i.e. array[offset] is the most-significant-byte
  194. and array[offset+3] is the least-significant-byte.
  195. @param array The array of bytes.
  196. @param offset The offset from which to start marshalling.
  197. */
  198. public static void intToBytes(int value, byte[] array, int offset)
  199. {
  200. array[offset++] = (byte)((value >>> 24) & 0xFF);
  201. array[offset++] = (byte)((value >>> 16) & 0xFF);
  202. array[offset++] = (byte)((value >>> 8) & 0xFF);
  203. array[offset++] = (byte)((value >>> 0) & 0xFF);
  204. }
  205. /** Converts an Ascii Character into Hexadecimal digit
  206. */
  207. public static int hexOf( char x )
  208. {
  209. int val;
  210. val = x - '0';
  211. if (val >=0 && val <= 9)
  212. return val;
  213. val = (x - 'a') + 10;
  214. if (val >= 10 && val <= 15)
  215. return val;
  216. val = (x - 'A') + 10;
  217. if (val >= 10 && val <= 15)
  218. return val;
  219. throw wrapper.badHexDigit() ;
  220. }
  221. // method moved from util.Utility
  222. /**
  223. * Static method for writing a CORBA standard exception to a stream.
  224. * @param strm The OutputStream to use for marshaling.
  225. */
  226. public static void writeSystemException(SystemException ex, OutputStream strm)
  227. {
  228. String s;
  229. s = repositoryIdOf(ex.getClass().getName());
  230. strm.write_string(s);
  231. strm.write_long(ex.minor);
  232. strm.write_long(ex.completed.value());
  233. }
  234. /**
  235. * Static method for reading a CORBA standard exception from a stream.
  236. * @param strm The InputStream to use for unmarshaling.
  237. */
  238. public static SystemException readSystemException(InputStream strm)
  239. {
  240. try {
  241. String name = classNameOf(strm.read_string());
  242. SystemException ex
  243. = (SystemException)ORBClassLoader.loadClass(name).newInstance();
  244. ex.minor = strm.read_long();
  245. ex.completed = CompletionStatus.from_int(strm.read_long());
  246. return ex;
  247. } catch ( Exception ex ) {
  248. throw wrapper.unknownSysex( CompletionStatus.COMPLETED_MAYBE, ex );
  249. }
  250. }
  251. /**
  252. * Get the class name corresponding to a particular repository Id.
  253. * This is used by the system to unmarshal (instantiate) the
  254. * appropriate exception class for an marshaled as the value of
  255. * its repository Id.
  256. * @param repositoryId The repository Id for which we want a class name.
  257. */
  258. public static String classNameOf(String repositoryId)
  259. {
  260. String className=null;
  261. className = (String) exceptionClassNames.get(repositoryId);
  262. if (className == null)
  263. className = "org.omg.CORBA.UNKNOWN";
  264. return className;
  265. }
  266. /**
  267. * Return true if this repositoryId is a SystemException.
  268. * @param repositoryId The repository Id to check.
  269. */
  270. public static boolean isSystemException(String repositoryId)
  271. {
  272. String className=null;
  273. className = (String) exceptionClassNames.get(repositoryId);
  274. if (className == null)
  275. return false;
  276. else
  277. return true;
  278. }
  279. /**
  280. * @return the Java serialization encoding version.
  281. */
  282. public static byte getEncodingVersion(ORB orb, IOR ior) {
  283. // Is Java serialization enabled?
  284. // Check the JavaSerializationComponent (tagged component)
  285. // in the IIOPProfile. If present, the peer ORB's GIOP is capable
  286. // of using Java serialization instead of CDR serialization.
  287. // In such a case, use Java serialization, iff the java serialization
  288. // versions match.
  289. if (orb.getORBData().isJavaSerializationEnabled()) {
  290. IIOPProfile prof = ior.getProfile();
  291. IIOPProfileTemplate profTemp =
  292. (IIOPProfileTemplate) prof.getTaggedProfileTemplate();
  293. java.util.Iterator iter = profTemp.iteratorById(
  294. ORBConstants.TAG_JAVA_SERIALIZATION_ID);
  295. if (iter.hasNext()) {
  296. JavaSerializationComponent jc =
  297. (JavaSerializationComponent) iter.next();
  298. byte jcVersion = jc.javaSerializationVersion();
  299. if (jcVersion >= Message.JAVA_ENC_VERSION) {
  300. return Message.JAVA_ENC_VERSION;
  301. } else if (jcVersion > Message.CDR_ENC_VERSION) {
  302. return jc.javaSerializationVersion();
  303. } else {
  304. // throw error?
  305. // Since encodingVersion is <= 0 (CDR_ENC_VERSION).
  306. }
  307. }
  308. }
  309. return Message.CDR_ENC_VERSION; // default
  310. }
  311. /**
  312. * Get the repository id corresponding to a particular class.
  313. * This is used by the system to write the
  314. * appropriate repository id for a system exception.
  315. * @param name The class name of the system exception.
  316. */
  317. public static String repositoryIdOf(String name)
  318. {
  319. String id;
  320. id = (String) exceptionRepositoryIds.get(name);
  321. if (id == null)
  322. id = "IDL:omg.org/CORBA/UNKNOWN:1.0";
  323. return id;
  324. }
  325. private static final Hashtable exceptionClassNames = new Hashtable();
  326. private static final Hashtable exceptionRepositoryIds = new Hashtable();
  327. static {
  328. //
  329. // construct repositoryId -> className hashtable
  330. //
  331. exceptionClassNames.put("IDL:omg.org/CORBA/BAD_CONTEXT:1.0",
  332. "org.omg.CORBA.BAD_CONTEXT");
  333. exceptionClassNames.put("IDL:omg.org/CORBA/BAD_INV_ORDER:1.0",
  334. "org.omg.CORBA.BAD_INV_ORDER");
  335. exceptionClassNames.put("IDL:omg.org/CORBA/BAD_OPERATION:1.0",
  336. "org.omg.CORBA.BAD_OPERATION");
  337. exceptionClassNames.put("IDL:omg.org/CORBA/BAD_PARAM:1.0",
  338. "org.omg.CORBA.BAD_PARAM");
  339. exceptionClassNames.put("IDL:omg.org/CORBA/BAD_TYPECODE:1.0",
  340. "org.omg.CORBA.BAD_TYPECODE");
  341. exceptionClassNames.put("IDL:omg.org/CORBA/COMM_FAILURE:1.0",
  342. "org.omg.CORBA.COMM_FAILURE");
  343. exceptionClassNames.put("IDL:omg.org/CORBA/DATA_CONVERSION:1.0",
  344. "org.omg.CORBA.DATA_CONVERSION");
  345. exceptionClassNames.put("IDL:omg.org/CORBA/IMP_LIMIT:1.0",
  346. "org.omg.CORBA.IMP_LIMIT");
  347. exceptionClassNames.put("IDL:omg.org/CORBA/INTF_REPOS:1.0",
  348. "org.omg.CORBA.INTF_REPOS");
  349. exceptionClassNames.put("IDL:omg.org/CORBA/INTERNAL:1.0",
  350. "org.omg.CORBA.INTERNAL");
  351. exceptionClassNames.put("IDL:omg.org/CORBA/INV_FLAG:1.0",
  352. "org.omg.CORBA.INV_FLAG");
  353. exceptionClassNames.put("IDL:omg.org/CORBA/INV_IDENT:1.0",
  354. "org.omg.CORBA.INV_IDENT");
  355. exceptionClassNames.put("IDL:omg.org/CORBA/INV_OBJREF:1.0",
  356. "org.omg.CORBA.INV_OBJREF");
  357. exceptionClassNames.put("IDL:omg.org/CORBA/MARSHAL:1.0",
  358. "org.omg.CORBA.MARSHAL");
  359. exceptionClassNames.put("IDL:omg.org/CORBA/NO_MEMORY:1.0",
  360. "org.omg.CORBA.NO_MEMORY");
  361. exceptionClassNames.put("IDL:omg.org/CORBA/FREE_MEM:1.0",
  362. "org.omg.CORBA.FREE_MEM");
  363. exceptionClassNames.put("IDL:omg.org/CORBA/NO_IMPLEMENT:1.0",
  364. "org.omg.CORBA.NO_IMPLEMENT");
  365. exceptionClassNames.put("IDL:omg.org/CORBA/NO_PERMISSION:1.0",
  366. "org.omg.CORBA.NO_PERMISSION");
  367. exceptionClassNames.put("IDL:omg.org/CORBA/NO_RESOURCES:1.0",
  368. "org.omg.CORBA.NO_RESOURCES");
  369. exceptionClassNames.put("IDL:omg.org/CORBA/NO_RESPONSE:1.0",
  370. "org.omg.CORBA.NO_RESPONSE");
  371. exceptionClassNames.put("IDL:omg.org/CORBA/OBJ_ADAPTER:1.0",
  372. "org.omg.CORBA.OBJ_ADAPTER");
  373. exceptionClassNames.put("IDL:omg.org/CORBA/INITIALIZE:1.0",
  374. "org.omg.CORBA.INITIALIZE");
  375. exceptionClassNames.put("IDL:omg.org/CORBA/PERSIST_STORE:1.0",
  376. "org.omg.CORBA.PERSIST_STORE");
  377. exceptionClassNames.put("IDL:omg.org/CORBA/TRANSIENT:1.0",
  378. "org.omg.CORBA.TRANSIENT");
  379. exceptionClassNames.put("IDL:omg.org/CORBA/UNKNOWN:1.0",
  380. "org.omg.CORBA.UNKNOWN");
  381. exceptionClassNames.put("IDL:omg.org/CORBA/OBJECT_NOT_EXIST:1.0",
  382. "org.omg.CORBA.OBJECT_NOT_EXIST");
  383. // SystemExceptions from OMG Transactions Service Spec
  384. exceptionClassNames.put("IDL:omg.org/CORBA/INVALID_TRANSACTION:1.0",
  385. "org.omg.CORBA.INVALID_TRANSACTION");
  386. exceptionClassNames.put("IDL:omg.org/CORBA/TRANSACTION_REQUIRED:1.0",
  387. "org.omg.CORBA.TRANSACTION_REQUIRED");
  388. exceptionClassNames.put("IDL:omg.org/CORBA/TRANSACTION_ROLLEDBACK:1.0",
  389. "org.omg.CORBA.TRANSACTION_ROLLEDBACK");
  390. // from portability RTF 98-07-01.txt
  391. exceptionClassNames.put("IDL:omg.org/CORBA/INV_POLICY:1.0",
  392. "org.omg.CORBA.INV_POLICY");
  393. // from orbrev/00-09-01 (CORBA 2.4 Draft Specification)
  394. exceptionClassNames.
  395. put("IDL:omg.org/CORBA/TRANSACTION_UNAVAILABLE:1.0",
  396. "org.omg.CORBA.TRANSACTION_UNAVAILABLE");
  397. exceptionClassNames.put("IDL:omg.org/CORBA/TRANSACTION_MODE:1.0",
  398. "org.omg.CORBA.TRANSACTION_MODE");
  399. // Exception types introduced between CORBA 2.4 and 3.0
  400. exceptionClassNames.put("IDL:omg.org/CORBA/CODESET_INCOMPATIBLE:1.0",
  401. "org.omg.CORBA.CODESET_INCOMPATIBLE");
  402. exceptionClassNames.put("IDL:omg.org/CORBA/REBIND:1.0",
  403. "org.omg.CORBA.REBIND");
  404. exceptionClassNames.put("IDL:omg.org/CORBA/TIMEOUT:1.0",
  405. "org.omg.CORBA.TIMEOUT");
  406. exceptionClassNames.put("IDL:omg.org/CORBA/BAD_QOS:1.0",
  407. "org.omg.CORBA.BAD_QOS");
  408. // Exception types introduced in CORBA 3.0
  409. exceptionClassNames.put("IDL:omg.org/CORBA/INVALID_ACTIVITY:1.0",
  410. "org.omg.CORBA.INVALID_ACTIVITY");
  411. exceptionClassNames.put("IDL:omg.org/CORBA/ACTIVITY_COMPLETED:1.0",
  412. "org.omg.CORBA.ACTIVITY_COMPLETED");
  413. exceptionClassNames.put("IDL:omg.org/CORBA/ACTIVITY_REQUIRED:1.0",
  414. "org.omg.CORBA.ACTIVITY_REQUIRED");
  415. //
  416. // construct className -> repositoryId hashtable
  417. //
  418. Enumeration keys = exceptionClassNames.keys();
  419. java.lang.Object s;
  420. String rId;
  421. String cName;
  422. try{
  423. while (keys.hasMoreElements()) {
  424. s = keys.nextElement();
  425. rId = (String) s;
  426. cName = (String) exceptionClassNames.get(rId);
  427. exceptionRepositoryIds.put (cName, rId);
  428. }
  429. } catch (NoSuchElementException e) { }
  430. }
  431. /** Parse a version string such as "1.1.6" or "jdk1.2fcs" into
  432. a version array of integers {1, 1, 6} or {1, 2}.
  433. A string of "n." or "n..m" is equivalent to "n.0" or "n.0.m" respectively.
  434. */
  435. public static int[] parseVersion(String version) {
  436. if (version == null)
  437. return new int[0];
  438. char[] s = version.toCharArray();
  439. //find the maximum span of the string "n.n.n..." where n is an integer
  440. int start = 0;
  441. for (; start < s.length && (s[start] < '0' || s[start] > '9'); ++start)
  442. if (start == s.length) //no digit found
  443. return new int[0];
  444. int end = start + 1;
  445. int size = 1;
  446. for (; end < s.length; ++end)
  447. if (s[end] == '.')
  448. ++size;
  449. else if (s[end] < '0' || s[end] > '9')
  450. break;
  451. int[] val = new int[size];
  452. for (int i = 0; i < size; ++i) {
  453. int dot = version.indexOf('.', start);
  454. if (dot == -1 || dot > end)
  455. dot = end;
  456. if (start >= dot) //cases like "n." or "n..m"
  457. val[i] = 0; //convert equivalent to "n.0" or "n.0.m"
  458. else
  459. val[i] = Integer.parseInt(version.substring(start, dot));
  460. start = dot + 1;
  461. }
  462. return val;
  463. }
  464. /** Compare two version arrays.
  465. Return 1, 0 or -1 if v1 is greater than, equal to, or less than v2.
  466. */
  467. public static int compareVersion(int[] v1, int[] v2) {
  468. if (v1 == null)
  469. v1 = new int[0];
  470. if (v2 == null)
  471. v2 = new int[0];
  472. for (int i = 0; i < v1.length; ++i) {
  473. if (i >= v2.length || v1[i] > v2[i]) //v1 is longer or greater than v2
  474. return 1;
  475. if (v1[i] < v2[i])
  476. return -1;
  477. }
  478. return v1.length == v2.length ? 0 : -1;
  479. }
  480. /** Compare two version strings.
  481. Return 1, 0 or -1 if v1 is greater than, equal to, or less than v2.
  482. */
  483. public static synchronized int compareVersion(String v1, String v2) {
  484. return compareVersion(parseVersion(v1), parseVersion(v2));
  485. }
  486. private static String compressClassName( String name )
  487. {
  488. // Note that this must end in . in order to be renamed correctly.
  489. String prefix = "com.sun.corba.se." ;
  490. if (name.startsWith( prefix ) ) {
  491. return "(ORB)." + name.substring( prefix.length() ) ;
  492. } else
  493. return name ;
  494. }
  495. // Return a compressed representation of the thread name. This is particularly
  496. // useful on the server side, where there are many SelectReaderThreads, and
  497. // we need a short unambiguous name for such threads.
  498. public static String getThreadName( Thread thr )
  499. {
  500. if (thr == null)
  501. return "null" ;
  502. // This depends on the formatting in SelectReaderThread and CorbaConnectionImpl.
  503. // Pattern for SelectReaderThreads:
  504. // SelectReaderThread CorbaConnectionImpl[ <host> <post> <state>]
  505. // Any other pattern in the Thread's name is just returned.
  506. String name = thr.getName() ;
  507. StringTokenizer st = new StringTokenizer( name ) ;
  508. int numTokens = st.countTokens() ;
  509. if (numTokens != 5)
  510. return name ;
  511. String[] tokens = new String[numTokens] ;
  512. for (int ctr=0; ctr<numTokens; ctr++ )
  513. tokens[ctr] = st.nextToken() ;
  514. if( !tokens[0].equals("SelectReaderThread"))
  515. return name ;
  516. return "SelectReaderThread[" + tokens[2] + ":" + tokens[3] + "]" ;
  517. }
  518. private static String formatStackTraceElement( StackTraceElement ste )
  519. {
  520. return compressClassName( ste.getClassName() ) + "." + ste.getMethodName() +
  521. (ste.isNativeMethod() ? "(Native Method)" :
  522. (ste.getFileName() != null && ste.getLineNumber() >= 0 ?
  523. "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")" :
  524. (ste.getFileName() != null ? "("+ste.getFileName()+")" : "(Unknown Source)")));
  525. }
  526. private static void printStackTrace( StackTraceElement[] trace )
  527. {
  528. System.out.println( " Stack Trace:" ) ;
  529. // print the stack trace, ommitting the zeroth element, which is
  530. // always this method.
  531. for ( int ctr = 1; ctr < trace.length; ctr++ ) {
  532. System.out.print( " >" ) ;
  533. System.out.println( formatStackTraceElement( trace[ctr] ) ) ;
  534. }
  535. }
  536. //
  537. // Implements all dprint calls in this package.
  538. //
  539. public static synchronized void dprint(java.lang.Object obj, String msg) {
  540. System.out.println(
  541. compressClassName( obj.getClass().getName() ) + "(" +
  542. getThreadName( Thread.currentThread() ) + "): " + msg);
  543. }
  544. public static synchronized void dprint(String className, String msg) {
  545. System.out.println(
  546. compressClassName( className ) + "(" +
  547. getThreadName( Thread.currentThread() ) + "): " + msg);
  548. }
  549. public synchronized void dprint(String msg) {
  550. ORBUtility.dprint(this, msg);
  551. }
  552. public static synchronized void dprintTrace(Object obj, String msg) {
  553. ORBUtility.dprint(obj, msg);
  554. Throwable thr = new Throwable() ;
  555. printStackTrace( thr.getStackTrace() ) ;
  556. }
  557. public static synchronized void dprint(java.lang.Object caller,
  558. String msg, Throwable t)
  559. {
  560. System.out.println(
  561. compressClassName( caller.getClass().getName() ) +
  562. '(' + Thread.currentThread() + "): " + msg);
  563. if (t != null)
  564. printStackTrace( t.getStackTrace() ) ;
  565. }
  566. public static String[] concatenateStringArrays( String[] arr1, String[] arr2 )
  567. {
  568. String[] result = new String[
  569. arr1.length + arr2.length ] ;
  570. for (int ctr = 0; ctr<arr1.length; ctr++)
  571. result[ctr] = arr1[ctr] ;
  572. for (int ctr = 0; ctr<arr2.length; ctr++)
  573. result[ctr + arr1.length] = arr2[ctr] ;
  574. return result ;
  575. }
  576. /**
  577. * Throws the CORBA equivalent of a java.io.NotSerializableException
  578. *
  579. * Duplicated from util/Utility for Pure ORB reasons. There are two
  580. * reasons for this:
  581. *
  582. * 1) We can't introduce dependencies on the util version from outside
  583. * of the io/util packages since it will not exist in the pure ORB
  584. * build running on JDK 1.3.x.
  585. *
  586. * 2) We need to pick up the correct minor code from OMGSystemException.
  587. */
  588. public static void throwNotSerializableForCorba(String className) {
  589. throw omgWrapper.notSerializable( CompletionStatus.COMPLETED_MAYBE,
  590. className ) ;
  591. }
  592. /**
  593. * Returns the maximum stream format version supported by our
  594. * ValueHandler.
  595. */
  596. public static byte getMaxStreamFormatVersion() {
  597. ValueHandler vh = Util.createValueHandler();
  598. if (!(vh instanceof javax.rmi.CORBA.ValueHandlerMultiFormat))
  599. return ORBConstants.STREAM_FORMAT_VERSION_1;
  600. else
  601. return ((ValueHandlerMultiFormat)vh).getMaximumStreamFormatVersion();
  602. }
  603. public static CorbaClientDelegate makeClientDelegate( IOR ior )
  604. {
  605. ORB orb = ior.getORB() ;
  606. CorbaContactInfoList ccil = orb.getCorbaContactInfoListFactory().create( ior ) ;
  607. CorbaClientDelegate del = orb.getClientDelegateFactory().create(ccil);
  608. return del ;
  609. }
  610. /** This method is used to create untyped object references.
  611. */
  612. public static org.omg.CORBA.Object makeObjectReference( IOR ior )
  613. {
  614. CorbaClientDelegate del = makeClientDelegate( ior ) ;
  615. org.omg.CORBA.Object objectImpl = new CORBAObjectImpl() ;
  616. StubAdapter.setDelegate( objectImpl, del ) ;
  617. return objectImpl ;
  618. }
  619. /** This method obtains an IOR from a CORBA object reference.
  620. * It will return null if obj is a local object, a null object,
  621. * or an object implemented by a different ORB. It will
  622. * throw BAD_OPERATION if obj is an unconnected RMI-IIOP object.
  623. * @return IOR the IOR that represents this objref. This will
  624. * never be null.
  625. * @exception BAD_OPERATION (from oi._get_delegate) if obj is a
  626. * normal objref, but does not have a delegate set.
  627. * @exception BAD_PARAM if obj is a local object, or else was
  628. * created by a foreign ORB.
  629. */
  630. public static IOR getIOR( org.omg.CORBA.Object obj )
  631. {
  632. if (obj == null)
  633. throw wrapper.nullObjectReference() ;
  634. IOR ior = null ;
  635. if (StubAdapter.isStub(obj)) {
  636. org.omg.CORBA.portable.Delegate del = StubAdapter.getDelegate(
  637. obj ) ;
  638. if (del instanceof CorbaClientDelegate) {
  639. CorbaClientDelegate cdel = (CorbaClientDelegate)del ;
  640. ContactInfoList cil = cdel.getContactInfoList() ;
  641. if (cil instanceof CorbaContactInfoList) {
  642. CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
  643. ior = ccil.getTargetIOR() ;
  644. if (ior == null)
  645. throw wrapper.nullIor() ;
  646. return ior ;
  647. } else {
  648. // This is our code, but the ContactInfoList is not a
  649. // CorbaContactInfoList. This should not happen, because
  650. // we are in the CORBA application of the DCSA framework.
  651. // This is a coding error, and thus an INTERNAL exception
  652. // should be thrown.
  653. // XXX needs minor code
  654. throw new INTERNAL() ;
  655. }
  656. }
  657. // obj is implemented by a foreign ORB, because the Delegate is not a
  658. // ClientDelegate.
  659. // XXX this case could be handled by marshalling and
  660. // unmarshalling. However, object_to_string cannot be used
  661. // here, as it is implemented with getIOR. Note that this
  662. // will require access to an ORB, so that we can create streams
  663. // as needed. The ORB is available simply as io._orb().
  664. throw wrapper.objrefFromForeignOrb() ;
  665. } else
  666. throw wrapper.localObjectNotAllowed() ;
  667. }
  668. /** Obtains an IOR for the object reference obj, first connecting it to
  669. * the ORB if necessary.
  670. * @return IOR the IOR that represents this objref. This will
  671. * never be null.
  672. * @exception BAD_OPERATION if the object could not be connected,
  673. * if a connection attempt was needed.
  674. * @exception BAD_PARAM if obj is a local object, or else was
  675. * created by a foreign ORB.
  676. */
  677. public static IOR connectAndGetIOR( ORB orb, org.omg.CORBA.Object obj )
  678. {
  679. IOR result ;
  680. try {
  681. result = getIOR( obj ) ;
  682. } catch (BAD_OPERATION bop) {
  683. if (StubAdapter.isStub(obj)) {
  684. try {
  685. StubAdapter.connect( obj, orb ) ;
  686. } catch (java.rmi.RemoteException exc) {
  687. throw wrapper.connectingServant( exc ) ;
  688. }
  689. } else {
  690. orb.connect( obj ) ;
  691. }
  692. result = getIOR( obj ) ;
  693. }
  694. return result ;
  695. }
  696. public static void setDaemon(Thread thread)
  697. {
  698. // Catch exceptions since setDaemon can cause a
  699. // security exception to be thrown under netscape
  700. // in the Applet mode
  701. final Thread finalThread = thread;
  702. try {
  703. AccessController.doPrivileged(new PrivilegedAction() {
  704. public java.lang.Object run() {
  705. finalThread.setDaemon(true);
  706. return null;
  707. }
  708. });
  709. } catch (Exception e) {
  710. // REVISIT: Object to get static method. Ignore it.
  711. dprint(new Object(), "setDaemon: Exception: " + e);
  712. }
  713. }
  714. public static String operationNameAndRequestId(CorbaMessageMediator m)
  715. {
  716. return "op/" + m.getOperationName() + " id/" + m.getRequestId();
  717. }
  718. public static boolean isPrintable(char c)
  719. {
  720. if (Character.isJavaIdentifierStart(c)) {
  721. // Letters and $ _
  722. return true;
  723. }
  724. if (Character.isDigit(c)) {
  725. return true;
  726. }
  727. switch (Character.getType(c)) {
  728. case Character.MODIFIER_SYMBOL : return true; // ` ^
  729. case Character.DASH_PUNCTUATION : return true; // -
  730. case Character.MATH_SYMBOL : return true; // = ~ + | < >
  731. case Character.OTHER_PUNCTUATION : return true; // !@#%&*;':",./?
  732. case Character.START_PUNCTUATION : return true; // ( [ {
  733. case Character.END_PUNCTUATION : return true; // ) ] }
  734. }
  735. return false;
  736. }
  737. public static String getClassSecurityInfo(final Class cl)
  738. {
  739. // Returns a String which looks similar to:
  740. // PermissionCollection java.security.Permissions@1053693 ...
  741. // (java.io.FilePermission <<ALL FILES>> ....)
  742. // (java.io.FilePermission /export0/sunwappserv/lib/- ...)
  743. // ... other permissions ...
  744. // Domain ProtectionDomain (file:/export0/sunwappserv/lib-)
  745. // java.security.Permissions@141fedb (
  746. // (java.io.FilePermission <<ALL FILES>> ...)
  747. // (java.io.FilePermission /var/tmp//- ...)
  748. String result =
  749. (String)AccessController.doPrivileged(new PrivilegedAction() {
  750. public java.lang.Object run() {
  751. StringBuffer sb = new StringBuffer(500);
  752. ProtectionDomain pd = cl.getProtectionDomain();
  753. Policy policy = Policy.getPolicy();
  754. PermissionCollection pc = policy.getPermissions(pd);
  755. sb.append("\nPermissionCollection ");
  756. sb.append(pc.toString());
  757. // Don't need to add 'Protection Domain' string, it's
  758. // in ProtectionDomain.toString() already.
  759. sb.append(pd.toString());
  760. return sb.toString();
  761. }
  762. });
  763. return result;
  764. }
  765. }
  766. // End of file.