001: /*
002: * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.spi.ior.iiop;
027:
028: import com.sun.corba.se.spi.ior.IOR;
029: import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
030: import com.sun.corba.se.spi.orb.ORB;
031: import com.sun.corba.se.spi.orb.ORBVersion;
032: import com.sun.corba.se.spi.orb.ORBVersionFactory;
033:
034: import com.sun.corba.se.impl.orbutil.ORBUtility;
035: import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
036:
037: public class GIOPVersion {
038:
039: // Static fields
040:
041: public static final GIOPVersion V1_0 = new GIOPVersion((byte) 1,
042: (byte) 0);
043: public static final GIOPVersion V1_1 = new GIOPVersion((byte) 1,
044: (byte) 1);
045: public static final GIOPVersion V1_2 = new GIOPVersion((byte) 1,
046: (byte) 2);
047: public static final GIOPVersion V1_3 = new GIOPVersion((byte) 1,
048: (byte) 3);
049:
050: // Major version 13 indicates Java serialization,
051: // Minor version [00-FF] is the version number.
052: public static final GIOPVersion V13_XX = new GIOPVersion((byte) 13,
053: (byte) Message.JAVA_ENC_VERSION);
054:
055: public static final GIOPVersion DEFAULT_VERSION = V1_2;
056:
057: public static final int VERSION_1_0 = 0x0100;
058: public static final int VERSION_1_1 = 0x0101;
059: public static final int VERSION_1_2 = 0x0102;
060: public static final int VERSION_1_3 = 0x0103;
061: public static final int VERSION_13_XX = ((0x0D << 8) & 0x0000FF00)
062: | Message.JAVA_ENC_VERSION;
063:
064: // Instance variables
065:
066: private byte major = (byte) 0;
067: private byte minor = (byte) 0;
068:
069: // Constructor
070:
071: public GIOPVersion() {
072: }
073:
074: public GIOPVersion(byte majorB, byte minorB) {
075: this .major = majorB;
076: this .minor = minorB;
077: }
078:
079: public GIOPVersion(int major, int minor) {
080: this .major = (byte) major;
081: this .minor = (byte) minor;
082: }
083:
084: // Accessor methods
085:
086: public byte getMajor() {
087: return this .major;
088: }
089:
090: public byte getMinor() {
091: return this .minor;
092: }
093:
094: // General methods
095:
096: public boolean equals(GIOPVersion gv) {
097: return gv.major == this .major && gv.minor == this .minor;
098: }
099:
100: public boolean equals(Object obj) {
101: if (obj != null && (obj instanceof GIOPVersion))
102: return equals((GIOPVersion) obj);
103: else
104: return false;
105: }
106:
107: public int hashCode() {
108: return 37 * major + minor;
109: }
110:
111: public boolean lessThan(GIOPVersion gv) {
112: if (this .major < gv.major) {
113: return true;
114: } else if (this .major == gv.major) {
115: if (this .minor < gv.minor) {
116: return true;
117: }
118: }
119:
120: return false;
121: }
122:
123: public int intValue() {
124: return (major << 8 | minor);
125: }
126:
127: public String toString() {
128: return major + "." + minor;
129: }
130:
131: public static GIOPVersion getInstance(byte major, byte minor) {
132: switch (((major << 8) | minor)) {
133: case VERSION_1_0:
134: return GIOPVersion.V1_0;
135: case VERSION_1_1:
136: return GIOPVersion.V1_1;
137: case VERSION_1_2:
138: return GIOPVersion.V1_2;
139: case VERSION_1_3:
140: return GIOPVersion.V1_3;
141: case VERSION_13_XX:
142: return GIOPVersion.V13_XX;
143: default:
144: return new GIOPVersion(major, minor);
145: }
146: }
147:
148: public static GIOPVersion parseVersion(String s) {
149: int dotIdx = s.indexOf('.');
150:
151: if (dotIdx < 1 || dotIdx == s.length() - 1)
152: throw new NumberFormatException(
153: "GIOP major, minor, and decimal point required: "
154: + s);
155:
156: int major = Integer.parseInt(s.substring(0, dotIdx));
157: int minor = Integer.parseInt(s
158: .substring(dotIdx + 1, s.length()));
159:
160: return GIOPVersion.getInstance((byte) major, (byte) minor);
161: }
162:
163: /**
164: * This chooses the appropriate GIOP version.
165: *
166: * @return the GIOP version 13.00 if Java serialization is enabled, or
167: * smallest(profGIOPVersion, orbGIOPVersion)
168: */
169: public static GIOPVersion chooseRequestVersion(ORB orb, IOR ior) {
170:
171: GIOPVersion orbVersion = orb.getORBData().getGIOPVersion();
172: IIOPProfile prof = ior.getProfile();
173: GIOPVersion profVersion = prof.getGIOPVersion();
174:
175: // Check if the profile is from a legacy Sun ORB.
176:
177: ORBVersion targetOrbVersion = prof.getORBVersion();
178: if (!(targetOrbVersion.equals(ORBVersionFactory.getFOREIGN()))
179: && targetOrbVersion.lessThan(ORBVersionFactory
180: .getNEWER())) {
181: // we are dealing with a SUN legacy orb which emits 1.1 IORs,
182: // in spite of being able to handle only GIOP 1.0 messages.
183: return V1_0;
184: }
185:
186: // Now the target has to be (FOREIGN | NEWER*)
187:
188: byte prof_major = profVersion.getMajor();
189: byte prof_minor = profVersion.getMinor();
190:
191: byte orb_major = orbVersion.getMajor();
192: byte orb_minor = orbVersion.getMinor();
193:
194: if (orb_major < prof_major) {
195: return orbVersion;
196: } else if (orb_major > prof_major) {
197: return profVersion;
198: } else { // both major version are the same
199: if (orb_minor <= prof_minor) {
200: return orbVersion;
201: } else {
202: return profVersion;
203: }
204: }
205: }
206:
207: public boolean supportsIORIIOPProfileComponents() {
208: return getMinor() > 0 || getMajor() > 1;
209: }
210:
211: // IO methods
212:
213: public void read(org.omg.CORBA.portable.InputStream istream) {
214: this .major = istream.read_octet();
215: this .minor = istream.read_octet();
216: }
217:
218: public void write(org.omg.CORBA.portable.OutputStream ostream) {
219: ostream.write_octet(this.major);
220: ostream.write_octet(this.minor);
221: }
222: }
|