summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/terminal/web/WebApplicationContext.java
blob: e91f300ba4f1f7d6d09017c2885a1a0ffe61d4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* *************************************************************************
 
 IT Mill Toolkit 

 Development of Browser User Interfaces Made Easy

 Copyright (C) 2000-2006 IT Mill Ltd
 
 *************************************************************************

 This product is distributed under commercial license that can be found
 from the product package on license.pdf. Use of this product might 
 require purchasing a commercial license from IT Mill Ltd. For guidelines 
 on usage, see licensing-guidelines.html

 *************************************************************************
 
 For more information, contact:
 
 IT Mill Ltd                           phone: +358 2 4802 7180
 Ruukinkatu 2-4                        fax:   +358 2 4802 7181
 20540, Turku                          email:  info@itmill.com
 Finland                               company www: www.itmill.com
 
 Primary source for information and releases: www.itmill.com

 ********************************************************************** */

package com.itmill.toolkit.terminal.web;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.WeakHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.service.ApplicationContext;
import com.itmill.toolkit.ui.Window;

/**
 * Web application context for the IT Mill Toolkit applications.
 * 
 * @author IT Mill Ltd.
 * @version
 * @VERSION@
 * @since 3.1
 */
public class WebApplicationContext implements ApplicationContext {

	private List listeners;

	private HttpSession session;

	private WeakHashMap formActions = new WeakHashMap();

	/** Create a new Web Application Context. */
	WebApplicationContext(HttpSession session) {
		this.session = session;
	}

	/**
	 * Get the form action for given window.
	 * 
	 * By default, this action is "", which preserves the current url. Commonly
	 * this is wanted to be set to <code>application.getUrl().toString()</code>
	 * or <code>window.getUrl().toString()</code> in order to clean any local
	 * links or parameters set from the action.
	 * 
	 * @param window
	 *            Window for which the action is queried
	 * @return Action to be set into Form action attribute
	 */
	public String getWindowFormAction(Window window) {
		String action = (String) formActions.get(window);
		return action == null ? "" : action;
	}

	/**
	 * Set the form action for given window.
	 * 
	 * By default, this action is "", which preserves the current url. Commonly
	 * this is wanted to be set to <code>application.getUrl().toString()</code>
	 * or <code>window.getUrl().toString()</code> in order to clean any local
	 * links or parameters set from the action.
	 * 
	 * @param window
	 *            Window for which the action is set
	 * @param action
	 *            New action for the window.
	 */
	public void setWindowFormAction(Window window, String action) {
		if (action == null || action == "")
			formActions.remove(window);
		else
			formActions.put(window, action);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.itmill.toolkit.service.ApplicationContext#getBaseDirectory()
	 */
	public File getBaseDirectory() {
		String realPath = session.getServletContext().getRealPath("/");
		if (realPath == null)
			return null;
		return new File(realPath);
	}

	/**
	 * Get the http-session application is running in.
	 * 
	 * @return HttpSession this application context resides in
	 */
	public HttpSession getHttpSession() {
		return session;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.itmill.toolkit.service.ApplicationContext#getApplications()
	 */
	public Collection getApplications() {
		LinkedList applications = (LinkedList) session
				.getAttribute(ApplicationServlet.SESSION_ATTR_APPS);

		return Collections
				.unmodifiableCollection(applications == null ? (new LinkedList())
						: applications);
	}

	/**
	 * Get application context for HttpSession.
	 * 
	 * @return application context for HttpSession.
	 */
	static public WebApplicationContext getApplicationContext(
			HttpSession session) {
		return new WebApplicationContext(session);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		return session.equals(obj);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return session.hashCode();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.itmill.toolkit.service.ApplicationContext#addTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
	 */
	public void addTransactionListener(TransactionListener listener) {
		if (this.listeners == null)
			this.listeners = new LinkedList();
		this.listeners.add(listener);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.itmill.toolkit.service.ApplicationContext#removeTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
	 */
	public void removeTransactionListener(TransactionListener listener) {
		if (this.listeners != null)
			this.listeners.remove(listener);

	}

	/** Notify transaction start */
	protected void startTransaction(Application application,
			HttpServletRequest request) {
		if (this.listeners == null)
			return;
		for (Iterator i = this.listeners.iterator(); i.hasNext();) {
			((ApplicationContext.TransactionListener) i.next())
					.transactionStart(application, request);
		}
	}

	/** Notify transaction end */
	protected void endTransaction(Application application,
			HttpServletRequest request) {
		if (this.listeners == null)
			return;

		LinkedList exceptions = null;
		for (Iterator i = this.listeners.iterator(); i.hasNext();)
			try {
				((ApplicationContext.TransactionListener) i.next())
						.transactionEnd(application, request);
			} catch (RuntimeException t) {
				if (exceptions == null)
					exceptions = new LinkedList();
				exceptions.add(t);
			}

		// If any runtime exceptions occurred, throw a combined exception
		if (exceptions != null) {
			StringBuffer msg = new StringBuffer();
			for (Iterator i = listeners.iterator(); i.hasNext();) {
				RuntimeException e = (RuntimeException) i.next();
				if (msg.length() == 0)
					msg.append("\n\n--------------------------\n\n");
				msg.append(e.getMessage() + "\n");
				StringWriter trace = new StringWriter();
				e.printStackTrace(new PrintWriter(trace,true));
				msg.append(trace.toString());
			}
			throw new RuntimeException(msg.toString());
		}
	}

}