KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > IOR


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.iiop;
30
31 import com.caucho.util.ByteBuffer;
32 import com.caucho.util.CharBuffer;
33
34 import java.io.IOException JavaDoc;
35 import java.io.UnsupportedEncodingException JavaDoc;
36
37 public class IOR {
38   private static final String JavaDoc RMI_VERSION = ":0000000000000000";
39   
40   public static final int TAG_INTERNET_IOP = 0;
41   public static final int TAG_MULTIPLE_COMPONENTS = 1;
42
43   public static final int TAG_ORB_TYPE = 0;
44   public static final int TAG_CODE_SETS = 1;
45   public static final int TAG_POLICIES = 2;
46   public static final int TAG_ALTERNATE_IIOP_ADDRESS = 3;
47   public static final int TAG_ASSOCIATION_OPTIONS = 13;
48   public static final int TAG_SEC_NAME = 14;
49   public static final int TAG_SPKM_1_SEC_MECH = 15;
50   public static final int TAG_SPKM_2_SEC_MECH = 16;
51   public static final int TAG_KerberosV5_SEC_MECH = 17;
52   public static final int TAG_CSI_ECMA_Secret_SEC_MECH = 18;
53   public static final int TAG_CSI_ECMA_Hybrid_SEC_MECH = 19;
54   public static final int TAG_SSL_SEC_TRANS = 20;
55   public static final int TAG_ECMA_Public_SEC_MECH = 21;
56   public static final int TAG_GENERIC_SEC_MECH = 22;
57   public static final int TAG_JAVA_CODEBASE = 25;
58
59   // ftp://ftp.opengroup.org/pub/code_set_registry/code_set_registry1.2g.txt
60
public static final int CS_ISO8859_1 = 0x10020;
61   public static final int CS_UTF16 = 0x10100; // ucs-16 level 1
62

63   String JavaDoc _typeId;
64   int major;
65   int minor;
66   String JavaDoc _host;
67   int port;
68   byte []oid;
69   String JavaDoc uri;
70
71   byte []bytes;
72
73   /**
74    * Null constructor for reading.
75    */

76   public IOR()
77   {
78   }
79
80   /**
81    * Null constructor for writing.
82    */

83   public IOR(Class JavaDoc type, String JavaDoc host, int port, String JavaDoc uri)
84   {
85     this("RMI:" + type.getName() + RMI_VERSION, host, port, uri);
86   }
87   
88   /**
89    * Null constructor for writing.
90    */

91   public IOR(String JavaDoc typeId, String JavaDoc host, int port, String JavaDoc uri)
92   {
93     try {
94       _typeId = typeId;
95       this.major = 1;
96       this.minor = 2;
97       _host = host;
98       this.port = port;
99       this.uri = uri;
100
101       oid = uri.getBytes("UTF8");
102     } catch (UnsupportedEncodingException JavaDoc e) {
103     }
104   }
105
106   /**
107    * Returns the type identifier. This is the java class.
108    */

109   public String JavaDoc getTypeId()
110   {
111     return _typeId;
112   }
113
114   /**
115    * Returns the IIOP major number (1)
116    */

117   public int getMajor()
118   {
119     return major;
120   }
121
122   /**
123    * Returns the IIOP minor number (2)
124    */

125   public int getMinor()
126   {
127     return minor;
128   }
129
130   public void setMinor(int minor)
131   {
132     this.minor = minor;
133   }
134
135   /**
136    * Returns the host
137    */

138   public String JavaDoc getHost()
139   {
140     return _host;
141   }
142
143   /**
144    * Returns the port
145    */

146   public int getPort()
147   {
148     return port;
149   }
150
151   /**
152    * returns the oid
153    */

154   public byte []getOid()
155   {
156     return oid;
157   }
158
159   /**
160    * Returns the object's URI
161    */

162   public String JavaDoc getURI()
163   {
164     if (uri == null) {
165       if (oid == null)
166         return null;
167       
168       try {
169         uri = new String JavaDoc(oid, 0, oid.length, "UTF8");
170       } catch (UnsupportedEncodingException JavaDoc e) {
171       }
172     }
173
174     return uri;
175   }
176   
177   /**
178    * Read directly from an IiopReader
179    */

180   IOR read(IiopReader is)
181     throws IOException JavaDoc
182   {
183     _typeId = is.readString();
184     int count = is.readInt();
185
186     for (int i = 0; i < count; i++) {
187       int tag = is.readInt();
188
189       if (tag != TAG_INTERNET_IOP)
190         throw new RuntimeException JavaDoc("unsupported iop " + tag);
191
192       int sublen = is.readInt();
193
194       int topEndian = is.read();
195       major = is.read();
196       minor = is.read();
197
198       _host = is.readString();
199       port = is.read_short() & 0xffff;
200
201       oid = is.readBytes();
202
203       uri = null;
204
205       if (minor >= 1) {
206         int tagCount = is.readInt();
207         for (int j = 0; j < tagCount; j++) {
208           int compType = is.readInt();
209
210           if (compType == TAG_CODE_SETS) {
211             int len = is.readInt();
212             int endian = is.readInt();
213             int charCode = is.readInt();
214             sublen = is.readInt();
215             for (int k = 0; k < sublen; k++)
216               is.readInt();
217             
218             int wcharCode = is.readInt();
219             sublen = is.readInt();
220             for (int k = 0; k < sublen; k++)
221               is.readInt();
222           }
223           else {
224             byte []bytes = is.readBytes();
225           }
226         }
227       }
228     }
229
230     if (count == 0)
231       return null;
232     else
233       return this;
234   }
235
236   /**
237    * Read from a byte array.
238    */

239   public void readByteArray(byte []buf, int offset, int length)
240   {
241     int i = 0;
242
243     int strlen = getInt(buf, offset + i);
244     i += 4;
245     
246     _typeId = getString(buf, offset + i, strlen);
247     i += strlen;
248     
249     i += (4 - i % 4) % 4;
250     int len = getInt(buf, offset + i);
251     i += 4;
252
253     for (int k = 0; k < len; k++) {
254       int tag = getInt(buf, offset + i);
255       i += 4;
256       
257       if (tag != TAG_INTERNET_IOP)
258         throw new RuntimeException JavaDoc("unsupported iop " + tag);
259
260       int sublen = getInt(buf, offset + i);
261       i += 4;
262
263       major = buf[offset + i++] & 0xff;
264       minor = buf[offset + i++] & 0xff;
265
266       i += 2;
267
268       int startOff = offset;
269       strlen = getInt(buf, offset + i);
270       i += 4;
271
272       _host = getString(buf, offset + i, strlen);
273       i += strlen;
274       
275       i += i & 1;
276       port = getShort(buf, offset + i);
277       i += 2;
278
279       i += (4 - i % 4) % 4;
280       strlen = getInt(buf, offset + i);
281       i += 4;
282       
283       uri = null;
284       oid = new byte[strlen];
285       for (int j = 0; j < strlen; j++)
286         oid[j] = buf[offset + i + j];
287       
288       i += strlen;
289       i += (4 - i % 4) % 4;
290     }
291   }
292
293   /**
294    * Read an integer from the byte array. This assumes big endian.
295    */

296   private static int getInt(byte []buf, int offset)
297   {
298     return (((buf[offset] & 0xff) << 24) +
299             ((buf[offset + 1] & 0xff) << 16) +
300             ((buf[offset + 2] & 0xff) << 8) +
301             (buf[offset + 3] & 0xff));
302   }
303
304   /**
305    * Read an integer from the byte array. This assumes big endian.
306    */

307   private static int getShort(byte []buf, int offset)
308   {
309     return (((buf[offset] & 0xff) << 8) +
310             ((buf[offset + 1] & 0xff)));
311   }
312
313   /**
314    * Reads a string from the byte array.
315    */

316   private static String JavaDoc getString(byte []buf, int offset, int len)
317   {
318     CharBuffer cb = CharBuffer.allocate();
319
320     for (int i = 0; i < len - 1; i++)
321       cb.append((char) buf[offset + i]);
322
323     return cb.close();
324   }
325
326   /**
327    * Read from a byte array.
328    */

329   public byte []getByteArray()
330   {
331     if (bytes != null)
332       return bytes;
333
334     ByteBuffer bb = new ByteBuffer();
335
336     writeString(bb, _typeId);
337     
338     align4(bb);
339     bb.addInt(1);
340     
341     bb.addInt(TAG_INTERNET_IOP);
342     int offset = bb.size();
343     bb.addInt(0);
344
345     bb.add(0); // encoding
346
bb.add(major);
347     bb.add(minor);
348
349     writeString(bb, _host);
350
351     if ((bb.size() & 0x1) == 1)
352       bb.add(0);
353     bb.addShort(port);
354
355     align4(bb);
356     bb.addInt(oid.length);
357     for (int i = 0; i < oid.length; i++)
358       bb.add(oid[i]);
359
360     if (minor >= 1) {
361       align4(bb);
362       bb.addInt(1); // tagged profiles
363

364       bb.addInt(TAG_CODE_SETS);
365       bb.addInt(20); // length
366
bb.addInt(0); // endian
367
bb.addInt(CS_ISO8859_1); // codeset - ISO-8859-1
368
bb.addInt(0); // no alt codesets
369
bb.addInt(CS_UTF16); // codeset - UTF-16
370
bb.addInt(0); // no alt codesets
371
}
372     
373     bb.setInt(offset, bb.size() - offset - 4);
374
375     bytes = bb.getByteArray();
376
377     return bytes;
378   }
379
380   private void writeString(ByteBuffer bb, String JavaDoc str)
381   {
382     align4(bb);
383     bb.addInt(str.length() + 1);
384     for (int i = 0; i < str.length(); i++)
385       bb.add(str.charAt(i));
386     bb.add(0);
387   }
388
389   private void align4(ByteBuffer bb)
390   {
391     int len = bb.getLength();
392     int delta = (4 - len % 4) % 4;
393     for (int i = 0; i < delta; i++)
394       bb.add(0);
395   }
396
397   /**
398    * Writes the canonical IOR representation.
399    */

400   public String JavaDoc toString()
401   {
402     return "IOR:" + _typeId + "//" + _host + ":" + port + "/" + bytesToHex(oid);
403   }
404
405   private static String JavaDoc toHex(int v)
406   {
407     CharBuffer cb = CharBuffer.allocate();
408     for (int i = 28; i >= 0; i -= 4) {
409       int h = (v >> i) & 0xf;
410
411       if (h >= 10)
412         cb.append((char) ('a' + h - 10));
413       else
414         cb.append(h);
415     }
416
417     return cb.close();
418   }
419
420   private static String JavaDoc toStr(int v)
421   {
422     CharBuffer cb = CharBuffer.allocate();
423     for (int i = 24; i >= 0; i -= 8) {
424       int ch = (v >> i) & 0xff;
425       
426       if (ch >= 0x20 && ch < 0x7f)
427         cb.append((char) ch);
428       else
429         break;
430     }
431
432     return cb.close();
433   }
434   /**
435    * Convert a byte array to hex.
436    */

437   private String JavaDoc bytesToHex(byte []bytes)
438   {
439     if (bytes == null)
440       return "null";
441     
442     CharBuffer cb = CharBuffer.allocate();
443
444     for (int i = 0; i < bytes.length; i++) {
445       int ch1 = (bytes[i] >> 4) & 0xf;
446       int ch2 = bytes[i] & 0xf;
447
448       if (ch1 < 10)
449         cb.append((char) (ch1 + '0'));
450       else
451         cb.append((char) (ch1 + 'a' - 10));
452       
453       if (ch2 < 10)
454         cb.append((char) (ch2 + '0'));
455       else
456         cb.append((char) (ch2 + 'a' - 10));
457     }
458
459     return cb.close();
460   }
461 }
462
Popular Tags