001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.exp;
020
021import lucee.runtime.Info;
022import lucee.runtime.PageContext;
023import lucee.runtime.dump.DumpData;
024import lucee.runtime.dump.DumpProperties;
025import lucee.runtime.dump.DumpTable;
026import lucee.runtime.op.Caster;
027import lucee.runtime.reflection.Reflector;
028import lucee.runtime.type.Collection;
029import lucee.runtime.type.util.KeyConstants;
030
031
032/**
033 * Box a Native Exception, Native = !PageException
034 */
035public class NativeException extends PageExceptionImpl {
036
037        private static final long serialVersionUID = 6221156691846424801L;
038        
039        private Throwable t;
040
041    /**
042         * Standart constructor for native Exception class
043         * @param t Throwable
044         */
045        public NativeException(Throwable t) {
046        super(t,t.getClass().getName());
047        this.t=t;
048        StackTraceElement[] st = t.getStackTrace();
049        if(hasLuceeRuntime(st))setStackTrace(st);
050        else {
051                StackTraceElement[] cst = Thread.currentThread().getStackTrace();
052                if(hasLuceeRuntime(cst)){
053                        StackTraceElement[] mst=new StackTraceElement[st.length+cst.length-1];
054                        System.arraycopy(st, 0, mst, 0, st.length);
055                        System.arraycopy(cst, 1, mst, st.length, cst.length-1);
056                        
057                        setStackTrace(mst);
058                }
059                else setStackTrace(st);
060        }
061        setAdditional(KeyConstants._Cause, t.getClass().getName());
062        }
063
064        private boolean hasLuceeRuntime(StackTraceElement[] st) {
065                if(st!=null)for(int i=0;i<st.length;i++){
066                        if(st[i].getClassName().indexOf("lucee.runtime")!=-1) return true;
067                }
068                return false;
069        }
070
071        @Override
072        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
073            DumpData data = super.toDumpData(pageContext, maxlevel,dp);
074            if(data instanceof DumpTable)
075        ((DumpTable)data).setTitle("Lucee ["+Info.getVersionAsString()+"] - Error ("+Caster.toClassName(t)+")");
076        
077        return data;
078    }
079
080    @Override
081    public boolean typeEqual(String type) {
082        if(super.typeEqual(type))return true;
083        return Reflector.isInstaneOfIgnoreCase(t.getClass(),type);
084    }
085
086        @Override
087        public void setAdditional(Collection.Key key, Object value) {
088                super.setAdditional(key, value);
089        }
090}