1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.orchestra.viewController.spring;
21
22 import org.apache.myfaces.orchestra.conversation.Conversation;
23 import org.apache.myfaces.orchestra.conversation.ConversationContext;
24 import org.apache.myfaces.orchestra.conversation.spring.AbstractSpringOrchestraScope;
25 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
26 import org.apache.myfaces.orchestra.lib.OrchestraException;
27 import org.apache.myfaces.orchestra.viewController.DefaultViewControllerManager;
28 import org.apache.myfaces.orchestra.viewController.ViewControllerManager;
29 import org.springframework.aop.scope.ScopedProxyFactoryBean;
30 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
31 import org.springframework.beans.factory.config.BeanDefinition;
32 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
33 import org.springframework.beans.factory.config.Scope;
34 import org.springframework.context.ConfigurableApplicationContext;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class SpringViewControllerScope extends AbstractSpringOrchestraScope
65 {
66 private final static ViewControllerManager DEFAULT_VCM = new DefaultViewControllerManager();
67
68 public SpringViewControllerScope()
69 {
70 }
71
72 public Conversation createConversation(ConversationContext context, String conversationName)
73 {
74 throw new IllegalStateException(
75 "The viewController scope is not supposed to start a conversation on its own. " +
76 "Conversation to start: " + conversationName);
77 }
78
79 protected void assertSameScope(String beanName, Conversation conversation)
80 {
81
82
83 }
84
85
86
87
88
89
90
91
92 public String getConversationNameForBean(String beanName)
93 {
94 ViewControllerManager viewControllerManager = getViewControllerManager();
95 String viewId = FrameworkAdapter.getCurrentInstance().getCurrentViewId();
96 String viewControllerName = viewControllerManager.getViewControllerName(viewId);
97 if (viewControllerName == null)
98 {
99
100
101
102 throw new OrchestraException(
103 "Error while processing bean " + beanName
104 + ": no view controller name found for view " + viewId);
105 }
106
107
108 ConfigurableApplicationContext appContext = getApplicationContext();
109 ConfigurableListableBeanFactory beanFactory = appContext.getBeanFactory();
110 BeanDefinition beanDefinition = beanFactory.getBeanDefinition(viewControllerName);
111
112 if (beanDefinition.getBeanClassName().equals(ScopedProxyFactoryBean.class.getName()))
113 {
114
115
116
117
118
119
120
121 beanDefinition = beanFactory.getBeanDefinition("scopedTarget." + viewControllerName);
122 }
123
124 String scopeName = beanDefinition.getScope();
125
126 if (scopeName == null)
127 {
128
129 throw new OrchestraException(
130 "Error while processing bean " + beanName
131 + ": view controller " + viewControllerName + " has no scope.");
132 }
133
134 Scope registeredScope = beanFactory.getRegisteredScope(scopeName);
135 if (registeredScope == null)
136 {
137 throw new OrchestraException(
138 "Error while processing bean " + beanName
139 + ": view controller " + viewControllerName
140 + " has unknown scope " + scopeName);
141 }
142
143 if (registeredScope instanceof AbstractSpringOrchestraScope)
144 {
145 return ((AbstractSpringOrchestraScope) registeredScope).getConversationNameForBean(viewControllerName);
146 }
147
148 throw new OrchestraException(
149 "Error while processing bean " + beanName
150 + ": the scope " + scopeName
151 + " should be of type AbstractSpringOrchestraScope"
152 + ", but is type " + registeredScope.getClass().getName());
153 }
154
155
156
157
158
159
160
161 private ViewControllerManager getViewControllerManager()
162 {
163 try
164 {
165 return (ViewControllerManager)
166 getApplicationContext().getBean(
167 ViewControllerManager.VIEW_CONTROLLER_MANAGER_NAME,
168 ViewControllerManager.class);
169 }
170 catch(NoSuchBeanDefinitionException e)
171 {
172 return DEFAULT_VCM;
173 }
174 }
175 }