001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.webadapter;
039:
040: import java.io.File;
041: import java.util.Collection;
042: import java.util.Collections;
043: import java.util.Iterator;
044: import java.util.LinkedList;
045: import java.util.List;
046: import java.util.WeakHashMap;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpSession;
050:
051: import org.millstone.base.Application;
052: import org.millstone.base.service.ApplicationContext;
053: import org.millstone.base.ui.Window;
054:
055: /** Web application context for Millstone applications.
056: *
057: * @author IT Mill Ltd.
058: * @version 3.1.1
059: * @since 3.1
060: */
061: public class WebApplicationContext implements ApplicationContext {
062:
063: private List listeners;
064: private HttpSession session;
065: private WeakHashMap formActions = new WeakHashMap();
066:
067: /** Create a new Web Application Context. */
068: WebApplicationContext(HttpSession session) {
069: this .session = session;
070: }
071:
072: /** Get the form action for given window.
073: *
074: * By default, this action is "", which preserves the current url. Commonly
075: * this is wanted to be set to <code>application.getUrl().toString()</code>
076: * or <code>window.getUrl().toString()</code> in order to clean any
077: * local links or parameters set from the action.
078: *
079: * @param window Window for which the action is queried
080: * @return Action to be set into Form action attribute
081: */
082: public String getWindowFormAction(Window window) {
083: String action = (String) formActions.get(window);
084: return action == null ? "" : action;
085: }
086:
087: /** Set the form action for given window.
088: *
089: * By default, this action is "", which preserves the current url. Commonly
090: * this is wanted to be set to <code>application.getUrl().toString()</code>
091: * or <code>window.getUrl().toString()</code> in order to clean any
092: * local links or parameters set from the action.
093: *
094: * @param window Window for which the action is set
095: * @param action New action for the window.
096: */
097: public void setWindowFormAction(Window window, String action) {
098: if (action == null || action == "")
099: formActions.remove(window);
100: else
101: formActions.put(window, action);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.millstone.base.service.ApplicationContext#getBaseDirectory()
106: */
107: public File getBaseDirectory() {
108: String realPath = session.getServletContext().getRealPath("/");
109: return new File(realPath);
110: }
111:
112: /** Get the http-session application is running in.
113: *
114: * @return HttpSession this application context resides in
115: */
116: public HttpSession getHttpSession() {
117: return session;
118: }
119:
120: /* (non-Javadoc)
121: * @see org.millstone.base.service.ApplicationContext#getApplications()
122: */
123: public Collection getApplications() {
124: LinkedList applications = (LinkedList) session
125: .getAttribute(WebAdapterServlet.SESSION_ATTR_APPS);
126:
127: return Collections
128: .unmodifiableCollection(applications == null ? (new LinkedList())
129: : applications);
130: }
131:
132: /** Get application context for HttpSession.
133: *
134: * @return application context for HttpSession.
135: */
136: static public WebApplicationContext getApplicationContext(
137: HttpSession session) {
138: return new WebApplicationContext(session);
139: }
140:
141: /* (non-Javadoc)
142: * @see java.lang.Object#equals(java.lang.Object)
143: */
144: public boolean equals(Object obj) {
145: return session.equals(obj);
146: }
147:
148: /* (non-Javadoc)
149: * @see java.lang.Object#hashCode()
150: */
151: public int hashCode() {
152: return session.hashCode();
153: }
154:
155: /* (non-Javadoc)
156: * @see org.millstone.base.service.ApplicationContext#addTransactionListener(org.millstone.base.service.ApplicationContext.TransactionListener)
157: */
158: public void addTransactionListener(TransactionListener listener) {
159: if (this .listeners == null)
160: this .listeners = new LinkedList();
161: this .listeners.add(listener);
162: }
163:
164: /* (non-Javadoc)
165: * @see org.millstone.base.service.ApplicationContext#removeTransactionListener(org.millstone.base.service.ApplicationContext.TransactionListener)
166: */
167: public void removeTransactionListener(TransactionListener listener) {
168: if (this .listeners != null)
169: this .listeners.remove(listener);
170:
171: }
172:
173: /** Notify transaction start */
174: protected void startTransaction(Application application,
175: HttpServletRequest request) {
176: if (this .listeners == null)
177: return;
178: for (Iterator i = this .listeners.iterator(); i.hasNext();) {
179: ((ApplicationContext.TransactionListener) i.next())
180: .transactionStart(application, request);
181: }
182: }
183:
184: /** Notify transaction end */
185: protected void endTransaction(Application application,
186: HttpServletRequest request) {
187: if (this .listeners == null)
188: return;
189: for (Iterator i = this .listeners.iterator(); i.hasNext();) {
190: ((ApplicationContext.TransactionListener) i.next())
191: .transactionEnd(application, request);
192: }
193: }
194: }
|