001    /*
002     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003     *
004     * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
005     *
006     * The contents of this file are subject to the terms of either the GNU
007     * General Public License Version 2 only ("GPL") or the Common Development
008     * and Distribution License("CDDL") (collectively, the "License").  You
009     * may not use this file except in compliance with the License.  You can
010     * obtain a copy of the License at
011     * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
012     * or packager/legal/LICENSE.txt.  See the License for the specific
013     * language governing permissions and limitations under the License.
014     *
015     * When distributing the software, include this License Header Notice in each
016     * file and include the License file at packager/legal/LICENSE.txt.
017     *
018     * GPL Classpath Exception:
019     * Oracle designates this particular file as subject to the "Classpath"
020     * exception as provided by Oracle in the GPL Version 2 section of the License
021     * file that accompanied this code.
022     *
023     * Modifications:
024     * If applicable, add the following below the License Header, with the fields
025     * enclosed by brackets [] replaced by your own identifying information:
026     * "Portions Copyright [year] [name of copyright owner]"
027     *
028     * Contributor(s):
029     * If you wish your version of this file to be governed by only the CDDL or
030     * only the GPL Version 2, indicate your decision by adding "[Contributor]
031     * elects to include this software in this distribution under the [CDDL or GPL
032     * Version 2] license."  If you don't indicate a single choice of license, a
033     * recipient has the option to distribute your version of this file under
034     * either the CDDL, the GPL Version 2 or to extend the choice of license to
035     * its licensees as provided above.  However, if you add GPL Version 2 code
036     * and therefore, elected the GPL Version 2 license, then the option applies
037     * only if the new code is made subject to such option by the copyright
038     * holder.
039     */
040    
041    package com.sun.enterprise.admin.cli.remote;
042    
043    import java.util.ArrayList;
044    import java.util.List;
045    
046    /** An immutable class to analyze the exception stack trace of a given
047     * instance of {@link java.lang.Exception}. Can be extended to handle
048     * throwables, but it is not done in this version on purpose. Takes the
049     * snapshot of given exception at the time of instantiation.
050     * @author केदा&#2352 (km@dev.java.net)
051     * @since GlassFish v3 Prelude
052     */
053    
054    final class ExceptionAnalyzer {
055        
056        private final Exception exc;
057        private final List<Throwable> chain;
058        ExceptionAnalyzer(Exception e) {
059            if (e == null)
060                throw new IllegalArgumentException("null arg");
061            this.exc   = e;
062            this.chain = new ArrayList<Throwable>();
063            chain.add(exc);
064            build();
065        }
066        
067        private void build() {
068            Throwable t = exc.getCause();
069            while (t != null) {
070                chain.add(t);
071                t = t.getCause();
072            }
073        }
074        
075        /** Returns the first instance of the given Exception class in the chain
076         *  of causes. The counting starts from the instance of the Exception that
077         *  created the ExceptionAnalyzer class itself.
078         * @param ac the unknown subclass of Exception that needs the chain to be examined for
079         * @return first instance of given Throwable (returned object will be an
080         * instance of the given class) or null if there is no such instance
081         */ 
082        Throwable getFirstInstanceOf(Class<? extends Exception> ac) {
083            for (Throwable t : chain) {
084                try {
085                    ac.cast(t);
086                    return t;
087                } catch(ClassCastException cce) {
088                    //ignore and continue
089                }
090            }
091            return null;
092        }
093    }
094