Coverage Report - javax.faces.webapp._ErrorPageWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
_ErrorPageWriter
0%
0/302
0%
0/172
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package javax.faces.webapp;
 20  
 
 21  
 import java.beans.BeanInfo;
 22  
 import java.beans.Introspector;
 23  
 import java.beans.PropertyDescriptor;
 24  
 import java.io.ByteArrayOutputStream;
 25  
 import java.io.FileNotFoundException;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.PrintWriter;
 29  
 import java.io.StringWriter;
 30  
 import java.io.Writer;
 31  
 import java.lang.reflect.Method;
 32  
 import java.text.DateFormat;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Arrays;
 35  
 import java.util.Collection;
 36  
 import java.util.Date;
 37  
 import java.util.Iterator;
 38  
 import java.util.List;
 39  
 import java.util.Map;
 40  
 import java.util.SortedMap;
 41  
 import java.util.TreeMap;
 42  
 import java.util.regex.Matcher;
 43  
 import java.util.regex.Pattern;
 44  
 
 45  
 import javax.el.Expression;
 46  
 import javax.el.ValueExpression;
 47  
 import javax.faces.component.UIComponent;
 48  
 import javax.faces.context.ExternalContext;
 49  
 import javax.faces.context.FacesContext;
 50  
 import javax.faces.el.MethodBinding;
 51  
 import javax.faces.el.ValueBinding;
 52  
 import javax.servlet.ServletException;
 53  
 import javax.servlet.http.HttpServletResponse;
 54  
 
 55  
 import org.apache.commons.logging.Log;
 56  
 import org.apache.commons.logging.LogFactory;
 57  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 58  
 
 59  
 /**
 60  
  * @author Jacob Hookom (ICLA with ASF filed)
 61  
  */
 62  
 final class _ErrorPageWriter {
 63  
 
 64  0
     private static final Log log = LogFactory.getLog(_ErrorPageWriter.class);
 65  
 
 66  
     private final static String TS = "<";
 67  
 
 68  
     private static final String ERROR_TEMPLATE = "META-INF/rsc/myfaces-dev-error.xml";
 69  
 
 70  
     @JSFWebConfigParam(defaultValue="META-INF/rsc/myfaces-dev-error.xml", since="1.2.4")
 71  
     private static final String ERROR_TEMPLATE_RESOURCE = "org.apache.myfaces.ERROR_TEMPLATE_RESOURCE";
 72  
 
 73  
     private static String[] ERROR_PARTS;
 74  
 
 75  
     private static final String DEBUG_TEMPLATE = "META-INF/rsc/myfaces-dev-debug.xml";
 76  
     
 77  
     @JSFWebConfigParam(defaultValue="META-INF/rsc/myfaces-dev-debug.xml", since="1.2.4")
 78  
     private static final String DEBUG_TEMPLATE_RESOURCE = "org.apache.myfaces.DEBUG_TEMPLATE_RESOURCE";    
 79  
 
 80  
     private static String[] DEBUG_PARTS;
 81  
 
 82  
     public _ErrorPageWriter() {
 83  0
         super();
 84  0
     }
 85  
     
 86  
     private static String getErrorTemplate(FacesContext context)
 87  
     {
 88  0
         String errorTemplate = context.getExternalContext().getInitParameter(ERROR_TEMPLATE_RESOURCE);
 89  0
         if (errorTemplate != null)
 90  
         {
 91  0
             return errorTemplate;
 92  
         }
 93  0
         return ERROR_TEMPLATE;
 94  
     }
 95  
     
 96  
     private static String getDebugTemplate(FacesContext context)
 97  
     {
 98  0
         String debugTemplate = context.getExternalContext().getInitParameter(DEBUG_TEMPLATE_RESOURCE);
 99  0
         if (debugTemplate != null)
 100  
         {
 101  0
             return debugTemplate;
 102  
         }        
 103  0
         return DEBUG_TEMPLATE;
 104  
     }
 105  
     
 106  
     private static void init(FacesContext context) throws IOException {
 107  0
         if (ERROR_PARTS == null) {
 108  0
             ERROR_PARTS = splitTemplate(getErrorTemplate(context));
 109  
         }
 110  
 
 111  0
         if (DEBUG_PARTS == null) {
 112  0
             DEBUG_PARTS = splitTemplate(getDebugTemplate(context));
 113  
         }
 114  0
     }
 115  
 
 116  
     private static String[] splitTemplate(String rsc) throws IOException {
 117  0
         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(rsc);
 118  0
         if (is == null)
 119  
         {
 120  0
             is = _ErrorPageWriter.class.getClassLoader().getResourceAsStream(rsc);
 121  
         }
 122  0
         if (is == null) {
 123  0
             throw new FileNotFoundException(rsc);
 124  
         }
 125  0
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 126  0
         byte[] buff = new byte[512];
 127  
         int read;
 128  0
         while ((read = is.read(buff)) != -1) {
 129  0
             baos.write(buff, 0, read);
 130  
         }
 131  0
         String str = baos.toString();
 132  0
         return str.split("@@");
 133  
     }
 134  
 
 135  
     private static ArrayList getErrorId(Throwable e){
 136  0
         String message = e.getMessage();
 137  
 
 138  0
         if(message==null)
 139  0
             return null;
 140  
 
 141  0
         ArrayList list = new ArrayList();
 142  0
         Pattern pattern = Pattern.compile(".*?\\Q,Id:\\E\\s*(\\S+)\\s*\\].*?");
 143  0
         Matcher matcher = pattern.matcher(message);
 144  
 
 145  0
         while (matcher.find()){
 146  0
             list.add(matcher.group(1));
 147  
         }
 148  0
         if (list.size()>0) return list;
 149  0
         return null;
 150  
     }
 151  
 
 152  
     public static void writeCause(Writer writer, Throwable ex) throws IOException {
 153  0
         String msg = ex.getMessage();
 154  0
         while (ex.getCause()!=null){
 155  0
             ex=ex.getCause();
 156  0
             if (ex.getMessage()!=null) msg = ex.getMessage();
 157  
         }
 158  
 
 159  0
         if (msg != null) {
 160  0
             msg =ex.getClass().getName() + " - " + msg;
 161  0
             writer.write(msg.replaceAll("<", TS));
 162  
         } else {
 163  0
             writer.write(ex.getClass().getName());
 164  
         }
 165  0
     }
 166  
 
 167  
     public static void debugHtml(Writer writer, FacesContext faces, Throwable e) throws IOException {
 168  0
         init(faces);
 169  0
         Date now = new Date();
 170  0
         for (int i = 0; i < ERROR_PARTS.length; i++) {
 171  0
             if ("message".equals(ERROR_PARTS[i])) {
 172  0
                 String msg = e.getMessage();
 173  0
                 if (msg != null) {
 174  0
                     writer.write(msg.replaceAll("<", TS));
 175  
                 } else {
 176  0
                     writer.write(e.getClass().getName());
 177  
                 }
 178  0
             } else if ("trace".equals(ERROR_PARTS[i])) {
 179  0
                 writeException(writer, e);
 180  0
             } else if ("now".equals(ERROR_PARTS[i])) {
 181  0
                 writer.write(DateFormat.getDateTimeInstance().format(now));
 182  0
             } else if ("tree".equals(ERROR_PARTS[i])) {
 183  0
                 if (faces.getViewRoot() != null) {
 184  0
                     writeComponent(writer, faces.getViewRoot(), getErrorId(e));
 185  
                 }
 186  0
             } else if ("vars".equals(ERROR_PARTS[i])) {
 187  0
                 writeVariables(writer, faces);
 188  0
             } else if ("cause".equals(ERROR_PARTS[i])) {
 189  0
                 writeCause(writer, e);
 190  
             } else {
 191  0
                 writer.write(ERROR_PARTS[i]);
 192  
             }
 193  
         }
 194  0
     }
 195  
     
 196  
     public static void debugHtml(Writer writer, FacesContext faces, List exceptionList) throws IOException
 197  
     {
 198  0
         init(faces);
 199  0
         Date now = new Date();
 200  0
         for (int i = 0; i < ERROR_PARTS.length; i++)
 201  
         {
 202  0
             if ("message".equals(ERROR_PARTS[i]))
 203  
             {
 204  0
                 for (int j = 0; j < exceptionList.size(); j++)
 205  
                 {
 206  0
                     Exception e = (Exception) exceptionList.get(j);
 207  0
                     String msg = e.getMessage();
 208  0
                     if (msg != null)
 209  
                     {
 210  0
                         writer.write(msg.replaceAll("<", TS));
 211  
                     }
 212  
                     else 
 213  
                     {
 214  0
                         writer.write(e.getClass().getName());
 215  
                     }
 216  0
                     if (!(j+1==exceptionList.size()))
 217  
                     {
 218  0
                         writer.write("<br>");
 219  
                     }
 220  
                 }
 221  
             }
 222  0
             else if ("trace".equals(ERROR_PARTS[i]))
 223  
             {
 224  0
                 for (int j = 0; j < exceptionList.size(); j++)
 225  
                 {
 226  0
                     Exception e = (Exception) exceptionList.get(j);
 227  0
                     writeException(writer, e);
 228  
                 }
 229  
             }
 230  0
             else if ("now".equals(ERROR_PARTS[i]))
 231  
             {
 232  0
                 writer.write(DateFormat.getDateTimeInstance().format(now));
 233  
             }
 234  0
             else if ("tree".equals(ERROR_PARTS[i]))
 235  
             {
 236  0
                 if (faces.getViewRoot() != null)
 237  
                 {
 238  0
                     List highlightId = null;
 239  0
                     for (int j = 0; j < exceptionList.size(); j++)
 240  
                     {
 241  0
                         Exception e = (Exception) exceptionList.get(j);
 242  0
                         if (highlightId == null)
 243  
                         {
 244  0
                             highlightId = getErrorId(e);
 245  
                         }
 246  
                         else
 247  
                         {
 248  0
                             highlightId.addAll(getErrorId(e));
 249  
                         }
 250  
                     }
 251  0
                     writeComponent(writer, faces.getViewRoot(), highlightId);
 252  0
                 }
 253  
             }
 254  0
             else if ("vars".equals(ERROR_PARTS[i]))
 255  
             {
 256  0
                 writeVariables(writer, faces);
 257  
             }
 258  0
             else if ("cause".equals(ERROR_PARTS[i]))
 259  
             {
 260  0
                 for (int j = 0; j < exceptionList.size(); j++)
 261  
                 {
 262  0
                     Exception e = (Exception) exceptionList.get(j);
 263  0
                     writeCause(writer, e);
 264  0
                     if (!(j+1==exceptionList.size()))
 265  
                     {
 266  0
                         writer.write("<br>");
 267  
                     }
 268  
                 }
 269  
             }
 270  
             else
 271  
             {
 272  0
                 writer.write(ERROR_PARTS[i]);
 273  
             }
 274  
         }
 275  0
     }    
 276  
 
 277  
     private static void writeException(Writer writer, Throwable e) throws IOException {
 278  0
         StringWriter str = new StringWriter(256);
 279  0
         PrintWriter pstr = new PrintWriter(str);
 280  0
         e.printStackTrace(pstr);
 281  0
         pstr.close();
 282  0
         writer.write(str.toString().replaceAll("<", TS));
 283  0
     }
 284  
 
 285  
     public static void debugHtml(Writer writer, FacesContext faces) throws IOException {
 286  0
         init(faces);
 287  0
         Date now = new Date();
 288  0
         for (int i = 0; i < DEBUG_PARTS.length; i++) {
 289  0
             if ("message".equals(DEBUG_PARTS[i])) {
 290  0
                 writer.write(faces.getViewRoot().getViewId());
 291  0
             } else if ("now".equals(DEBUG_PARTS[i])) {
 292  0
                 writer.write(DateFormat.getDateTimeInstance().format(now));
 293  0
             } else if ("tree".equals(DEBUG_PARTS[i])) {
 294  0
                 writeComponent(writer, faces.getViewRoot(), null);
 295  0
             } else if ("vars".equals(DEBUG_PARTS[i])) {
 296  0
                 writeVariables(writer, faces);
 297  
             } else {
 298  0
                 writer.write(DEBUG_PARTS[i]);
 299  
             }
 300  
         }
 301  0
     }
 302  
 
 303  
     private static void writeVariables(Writer writer, FacesContext faces) throws IOException {
 304  0
         ExternalContext ctx = faces.getExternalContext();
 305  0
         writeVariables(writer, ctx.getRequestParameterMap(), "Request Parameters");
 306  0
         writeVariables(writer, ctx.getRequestMap(), "Request Attributes");
 307  0
         if (ctx.getSession(false) != null) {
 308  0
             writeVariables(writer, ctx.getSessionMap(), "Session Attributes");
 309  
         }
 310  0
         writeVariables(writer, ctx.getApplicationMap(), "Application Attributes");
 311  0
     }
 312  
 
 313  
     private static void writeVariables(Writer writer, Map vars, String caption) throws IOException {
 314  0
         writer.write("<table><caption>");
 315  0
         writer.write(caption);
 316  0
         writer.write("</caption><thead><tr><th style=\"width: 10%; \">Name</th><th style=\"width: 90%; \">Value</th></tr></thead><tbody>");
 317  0
         boolean written = false;
 318  0
         if (!vars.isEmpty()) {
 319  0
             SortedMap map = new TreeMap(vars);
 320  0
             Map.Entry entry = null;
 321  0
             String key = null;
 322  0
             for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); ) {
 323  0
                 entry = (Map.Entry) itr.next();
 324  0
                 key = entry.getKey().toString();
 325  0
                 if (key.indexOf('.') == -1) {
 326  0
                     writer.write("<tr><td>");
 327  0
                     writer.write(key.replaceAll("<", TS));
 328  0
                     writer.write("</td><td>");
 329  0
                     writer.write(entry.getValue().toString().replaceAll("<", TS));
 330  0
                     writer.write("</td></tr>");
 331  0
                     written = true;
 332  
                 }
 333  
             }
 334  
         }
 335  0
         if (!written) {
 336  0
             writer.write("<tr><td colspan=\"2\"><em>None</em></td></tr>");
 337  
         }
 338  0
         writer.write("</tbody></table>");
 339  0
     }
 340  
 
 341  
     private static void writeComponent(Writer writer, UIComponent c, List highlightId) throws IOException {
 342  0
         writer.write("<dl><dt");
 343  0
         if (isText(c)) {
 344  0
             writer.write(" class=\"uicText\"");
 345  
         }
 346  0
         if (highlightId != null){
 347  0
             if ((highlightId.size() > 0) && (highlightId.get(0).equals(c.getId()))){
 348  0
                 highlightId.remove(0);
 349  0
                 if (highlightId.size()==0){
 350  0
                     writer.write(" class=\"highlightComponent\"");
 351  
                 }
 352  
             }
 353  
         }
 354  0
         writer.write(">");
 355  
 
 356  0
         boolean hasChildren = c.getChildCount() > 0 || c.getFacets().size() > 0;
 357  
 
 358  0
         writeStart(writer, c, hasChildren);
 359  0
         writer.write("</dt>");
 360  0
         if (hasChildren) {
 361  0
             if (c.getFacets().size() > 0) {
 362  
                 Map.Entry entry;
 363  0
                 for (Iterator itr = c.getFacets().entrySet().iterator(); itr.hasNext(); ) {
 364  0
                     entry = (Map.Entry) itr.next();
 365  0
                     writer.write("<dd class=\"uicFacet\">");
 366  0
                     writer.write("<span>");
 367  0
                     writer.write((String) entry.getKey());
 368  0
                     writer.write("</span>");
 369  0
                     writeComponent(writer, (UIComponent) entry.getValue(), highlightId);
 370  0
                     writer.write("</dd>");
 371  
                 }
 372  
             }
 373  0
             if (c.getChildCount() > 0) {
 374  0
                 for (Iterator itr = c.getChildren().iterator(); itr.hasNext(); ) {
 375  0
                     writer.write("<dd>");
 376  0
                     writeComponent(writer, (UIComponent) itr.next(), highlightId);
 377  0
                     writer.write("</dd>");
 378  
                 }
 379  
             }
 380  0
             writer.write("<dt>");
 381  0
             writeEnd(writer, c);
 382  0
             writer.write("</dt>");
 383  
         }
 384  0
         writer.write("</dl>");
 385  0
     }
 386  
 
 387  
     private static void writeEnd(Writer writer, UIComponent c) throws IOException {
 388  0
         if (!isText(c)) {
 389  0
             writer.write(TS);
 390  0
             writer.write('/');
 391  0
             writer.write(getName(c));
 392  0
             writer.write('>');
 393  
         }
 394  0
     }
 395  
 
 396  0
     private final static String[] IGNORE = new String[] { "parent", "rendererType" };
 397  
 
 398  
     private static void writeAttributes(Writer writer, UIComponent c) {
 399  
         try {
 400  0
             BeanInfo info = Introspector.getBeanInfo(c.getClass());
 401  0
             PropertyDescriptor[] pd = info.getPropertyDescriptors();
 402  0
             Method m = null;
 403  0
             Object v = null;
 404  0
             String str = null;
 405  0
             for (int i = 0; i < pd.length; i++) {
 406  0
                 if (pd[i].getWriteMethod() != null && Arrays.binarySearch(IGNORE, pd[i].getName()) < 0) {
 407  0
                     m = pd[i].getReadMethod();
 408  
                     try {
 409  0
                         v = m.invoke(c, null);
 410  0
                         if (v != null) {
 411  0
                             if (v instanceof Collection || v instanceof Map || v instanceof Iterator) {
 412  0
                                 continue;
 413  
                             }
 414  0
                             writer.write(" ");
 415  0
                             writer.write(pd[i].getName());
 416  0
                             writer.write("=\"");
 417  0
                             if (v instanceof Expression) {
 418  0
                                 str = ((Expression) v).getExpressionString();
 419  
                             }
 420  0
                             else if (v instanceof ValueBinding)
 421  
                             {
 422  0
                                 str = ((ValueBinding) v).getExpressionString();
 423  
                             }
 424  0
                             else if (v instanceof MethodBinding)
 425  
                             {
 426  0
                                 str = ((MethodBinding) v).getExpressionString();
 427  
                             }
 428  
                             else
 429  
                             {
 430  0
                                 str = v.toString();
 431  
                             }
 432  0
                             writer.write(str.replaceAll("<", TS));
 433  0
                             writer.write("\"");
 434  
                         }
 435  0
                     } catch (Exception e) {
 436  
                         // do nothing
 437  0
                     }
 438  
                 }
 439  
             }
 440  
 
 441  0
             ValueExpression binding = c.getValueExpression("binding");
 442  0
             if (binding != null) {
 443  0
                 writer.write(" binding=\"");
 444  0
                 writer.write(binding.getExpressionString().replaceAll("<", TS));
 445  0
                 writer.write("\"");
 446  
             }
 447  0
         } catch (Exception e) {
 448  
             // do nothing
 449  0
         }
 450  0
     }
 451  
 
 452  
     private static void writeStart(Writer writer, UIComponent c, boolean children) throws IOException {
 453  0
         if (isText(c)) {
 454  0
             String str = c.toString().trim();
 455  0
             writer.write(str.replaceAll("<", TS));
 456  0
         } else {
 457  0
             writer.write(TS);
 458  0
             writer.write(getName(c));
 459  0
             writeAttributes(writer, c);
 460  0
             if (children) {
 461  0
                 writer.write('>');
 462  
             } else {
 463  0
                 writer.write("/>");
 464  
             }
 465  
         }
 466  0
     }
 467  
 
 468  
     private static String getName(UIComponent c) {
 469  0
         String nm = c.getClass().getName();
 470  0
         return nm.substring(nm.lastIndexOf('.') + 1);
 471  
     }
 472  
 
 473  
     private static boolean isText(UIComponent c) {
 474  0
         return (c.getClass().getName().startsWith("com.sun.facelets.compiler"));
 475  
     }
 476  
 
 477  
     public static void handleException(FacesContext facesContext, Exception ex) throws ServletException, IOException
 478  
     {
 479  0
         handleThrowable(facesContext, ex);
 480  0
     }
 481  
     
 482  
     public static void handleThrowable(FacesContext facesContext, Throwable ex) throws ServletException, IOException {
 483  
 
 484  0
         prepareExceptionStack(ex);
 485  
 
 486  0
         Object response = facesContext.getExternalContext().getResponse();
 487  0
         if(response instanceof HttpServletResponse) {
 488  0
             HttpServletResponse httpResp = (HttpServletResponse) response;
 489  0
             if (!httpResp.isCommitted()) {
 490  0
                 httpResp.reset();
 491  0
                 httpResp.setContentType("text/html; charset=UTF-8");
 492  0
                 Writer writer = httpResp.getWriter();
 493  
 
 494  0
                 debugHtml(writer, facesContext, ex);
 495  
 
 496  0
                 log.error("An exception occurred", ex);
 497  0
             }
 498  
             else {
 499  0
                 throwException(ex);
 500  
             }
 501  0
         }
 502  
         else {
 503  0
             throwException(ex);
 504  
         }
 505  0
     }
 506  
     
 507  
     public static void handleExceptionList(FacesContext facesContext, List exceptionList) throws ServletException, IOException
 508  
     {
 509  0
         for (int i = 0; i < exceptionList.size(); i++)
 510  
         {
 511  0
             prepareExceptionStack( (Exception) exceptionList.get(i));
 512  
         }
 513  
 
 514  0
         Object response = facesContext.getExternalContext().getResponse();
 515  0
         if(response instanceof HttpServletResponse)
 516  
         {
 517  0
             HttpServletResponse httpResp = (HttpServletResponse) response;
 518  0
             if (!httpResp.isCommitted())
 519  
             {
 520  0
                 httpResp.reset();
 521  0
                 httpResp.setContentType("text/html; charset=UTF-8");
 522  0
                 Writer writer = httpResp.getWriter();
 523  
 
 524  0
                 debugHtml(writer, facesContext, exceptionList);
 525  
 
 526  0
                 for (int i = 0; i < exceptionList.size(); i++)
 527  
                 {
 528  0
                     log.error("An exception occurred", (Exception) exceptionList.get(i));
 529  
                 }
 530  0
             }
 531  
             else
 532  
             {
 533  0
                 throwException((Exception)exceptionList.get(0));
 534  
             }
 535  0
         }
 536  
         else
 537  
         {
 538  0
             throwException((Exception)exceptionList.get(0));
 539  
         }
 540  0
     }
 541  
 
 542  
     private static void prepareExceptionStack(Throwable ex) {
 543  
 
 544  0
         if(ex==null)
 545  0
             return;
 546  
 
 547  
         //check for getRootCause and getCause-methods
 548  0
         if(!initCausePerReflection(ex,"getRootCause")) {
 549  0
            initCausePerReflection(ex,"getCause");
 550  
         }
 551  
 
 552  0
         prepareExceptionStack(ex.getCause());
 553  0
     }
 554  
 
 555  
     private static boolean initCausePerReflection(Throwable ex, String methodName) {
 556  
         try {
 557  0
             Method causeGetter = ex.getClass().getMethod(methodName,new Class[]{});
 558  0
             Throwable rootCause = (Throwable) causeGetter.invoke(ex,new Class[]{});
 559  0
             return initCauseIfAvailable(ex,rootCause);
 560  0
         } catch (Exception e1) {
 561  0
             return false;
 562  
         }
 563  
     }
 564  
 
 565  
     static void throwException(Throwable e) throws IOException, ServletException {
 566  
 
 567  0
         prepareExceptionStack(e);
 568  
 
 569  0
         if (e instanceof IOException)
 570  
         {
 571  0
             throw (IOException)e;
 572  
         }
 573  0
         else if (e instanceof ServletException)
 574  
         {
 575  0
             throw (ServletException)e;
 576  
         }
 577  
         else
 578  
         {
 579  
             ServletException ex;
 580  
 
 581  0
             if (e.getMessage() != null) {
 582  0
                 ex=new ServletException(e.getMessage(), e);
 583  
             }
 584  
             else {
 585  0
                 ex=new ServletException(e);
 586  
             }
 587  
 
 588  0
             initCauseIfAvailable(ex, e);
 589  
 
 590  0
             throw ex;
 591  
         }
 592  
     }
 593  
 
 594  
     private static boolean initCauseIfAvailable(Throwable th, Throwable cause) {
 595  
 
 596  0
         if(cause == null)
 597  0
             return false;
 598  
 
 599  
         try {
 600  0
             Method m = Throwable.class.getMethod("initCause",new Class[]{Throwable.class});
 601  0
             m.invoke(th,new Object[]{cause});
 602  0
             return true;
 603  
         }
 604  0
         catch(Exception e) {
 605  0
             return false;
 606  
         }
 607  
     }
 608  
 }
 609