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.lib.jsf;
21  
22  import org.apache.myfaces.orchestra.conversation.ConversationManager;
23  
24  import javax.faces.event.PhaseEvent;
25  import javax.faces.event.PhaseId;
26  import javax.faces.event.PhaseListener;
27  import java.util.Map;
28  
29  /***
30   * Ensure that Orchestra is initialised for the current user.
31   * <p>
32   * At start of request processing, if this is a new http session then initialise Orchestra
33   * for this session.
34   */
35  public class OrchestraInitializationPhaseListener implements PhaseListener
36  {
37  	private static final long serialVersionUID = 1L;
38  
39  	private final static String SYSTEM_INITIALIZED = OrchestraInitializationPhaseListener.class.getName() + ".INITIALIZED";
40  
41  	public void afterPhase(PhaseEvent event)
42  	{
43  	}
44  
45  	public void beforePhase(PhaseEvent event)
46  	{
47  		if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId()))
48  		{
49  			initialize(event);
50  		}
51  	}
52  
53  	private void initialize(PhaseEvent event)
54  	{
55  		Map sessionMap = event.getFacesContext().getExternalContext().getSessionMap();
56  		if (sessionMap != null)
57  		{
58  			if (!Boolean.TRUE.equals(sessionMap.get(SYSTEM_INITIALIZED)))
59  			{
60  				// ensure correct setup of system
61  				ConversationManager.getInstance();
62  
63  				sessionMap.put(SYSTEM_INITIALIZED, Boolean.TRUE);
64  			}
65  		}
66  	}
67  
68  	public PhaseId getPhaseId()
69  	{
70  		return PhaseId.RESTORE_VIEW;
71  	}
72  }