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 org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /***
27   * Manager to deal with page scoped beans.
28   * <p>
29   * Instances of this type are expected to be request-scoped, ie a new instance is used for
30   * each request. The FlashScopeManagerConfiguration object that it references can be
31   * of application scope.
32   */
33  public final class FlashScopeManager
34  {
35  	private FlashScopeManagerConfiguration flashScopeManagerConfiguration;
36  
37  	private Set accessedConversations = new HashSet();
38  	private boolean ignoreRequest;
39  
40  	public static FlashScopeManager getInstance()
41  	{
42  		// Get the instance by looking up a variable whose name is this class name, using the normal
43  		// managed bean lookup process. When an IOC framework like Spring is being used to extend
44  		// the standard JSF managed bean declaration facilities, then the bean may be retrieved
45  		// from there.
46  		//
47  		// Using a lookup of a managed bean allows the user to set configuration properties on the
48  		// manager class and its properties.
49  		FlashScopeManager manager = (FlashScopeManager) FrameworkAdapter.getCurrentInstance().getBean(FlashScopeManager.class.getName());
50  		if (manager == null)
51  		{
52  			// TODO: Make this error message less spring-specific. Spring is not the only IOC container
53  			// that Orchestra can be used with.
54  			throw new IllegalArgumentException("no FlashScopeManager found. Propably you forgot to add <import resource=\"classpath*:/META-INF/spring-orchestra-init.xml\" /> to your spring configuration.");
55  		}
56  
57  		return manager;
58  	}
59  
60  	public FlashScopeManagerConfiguration getFlashScopeManagerConfiguration()
61  	{
62  		return flashScopeManagerConfiguration;
63  	}
64  
65  	public void setFlashScopeManagerConfiguration(FlashScopeManagerConfiguration flashScopeManagerConfiguration)
66  	{
67  		this.flashScopeManagerConfiguration = flashScopeManagerConfiguration;
68  	}
69  
70  	/***
71  	 * Add a conversation to the list of accessed conversations.
72  	 * <p>
73  	 * Notice: this method is final for performance reasons.
74  	 * <p>
75  	 * This method is expected to be called via AOP proxies wrapped around each conversation-scoped
76  	 * bean; any invocation of a method on such a bean causes the conversation associated with that
77  	 * bean to be added to the accessed list here.
78  	 */
79  	public final void addConversationAccess(String conversationName)
80  	{
81  		// Don't bother tracking accessed conversations if we will never use the data.
82  		// Otherwise, add this conversation name to the list of accessed conversations.
83  		if (!ignoreRequest && !accessedConversations.contains(conversationName))
84  		{
85  			accessedConversations.add(conversationName);
86  		}
87  	}
88  
89  	public boolean isIgnoreRequest()
90  	{
91  		return ignoreRequest;
92  	}
93  
94  	/***
95  	 * Suppress flash scope for the current request, ie do not terminate conversations that are
96  	 * not accessed by this request.
97  	 * <p>
98  	 * This can come in useful occasionally, particularly when handling AJAX requests which
99  	 * only access some of the beans associated with the current view.
100 	 */
101 	public void setIgnoreRequest()
102 	{
103 		this.ignoreRequest = true;
104 	}
105 
106 	public boolean isConversationAccessed(String name)
107 	{
108 		if (ignoreRequest)
109 		{
110 			throw new IllegalStateException();
111 		}
112 
113 		return accessedConversations.contains(name);
114 	}
115 }