1 /* UnknownExceptionCtxHandler.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.CORBA
.CDR
;
41 import gnu
.CORBA
.Minor
;
42 import gnu
.CORBA
.ObjectCreator
;
43 import gnu
.CORBA
.GIOP
.ServiceContext
;
45 import org
.omg
.CORBA
.MARSHAL
;
46 import org
.omg
.CORBA
.NO_IMPLEMENT
;
47 import org
.omg
.CORBA
.StringValueHelper
;
48 import org
.omg
.CORBA
.portable
.OutputStream
;
50 import java
.lang
.reflect
.Constructor
;
51 import java
.util
.StringTokenizer
;
53 import javax
.rmi
.CORBA
.Util
;
56 * Reads the data about an unknown exception from the UnknownExceptionInfo.
58 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
60 public class UnknownExceptionCtxHandler
64 * Encode exception and add its recored to the message service contexts.
66 public static ServiceContext
[] addExceptionContext(ServiceContext
[] current
,
67 Throwable exception
, Object details
)
71 ServiceContext
[] c
= new ServiceContext
[current
.length
+ 1];
72 if (current
.length
> 0)
73 System
.arraycopy(current
, 0, c
, 0, current
.length
);
75 BufferedCdrOutput output
= new BufferedCdrOutput();
77 if (details
instanceof OutputStream
)
78 output
.setOrb(((OutputStream
) output
).orb());
80 if (details
instanceof AbstractCdrOutput
)
81 ((AbstractCdrOutput
) details
).cloneSettings(output
);
83 write(output
, exception
);
85 ServiceContext xc
= new ServiceContext();
86 xc
.context_id
= ServiceContext
.UnknownExceptionInfo
;
87 xc
.context_data
= output
.buffer
.toByteArray();
88 c
[current
.length
] = xc
;
99 * Write data about unknown exception.
101 public static void write(BufferedCdrOutput output
, Throwable t
)
103 t
.fillInStackTrace();
104 output
.write_Value(t
);
108 * Read the data about an unknown exception from the UnknownExceptionInfo.
109 * Following the documentation, this must be just value type, but it seems
110 * that in Sun's implementation is is not, as starts from 0x0. For value type,
111 * this would be null.
113 * TODO Implement reading and writing in Sun format, making Classpath IIOP
114 * interoperable with Sun's implementation. Current inmplementation reads and
115 * reproduces the exception class type only.
117 * @param input the input stream to read the context (orb and other settings
118 * are inherited from the main stream that received the message).
120 * @param contexts all service contexts that were present in the message.
122 * @return the Throwable, extracted from context, on null, if this has failed.
124 public static Throwable
read(BufferredCdrInput input
, ServiceContext
[] contexts
)
126 input
.mark(Integer
.MAX_VALUE
);
128 int h
= input
.read_long();
131 // This block reads exception info in the Sun specific format.
132 // (currently we read the exception name only).
135 // We may need to jump back if the value is read via value
139 int value_tag
= input
.read_long();
142 String codebase
= null;
146 // Check for the agreed null value.
147 if (value_tag
== vt_NULL
)
149 else if (value_tag
== vt_INDIRECTION
)
150 return (Throwable
) readIndirection(input
);
154 if ((value_tag
& vf_CODEBASE
) != 0)
156 // The codebase is present. The codebase is a space
157 // separated list of URLs from where the implementing
158 // code can be downloaded.
159 codebase
= read_string(input
);
162 if ((value_tag
& vf_MULTIPLE_IDS
) != 0)
164 // Multiple supported repository ids are present.
165 ids
= read_string_array(input
);
167 else if ((value_tag
& vf_ID
) != 0)
169 // Single supported repository id is present.
170 id
= read_string(input
);
174 java
.lang
.Object ox
= createInstance(id
, ids
, codebase
);
176 return (Throwable
) ox
;
180 ex
.printStackTrace();
187 // Read as defined in OMG documentation.
188 return (Throwable
) input
.read_Value();
193 * Load exception by name and create the instance. The reason why this is
194 * different from Vio is because some exceptions have no parameterless
195 * constructor, but have a constructor with the string parameter instead.
197 static Object
createInstance(String id
, String
[] ids
, String codebase
)
199 Object o
= _createInstance(id
, codebase
);
202 for (int i
= 0; i
< ids
.length
&& o
== null; i
++)
203 o
= _createInstance(ids
[i
], codebase
);
207 static Object
_createInstance(String id
, String codebase
)
211 if (id
.equals(StringValueHelper
.id()))
213 StringTokenizer st
= new StringTokenizer(id
, ":");
215 String prefix
= st
.nextToken();
216 if (prefix
.equalsIgnoreCase("IDL"))
217 return ObjectCreator
.Idl2Object(id
);
218 else if (prefix
.equalsIgnoreCase("RMI"))
220 String className
= st
.nextToken();
221 String hashCode
= st
.nextToken();
223 if (st
.hasMoreElements())
224 sid
= st
.nextToken();
228 Class objectClass
= Util
.loadClass(className
, codebase
,
229 Vio
.class.getClassLoader());
231 String rid
= ObjectCreator
.getRepositoryId(objectClass
);
235 // If direct string comparison fails, compare by meaning.
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);
244 long h1
= Long
.parseLong(hashCode
, 16);
245 long h2
= Long
.parseLong(st2
.nextToken(), 16);
247 throwIt("Hashcode mismatch", id
, rid
, null);
249 if (sid
!= null && st2
.hasMoreTokens())
251 long s1
= Long
.parseLong(hashCode
, 16);
252 long s2
= Long
.parseLong(st2
.nextToken(), 16);
254 throwIt("serialVersionUID mismatch", id
, rid
, null);
257 catch (NumberFormatException e
)
259 throwIt("Invalid hashcode or svuid format: ", id
, rid
, e
);
263 // Some RemoteExceptions have no public parameterless constructor,
264 // but they have constructor taking string as parameter.
267 return objectClass
.newInstance();
271 // Try instantiate passing string as parameter.
272 Constructor c
= objectClass
.getConstructor(new Class
[] { String
.class });
273 return c
.newInstance(new Object
[] { "<message unavailable>" });
278 m
.minor
= Minor
.Instantiation
;
283 MARSHAL m
= new MARSHAL("Unable to instantiate " + id
);
284 m
.minor
= Minor
.Instantiation
;
290 throw new NO_IMPLEMENT("Unsupported prefix " + prefix
+ ":");