View Javadoc

    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
    20  package org.apache.myfaces.orchestra.conversation.jsf;
    21
    22  import java.io.PrintWriter;
    23  import java.io.StringWriter;
    24
    25  import javax.faces.application.FacesMessage;
    26  import javax.faces.context.FacesContext;
    27
    28  import org.apache.myfaces.orchestra.conversation.ConversationMessager;
    29
    30  /**
    31   * Inform the user about some anomalies by displaying the problems as standard JSF messages.
    32   * <p>
    33   * This class just provides some default behaviour; if you don't like it then implement your
    34   * own version of this class and configure your alternative as the standard "Conversation Messager"
    35   * instead. See the documentation on interface ConversationMessager for details.
    36   * <p>
    37   * As required by the ConversationMessager interface, all methods on this class are thread-safe.
    38   */
    39  public class JsfConversationMessager extends ConversationMessager
    40  {
    41      public void setConversationException(Throwable t)
    42      {
    43          String msgSummary = t.getLocalizedMessage();
    44          String msgDetail = getThrowableText(t);
    45          FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_FATAL, msgSummary, msgDetail);
    46          FacesContext.getCurrentInstance().addMessage(null, msg);
    47      }
    48
    49      public void setConversationNotActive(String name)
    50      {
    51          String msgSummary = "Conversation not active";
    52          String msgDetail = "Conversation not active. Please start over. (Conversation Name:" + name + ")";
    53          FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_FATAL, msgSummary, msgDetail);
    54          FacesContext.getCurrentInstance().addMessage(null, msg);
    55      }
    56
    57      protected String getThrowableText(Throwable t)
    58      {
    59          StringWriter sw = new StringWriter();
    60          PrintWriter pw = new PrintWriter(sw);
    61          t.printStackTrace(pw);
    62          pw.close();
    63          return sw.toString();
    64      }
    65  }