1. /*
  2. * @(#)GIOPVersion.java 1.16 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.spi.ior.iiop ;
  8. import com.sun.corba.se.spi.ior.IOR ;
  9. import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
  10. import com.sun.corba.se.spi.orb.ORB;
  11. import com.sun.corba.se.spi.orb.ORBVersion;
  12. import com.sun.corba.se.spi.orb.ORBVersionFactory;
  13. import com.sun.corba.se.impl.orbutil.ORBUtility;
  14. import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  15. public class GIOPVersion {
  16. // Static fields
  17. public static final GIOPVersion V1_0 = new GIOPVersion((byte)1, (byte)0);
  18. public static final GIOPVersion V1_1 = new GIOPVersion((byte)1, (byte)1);
  19. public static final GIOPVersion V1_2 = new GIOPVersion((byte)1, (byte)2);
  20. public static final GIOPVersion V1_3 = new GIOPVersion((byte)1, (byte)3);
  21. // Major version 13 indicates Java serialization,
  22. // Minor version [00-FF] is the version number.
  23. public static final GIOPVersion V13_XX =
  24. new GIOPVersion((byte)13, (byte)Message.JAVA_ENC_VERSION);
  25. public static final GIOPVersion DEFAULT_VERSION = V1_2;
  26. public static final int VERSION_1_0 = 0x0100;
  27. public static final int VERSION_1_1 = 0x0101;
  28. public static final int VERSION_1_2 = 0x0102;
  29. public static final int VERSION_1_3 = 0x0103;
  30. public static final int VERSION_13_XX =
  31. ((0x0D << 8) & 0x0000FF00) | Message.JAVA_ENC_VERSION;
  32. // Instance variables
  33. private byte major = (byte) 0;
  34. private byte minor = (byte) 0;
  35. // Constructor
  36. public GIOPVersion() {}
  37. public GIOPVersion(byte majorB, byte minorB) {
  38. this.major = majorB;
  39. this.minor = minorB;
  40. }
  41. public GIOPVersion(int major, int minor) {
  42. this.major = (byte)major;
  43. this.minor = (byte)minor;
  44. }
  45. // Accessor methods
  46. public byte getMajor() {
  47. return this.major;
  48. }
  49. public byte getMinor() {
  50. return this.minor;
  51. }
  52. // General methods
  53. public boolean equals(GIOPVersion gv){
  54. return gv.major == this.major && gv.minor == this.minor ;
  55. }
  56. public boolean equals(Object obj) {
  57. if (obj != null && (obj instanceof GIOPVersion))
  58. return equals((GIOPVersion)obj);
  59. else
  60. return false;
  61. }
  62. public int hashCode()
  63. {
  64. return 37*major + minor ;
  65. }
  66. public boolean lessThan(GIOPVersion gv) {
  67. if (this.major < gv.major) {
  68. return true;
  69. } else if (this.major == gv.major) {
  70. if (this.minor < gv.minor) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. public int intValue()
  77. {
  78. return (major << 8 | minor);
  79. }
  80. public String toString()
  81. {
  82. return major + "." + minor;
  83. }
  84. public static GIOPVersion getInstance(byte major, byte minor)
  85. {
  86. switch(((major << 8) | minor)) {
  87. case VERSION_1_0:
  88. return GIOPVersion.V1_0;
  89. case VERSION_1_1:
  90. return GIOPVersion.V1_1;
  91. case VERSION_1_2:
  92. return GIOPVersion.V1_2;
  93. case VERSION_1_3:
  94. return GIOPVersion.V1_3;
  95. case VERSION_13_XX:
  96. return GIOPVersion.V13_XX;
  97. default:
  98. return new GIOPVersion(major, minor);
  99. }
  100. }
  101. public static GIOPVersion parseVersion(String s)
  102. {
  103. int dotIdx = s.indexOf('.');
  104. if (dotIdx < 1 || dotIdx == s.length() - 1)
  105. throw new NumberFormatException("GIOP major, minor, and decimal point required: " + s);
  106. int major = Integer.parseInt(s.substring(0, dotIdx));
  107. int minor = Integer.parseInt(s.substring(dotIdx + 1, s.length()));
  108. return GIOPVersion.getInstance((byte)major, (byte)minor);
  109. }
  110. /**
  111. * This chooses the appropriate GIOP version.
  112. *
  113. * @return the GIOP version 13.00 if Java serialization is enabled, or
  114. * smallest(profGIOPVersion, orbGIOPVersion)
  115. */
  116. public static GIOPVersion chooseRequestVersion(ORB orb, IOR ior ) {
  117. GIOPVersion orbVersion = orb.getORBData().getGIOPVersion();
  118. IIOPProfile prof = ior.getProfile() ;
  119. GIOPVersion profVersion = prof.getGIOPVersion();
  120. // Check if the profile is from a legacy Sun ORB.
  121. ORBVersion targetOrbVersion = prof.getORBVersion();
  122. if (!(targetOrbVersion.equals(ORBVersionFactory.getFOREIGN())) &&
  123. targetOrbVersion.lessThan(ORBVersionFactory.getNEWER())) {
  124. // we are dealing with a SUN legacy orb which emits 1.1 IORs,
  125. // in spite of being able to handle only GIOP 1.0 messages.
  126. return V1_0;
  127. }
  128. // Now the target has to be (FOREIGN | NEWER*)
  129. byte prof_major = profVersion.getMajor();
  130. byte prof_minor = profVersion.getMinor();
  131. byte orb_major = orbVersion.getMajor();
  132. byte orb_minor = orbVersion.getMinor();
  133. if (orb_major < prof_major) {
  134. return orbVersion;
  135. } else if (orb_major > prof_major) {
  136. return profVersion;
  137. } else { // both major version are the same
  138. if (orb_minor <= prof_minor) {
  139. return orbVersion;
  140. } else {
  141. return profVersion;
  142. }
  143. }
  144. }
  145. public boolean supportsIORIIOPProfileComponents()
  146. {
  147. return getMinor() > 0 || getMajor() > 1;
  148. }
  149. // IO methods
  150. public void read(org.omg.CORBA.portable.InputStream istream) {
  151. this.major = istream.read_octet();
  152. this.minor = istream.read_octet();
  153. }
  154. public void write(org.omg.CORBA.portable.OutputStream ostream) {
  155. ostream.write_octet(this.major);
  156. ostream.write_octet(this.minor);
  157. }
  158. }