01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.remoting.support;
18:
19: import java.util.HashSet;
20: import java.util.Set;
21:
22: import org.springframework.core.JdkVersion;
23:
24: /**
25: * General utilities for handling remote invocations.
26: *
27: * <p>Mainly intended for use within the remoting framework.
28: *
29: * @author Juergen Hoeller
30: * @since 2.0
31: */
32: public abstract class RemoteInvocationUtils {
33:
34: /**
35: * Fill the current client-side stack trace into the given exception.
36: * <p>The given exception is typically thrown on the server and serialized
37: * as-is, with the client wanting it to contain the client-side portion
38: * of the stack trace as well. What we can do here is to update the
39: * <code>StackTraceElement</code> array with the current client-side stack
40: * trace, provided that we run on JDK 1.4+.
41: * @param ex the exception to update
42: * @see java.lang.Throwable#getStackTrace()
43: * @see java.lang.Throwable#setStackTrace(StackTraceElement[])
44: */
45: public static void fillInClientStackTraceIfPossible(Throwable ex) {
46: if (ex != null) {
47: StackTraceElement[] clientStack = new Throwable()
48: .getStackTrace();
49: Set visitedExceptions = new HashSet();
50: Throwable exToUpdate = ex;
51: while (exToUpdate != null
52: && !visitedExceptions.contains(exToUpdate)) {
53: StackTraceElement[] serverStack = exToUpdate
54: .getStackTrace();
55: StackTraceElement[] combinedStack = new StackTraceElement[serverStack.length
56: + clientStack.length];
57: System.arraycopy(serverStack, 0, combinedStack, 0,
58: serverStack.length);
59: System.arraycopy(clientStack, 0, combinedStack,
60: serverStack.length, clientStack.length);
61: exToUpdate.setStackTrace(combinedStack);
62: visitedExceptions.add(exToUpdate);
63: exToUpdate = exToUpdate.getCause();
64: }
65: }
66: }
67:
68: }
|