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
67 private final static ViewControllerManager DEFAULT_VCM = new DefaultViewControllerManager();
68
69 public SpringViewControllerScope()
70 {
71 }
72
73 public Conversation createConversation(ConversationContext context, String conversationName)
74 {
75 throw new IllegalStateException(
76 "The viewController scope is not supposed to start a conversation on its own. " +
77 "Conversation to start: " + conversationName);
78 }
79
80 protected void assertSameScope(String beanName, Conversation conversation)
81 {
82
83
84 }
85
86
87
88
89
90
91
92
93 public String getConversationNameForBean(String beanName)
94 {
95 ViewControllerManager viewControllerManager = getViewControllerManager();
96 String viewId = FrameworkAdapter.getCurrentInstance().getCurrentViewId();
97 String viewControllerName = viewControllerManager.getViewControllerName(viewId);
98 if (viewControllerName == null)
99 {
100
101
102
103 throw new OrchestraException(
104 "Error while processing bean " + beanName
105 + ": no view controller name found for view " + viewId);
106 }
107
108
109 ConfigurableApplicationContext appContext = getApplicationContext();
110 ConfigurableListableBeanFactory beanFactory = appContext.getBeanFactory();
111 BeanDefinition beanDefinition = beanFactory.getBeanDefinition(viewControllerName);
112
113 if (beanDefinition.getBeanClassName().equals(ScopedProxyFactoryBean.class.getName()))
114 {
115
116
117
118
119
120
121
122 beanDefinition = beanFactory.getBeanDefinition("scopedTarget." + viewControllerName);
123 }
124
125 String scopeName = beanDefinition.getScope();
126
127 if (scopeName == null)
128 {
129
130 throw new OrchestraException(
131 "Error while processing bean " + beanName
132 + ": view controller " + viewControllerName + " has no scope.");
133 }
134
135 Scope registeredScope = beanFactory.getRegisteredScope(scopeName);
136 if (registeredScope == null)
137 {
138 throw new OrchestraException(
139 "Error while processing bean " + beanName
140 + ": view controller " + viewControllerName
141 + " has unknown scope " + scopeName);
142 }
143
144 if (registeredScope instanceof AbstractSpringOrchestraScope)
145 {
146 return ((AbstractSpringOrchestraScope) registeredScope).getConversationNameForBean(viewControllerName);
147 }
148
149 throw new OrchestraException(
150 "Error while processing bean " + beanName
151 + ": the scope " + scopeName
152 + " should be of type AbstractSpringOrchestraScope"
153 + ", but is type " + registeredScope.getClass().getName());
154 }
155
156
157
158
159
160
161
162 private ViewControllerManager getViewControllerManager()
163 {
164 try
165 {
166 return (ViewControllerManager)
167 getApplicationContext().getBean(
168 ViewControllerManager.VIEW_CONTROLLER_MANAGER_NAME,
169 ViewControllerManager.class);
170 }
171 catch(NoSuchBeanDefinitionException e)
172 {
173 return DEFAULT_VCM;
174 }
175 }
176 }