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  package org.apache.myfaces.orchestra.conversation;
    20
    21  import java.io.Serializable;
    22
    23  /**
    24   * Provide information about the current conversation.
    25   * <p>
    26   * An instance of this type is stored in a thread-local variable to indicate
    27   * what the "current conversation state" is. The getConversation() method
    28   * can therefore be used to determine what conversation is currently active,
    29   * and getBeanName() can be used to determine what the most recently-invoked
    30   * conversation-scoped-bean was. This thread-local variable is maintained
    31   * via the CurrentConversationAdvice which wraps every conversation-scoped
    32   * bean and intercepts all method calls to the bean.
    33   * <p>
    34   * This object also records the fact that a specific bean is within a
    35   * specific conversation. This data is saved during serialization so
    36   * that on deserialize we know which conversation to reattach which
    37   * bean to./
    38   */
    39  public class CurrentConversationInfo implements Serializable
    40  {
    41      private final String conversationName;
    42      private final String beanName;
    43
    44      /**
    45       * The conversation object itself is not serializable (it is a Spring proxy
    46       * object with various AOP advices attached to it). Therefore this reference
    47       * must be transient. After deserialisation, this member is of course null,
    48       * but is recalculated on demand in the getConversation method.
    49       * <p>
    50       * The beans that are <i>in</i> the conversation are hopefully serializable;
    51       * they are saved directly and then reattached to the new Conversation instance.
    52       */
    53      private transient Conversation conversation;
    54
    55      public CurrentConversationInfo(Conversation conversation, String beanName)
    56      {
    57          this.conversation = conversation;
    58          this.conversationName = conversation.getName();
    59          this.beanName = beanName;
    60      }
    61
    62      /**
    63       * The conversation the bean is associated with.
    64       */
    65      public Conversation getConversation()
    66      {
    67          if(conversation == null)
    68          {
    69              ConversationManager conversationManager = ConversationManager.getInstance();
    70              conversation = conversationManager.getConversation(conversationName);
    71          }
    72          return conversation;
    73      }
    74
    75      /**
    76       * The bean name.
    77       */
    78      public String getBeanName()
    79      {
    80          return beanName;
    81      }
    82  }