KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > SystemExceptionHelper


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.orb;
22
23 import java.lang.reflect.*;
24
25 import org.jacorb.orb.giop.ReplyInputStream;
26 import org.jacorb.util.ObjectUtil;
27 import org.omg.IOP.*;
28
29 public class SystemExceptionHelper
30 {
31     private static final String JavaDoc className( String JavaDoc repId )
32     {
33         // cut "IDL:" and version
34
String JavaDoc id_base = repId.substring(4, repId.lastIndexOf(':'));
35     return ir2scopes("org.omg",id_base.substring(7));
36     }
37
38     private static final String JavaDoc ir2scopes( String JavaDoc prefix, String JavaDoc s )
39     {
40         if( s.indexOf("/") < 0)
41             return s;
42         java.util.StringTokenizer JavaDoc strtok =
43             new java.util.StringTokenizer JavaDoc( s, "/" );
44
45         int count = strtok.countTokens();
46         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
47         sb.append(prefix);
48
49         for( int i = 0; strtok.hasMoreTokens(); i++ )
50         {
51             String JavaDoc sc = strtok.nextToken();
52             try
53             {
54                 Class JavaDoc c = null;
55                 if( sb.toString().length() > 0 )
56                     c = ObjectUtil.classForName( sb.toString() + "." + sc );
57                 else
58                     c = ObjectUtil.classForName( sc );
59
60                 if( i < count-1)
61                 {
62                     sb.append( "." + sc + "Package");
63                 }
64                 else
65                     sb.append( "." + sc );
66             }
67             catch ( ClassNotFoundException JavaDoc cnfe )
68             {
69                 if( sb.toString().length() > 0 )
70                     sb.append( "." + sc );
71                 else
72                     sb.append( sc );
73             }
74         }
75
76         return sb.toString();
77     }
78
79     private static final String JavaDoc repId( Class JavaDoc c )
80     {
81         String JavaDoc className = c.getName();
82     String JavaDoc body = className.substring(7);
83     return "IDL:omg.org/" + scopesToIR(body) + ":1.0";
84     }
85
86     private static final String JavaDoc scopesToIR( String JavaDoc s )
87     {
88         if( s.indexOf(".") < 0)
89             return s;
90         java.util.StringTokenizer JavaDoc strtok =
91             new java.util.StringTokenizer JavaDoc( s, "." );
92         String JavaDoc scopes[] = new String JavaDoc[strtok.countTokens()];
93         for( int i = 0; strtok.hasMoreTokens(); i++ ){
94             String JavaDoc sc = strtok.nextToken();
95             if( sc.endsWith("Package"))
96                 scopes[i] = sc.substring(0,sc.indexOf("Package"));
97             else
98                 scopes[i] = sc;
99         }
100         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
101         if( scopes.length > 1 )
102         {
103             for( int i = 0; i < scopes.length-1; i++)
104                 sb.append( scopes[i] + "/" );
105         }
106
107         sb.append( scopes[scopes.length-1] );
108         return sb.toString();
109     }
110
111
112     public static void insert(org.omg.CORBA.Any JavaDoc any, org.omg.CORBA.SystemException JavaDoc s)
113     {
114     any.type( type( s ));
115     write( any.create_output_stream(), s);
116     }
117
118
119     public static org.omg.CORBA.TypeCode JavaDoc type( org.omg.CORBA.SystemException JavaDoc s)
120     {
121     String JavaDoc name = s.getClass().getName();
122     name = name.substring(name.lastIndexOf('.') + 1);
123     org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init();
124
125         org.omg.CORBA.TypeCode JavaDoc _type =
126             orb.create_struct_tc(
127                                  "IDL:omg.org/CORBA/" + name + ":1.0",
128                                  name,
129                                  new org.omg.CORBA.StructMember JavaDoc[]{
130                                      new org.omg.CORBA.StructMember JavaDoc(
131                                          "minor",
132                                          orb.get_primitive_tc(org.omg.CORBA.TCKind.from_int(3)),
133                                          null),
134                                      new org.omg.CORBA.StructMember JavaDoc(
135                                          "completed",
136                                          orb.create_enum_tc("IDL:omg.org/CORBA/CompletionStatus:1.0",
137                                                             "CompletionStatus",
138                                                             new String JavaDoc[]{"COMPLETED_YES","COMPLETED_NO","COMPLETED_MAYBE"}),
139                                          null)
140                                  });
141     return _type;
142     }
143
144
145
146     public static org.omg.CORBA.SystemException JavaDoc read(org.omg.CORBA.portable.InputStream JavaDoc in)
147     {
148     String JavaDoc className = className(in.read_string());
149     int minor = in.read_long();
150     org.omg.CORBA.CompletionStatus JavaDoc completed =
151             org.omg.CORBA.CompletionStatusHelper.read(in);
152     String JavaDoc message = null;
153     if (in instanceof ReplyInputStream)
154     {
155         ReplyInputStream input = (ReplyInputStream)in;
156         ServiceContext c = input.getServiceContext(ExceptionDetailMessage.value);
157         if (c != null)
158         {
159             CDRInputStream data = new CDRInputStream(null, c.context_data);
160             data.openEncapsulatedArray();
161             message = data.read_wstring();
162         }
163     }
164     try
165     {
166             Class JavaDoc ex = ObjectUtil.classForName( className );
167         Constructor constr =
168                 ex.getConstructor(
169                                   new Class JavaDoc[]{ String JavaDoc.class,
170                                                int.class,
171                                                org.omg.CORBA.CompletionStatus JavaDoc.class});
172
173         return (org.omg.CORBA.SystemException JavaDoc)constr.newInstance(
174                            new Object JavaDoc[]{"Server-side Exception: " + message,
175                                         new Integer JavaDoc(minor),
176                                         completed});
177     }
178     catch (Exception JavaDoc e )
179     {
180         throw new org.omg.CORBA.UNKNOWN JavaDoc(className);
181     }
182     }
183
184     public static void write(org.omg.CORBA.portable.OutputStream JavaDoc out,
185                              org.omg.CORBA.SystemException JavaDoc s)
186     {
187     out.write_string(repId(s.getClass()));
188     out.write_long(s.minor);
189     org.omg.CORBA.CompletionStatusHelper.write(out,s.completed);
190     }
191 }
192
Popular Tags