KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > explorer > CORBA > TypageCORBA


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.openccm.explorer.CORBA;
27
28 import org.objectweb.util.explorer.core.common.lib.ClassResolver;
29
30 /************************************************************************
31  **
32  ** Ce programme permet d'obtenir le type_id d'un objet CORBA
33  ** ainsi que la classe Helper associée permettant de faire un narrow.
34  **
35  ** Auteur : Dr. Philippe Merle - LIFL & INRIA - 2001
36  **
37  ************************************************************************/

38
39 /************************************************************************
40  **
41  ** Cette 1ère classe permet de partiellement décoder une IOR.
42  **
43  ** Auteur initial : Marek Malowidzki - 1999
44  **
45  ************************************************************************/

46
47 final class ModestInputStream {
48     private byte bytes[];
49     private int index;
50     private boolean little_endian;
51
52     public ModestInputStream(byte bytes[]) {
53         this.bytes = bytes;
54         little_endian = bytes[0] != 0;
55         index = 1;
56     }
57
58     public boolean getEndian() {
59         return little_endian;
60     }
61
62     public byte readOctet() {
63         return bytes[index++];
64     }
65
66     public int readULong() {
67         int al = index % 4;
68         if (al != 0)
69             index += 4 - al;
70         if (little_endian)
71             return ((bytes[index++]) & 0x000000ff) | ((bytes[index++] << 8) & 0x0000ff00) | ((bytes[index++] << 16) & 0x00ff0000) | ((bytes[index++] << 24) & 0xff000000);
72         else
73             return ((bytes[index++] << 24) & 0xff000000) | ((bytes[index++] << 16) & 0x00ff0000) | ((bytes[index++] << 8) & 0x0000ff00) | ((bytes[index++]) & 0x000000ff);
74     }
75
76     public String JavaDoc readString() {
77         int len = readULong() - 1;
78         String JavaDoc str = new String JavaDoc(bytes, index, len);
79         index += len;
80         if (readOctet() != 0)
81             throw new org.omg.CORBA.INV_OBJREF JavaDoc("String should end with byte 0");
82         return str;
83     }
84 }
85
86 /************************************************************************
87  **
88  ** Classe permettant d'obtenir le type_id d'un objet CORBA
89  ** ainsi que sa classe Helper associée.
90  **
91  ************************************************************************/

92 /**
93  * This class allows to obtain a CORBA Object type_id and its associated Helpper class
94  *
95  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
96  *
97  * @version 0.1
98  */

