1. /*
  2. * @(#)GIOPVersion.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.core;
  8. public class GIOPVersion {
  9. // Static fields
  10. public static final GIOPVersion V1_0 = new GIOPVersion((byte)1, (byte)0);
  11. public static final GIOPVersion V1_1 = new GIOPVersion((byte)1, (byte)1);
  12. public static final GIOPVersion V1_2 = new GIOPVersion((byte)1, (byte)2);
  13. public static final GIOPVersion DEFAULT_VERSION = V1_2;
  14. public static final int VERSION_1_0 = 0x0100;
  15. public static final int VERSION_1_1 = 0x0101;
  16. public static final int VERSION_1_2 = 0x0102;
  17. // Instance variables
  18. public byte major = (byte) 0;
  19. public byte minor = (byte) 0;
  20. // Constructor
  21. public GIOPVersion() {}
  22. public GIOPVersion(byte majorB, byte minorB) {
  23. this.major = majorB;
  24. this.minor = minorB;
  25. }
  26. public GIOPVersion(int major, int minor) {
  27. this.major = (byte)major;
  28. this.minor = (byte)minor;
  29. }
  30. // Accessor methods
  31. public byte getMajor() {
  32. return this.major;
  33. }
  34. public byte getMinor() {
  35. return this.minor;
  36. }
  37. // General methods
  38. public boolean equals(GIOPVersion gv){
  39. return gv.major == this.major && gv.minor == this.minor ;
  40. }
  41. public boolean equals(Object obj) {
  42. if (obj != null && (obj instanceof GIOPVersion))
  43. return equals((GIOPVersion)obj);
  44. else
  45. return false;
  46. }
  47. public boolean lessThan(GIOPVersion gv) {
  48. if (this.major < gv.major) {
  49. return true;
  50. } else if (this.major == gv.major) {
  51. if (this.minor < gv.minor) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. public int intValue()
  58. {
  59. return (major << 8 | minor);
  60. }
  61. public String toString()
  62. {
  63. return major + "." + minor;
  64. }
  65. public static GIOPVersion getInstance(byte major, byte minor)
  66. {
  67. switch(((major << 8) | minor)) {
  68. case VERSION_1_0:
  69. return GIOPVersion.V1_0;
  70. case VERSION_1_1:
  71. return GIOPVersion.V1_1;
  72. case VERSION_1_2:
  73. return GIOPVersion.V1_2;
  74. default:
  75. return new GIOPVersion(major, minor);
  76. }
  77. }
  78. public static GIOPVersion parseVersion(String s)
  79. {
  80. int dotIdx = s.indexOf('.');
  81. if (dotIdx < 1 || dotIdx == s.length() - 1)
  82. throw new NumberFormatException("GIOP major, minor, and decimal point required: " + s);
  83. int major = Integer.parseInt(s.substring(0, dotIdx));
  84. int minor = Integer.parseInt(s.substring(dotIdx + 1, s.length()));
  85. return GIOPVersion.getInstance((byte)major, (byte)minor);
  86. }
  87. /**
  88. * This computes the smallest among the iorGIOPVersion and orbGIOPVersion.
  89. *
  90. * @return the smallest(iorGIOPVersion, orbGIOPVersion)
  91. */
  92. public static GIOPVersion chooseRequestVersion(ORB orb, IOR ior) {
  93. GIOPVersion orbVersion = orb.getGIOPVersion();
  94. GIOPVersion iorVersion = ior.getGIOPVersion();
  95. // Check if the IOR is from a legacy Sun ORB.
  96. ORBVersionImpl targetOrbVersion = (ORBVersionImpl) ior.getORBVersion();
  97. if (!(targetOrbVersion.equals(ORBVersionImpl.FOREIGN)) &&
  98. targetOrbVersion.lessThan(ORBVersionImpl.NEWER)) {
  99. // we are dealing with a SUN legacy orb which emits 1.1 IORs,
  100. // in spite of being able to handle only GIOP 1.0 messages.
  101. return V1_0;
  102. }
  103. // Now the target has to be (FOREIGN | NEWER*)
  104. byte ior_major = iorVersion.getMajor();
  105. byte ior_minor = iorVersion.getMinor();
  106. byte orb_major = orbVersion.getMajor();
  107. byte orb_minor = orbVersion.getMinor();
  108. if (orb_major < ior_major) {
  109. return orbVersion;
  110. } else if (orb_major > ior_major) {
  111. return iorVersion;
  112. } else { // both major version are the same
  113. if (orb_minor <= ior_minor) {
  114. return orbVersion;
  115. } else {
  116. return iorVersion;
  117. }
  118. }
  119. }
  120. /* Could be useful for Messages?
  121. public GIOPVersion(Message m){
  122. this( m.getGIOPMajorVersion() , m.getGIOPMinorVersion());
  123. }
  124. public boolean hasSameGIOPVersion(Message m){
  125. return this.equals(m.getGIOPVersion());
  126. }
  127. */
  128. // IO methods
  129. public void read(org.omg.CORBA.portable.InputStream istream) {
  130. this.major = istream.read_octet();
  131. this.minor = istream.read_octet();
  132. }
  133. public void write(org.omg.CORBA.portable.OutputStream ostream) {
  134. ostream.write_octet(this.major);
  135. ostream.write_octet(this.minor);
  136. }
  137. }