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