001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.iiop;
030:
031: import com.caucho.util.ByteBuffer;
032: import com.caucho.util.CharBuffer;
033:
034: import java.io.IOException;
035: import java.io.UnsupportedEncodingException;
036:
037: public class IOR {
038: private static final String RMI_VERSION = ":0000000000000000";
039:
040: public static final int TAG_INTERNET_IOP = 0;
041: public static final int TAG_MULTIPLE_COMPONENTS = 1;
042:
043: public static final int TAG_ORB_TYPE = 0;
044: public static final int TAG_CODE_SETS = 1;
045: public static final int TAG_POLICIES = 2;
046: public static final int TAG_ALTERNATE_IIOP_ADDRESS = 3;
047: public static final int TAG_ASSOCIATION_OPTIONS = 13;
048: public static final int TAG_SEC_NAME = 14;
049: public static final int TAG_SPKM_1_SEC_MECH = 15;
050: public static final int TAG_SPKM_2_SEC_MECH = 16;
051: public static final int TAG_KerberosV5_SEC_MECH = 17;
052: public static final int TAG_CSI_ECMA_Secret_SEC_MECH = 18;
053: public static final int TAG_CSI_ECMA_Hybrid_SEC_MECH = 19;
054: public static final int TAG_SSL_SEC_TRANS = 20;
055: public static final int TAG_ECMA_Public_SEC_MECH = 21;
056: public static final int TAG_GENERIC_SEC_MECH = 22;
057: public static final int TAG_JAVA_CODEBASE = 25;
058:
059: // ftp://ftp.opengroup.org/pub/code_set_registry/code_set_registry1.2g.txt
060: public static final int CS_ISO8859_1 = 0x10020;
061: public static final int CS_UTF16 = 0x10100; // ucs-16 level 1
062:
063: String _typeId;
064: int _major;
065: int _minor;
066: String _host;
067: int _port;
068: byte[] oid;
069: String uri;
070:
071: byte[] bytes;
072:
073: /**
074: * Null constructor for reading.
075: */
076: public IOR() {
077: }
078:
079: /**
080: * Null constructor for writing.
081: */
082: public IOR(Class type, String host, int port, String uri) {
083: this ("RMI:" + type.getName() + RMI_VERSION, host, port, uri);
084: }
085:
086: /**
087: * Null constructor for writing.
088: */
089: public IOR(String typeId, String host, int port, String uri) {
090: try {
091: _typeId = typeId;
092: _major = 1;
093: _minor = 2;
094: _host = host;
095: _port = port;
096: this .uri = uri;
097:
098: oid = uri.getBytes("UTF8");
099: } catch (UnsupportedEncodingException e) {
100: }
101: }
102:
103: /**
104: * Returns the type identifier. This is the java class.
105: */
106: public String getTypeId() {
107: return _typeId;
108: }
109:
110: /**
111: * Returns the IIOP major number (1)
112: */
113: public int getMajor() {
114: return _major;
115: }
116:
117: /**
118: * Returns the IIOP minor number (2)
119: */
120: public int getMinor() {
121: return _minor;
122: }
123:
124: public void setMinor(int minor) {
125: _minor = minor;
126: }
127:
128: /**
129: * Returns the host
130: */
131: public String getHost() {
132: return _host;
133: }
134:
135: /**
136: * Returns the port
137: */
138: public int getPort() {
139: return _port;
140: }
141:
142: /**
143: * returns the oid
144: */
145: public byte[] getOid() {
146: return oid;
147: }
148:
149: /**
150: * Returns the object's URI
151: */
152: public String getURI() {
153: if (uri == null) {
154: if (oid == null)
155: return null;
156:
157: try {
158: uri = new String(oid, 0, oid.length, "UTF8");
159: } catch (UnsupportedEncodingException e) {
160: }
161: }
162:
163: return uri;
164: }
165:
166: /**
167: * Read directly from an IiopReader
168: */
169: IOR read(IiopReader is) throws IOException {
170: _typeId = is.read_string();
171: int count = is.readInt();
172:
173: for (int i = 0; i < count; i++) {
174: int tag = is.readInt();
175:
176: if (tag != TAG_INTERNET_IOP)
177: throw new RuntimeException("unsupported iop " + tag);
178:
179: int sublen = is.readInt();
180:
181: int topEndian = is.read();
182: _major = is.read();
183: _minor = is.read();
184:
185: _host = is.read_string();
186: _port = is.read_short() & 0xffff;
187:
188: oid = is.readBytes();
189:
190: uri = null;
191:
192: if (_minor >= 1) {
193: int tagCount = is.readInt();
194: for (int j = 0; j < tagCount; j++) {
195: int compType = is.readInt();
196:
197: if (compType == TAG_CODE_SETS) {
198: int len = is.readInt();
199: int endian = is.readInt();
200: int charCode = is.readInt();
201: sublen = is.readInt();
202: for (int k = 0; k < sublen; k++)
203: is.readInt();
204:
205: int wcharCode = is.readInt();
206: sublen = is.readInt();
207: for (int k = 0; k < sublen; k++)
208: is.readInt();
209: } else {
210: byte[] bytes = is.readBytes();
211: }
212: }
213: }
214: }
215:
216: if (count == 0)
217: return null;
218: else
219: return this ;
220: }
221:
222: /**
223: * Read from a byte array.
224: */
225: public void readByteArray(byte[] buf, int offset, int length) {
226: int i = 0;
227:
228: int strlen = getInt(buf, offset + i);
229: i += 4;
230:
231: _typeId = getString(buf, offset + i, strlen);
232: i += strlen;
233:
234: i += (4 - i % 4) % 4;
235: int len = getInt(buf, offset + i);
236: i += 4;
237:
238: for (int k = 0; k < len; k++) {
239: int tag = getInt(buf, offset + i);
240: i += 4;
241:
242: if (tag != TAG_INTERNET_IOP)
243: throw new RuntimeException("unsupported iop " + tag);
244:
245: int sublen = getInt(buf, offset + i);
246: i += 4;
247:
248: _major = buf[offset + i++] & 0xff;
249: _minor = buf[offset + i++] & 0xff;
250:
251: i += 2;
252:
253: int startOff = offset;
254: strlen = getInt(buf, offset + i);
255: i += 4;
256:
257: _host = getString(buf, offset + i, strlen);
258: i += strlen;
259:
260: i += i & 1;
261: _port = getShort(buf, offset + i);
262: i += 2;
263:
264: i += (4 - i % 4) % 4;
265: strlen = getInt(buf, offset + i);
266: i += 4;
267:
268: uri = null;
269: oid = new byte[strlen];
270: for (int j = 0; j < strlen; j++)
271: oid[j] = buf[offset + i + j];
272:
273: i += strlen;
274: i += (4 - i % 4) % 4;
275: }
276: }
277:
278: /**
279: * Read an integer from the byte array. This assumes big endian.
280: */
281: private static int getInt(byte[] buf, int offset) {
282: return (((buf[offset] & 0xff) << 24)
283: + ((buf[offset + 1] & 0xff) << 16)
284: + ((buf[offset + 2] & 0xff) << 8) + (buf[offset + 3] & 0xff));
285: }
286:
287: /**
288: * Read an integer from the byte array. This assumes big endian.
289: */
290: private static int getShort(byte[] buf, int offset) {
291: return (((buf[offset] & 0xff) << 8) + ((buf[offset + 1] & 0xff)));
292: }
293:
294: /**
295: * Reads a string from the byte array.
296: */
297: private static String getString(byte[] buf, int offset, int len) {
298: CharBuffer cb = CharBuffer.allocate();
299:
300: for (int i = 0; i < len - 1; i++)
301: cb.append((char) buf[offset + i]);
302:
303: return cb.close();
304: }
305:
306: /**
307: * Read from a byte array.
308: */
309: public byte[] getByteArray() {
310: if (bytes != null)
311: return bytes;
312:
313: ByteBuffer bb = new ByteBuffer();
314:
315: writeString(bb, _typeId);
316:
317: align4(bb);
318: bb.addInt(1);
319:
320: bb.addInt(TAG_INTERNET_IOP);
321: int offset = bb.size();
322: bb.addInt(0);
323:
324: bb.add(0); // encoding
325: bb.add(_major);
326: bb.add(_minor);
327:
328: writeString(bb, _host);
329:
330: if ((bb.size() & 0x1) == 1)
331: bb.add(0);
332: bb.addShort(_port);
333:
334: align4(bb);
335: bb.addInt(oid.length);
336: for (int i = 0; i < oid.length; i++)
337: bb.add(oid[i]);
338:
339: if (_minor >= 1) {
340: align4(bb);
341: bb.addInt(1); // tagged profiles
342:
343: bb.addInt(TAG_CODE_SETS);
344: bb.addInt(20); // length
345: bb.addInt(0); // endian
346: bb.addInt(CS_ISO8859_1); // codeset - ISO-8859-1
347: bb.addInt(0); // no alt codesets
348: bb.addInt(CS_UTF16); // codeset - UTF-16
349: bb.addInt(0); // no alt codesets
350: }
351:
352: bb.setInt(offset, bb.size() - offset - 4);
353:
354: bytes = bb.getByteArray();
355:
356: return bytes;
357: }
358:
359: private void writeString(ByteBuffer bb, String str) {
360: align4(bb);
361: bb.addInt(str.length() + 1);
362: for (int i = 0; i < str.length(); i++)
363: bb.add(str.charAt(i));
364: bb.add(0);
365: }
366:
367: private void align4(ByteBuffer bb) {
368: int len = bb.getLength();
369: int delta = (4 - len % 4) % 4;
370: for (int i = 0; i < delta; i++)
371: bb.add(0);
372: }
373:
374: /**
375: * Writes the canonical IOR representation.
376: */
377: public String toString() {
378: return "IOR:" + _typeId + "//" + _host + ":" + _port + "/"
379: + bytesToHex(oid);
380: }
381:
382: private static String toHex(int v) {
383: CharBuffer cb = CharBuffer.allocate();
384: for (int i = 28; i >= 0; i -= 4) {
385: int h = (v >> i) & 0xf;
386:
387: if (h >= 10)
388: cb.append((char) ('a' + h - 10));
389: else
390: cb.append(h);
391: }
392:
393: return cb.close();
394: }
395:
396: private static String toStr(int v) {
397: CharBuffer cb = CharBuffer.allocate();
398: for (int i = 24; i >= 0; i -= 8) {
399: int ch = (v >> i) & 0xff;
400:
401: if (ch >= 0x20 && ch < 0x7f)
402: cb.append((char) ch);
403: else
404: break;
405: }
406:
407: return cb.close();
408: }
409:
410: /**
411: * Convert a byte array to hex.
412: */
413: private String bytesToHex(byte[] bytes) {
414: if (bytes == null)
415: return "null";
416:
417: CharBuffer cb = CharBuffer.allocate();
418:
419: for (int i = 0; i < bytes.length; i++) {
420: int ch1 = (bytes[i] >> 4) & 0xf;
421: int ch2 = bytes[i] & 0xf;
422:
423: if (ch1 < 10)
424: cb.append((char) (ch1 + '0'));
425: else
426: cb.append((char) (ch1 + 'a' - 10));
427:
428: if (ch2 < 10)
429: cb.append((char) (ch2 + '0'));
430: else
431: cb.append((char) (ch2 + 'a' - 10));
432: }
433:
434: return cb.close();
435: }
436: }
|