1 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 className( String repId ) 32 { 33 String id_base = repId.substring(4, repId.lastIndexOf(':')); 35 return ir2scopes("org.omg",id_base.substring(7)); 36 } 37 38 private static final String ir2scopes( String prefix, String s ) 39 { 40 if( s.indexOf("/") < 0) 41 return s; 42 java.util.StringTokenizer strtok = 43 new java.util.StringTokenizer ( s, "/" ); 44 45 int count = strtok.countTokens(); 46 StringBuffer sb = new StringBuffer (); 47 sb.append(prefix); 48 49 for( int i = 0; strtok.hasMoreTokens(); i++ ) 50 { 51 String sc = strtok.nextToken(); 52 try 53 { 54 Class 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 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 repId( Class c ) 80 { 81 String className = c.getName(); 82 String body = className.substring(7); 83 return "IDL:omg.org/" + scopesToIR(body) + ":1.0"; 84 } 85 86 private static final String scopesToIR( String s ) 87 { 88 if( s.indexOf(".") < 0) 89 return s; 90 java.util.StringTokenizer strtok = 91 new java.util.StringTokenizer ( s, "." ); 92 String scopes[] = new String [strtok.countTokens()]; 93 for( int i = 0; strtok.hasMoreTokens(); i++ ){ 94 String 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 sb = new StringBuffer (); 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 any, org.omg.CORBA.SystemException s) 113 { 114 any.type( type( s )); 115 write( any.create_output_stream(), s); 116 } 117 118 119 public static org.omg.CORBA.TypeCode type( org.omg.CORBA.SystemException s) 120 { 121 String name = s.getClass().getName(); 122 name = name.substring(name.lastIndexOf('.') + 1); 123 org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(); 124 125 org.omg.CORBA.TypeCode _type = 126 orb.create_struct_tc( 127 "IDL:omg.org/CORBA/" + name + ":1.0", 128 name, 129 new org.omg.CORBA.StructMember []{ 130 new org.omg.CORBA.StructMember ( 131 "minor", 132 orb.get_primitive_tc(org.omg.CORBA.TCKind.from_int(3)), 133 null), 134 new org.omg.CORBA.StructMember ( 135 "completed", 136 orb.create_enum_tc("IDL:omg.org/CORBA/CompletionStatus:1.0", 137 "CompletionStatus", 138 new String []{"COMPLETED_YES","COMPLETED_NO","COMPLETED_MAYBE"}), 139 null) 140 }); 141 return _type; 142 } 143 144 145 146 public static org.omg.CORBA.SystemException read(org.omg.CORBA.portable.InputStream in) 147 { 148 String className = className(in.read_string()); 149 int minor = in.read_long(); 150 org.omg.CORBA.CompletionStatus completed = 151 org.omg.CORBA.CompletionStatusHelper.read(in); 152 String 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 ex = ObjectUtil.classForName( className ); 167 Constructor constr = 168 ex.getConstructor( 169 new Class []{ String .class, 170 int.class, 171 org.omg.CORBA.CompletionStatus .class}); 172 173 return (org.omg.CORBA.SystemException )constr.newInstance( 174 new Object []{"Server-side Exception: " + message, 175 new Integer (minor), 176 completed}); 177 } 178 catch (Exception e ) 179 { 180 throw new org.omg.CORBA.UNKNOWN (className); 181 } 182 } 183 184 public static void write(org.omg.CORBA.portable.OutputStream out, 185 org.omg.CORBA.SystemException 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 |