Esempio n. 1
0
  private void printComponentTree(
      PrintWriter out, String errorId, FacesContext context, UIComponent comp, int depth) {
    for (int i = 0; i < depth; i++) out.print(' ');

    boolean isError = false;
    if (errorId != null && errorId.equals(comp.getClientId(context))) {
      isError = true;
      out.print("<span style='color:red'>");
    }

    out.print("&lt;" + comp.getClass().getSimpleName());
    if (comp.getId() != null) out.print(" id=\"" + comp.getId() + "\"");

    for (Method method : comp.getClass().getMethods()) {
      if (!method.getName().startsWith("get") && !method.getName().startsWith("is")) continue;
      else if (method.getParameterTypes().length != 0) continue;

      String name;

      if (method.getName().startsWith("get")) name = method.getName().substring(3);
      else if (method.getName().startsWith("is")) name = method.getName().substring(2);
      else continue;

      // XXX: getURL
      name = Character.toLowerCase(name.charAt(0)) + name.substring(1);

      ValueExpression expr = comp.getValueExpression(name);

      Class type = method.getReturnType();

      if (expr != null) {
        out.print(" " + name + "=\"" + expr.getExpressionString() + "\"");
      } else if (method.getDeclaringClass().equals(UIComponent.class)
          || method.getDeclaringClass().equals(UIComponentBase.class)) {
      } else if (name.equals("family")) {
      } else if (String.class.equals(type)) {
        try {
          Object value = method.invoke(comp);

          if (value != null) out.print(" " + name + "=\"" + value + "\"");
        } catch (Exception e) {
        }
      }
    }

    int facetCount = comp.getFacetCount();
    int childCount = comp.getChildCount();

    if (facetCount == 0 && childCount == 0) {
      out.print("/>");

      if (isError) out.print("</span>");

      out.println();
      return;
    }
    out.println(">");

    if (isError) out.print("</span>");

    for (int i = 0; i < childCount; i++) {
      printComponentTree(out, errorId, context, comp.getChildren().get(i), depth + 1);
    }

    for (int i = 0; i < depth; i++) out.print(' ');

    if (isError) out.print("<span style='color:red'>");

    out.println("&lt;/" + comp.getClass().getSimpleName() + ">");

    if (isError) out.print("</span>");
  }
Esempio n. 2
0
  private boolean sendError(FacesContext context, String lifecycle, Exception e) {
    for (Throwable cause = e; cause != null; cause = cause.getCause()) {
      if (cause instanceof DisplayableException) {
        if (e instanceof RuntimeException) throw (RuntimeException) e;
        else throw new FacesException(e);
      } else if (cause instanceof ServletException) throw new FacesException(e);
      else if (cause instanceof JspException) throw new FacesException(e);
    }

    ExternalContext extContext = context.getExternalContext();
    Object response = extContext.getResponse();

    if (!(response instanceof HttpServletResponse)) {
      context.renderResponse();

      if (e instanceof RuntimeException) throw (RuntimeException) e;
      else throw new RuntimeException(e);
    }

    log.log(Level.WARNING, e.toString(), e);

    HttpServletResponse res = (HttpServletResponse) response;

    try {
      context.renderResponse();
      context.responseComplete();

      res.setStatus(500, "JSF Exception");
      res.setContentType("text/html");

      PrintWriter out = res.getWriter();

      out.println("<body>");

      out.println("<h3>JSF exception detected in " + lifecycle + " phase</h3>");

      String msg = e.getMessage();
      out.println("<span style='color:red;font:bold'>" + Html.escapeHtml(msg) + "</span><br/>");

      out.println("<h3>Context: " + context.getViewRoot() + "</h3>");
      out.println("<code><pre>");

      String errorId = null;

      if (e instanceof FacesException && msg.startsWith("id=")) {
        int p = msg.indexOf(' ');
        errorId = msg.substring(3, p);
      }

      printComponentTree(out, errorId, context, context.getViewRoot(), 0);

      out.println("</pre></code>");

      if (!Alarm.isTest()) {
        out.println("<h3>Stack Trace</h3>");
        out.println("<pre>");
        if (e.getCause() != null) e.getCause().printStackTrace(out);
        else e.printStackTrace(out);
        out.println("</pre>");
      }

      out.println("</body>");

      // clear, so we don't just loop
      Application app = context.getApplication();

      ViewHandler view = app.getViewHandler();

      UIViewRoot viewRoot = context.getViewRoot();

      viewRoot = view.createView(context, viewRoot.getViewId());

      context.setViewRoot(viewRoot);

      // view.writeState(context); // XXX: no need to output state, but review.

      return true;
    } catch (IOException e1) {
      throw new RuntimeException(e);
    }
  }