View Javadoc

1   /*
2    * Copyright (c) 2007, Your Corporation. All Rights Reserved.
3    */
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  package org.apache.myfaces.orchestra.frameworkAdapter.local;
24  
25  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
26  import org.springframework.context.ApplicationContextAware;
27  import org.springframework.context.ConfigurableApplicationContext;
28  import org.springframework.context.ApplicationContext;
29  import org.springframework.beans.BeansException;
30  
31  import java.io.IOException;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  /***
36   * A FrameworkAdapter which uses local maps to simulate a servlet environment.
37   * <p>
38   * This is intended when Orchestra functionality is desired outside of any real
39   * request/response type system. One example is where a job scheduler (eg quartz)
40   * is used to execute background threads that want to execute the same beans that
41   * can also be used. In this case:
42   * <ol>
43   * <li>Create a new Thread object
44   * <li>Create a new LocalFrameworkAdapter instance
45   * <li>Within the new thread, call FrameworkAdapter.setInstance.
46   * </ol>
47   * When Orchestra methods are invoked from within the new thread, calls made to the
48   * framework adapter will be handled by the created LocalFrameworkAdapter instance,
49   * and will not interact with the user session, the original request or response.
50   * Because the session is new, no conversations will be inherited from the environment
51   * in which that new thread was created.
52   * <p>
53   * This class is not expected to be used very often.
54   * <p>
55   * Note that this adapter relies on Spring and thus you have to ensure
56   * {@link #setApplicationContext} is called.
57   * <p>
58   * Note also that because this is intended for use only without a real request
59   * and response, there is no corresponding Filter class.
60   * <p>
61   * This class does not override the inherited createConversationMessager method, so
62   * it is mandatory for anyone using this class to explicitly call method
63   * setConversationMessager before using an instance of this adapter.
64   */
65  public class LocalFrameworkAdapter extends FrameworkAdapter implements ApplicationContextAware
66  {
67  	private ConfigurableApplicationContext configurableApplicationContext;
68  
69  	private final Map sessionMap = new HashMap();
70  	private final Map requestMap = new HashMap();
71  	private final Map requestParameterMap = new HashMap();
72  	private final Map initMap = new HashMap();
73  
74  	public String getInitParameter(String key)
75  	{
76  		return (String) initMap.get(key);
77  	}
78  
79  	public Object getRequestParameterAttribute(String key)
80  	{
81  		return requestParameterMap.get(key);
82  	}
83  
84  	public boolean containsRequestParameterAttribute(String key)
85  	{
86  		return requestParameterMap.containsKey(key);
87  	}
88  
89  	public void setRequestParameterAttribute(String key , Object value)
90  	{
91  		requestParameterMap.put(key, value);
92  	}
93  
94  	public Object getRequestAttribute(String key)
95  	{
96  		return requestMap.get(key);
97  	}
98  
99  	public void setRequestAttribute(String key, Object value)
100 	{
101 		requestMap.put(key, value);
102 	}
103 
104 	public void clearRequestMap()
105 	{
106 		requestMap.clear();
107 	}
108 
109 	public boolean containsRequestAttribute(String key)
110 	{
111 		return requestMap.containsKey(key);
112 	}
113 
114 	public Object getSessionAttribute(String key)
115 	{
116 		return sessionMap.get(key);
117 	}
118 
119 	public void setSessionAttribute(String key, Object value)
120 	{
121 		sessionMap.put(key, value);
122 	}
123 
124 	public boolean containsSessionAttribute(String key)
125 	{
126 		return sessionMap.containsKey(key);
127 	}
128 
129 	protected ConfigurableApplicationContext getApplicationContext()
130 	{
131 		return configurableApplicationContext;
132 	}
133 
134     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
135     {
136         setApplicationContext((ConfigurableApplicationContext) applicationContext);
137     }
138 
139     public void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext)
140 	{
141 		this.configurableApplicationContext = configurableApplicationContext;
142 	}
143 
144 	public void redirect(String url) throws IOException
145 	{
146 	}
147 
148 	public Object getBean(String name)
149 	{
150 		if (!getApplicationContext().containsBean(name))
151 		{
152 			return null;
153 		}
154 		return getApplicationContext().getBean(name);
155 	}
156 
157 	public void invokeNavigation(String navigationName)
158 	{
159 	}
160 }