KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > util > IorPrinter


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.util;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import org.omg.CORBA.StringHolder JavaDoc;
30
31 public final class IorPrinter {
32     private static final char CHARS[] = { ';', '/', ':', '?', '@', '&', '=', '+', '$', ',', '-', '_', '.', '!', '~', '*', '`', '(', ')' };
33     private static final char NOT_ESCAPED[] = Sort(CHARS);
34     private static char[] Sort(char chars[]) {
35         Arrays.sort(chars);
36         return chars;
37     }
38
39     private boolean little_endian_;
40     //byte order
41
private String JavaDoc type_id_;
42     //repository type id
43
private ArrayList JavaDoc iprofiles_ = new ArrayList JavaDoc();
44     //InternetProfiles
45
private ArrayList JavaDoc profiles_ = new ArrayList JavaDoc();
46     //all profiles (TaggedProfiles and InternetProfiles)
47
private static boolean IsPrintableCharacter(char c) {
48         return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || Arrays.binarySearch(NOT_ESCAPED, c) >= 0;
49     }
50
51     private byte[] GetBytes(String JavaDoc str, int offset) {
52         int nbytes = str.length() - offset;
53
54         if ((nbytes & 1) == 1) {
55             throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad string length");
56         }
57
58         nbytes /= 2;
59         byte bytes[] = new byte[nbytes];
60
61         for (int i = 0, j = 4; i < nbytes; i++, j += 2) {
62             String JavaDoc bstr = str.substring(j, j + 2);
63
64             try {
65                 bytes[i] = (byte)Integer.parseInt(bstr, 16);
66             } catch (NumberFormatException JavaDoc exc) {
67                 throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad byte value: " + bstr);
68             }
69         }
70
71         return bytes;
72     }
73
74     ///////////////////////////////////////////////////////////////////////
75
// Methods read IORs, respectively, in "IOR:" and "corbaloc:" notations.
76
///////////////////////////////////////////////////////////////////////
77
private void Read_ior(String JavaDoc ior) {
78         Reset();
79         byte bytes[] = GetBytes(ior, 4);
80
81         try {
82             ModestInputStream in = new ModestInputStream(bytes);
83             little_endian_ = in.GetEndian();
84             type_id_ = in.ReadString();
85             int nprofiles = in.ReadULong();
86
87             for (int i = 0; i < nprofiles; i++) {
88                 int tag = in.ReadULong();
89                 byte profile_data[] = in.ReadOctetArray();
90
91                 if (tag == org.omg.IOP.TAG_INTERNET_IOP.value) {
92                     InternetProfile iprofile = new InternetProfile();
93                     iprofile.Read_ior(profile_data);
94                     iprofiles_.add(iprofile);
95                     profiles_.add(iprofile);
96                 } else {
97                     org.omg.IOP.TaggedProfile JavaDoc profile = new org.omg.IOP.TaggedProfile JavaDoc();
98                     profile.tag = tag;
99                     profile.profile_data = profile_data;
100                     profiles_.add(profile);
101                 }
102             }
103         } catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
104             throw new org.omg.CORBA.INV_OBJREF JavaDoc("Invalid IOR (too short?)");
105         }
106     }
107
108     private void Read_corbaloc(String JavaDoc ior) {
109         Reset();
110         int index = ior.indexOf(':') + 1;
111         //here reference starts
112
boolean lastaddr = false;
113
114         do {
115             int ind = index + 2, addrend = ior.indexOf(',', ind);
116
117             if (addrend == - 1) {
118                 addrend = ior.indexOf('/', ind);
119                 lastaddr = true;
120             }
121
122             if (addrend == - 1) {
123                 throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad reference: no '/'<key_string>");
124             }
125             InternetProfile profile = new InternetProfile();
126
127             profile.Read_corbaloc(ior.substring(index, addrend));
128             iprofiles_.add(profile);
129             profiles_.add(profile);
130             index = addrend + 1;
131         } while (!lastaddr);
132         byte objkey[] = index < ior.length() ? Decode(ior.substring(index)) : new byte[0];
133
134         for (int i = 0; i < iprofiles_.size(); i++) {
135             ((InternetProfile)iprofiles_.get(i)).object_key = objkey;
136         //IOR returns copies
137
}
138
139         type_id_ = "IDL:omg.org/CORBA/Object:1.0";
140     }
141
142     ///////////////////////////////////////////////////////////////////////
143
// Methods deal with "corbaloc:" notation.
144
///////////////////////////////////////////////////////////////////////
145
private boolean CheckIfCorbalocPossible(StringHolder JavaDoc strh) {
146         // check if any additional profiles
147
if (iprofiles_.size() != profiles_.size()) {
148             if (strh != null) {
149                 strh.value = "Additional profiles present in reference";
150             }
151
152             return false;
153         }
154         // check if InternetProfiles don't contain components
155
// and if object keys are the same in all profiles.
156
InternetProfile previous = null;
157
158         for (int i = 0; i < iprofiles_.size(); i++) {
159             InternetProfile iprofile = (InternetProfile)iprofiles_.get(i);
160
161             if (iprofile.components != null && iprofile.components.length > 0) {
162                 if (strh != null) {
163                     strh.value = "Components present in Internet Profiles";
164                 }
165
166                 return false;
167             }
168
169             if (i > 0) {
170                 if (!Arrays.equals(iprofile.object_key, previous.object_key)) {
171                     if (strh != null) {
172                         strh.value = "Different object keys in Internet Profiles";
173                     }
174
175                     return false;
176                 }
177             }
178
179             previous = iprofile;
180         }
181
182         return true;
183     }
184
185     private String JavaDoc Write_corbaloc() {
186         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(512);
187         byte objkey[] = null;
188         sb.append("corbaloc:");
189
190         for (int i = 0; i < iprofiles_.size(); i++) {
191             InternetProfile profile = (InternetProfile)iprofiles_.get(i);
192
193             if (i == 0) {
194                 objkey = profile.object_key;
195             } else {
196                 sb.append(',');
197             }
198
199             profile.Write_corbaloc(sb);
200         }
201
202         sb.append('/');
203         sb.append(Encode(objkey));
204         return sb.toString();
205     }
206
207     ///////////////////////////////////////////////////////////////////////
208
// Constructors.
209
///////////////////////////////////////////////////////////////////////
210
public IorPrinter() {
211     }
212
213     public IorPrinter(String JavaDoc ior) {
214
215         Read(ior);
216     }
217
218     ///////////////////////////////////////////////////////////////////////
219
// Reset and access methods.
220
///////////////////////////////////////////////////////////////////////
221
public synchronized void Reset() {
222         little_endian_ = false;
223         type_id_ = null;
224         iprofiles_.clear();
225         profiles_.clear();
226     }
227
228     public synchronized boolean IsLittleEndian() {
229         return little_endian_;
230     }
231
232     public synchronized void SetLittleEndian(boolean le) {
233         little_endian_ = le;
234     }
235
236     public synchronized String JavaDoc getTypeId() {
237         return type_id_;
238     }
239
240     public synchronized void SetTypeId(String JavaDoc type_id) {
241         type_id_ = type_id;
242     }
243
244     public synchronized int GetInternetProfilesCount() {
245         return iprofiles_.size();
246     }
247
248     public synchronized InternetProfile GetInternetProfile(int index) {
249         return new InternetProfile((InternetProfile)iprofiles_.get(index));
250     }
251
252     public synchronized void SetInternetProfile(int index, InternetProfile iprofile) {
253         InternetProfile iprof = (InternetProfile)iprofiles_.get(index);
254         iprof.Set(iprofile);
255     }
256
257     public synchronized void AddInternetProfile(InternetProfile iprofile) {
258         iprofile = new InternetProfile(iprofile);
259         iprofiles_.add(iprofile);
260         profiles_.add(iprofile);
261     }
262
263     public synchronized void InsertInternetProfile(int index, InternetProfile iprofile) {
264         iprofile = new InternetProfile(iprofile);
265         iprofiles_.add(index, iprofile);
266
267         if (index > 0) {
268             Object JavaDoc object = iprofiles_.get(index - 1);
269             index = profiles_.indexOf(object) + 1;
270         }
271
272         profiles_.add(index, iprofile);
273     }
274
275     public synchronized void RemoveInternetProfile(int index) {
276         Object JavaDoc object = iprofiles_.remove(index);
277         profiles_.remove(object);
278     }
279
280     ///////////////////////////////////////////////////////////////////////
281
// Method reads reference.
282
///////////////////////////////////////////////////////////////////////
283
public synchronized void Read(String JavaDoc ior) {
284         ior = ior.trim();
285         int len = ior.length();
286
287         if (len < 4) {
288             //"IOR:".length()
289
throw new org.omg.CORBA.INV_OBJREF JavaDoc("Object reference too short");
290         } else if (ior.substring(0, 4).compareToIgnoreCase("IOR:") == 0) {
291             Read_ior(ior);
292         } else if (len < 9) {
293             //"corbaloc:".length()
294
throw new org.omg.CORBA.INV_OBJREF JavaDoc("Object reference too short or invalid");
295         } else if (ior.substring(0, 9).compareToIgnoreCase("corbaloc:") == 0) {
296             Read_corbaloc(ior);
297         } else {
298             throw new org.omg.CORBA.INV_OBJREF JavaDoc("Object reference should start with \"IOR:\" or \"corbaloc:\"");
299         }
300     }
301
302     ///////////////////////////////////////////////////////////////////////
303
// Methods write IORs, respectively, in "IOR:", "corbaloc:" and default
304
// notation.
305
///////////////////////////////////////////////////////////////////////
306
public synchronized String JavaDoc toString_ior() {
307         ModestOutputStream out = new ModestOutputStream(little_endian_);
308         out.WriteString(type_id_);
309         out.WriteULong(profiles_.size());
310
311         for (int i = 0; i < profiles_.size(); i++) {
312             Object JavaDoc object = profiles_.get(i);
313
314             if (object instanceof InternetProfile) {
315                 out.WriteULong(org.omg.IOP.TAG_INTERNET_IOP.value);
316                 out.WriteOctetArray(((InternetProfile)object).Write_ior(little_endian_));
317             } else {
318                 org.omg.IOP.TaggedProfile JavaDoc profile = (org.omg.IOP.TaggedProfile JavaDoc)object;
319                 out.WriteULong(profile.tag);
320                 out.WriteOctetArray(profile.profile_data);
321             }
322         }
323         byte bytes[] = out.GetBytes();
324         String JavaDoc str = "IOR:";
325
326         for (int i = 0; i < bytes.length; i++) {
327             String JavaDoc hexstr = Integer.toHexString((int)bytes[i] & 0xFF);
328
329             if (hexstr.length() == 1) {
330                 hexstr = '0' + hexstr;
331             }
332
333             str += hexstr;
334         }
335
336         return str;
337     }
338
339     public synchronized String JavaDoc toString_corbaloc() throws UnsupportedOperationException JavaDoc {
340         StringHolder JavaDoc strh = new StringHolder JavaDoc();
341
342         if (!CheckIfCorbalocPossible(strh)) {
343             throw new UnsupportedOperationException JavaDoc(strh.value);
344         }
345
346         return Write_corbaloc();
347     }
348
349     // Return reference in default format: the preferred one is user-readable
350
// "corbaloc:" notation; if not possible (addition profiles, components in
351
// InternetProfiles or different object keys in profiles), returns "IOR:".
352
public synchronized String JavaDoc toString() {
353         return CheckIfCorbalocPossible(null) ? toString_corbaloc() : toString_ior();
354     }
355
356     ///////////////////////////////////////////////////////////////////////
357
// Various describing methods.
358
///////////////////////////////////////////////////////////////////////
359
public synchronized String JavaDoc Describe() {
360         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(512);
361         sb.append("byte order: " + (little_endian_ ? "little" : "big") + " endian; type_id: " + type_id_ + ';');
362
363         for (int i = 0; i < iprofiles_.size(); i++) {
364             InternetProfile profile = (InternetProfile)iprofiles_.get(i);
365             sb.append(" [IIOPv. " + profile.major + '.' + profile.minor + "; " + "host: " + profile.host + ':' + profile.port + "; object key: ");
366             byte object_key[] = profile.object_key;
367
368             for (int j = 0; j < object_key.length; j++) {
369                 String JavaDoc hexstr = Integer.toHexString((int)object_key[j] & 0xFF);
370                 sb.append(' ');
371
372                 if (hexstr.length() == 1) {
373                     sb.append('0');
374                 }
375
376                 sb.append(hexstr);
377             }
378
379             sb.append(']');
380         }
381
382         return sb.toString();
383     }
384
385     ///////////////////////////////////////////////////////////////////////
386
// Methods perform mapping from '%HH' notation to plain byte array and vice-versa
387
///////////////////////////////////////////////////////////////////////
388
public static byte[] Decode(String JavaDoc str) {
389         byte bytes[] = new byte[str.length()];
390         int blen = 0;
391
392         for (int i = 0, len = str.length(); i < len; ) {
393             char c = str.charAt(i++);
394
395             if (c == '%') {
396                 bytes[blen++] = (byte)Integer.parseInt(str.substring(i, i + 2), 16);
397                 i += 2;
398             } else {
399                 bytes[blen++] = (byte)c;
400             }
401         }
402
403         if (blen < bytes.length) {
404             byte b[] = new byte[blen];
405             System.arraycopy(bytes, 0, b, 0, b.length);
406             bytes = b;
407         }
408
409         return bytes;
410     }
411
412     public static String JavaDoc Encode(byte bytes[]) {
413         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(3 * bytes.length);
414
415         for (int i = 0; i < bytes.length; ) {
416             char c = (char)bytes[i++];
417
418             if (IsPrintableCharacter(c)) {
419                 sb.append(c);
420             } else {
421                 String JavaDoc hex = Integer.toHexString((int)c & 0xFF);
422                 sb.append('%');
423
424                 if (hex.length() == 1) {
425                     sb.append('0');
426                 }
427
428                 sb.append(hex);
429             }
430         }
431
432         return sb.toString();
433     }
434
435     public static void printIOR(String JavaDoc iorstr) {
436         try {
437             IorPrinter ior = new IorPrinter(iorstr);
438             System.out.println(ior.Describe());
439         } catch (Throwable JavaDoc exc) {
440             exc.printStackTrace();
441         }
442     }
443
444     public String JavaDoc getPort() {
445         InternetProfile profile = (InternetProfile)iprofiles_.get(0);
446         int port = profile.port;
447
448         if (port < 0) {
449             port += 65536;
450         }
451
452         return "" + port;
453     }
454
455     public String JavaDoc getHost() {
456         InternetProfile profile = (InternetProfile)iprofiles_.get(0);
457         return profile.host;
458     }
459
460     public String JavaDoc getVersion() {
461         InternetProfile profile = (InternetProfile)iprofiles_.get(0);
462         return "" + (int)profile.major + "." + (int)profile.minor;
463     }
464
465     public void print() {
466         InternetProfile profile = (InternetProfile)iprofiles_.get(0);
467         System.out.println("------IOR components-----");
468         System.out.println("TypeId: " + getTypeId());
469         System.out.println("IIOP Version: " + "" + (int)profile.major + "." + (int)profile.minor);
470         System.out.println("Host: " + profile.host);
471         System.out.println("Port: " + profile.port);
472     }
473
474     public String JavaDoc getProfileId(long tag) {
475         if (tag == 0) {
476             return "TAG_INTERNET_IOP";
477         } else {
478             return "TAG_MULTIPLE_COMPONENTS";
479         }
480     }
481
482     private String JavaDoc toHex(byte bs[]) {
483         String JavaDoc hex = "";
484
485         for (int i = 0; i < bs.length; i++) {
486             int n1 = (bs[i] & 0xff) / 16;
487             int n2 = (bs[i] & 0xff) % 16;
488             char c1 = (char)(n1 > 9 ? ('A' + (n1 - 10)) : ('0' + n1));
489             char c2 = (char)(n2 > 9 ? ('A' + (n2 - 10)) : ('0' + n2));
490             hex += ((i > 0) ? " " : "") + c1 + c2;
491         }
492
493         return hex;
494     }
495 }
496
Popular Tags