001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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: 513482 $ $Date: 2007-03-01 15:19:42 -0500 (Thu, 01 Mar 2007) $
023     */
024    public class ExceptionUtil {
025    
026        private static final String[] excludedPackages = {
027            "org.apache.geronimo.gbean.jmx.", "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    }