KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > support > RemoteInvocationUtils


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import java.util.Set JavaDoc;
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 JavaDoc ex) {
46         if (JdkVersion.isAtLeastJava14() && ex != null) {
47             StackTraceElement JavaDoc[] clientStack = new Throwable JavaDoc().getStackTrace();
48             Set JavaDoc visitedExceptions = new HashSet JavaDoc();
49             Throwable JavaDoc exToUpdate = ex;
50             while (exToUpdate != null && !visitedExceptions.contains(exToUpdate)) {
51                 StackTraceElement JavaDoc[] serverStack = exToUpdate.getStackTrace();
52                 StackTraceElement JavaDoc[] combinedStack = new StackTraceElement JavaDoc[serverStack.length + clientStack.length];
53                 System.arraycopy(serverStack, 0, combinedStack, 0, serverStack.length);
54                 System.arraycopy(clientStack, 0, combinedStack, serverStack.length, clientStack.length);
55                 exToUpdate.setStackTrace(combinedStack);
56                 visitedExceptions.add(exToUpdate);
57                 exToUpdate = exToUpdate.getCause();
58             }
59         }
60     }
61
62 }
63
Popular Tags