99 public class TypageCORBA {
100     // **********************************************************************
101
//
102
// Ces 3 fonctions suivantes sont issues de ORBacus 4.0.5 for Java.
103
// Toutefois elles sont indépendantes de cet ORB car celles-ci ne font
104
// que des manipulations de chaines de caracteres.
105
//
106
// Copyright (c) 2000
107
//
108
// Object Oriented Concepts, Inc.
109
// Billerica, MA, USA
110
//
111
// All Rights Reserved
112
//
113
// **********************************************************************
114
//
115
// Check the given name against Java keywords and reserved suffixes
116
//
117
public static String JavaDoc fixName(String JavaDoc name) {
118         // Version simplifiee !!!
119
return name;
120     }
121
122     //
123
// Convert a repository ID into a class name.
124
// See the IDL-to-Java mapping, section 1.13.8.
125
//
126
public static String JavaDoc idToClassName(String JavaDoc id, String JavaDoc suffix) {
127         String JavaDoc result = null;
128
129         if (id != null) {
130             if (id.startsWith("IDL:")) {
131                 try {
132                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
133
134                     int end = id.lastIndexOf(':');
135                     String JavaDoc s;
136                     if (end < 0)
137                         s = id.substring(4);
138                     else
139                         s = id.substring(4, end);
140
141                     //
142
// If the ID contains a prefix, then reverse the
143
// dotted components of the prefix
144
//
145
int slash = s.indexOf('/');
146                     if (slash > 0) {
147                         String JavaDoc prefix = s.substring(0, slash);
148                         String JavaDoc rest = s.substring(slash + 1);
149                         java.util.StringTokenizer JavaDoc tokenizer = new java.util.StringTokenizer JavaDoc(prefix, ".");
150                         while (tokenizer.hasMoreTokens()) {
151                             String JavaDoc tok = tokenizer.nextToken();
152                             buf.insert(0, fixName(tok) + ".");
153                         }
154
155                         s = rest;
156                     }
157
158                     //
159
// "Fix" the remainder of the ID
160
//
161
java.util.StringTokenizer JavaDoc tokenizer = new java.util.StringTokenizer JavaDoc(s, "/");
162                     while (tokenizer.hasMoreTokens()) {
163                         String JavaDoc tok = tokenizer.nextToken();
164                         buf.append(fixName(tok));
165                         if (tokenizer.hasMoreTokens())
166                             buf.append('.');
167                     }
168
169                     result = buf.toString() + suffix;
170                 } catch (IndexOutOfBoundsException JavaDoc ex) { // if id has bad format
171
result = null;
172                 }
173             }
174         }
175
176         return result;
177     }
178
179     //
180
// Convert a repository ID into a class.
181
// See the IDL-to-Java mapping, section 1.13.8.
182
//
183
public static Class JavaDoc idToClass(String JavaDoc id, String JavaDoc suffix) {
184         Class JavaDoc result = null;
185         String JavaDoc className = idToClassName(id, suffix);
186
187         if (className != null) {
188             try {
189                 result = ClassResolver.resolve(className);
190             } catch (ClassNotFoundException JavaDoc ex) {
191                 // ignore
192
}
193         }
194
195         return result;
196     }
197
198     // **********************************************************************
199
//
200
// **********************************************************************
201

202     static String JavaDoc decode_type_id(String JavaDoc ior) {
203         int nbytes = ior.length() - 4;
204         if ((nbytes & 1) == 1)
205             throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad string length");
206         nbytes /= 2;
207         byte bytes[] = new byte[nbytes];
208         for (int i = 0, j = 4; i < nbytes; i++, j += 2) {
209             String JavaDoc bstr = ior.substring(j, j + 2);
210             try {
211                 bytes[i] = (byte) Integer.parseInt(bstr, 16);
212             } catch (NumberFormatException JavaDoc exc) {
213                 throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad byte value: " + bstr);
214             }
215         }
216
217         ModestInputStream in = new ModestInputStream(bytes);
218
219         String JavaDoc result = in.readString();
220
221         // Deal with null CORBA object references.
222
if((result == null) || (result.length() == 0))
223             result = "IDL:omg.org/CORBA/Object:1.0";
224
225         return result;
226     }
227
228     // **********************************************************************
229
//
230
// **********************************************************************
231

232     protected String JavaDoc type_id_;
233
234     public TypageCORBA(org.omg.CORBA.Object JavaDoc objref, org.omg.CORBA.ORB JavaDoc orb) {
235         String JavaDoc ior = null;
236         try {
237             ior = orb.object_to_string(objref);
238         } catch (Exception JavaDoc e) {
239             throw new ObjectToStringNotSupportedException();
240         }
241         type_id_ = decode_type_id(ior);
242     }
243
244     public TypageCORBA(String JavaDoc ior) {
245         type_id_ = decode_type_id(ior);
246     }
247
248     public String JavaDoc getTypeID() {
249         return type_id_;
250     }
251
252     public String JavaDoc getJavaInterfaceName() {
253         return idToClassName(type_id_, "");
254     }
255
256     public Class JavaDoc getJavaInterface() {
257         return idToClass(type_id_, "");
258     }
259
260     public String JavaDoc getJavaHelperClassName() {
261         return idToClassName(type_id_, "Helper");
262     }
263
264     public Class JavaDoc getJavaHelperClass() {
265         return idToClass(type_id_, "Helper");
266     }
267
268 }
269
Popular Tags