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.spring;
21  
22  import org.apache.myfaces.orchestra.conversation.Conversation;
23  import org.apache.myfaces.orchestra.conversation.ConversationAccessLifetimeAspect;
24  import org.apache.myfaces.orchestra.conversation.ConversationAspect;
25  import org.apache.myfaces.orchestra.conversation.ConversationContext;
26  import org.apache.myfaces.orchestra.conversation.ConversationTimeoutableAspect;
27  
28  /**
29   * Handles creation and lookup of any bean whose bean-definition specifies a scope
30   * that maps to an instance of this type.
31   * <p>
32   * A scope bean handles Spring-specific callbacks in order to locate or create any beans whose definition
33   * specifies that scope. A scope can also be thought of as a "conversation template", as this object
34   * is responsible for creating a conversation when one is needed but does not yet exist.
35   * <p>
36   * <h2>Example</h2>
37   * A sample configuration for a conversation scope with persistence:
38   * <pre>
39   * &lt;bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"&gt;
40   *   &lt;property name="scopes"&gt;
41   *     &lt;map&gt;
42   *       &lt;entry key="conversation.manual"&gt;
43   *         &lt;bean class="org.apache.myfaces.orchestra.conversation.spring.SpringConversationScope"&gt;
44   *           &lt;property name="advices"&gt;
45   *             &lt;list&gt;
46   *               &lt;ref bean="persistentContextConversationInterceptor" /&gt;
47   *             &lt;/list&gt;
48   *           &lt;/property&gt;
49   *         &lt;/bean&gt;
50   *       &lt;/entry&gt;
51   *     &lt;/map&gt;
52   *   &lt;/property&gt;
53   * &lt;/bean&gt;
54   * 
55   * 
56   * &lt;bean id="persistentContextConversationInterceptor" class="org.apache.myfaces.orchestra.conversation.spring.PersistenceContextConversationInterceptor"&gt;
57   *   &lt;property name="persistenceContextFactory" ref="yourPersistentContextFactory" /&gt;
58   * &lt;/bean&gt;
59   * </pre>
60   * <p>
61   *
62   * <h2>Conversation properties</h2>
63   * The following properties can be defined on a scope and then apply to any conversation that is created
64   * to hold a bean of this scope:
65   * <ul>
66   * <li>lifetime: may be "manual" or "access". If not specified, then defaults to "manual".</li>
67   * <li>timeout: a time period (in minutes) after which inactive conversations are terminated.
68   * If not specified, then inactive conversations are never automatically terminated.</li>
69   * </ul>
70   *
71   * <h2>Other Notes</h2>
72   * If the bean definition does not specify a conversation name, then the bean name is used
73   * as the conversation name.
74   * <p>
75   * As shown above, a list of AOP Advices can be specified for the scope, in which case each of the
76   * advices gets configured for any bean declared with this scope.
77   */
78  public class SpringConversationScope extends AbstractSpringOrchestraScope
79  {
80      /** @deprecated Use LIFETIME_ACCESS instead. */
81      public static final String LIFETIME_FLASH = "flash";
82  
83      public static final String LIFETIME_ACCESS = "access";
84      public static final String LIFETIME_MANUAL = "manual";
85  
86      private Integer timeout;
87      private String lifetime = LIFETIME_MANUAL;
88  
89      /**
90       * See {@link #setTimeout}.
91       */
92      public Integer getTimeout()
93      {
94          return timeout;
95      }
96  
97      /**
98       * The timeout in minutes when the conversation will end.
99       * See {@link ConversationTimeoutableAspect#timeout} for the default timeout.
100      */
101     public void setTimeout(Integer timeout)
102     {
103         this.timeout = timeout;
104     }
105 
106     /**
107      * See {@link #setLifetime}.
108      */
109     public String getLifetime()
110     {
111         return lifetime;
112     }
113 
114     /**
115      * Must be one of "manual" or "access".
116      * <p>
117      * Defaults to "manual".
118      * <p>
119      * Note that "flash" is also supported as an alias for "access", for
120      * reasons of backwards compatibility with release 1.0.
121      */
122     public void setLifetime(String lifetime)
123     {
124         // Check for validity here so that an exception gets thrown on startup
125         // rather than when the first bean in this scope is created.
126         if (LIFETIME_FLASH.equals(lifetime))
127         {
128             this.lifetime = LIFETIME_ACCESS;
129         }
130         else if (LIFETIME_ACCESS.equals(lifetime))
131         {
132             this.lifetime = LIFETIME_ACCESS;
133         }
134         else if (LIFETIME_MANUAL.equals(lifetime))
135         {
136             this.lifetime = LIFETIME_MANUAL;
137         }
138         else
139         {
140             throw new IllegalArgumentException("Invalid lifetime:" + lifetime);
141         }
142     }
143 
144     /**
145      * Implementation of ConversationFactory interface.
146      */
147     public Conversation createConversation(ConversationContext context, String name)
148     {
149         Conversation conversation = new Conversation(context, name, this);
150 
151         // invoke child scope classes so they can add any aspects they desire.
152         initAspects(conversation);
153 
154         return conversation;
155     }
156 
157     /**
158      * Add aspects to a newly-created conversation.
159      * <p>
160      * Subclasses are expected to call super.initAspects, then make
161      * zero or more calls to conversation.addAspect.
162      */
163     protected void initAspects(Conversation conversation)
164     {
165         // conversation timeout
166         if (timeout != null)
167         {
168             long timeoutMsecs = timeout.longValue() * 60L * 1000L;
169             ConversationTimeoutableAspect aspect = new ConversationTimeoutableAspect(conversation);
170             aspect.setTimeout(timeoutMsecs);
171             conversation.addAspect(aspect);
172         }
173 
174         // conversation lifetime
175         if (LIFETIME_ACCESS.equals(lifetime))
176         {
177             ConversationAspect aspect = new ConversationAccessLifetimeAspect(conversation);
178             conversation.addAspect(aspect);
179         }
180     }
181 
182     /**
183      * Mark the specified conversation as having been accessed.
184      * <p>
185      * This affects conversation timeouts, and the removal of access-scoped conversations.
186      */
187     protected void notifyAccessConversation(Conversation conversation)
188     {
189         super.notifyAccessConversation(conversation);
190 
191         ConversationAccessLifetimeAspect aspect = (ConversationAccessLifetimeAspect) conversation.getAspect(ConversationAccessLifetimeAspect.class);
192         if (aspect != null)
193         {
194             aspect.markAsAccessed();
195         }
196     }
197 }