1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52:
53: import ;
54:
55:
60: public class UnknownExceptionCtxHandler
61: extends Vio
62: {
63:
66: public static ServiceContext[] addExceptionContext(ServiceContext[] current,
67: Throwable exception, Object details)
68: {
69: try
70: {
71: ServiceContext[] c = new ServiceContext[current.length + 1];
72: if (current.length > 0)
73: System.arraycopy(current, 0, c, 0, current.length);
74:
75: BufferedCdrOutput output = new BufferedCdrOutput();
76:
77: if (details instanceof OutputStream)
78: output.setOrb(((OutputStream) output).orb());
79:
80: if (details instanceof AbstractCdrOutput)
81: ((AbstractCdrOutput) details).cloneSettings(output);
82:
83: write(output, exception);
84:
85: ServiceContext xc = new ServiceContext();
86: xc.context_id = ServiceContext.UnknownExceptionInfo;
87: xc.context_data = output.buffer.toByteArray();
88: c[current.length] = xc;
89: return c;
90: }
91: catch (Exception ex)
92: {
93: ex.printStackTrace();
94: return current;
95: }
96: }
97:
98:
101: public static void write(BufferedCdrOutput output, Throwable t)
102: {
103: t.fillInStackTrace();
104: output.write_Value(t);
105: }
106:
107:
124: public static Throwable read(BufferredCdrInput input, ServiceContext[] contexts)
125: {
126: input.mark(Integer.MAX_VALUE);
127:
128: int h = input.read_long();
129: if (h == 0)
130: {
131:
132:
133: try
134: {
135:
136:
137: input.mark(512);
138:
139: int value_tag = input.read_long();
140: checkTag(value_tag);
141:
142: String codebase = null;
143: String[] ids = null;
144: String id = null;
145:
146:
147: if (value_tag == vt_NULL)
148: return null;
149: else if (value_tag == vt_INDIRECTION)
150: return (Throwable) readIndirection(input);
151: else
152: {
153:
154: if ((value_tag & vf_CODEBASE) != 0)
155: {
156:
157:
158:
159: codebase = read_string(input);
160: }
161:
162: if ((value_tag & vf_MULTIPLE_IDS) != 0)
163: {
164:
165: ids = read_string_array(input);
166: }
167: else if ((value_tag & vf_ID) != 0)
168: {
169:
170: id = read_string(input);
171: }
172: }
173:
174: java.lang.Object ox = createInstance(id, ids, codebase);
175:
176: return (Throwable) ox;
177: }
178: catch (Exception ex)
179: {
180: ex.printStackTrace();
181: return null;
182: }
183: }
184: else
185: {
186: input.reset();
187:
188: return (Throwable) input.read_Value();
189: }
190: }
191:
192:
197: static Object createInstance(String id, String[] ids, String codebase)
198: {
199: Object o = _createInstance(id, codebase);
200:
201: if (ids != null)
202: for (int i = 0; i < ids.length && o == null; i++)
203: o = _createInstance(ids[i], codebase);
204: return o;
205: }
206:
207: static Object _createInstance(String id, String codebase)
208: {
209: if (id == null)
210: return null;
211: if (id.equals(StringValueHelper.id()))
212: return "";
213: StringTokenizer st = new StringTokenizer(id, ":");
214:
215: String prefix = st.nextToken();
216: if (prefix.equalsIgnoreCase("IDL"))
217: return ObjectCreator.Idl2Object(id);
218: else if (prefix.equalsIgnoreCase("RMI"))
219: {
220: String className = st.nextToken();
221: String hashCode = st.nextToken();
222: String sid = null;
223: if (st.hasMoreElements())
224: sid = st.nextToken();
225:
226: try
227: {
228: Class objectClass = Util.loadClass(className, codebase,
229: Vio.class.getClassLoader());
230:
231: String rid = ObjectCreator.getRepositoryId(objectClass);
232:
233: if (!rid.equals(id))
234: {
235:
236: StringTokenizer st2 = new StringTokenizer(rid, ":");
237: if (!st2.nextToken().equals("RMI"))
238: throw new InternalError("RMI format expected: '" + rid + "'");
239: if (!st2.nextToken().equals(className))
240: throwIt("Class name mismatch", id, rid, null);
241:
242: try
243: {
244: long h1 = Long.parseLong(hashCode, 16);
245: long h2 = Long.parseLong(st2.nextToken(), 16);
246: if (h1 != h2)
247: throwIt("Hashcode mismatch", id, rid, null);
248:
249: if (sid != null && st2.hasMoreTokens())
250: {
251: long s1 = Long.parseLong(hashCode, 16);
252: long s2 = Long.parseLong(st2.nextToken(), 16);
253: if (s1 != s2)
254: throwIt("serialVersionUID mismatch", id, rid, null);
255: }
256: }
257: catch (NumberFormatException e)
258: {
259: throwIt("Invalid hashcode or svuid format: ", id, rid, e);
260: }
261: }
262:
263:
264:
265: try
266: {
267: return objectClass.newInstance();
268: }
269: catch (Exception ex)
270: {
271:
272: Constructor c = objectClass.getConstructor(new Class[] { String.class });
273: return c.newInstance(new Object[] { "<message unavailable>" });
274: }
275: }
276: catch (MARSHAL m)
277: {
278: m.minor = Minor.Instantiation;
279: throw m;
280: }
281: catch (Exception ex)
282: {
283: MARSHAL m = new MARSHAL("Unable to instantiate " + id);
284: m.minor = Minor.Instantiation;
285: m.initCause(ex);
286: throw m;
287: }
288: }
289: else
290: throw new NO_IMPLEMENT("Unsupported prefix " + prefix + ":");
291: }
292: }