001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.main;
018    
019    import java.util.ArrayList;
020    
021    /**
022     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
023     */
024    public class ExceptionUtil {
025    
026        private static final String[] excludedPackages = {
027            "org.apache.geronimo.gbean.jmx.", "mx4j.", "net.sf.cglib.reflect.", "sun.reflect."
028        };
029    
030        private static final String[] excludedStrings = {
031            "$$EnhancerByCGLIB$$","$$FastClassByCGLIB$$"
032        };
033    
034        public static void trimStackTrace(Throwable t) {
035            if (t == null) {
036                return;
037            }
038    
039            StackTraceElement[] trace = t.getStackTrace();
040            ArrayList list = new ArrayList();
041    
042            boolean skip = true;
043    
044            int i = 0;
045    
046            // If the start of the stack trace is something
047            // on the exclude list, don't exclude it.
048            for (; i < trace.length && skip; i++) {
049                skip = skip && isExcluded(trace[i].getClassName());
050            }
051            list.add(trace[i-1]);
052    
053    
054            for (; i < trace.length; i++) {
055                if ( !isExcluded(trace[i].getClassName()) ){
056                    list.add(trace[i]);
057                }
058            }
059    
060            t.setStackTrace((StackTraceElement[]) list.toArray(new StackTraceElement[0]));
061            trimStackTrace(t.getCause());
062        }
063    
064        private static boolean isExcluded(String className) {
065            for (int j = 0; j < excludedPackages.length; j++) {
066                if (className.startsWith(excludedPackages[j])) {
067                    return true;
068                }
069            }
070            for (int j = 0; j < excludedStrings.length; j++) {
071                if (className.indexOf(excludedStrings[j]) != -1) {
072                    return true;
073                }
074            }
075            return false;
076        }
077    }