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;
21  
22  import org.aopalliance.intercept.MethodInterceptor;
23  import org.aopalliance.intercept.MethodInvocation;
24  
25  import java.io.Serializable;
26  
27  /**
28   * An advice which is added to all conversation scoped beans.
29   * 
30   * <p>It does the following:
31   * <ul>
32   * <li>Maintain the {@link Conversation#getCurrentInstance()}</li>
33   * <li>End the conversation if it has been invalidated</li>
34   * </ul>
35   * </p>
36   * 
37   * <p>A bean that is declared as belonging to a particular conversation is
38   * always wrapped by the IOC system in a proxy object. When any method is
39   * called on that proxy, the call is forwarded to the "invoke" method here.
40   * This class then ensures that the "current conversation" is set to the
41   * conversation configured for that target bean. The result is that the real
42   * bean can call Conversation.getCurrentInstance() and always receives a
43   * reference to the conversation object that has been configured for it.</p>
44   * 
45   * <p>In addition, on return from the target bean method, this object
46   * checks whether the conversation has been marked as invalid. If so (and
47   * the conversation is not in use by something further up the call stack)
48   * then destroy the conversation object.</p>
49   */
50  public class CurrentConversationAdvice implements MethodInterceptor, Serializable
51  {
52      private final CurrentConversationInfo conversationInfo;
53  
54      public CurrentConversationAdvice(Conversation conversation, String beanName)
55      {
56          conversationInfo = new CurrentConversationInfo(conversation, beanName);
57      }
58  
59      public Object invoke(MethodInvocation methodInvocation) throws Throwable
60      {
61          // Save the current conversation so it can be restored later.
62          // Note that for "top-level" calls, the saved value is null.
63          CurrentConversationInfo previous = Conversation.getCurrentInstanceInfo();
64  
65          // Set the appropriate conversation for the target object. 
66          Conversation.setCurrentInstance(conversationInfo);
67  
68          try
69          {
70              conversationInfo.getConversation().enterConversation();
71  
72              return methodInvocation.proceed();
73          }
74          finally
75          {
76              // Always restore the previous conversation (which may be null).
77              // Do this before anything else in case other methods throw exceptions.
78              Conversation.setCurrentInstance(previous);
79  
80              conversationInfo.getConversation().leaveConversation();
81  
82              if (conversationInfo.getConversation().isQueueInvalid())
83              {
84                  conversationInfo.getConversation().invalidate();
85              }
86          }
87      }
88  }