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.op;
020
021import lucee.commons.lang.ExceptionUtil;
022import lucee.runtime.PageContext;
023import lucee.runtime.PageSource;
024import lucee.runtime.db.SQL;
025import lucee.runtime.exp.Abort;
026import lucee.runtime.exp.AbortException;
027import lucee.runtime.exp.ApplicationException;
028import lucee.runtime.exp.CasterException;
029import lucee.runtime.exp.CustomTypeException;
030import lucee.runtime.exp.DatabaseException;
031import lucee.runtime.exp.ExpressionException;
032import lucee.runtime.exp.FunctionException;
033import lucee.runtime.exp.LockException;
034import lucee.runtime.exp.MissingIncludeException;
035import lucee.runtime.exp.NativeException;
036import lucee.runtime.exp.PageException;
037import lucee.runtime.exp.SecurityException;
038import lucee.runtime.exp.TemplateException;
039import lucee.runtime.exp.XMLException;
040import lucee.runtime.reflection.Reflector;
041import lucee.runtime.util.Excepton;
042
043/**
044 * Implementation of Exception Util
045 */
046public final class ExceptonImpl implements Excepton {
047
048    private static Class[] exceptions=new Class[14];
049    
050    static {
051        exceptions[TYPE_ABORT]=Abort.class;
052        exceptions[TYPE_ABORT_EXP]=AbortException.class;
053        exceptions[TYPE_APPLICATION_EXP]=ApplicationException.class;
054        exceptions[TYPE_CASTER_EXP]=CasterException.class;
055        exceptions[TYPE_CUSTOM_TYPE_EXP]=CustomTypeException.class;
056        exceptions[TYPE_DATABASE_EXP]=DatabaseException.class;
057        exceptions[TYPE_EXPRESSION_EXP]=ExpressionException.class;
058        exceptions[TYPE_FUNCTION_EXP]=FunctionException.class;
059        exceptions[TYPE_LOCK_EXP]=LockException.class;
060        exceptions[TYPE_MISSING_INCLUDE_EXP]=MissingIncludeException.class;
061        exceptions[TYPE_NATIVE_EXP]=NativeException.class;
062        exceptions[TYPE_SECURITY_EXP]=SecurityException.class;
063        exceptions[TYPE_TEMPLATE_EXP]=TemplateException.class;
064        exceptions[TYPE_XML_EXP]=XMLException.class;
065    }
066    
067    private static ExceptonImpl singelton;
068
069    /**
070     * @return singleton instance
071     */
072    public static Excepton getInstance() {
073        if(singelton==null)singelton=new ExceptonImpl();
074        return singelton;
075    }
076
077    @Override
078    public PageException createAbort() {
079        return new Abort(Abort.SCOPE_REQUEST);
080    }
081    
082    @Override
083    public PageException createAbortException(String showError) {
084        return new AbortException(showError);
085    }
086    
087    @Override
088    public PageException createApplicationException(String message) {
089        return new ApplicationException(message);
090    }
091    
092    @Override
093    public PageException createApplicationException(String message, String detail) {
094        return new ApplicationException(message,detail);
095    }
096    
097    @Override
098    public PageException createCasterException(String message) {
099        return new CasterException(message);
100    }
101    
102    @Override
103    public PageException createCustomTypeException(String message, String detail, String errorcode, String customType) {
104        return createCustomTypeException(message, detail, errorcode, customType, null);
105    }
106    
107    public PageException createCustomTypeException(String message, String detail, String errorcode, String customType,String extendedInfo) {
108        return new CustomTypeException(message,detail,errorcode,customType,extendedInfo);
109    }
110    
111    @Override
112    public PageException createDatabaseException(String message) {
113        return new DatabaseException(message,null,null,null);
114    }
115    
116    @Override
117    public PageException createDatabaseException(String message, String detail) {
118        return new DatabaseException(message,detail,null,null);
119    }
120    
121    @Override
122    public PageException createDatabaseException(String message, SQL sql) {
123        return new DatabaseException(message,null,sql,null);
124    }
125    
126    @Override
127    public PageException createExpressionException(String message) {
128        return new ExpressionException(message);
129    }
130    
131    @Override
132    public PageException createExpressionException(String message, String detail) {
133        return new ExpressionException(message, detail);
134    }
135    
136    @Override
137    public PageException createFunctionException(PageContext pc,String functionName, String badArgumentPosition, String badArgumentName, String message) {
138        return new FunctionException(pc,functionName, badArgumentPosition, badArgumentName,message,null);
139    }
140    
141    @Override
142    public PageException createFunctionException(PageContext pc,String functionName, int badArgumentPosition, String badArgumentName, String message, String detail) {
143        return new FunctionException(pc,functionName, badArgumentPosition, badArgumentName,message,detail);
144    }
145    
146    @Override
147    public PageException createLockException(String operation, String name, String message) {
148        return new LockException(operation,name,message);
149    }
150    
151    @Override
152    public PageException createMissingIncludeException(PageSource ps) {
153        return new MissingIncludeException(ps);
154    }
155    
156    @Override
157    public PageException createNativeException(Throwable t) {
158                ExceptionUtil.rethrowIfNecessary(t);
159        return new NativeException(t);
160    }
161    
162    @Override
163    public PageException createSecurityException(String message) {
164        return new SecurityException(message);
165    }
166    
167    @Override
168    public PageException createSecurityException(String message, String detail) {
169        return new SecurityException(message,detail);
170    }
171    
172    @Override
173    public PageException createTemplateException(String message) {
174        return new TemplateException(message);
175    }
176    
177    @Override
178    public PageException createTemplateException(String message, String detail) {
179        return new TemplateException(message,detail);
180    }
181    
182    @Override
183    public PageException createXMLException(String message) {
184        return new XMLException(message);
185    }
186    
187    @Override
188    public PageException createXMLException(String message, String detail) {
189        return new XMLException(message,detail);
190    }
191
192    @Override
193    
194    public boolean isOfType(int type, Throwable t) {
195        switch(type){
196                case TYPE_ABORT:                                return Abort.isSilentAbort(t);
197                case TYPE_ABORT_EXP:                    return t instanceof AbortException;
198                case TYPE_APPLICATION_EXP:              return t instanceof ApplicationException;
199                case TYPE_CASTER_EXP:                   return t instanceof CasterException;
200                case TYPE_CUSTOM_TYPE_EXP:              return t instanceof CustomTypeException;
201                case TYPE_DATABASE_EXP:                 return t instanceof DatabaseException;
202                case TYPE_EXPRESSION_EXP:               return t instanceof ExpressionException;
203                case TYPE_FUNCTION_EXP:                 return t instanceof FunctionException;
204                case TYPE_LOCK_EXP:                             return t instanceof LockException;
205                case TYPE_MISSING_INCLUDE_EXP:  return t instanceof MissingIncludeException;
206                case TYPE_NATIVE_EXP:                   return t instanceof NativeException;
207                case TYPE_SECURITY_EXP:                 return t instanceof SecurityException;
208                case TYPE_TEMPLATE_EXP:                 return t instanceof TemplateException;
209                case TYPE_XML_EXP:                              return t instanceof XMLException;
210        }
211        return Reflector.isInstaneOf(t.getClass(),exceptions[type]);
212    }
213    
214
215}