KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > ior > iiop > GIOPVersion


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
8 package com.sun.corba.se.spi.ior.iiop ;
9
10 import com.sun.corba.se.spi.ior.IOR ;
11 import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
12 import com.sun.corba.se.spi.orb.ORB;
13 import com.sun.corba.se.spi.orb.ORBVersion;
14 import com.sun.corba.se.spi.orb.ORBVersionFactory;
15
16 import com.sun.corba.se.impl.orbutil.ORBUtility;
17 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
18
19 public class GIOPVersion {
20
21     // Static fields
22

23     public static final GIOPVersion V1_0 = new GIOPVersion((byte)1, (byte)0);
24     public static final GIOPVersion V1_1 = new GIOPVersion((byte)1, (byte)1);
25     public static final GIOPVersion V1_2 = new GIOPVersion((byte)1, (byte)2);
26     public static final GIOPVersion V1_3 = new GIOPVersion((byte)1, (byte)3);
27
28     // Major version 13 indicates Java serialization,
29
// Minor version [00-FF] is the version number.
30
public static final GIOPVersion V13_XX =
31     new GIOPVersion((byte)13, (byte)Message.JAVA_ENC_VERSION);
32
33     public static final GIOPVersion DEFAULT_VERSION = V1_2;
34
35     public static final int VERSION_1_0 = 0x0100;
36     public static final int VERSION_1_1 = 0x0101;
37     public static final int VERSION_1_2 = 0x0102;
38     public static final int VERSION_1_3 = 0x0103;
39     public static final int VERSION_13_XX =
40     ((0x0D << 8) & 0x0000FF00) | Message.JAVA_ENC_VERSION;
41
42     // Instance variables
43

44     private byte major = (byte) 0;
45     private byte minor = (byte) 0;
46
47     // Constructor
48

49     public GIOPVersion() {}
50
51     public GIOPVersion(byte majorB, byte minorB) {
52         this.major = majorB;
53         this.minor = minorB;
54     }
55
56     public GIOPVersion(int major, int minor) {
57         this.major = (byte)major;
58         this.minor = (byte)minor;
59     }
60
61     // Accessor methods
62

63     public byte getMajor() {
64         return this.major;
65     }
66
67     public byte getMinor() {
68         return this.minor;
69     }
70
71     // General methods
72

73     public boolean equals(GIOPVersion gv){
74         return gv.major == this.major && gv.minor == this.minor ;
75     }
76
77     public boolean equals(Object JavaDoc obj) {
78         if (obj != null && (obj instanceof GIOPVersion))
79             return equals((GIOPVersion)obj);
80         else
81             return false;
82     }
83
84     public int hashCode()
85     {
86     return 37*major + minor ;
87     }
88
89     public boolean lessThan(GIOPVersion gv) {
90         if (this.major < gv.major) {
91             return true;
92         } else if (this.major == gv.major) {
93             if (this.minor < gv.minor) {
94                 return true;
95             }
96         }
97
98         return false;
99     }
100
101     public int intValue()
102     {
103         return (major << 8 | minor);
104     }
105
106     public String JavaDoc toString()
107     {
108         return major + "." + minor;
109     }
110
111     public static GIOPVersion getInstance(byte major, byte minor)
112     {
113         switch(((major << 8) | minor)) {
114             case VERSION_1_0:
115                 return GIOPVersion.V1_0;
116             case VERSION_1_1:
117                 return GIOPVersion.V1_1;
118             case VERSION_1_2:
119                 return GIOPVersion.V1_2;
120             case VERSION_1_3:
121                 return GIOPVersion.V1_3;
122             case VERSION_13_XX:
123                 return GIOPVersion.V13_XX;
124             default:
125                 return new GIOPVersion(major, minor);
126         }
127     }
128
129     public static GIOPVersion parseVersion(String JavaDoc s)
130     {
131         int dotIdx = s.indexOf('.');
132
133         if (dotIdx < 1 || dotIdx == s.length() - 1)
134             throw new NumberFormatException JavaDoc("GIOP major, minor, and decimal point required: " + s);
135
136         int major = Integer.parseInt(s.substring(0, dotIdx));
137         int minor = Integer.parseInt(s.substring(dotIdx + 1, s.length()));
138
139         return GIOPVersion.getInstance((byte)major, (byte)minor);
140     }
141
142     /**
143      * This chooses the appropriate GIOP version.
144      *
145      * @return the GIOP version 13.00 if Java serialization is enabled, or
146      * smallest(profGIOPVersion, orbGIOPVersion)
147      */

148     public static GIOPVersion chooseRequestVersion(ORB orb, IOR ior ) {
149
150         GIOPVersion orbVersion = orb.getORBData().getGIOPVersion();
151     IIOPProfile prof = ior.getProfile() ;
152         GIOPVersion profVersion = prof.getGIOPVersion();
153
154         // Check if the profile is from a legacy Sun ORB.
155

156         ORBVersion targetOrbVersion = prof.getORBVersion();
157         if (!(targetOrbVersion.equals(ORBVersionFactory.getFOREIGN())) &&
158                 targetOrbVersion.lessThan(ORBVersionFactory.getNEWER())) {
159             // we are dealing with a SUN legacy orb which emits 1.1 IORs,
160
// in spite of being able to handle only GIOP 1.0 messages.
161
return V1_0;
162         }
163
164         // Now the target has to be (FOREIGN | NEWER*)
165

166         byte prof_major = profVersion.getMajor();
167         byte prof_minor = profVersion.getMinor();
168
169         byte orb_major = orbVersion.getMajor();
170         byte orb_minor = orbVersion.getMinor();
171
172         if (orb_major < prof_major) {
173             return orbVersion;
174         } else if (orb_major > prof_major) {
175             return profVersion;
176         } else { // both major version are the same
177
if (orb_minor <= prof_minor) {
178                 return orbVersion;
179             } else {
180                 return profVersion;
181             }
182         }
183     }
184
185     public boolean supportsIORIIOPProfileComponents()
186     {
187         return getMinor() > 0 || getMajor() > 1;
188     }
189
190     // IO methods
191

192     public void read(org.omg.CORBA.portable.InputStream JavaDoc istream) {
193         this.major = istream.read_octet();
194         this.minor = istream.read_octet();
195     }
196
197     public void write(org.omg.CORBA.portable.OutputStream JavaDoc ostream) {
198         ostream.write_octet(this.major);
199         ostream.write_octet(this.minor);
200     }
201 }
202
Popular Tags