001    package railo.runtime.tag;
002    
003    import railo.commons.lang.StringUtil;
004    import railo.runtime.exp.CatchBlock;
005    import railo.runtime.exp.CustomTypeException;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.ext.tag.TagImpl;
008    import railo.runtime.op.Caster;
009    import railo.runtime.type.ObjectWrap;
010    
011    /**
012    * The cfthrow tag raises a developer-specified exception that can be caught with cfcatch tag 
013    *   having any of the following type specifications - cfcatch type = 'custom_type', cfcatch type = 'Application'
014    *   'cfcatch' type = 'Any'
015    *
016    *
017    *
018    **/
019    public final class Throw extends TagImpl {
020    
021            /** A custom error code that you supply. */
022            private String extendedinfo=null;
023    
024            private String type="application";
025    
026            private String detail="";
027    
028            /** A message that describes the exceptional event. */
029            private Object message;
030    
031            /** A custom error code that you supply. */
032            private String errorcode="";
033    
034            private Object object;
035    
036            /**
037            * @see javax.servlet.jsp.tagext.Tag#release()
038            */
039            public void release()   {
040                    super.release();
041                    extendedinfo=null;
042                    type="application";
043                    detail="";
044                    message=null;
045                    errorcode="";
046                    object=null;
047            }
048    
049    
050    
051            /** set the value extendedinfo
052            *  A custom error code that you supply.
053            * @param extendedinfo value to set
054            **/
055            public void setExtendedinfo(String extendedinfo)        {
056                    this.extendedinfo=extendedinfo;
057            }
058    
059            /** set the value type
060            * @param type value to set
061            **/
062            public void setType(String type)        {
063                    this.type=type;
064            }
065    
066            /** set the value detail
067            * @param detail value to set
068            **/
069            public void setDetail(String detail)    {
070                    this.detail=detail;
071            }
072    
073            /** set the value message
074            *  A message that describes the exceptional event.
075            * @param message value to set
076            **/
077            public void setMessage(Object message)  {
078                    this.message=message;
079            }
080            
081            /**
082             * @deprecated this method should no longer be used.
083             * */
084            public void setMessage(String message)  {
085                    this.message=message;
086            }
087    
088            /** set the value errorcode
089            *  A custom error code that you supply.
090            * @param errorcode value to set
091            **/
092            public void setErrorcode(String errorcode)      {
093                    this.errorcode=errorcode;
094            }
095    
096            /** set the value object
097            *  a native java exception Object, if this attribute is defined all other will be ignored.
098            * @param object object to set
099             * @throws PageException
100            **/
101            public void setObject(Object object) throws PageException       {
102                    this.object=object;     
103            }
104    
105    
106            private PageException toPageException(Object object, PageException defaultValue) throws PageException {
107                    if((object instanceof ObjectWrap))
108                            return toPageException(((ObjectWrap)object).getEmbededObject(),defaultValue);
109                    
110                    
111                    if(object instanceof CatchBlock) {
112                            CatchBlock cb = (CatchBlock)object;
113                            return cb.getPageException();
114                    }
115                    if(object instanceof PageException) return (PageException)object;
116                    if(object instanceof Throwable) {
117                            Throwable t=(Throwable)object;
118                            return new CustomTypeException(t.getMessage(),"","",t.getClass().getName(),"");
119                    }
120                    return defaultValue;
121                    
122            }
123    
124    
125    
126            /**
127            * @see javax.servlet.jsp.tagext.Tag#doStartTag()
128            */
129            public int doStartTag() throws PageException    {
130                    
131                    _doStartTag(message);
132                    _doStartTag(object);
133                    
134                    throw new CustomTypeException( "",detail,errorcode,type,extendedinfo);
135            }
136    
137            private void _doStartTag(Object obj) throws PageException {
138                    if(!StringUtil.isEmpty(obj)){
139                            PageException pe = toPageException(obj,null);
140                            if(pe!=null) throw pe;
141                            
142                            CustomTypeException exception = new CustomTypeException(Caster.toString(obj),detail,errorcode,type,extendedinfo);
143                            throw exception;
144                    }
145            }
146    
147    
148    
149            /**
150            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
151            */
152            public int doEndTag()   {
153                    return EVAL_PAGE;
154            }
155    }