KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > executor > jsf > FlowFacesUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.executor.jsf;
17
18 import javax.faces.context.FacesContext;
19
20 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.web.jsf.FacesContextUtils;
23 import org.springframework.webflow.conversation.impl.SessionBindingConversationManager;
24 import org.springframework.webflow.definition.registry.FlowDefinitionLocator;
25 import org.springframework.webflow.engine.impl.FlowExecutionImplFactory;
26 import org.springframework.webflow.engine.impl.FlowExecutionImplStateRestorer;
27 import org.springframework.webflow.execution.FlowExecutionFactory;
28 import org.springframework.webflow.execution.repository.FlowExecutionRepository;
29 import org.springframework.webflow.execution.repository.support.SimpleFlowExecutionRepository;
30
31 /**
32  * Trivial helper utility class for SWF within a JSF environment.
33  *
34  * @author Keith Donald
35  */

36 public class FlowFacesUtils {
37
38     private static final String JavaDoc REPOSITORY_BEAN_NAME = "flowExecutionRepository";
39
40     private static final String JavaDoc LOCATOR_BEAN_NAME = "flowDefinitionLocator";
41
42     private static final String JavaDoc FACTORY_BEAN_NAME = "flowExecutionFactory";
43
44     private static SimpleFlowExecutionRepository defaultRepository;
45
46     private static FlowExecutionImplFactory defaultFactory;
47
48     public static FlowDefinitionLocator getDefinitionLocator(FacesContext context) {
49         ApplicationContext ac = FacesContextUtils.getRequiredWebApplicationContext(context);
50         try {
51             return (FlowDefinitionLocator)ac.getBean(LOCATOR_BEAN_NAME, FlowDefinitionLocator.class);
52         }
53         catch (NoSuchBeanDefinitionException e) {
54             String JavaDoc message = "No bean definition with id '" + LOCATOR_BEAN_NAME
55                     + "' could be found; to use Spring Web Flow with JSF you must "
56                     + "configure your context with a FlowDefinitionLocator "
57                     + "exposing a registry of flow definitions.";
58             throw new JsfFlowConfigurationException(message, e);
59         }
60     }
61
62     public synchronized static FlowExecutionRepository getExecutionRepository(FacesContext context) {
63         ApplicationContext ac = FacesContextUtils.getRequiredWebApplicationContext(context);
64         if (ac.containsBean(REPOSITORY_BEAN_NAME)) {
65             return (FlowExecutionRepository)ac.getBean(REPOSITORY_BEAN_NAME, FlowExecutionRepository.class);
66         }
67         else {
68             if (defaultRepository == null) {
69                 defaultRepository = new SimpleFlowExecutionRepository(new FlowExecutionImplStateRestorer(
70                         getDefinitionLocator(context)), new SessionBindingConversationManager());
71             }
72             return defaultRepository;
73         }
74     }
75
76     public synchronized static FlowExecutionFactory getExecutionFactory(FacesContext context) {
77         ApplicationContext ac = FacesContextUtils.getRequiredWebApplicationContext(context);
78         if (ac.containsBean(FACTORY_BEAN_NAME)) {
79             return (FlowExecutionFactory)ac.getBean(FACTORY_BEAN_NAME, FlowExecutionFactory.class);
80         }
81         else {
82             if (defaultFactory == null) {
83                 defaultFactory = new FlowExecutionImplFactory();
84             }
85             return defaultFactory;
86         }
87     }
88 }
Popular Tags