001: /**
002: *
003: */package clime.messadmin.model;
004:
005: import java.util.ArrayList;
006: import java.util.Collections;
007: import java.util.HashSet;
008: import java.util.Hashtable;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import javax.servlet.ServletContext;
015: import javax.servlet.ServletContextEvent;
016: import javax.servlet.ServletContextListener;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpSession;
019:
020: import clime.messadmin.filter.MessAdminRequestWrapper;
021: import clime.messadmin.filter.MessAdminResponseWrapper;
022: import clime.messadmin.providers.ProviderUtils;
023: import clime.messadmin.providers.spi.ServerLifeCycleProvider;
024: import clime.messadmin.utils.SessionUtils;
025:
026: /**
027: * @author Cédrik LIME
028: */
029: public class Server implements ServletContextListener, IRequestListener {
030: private static final Server INSTANCE = new Server();
031: protected final ServerInfo serverInfo = new ServerInfo();
032: protected final Map webApps = new Hashtable();//<String, Application> // must be synchronized
033: // private final Map webAppsByContextPath = new Hashtable();//<String, Application> // must be synchronized // cache
034:
035: /**
036: *
037: */
038: private Server() {
039: super ();
040: }
041:
042: public static Server getInstance() {
043: return INSTANCE;
044: }
045:
046: /**
047: * @return Returns the serverInfo.
048: */
049: public IServerInfo getServerInfo() {
050: return serverInfo;
051: }
052:
053: /**
054: * @param internalContext
055: * @return Application associated with given internal context in this server
056: */
057: public Application getApplication(String internalContext) {
058: return (Application) webApps.get(internalContext);
059: }
060:
061: /**
062: * @param servletContext
063: * @return Application associated with given servletContext in this server
064: */
065: public Application getApplication(ServletContext servletContext) {
066: return getApplication(getInternalContext(servletContext));
067: }
068:
069: // /**
070: // * @param contextPath
071: // * @return Application associated with given contextPath in this server or null
072: // */
073: // public Application getApplicationForContextPath(final String contextPath) {
074: // if (null == contextPath) {
075: // return null;
076: // }
077: // Application webApp = (Application) webAppsByContextPath.get(contextPath);
078: // if (webApp != null) {
079: // return webApp;
080: // }
081: // Iterator iter = new ArrayList(webApps.values()).iterator();
082: // while (iter.hasNext()) {
083: // webApp = (Application) iter.next();
084: // if (contextPath.equals(webApp.getApplicationInfo().getContextPath())) {
085: // // cache for next request
086: // webAppsByContextPath.put(contextPath, webApp);
087: // return webApp;
088: // }
089: // }
090: // return null;
091: // }
092: /**
093: * @return all known Applications for this server
094: */
095: public Set/*<Application>*/getApplications() {
096: return new HashSet(webApps.values());
097: }
098:
099: /**
100: * @return all known ApplicationInfos for this server
101: */
102: public Set/*<ApplicationInfo>*/getApplicationInfos() {
103: Set result = new HashSet(webApps.size());
104: Iterator iter = webApps.values().iterator();
105: while (iter.hasNext()) {
106: Application application = (Application) iter.next();
107: result.add(application.getApplicationInfo());
108: }
109: return result;
110: }
111:
112: /**
113: * @return all known application internal contexts for this server
114: */
115: public Set/*<String>*/getAllKnownInternalContexts() {
116: return new HashSet(webApps.keySet());
117: }
118:
119: public String getInternalContext(final ServletContext servletContext) {
120: String internalContext = SessionUtils
121: .getContext(servletContext);
122: return internalContext;
123: }
124:
125: /**
126: * Convenience method to directly get a Session without going through Application
127: * @param in_session
128: * @return Session
129: */
130: public Session getSession(HttpSession in_session) {
131: if (in_session == null) {
132: return null;
133: }
134: try {
135: Application application = getApplication(in_session
136: .getServletContext());
137: if (application != null) {
138: return application.getSession(in_session.getId());
139: } else {
140: return null;
141: }
142: } catch (IllegalStateException ise) {
143: return null;
144: }
145: }
146:
147: /*****************************************/
148: /** Request/Response Listener methods **/
149: /*****************************************/
150:
151: // protected void registerContextPath(final HttpServletRequest request, final Application webApp) {
152: // final String contextPath = request.getContextPath();
153: // if (webAppsByContextPath.get(contextPath) == null) {
154: // //webApp == getApplication(request.getSession(false).getServletContext());
155: // webAppsByContextPath.put(contextPath, webApp);
156: // webApp.registerContextPath(contextPath);
157: // }
158: // }
159: /** {@inheritDoc} */
160: public void requestInitialized(final HttpServletRequest request,
161: final ServletContext servletContext) {
162: if (request == null || servletContext == null) {
163: return;
164: }
165: // final HttpSession httpSession = request.getSession(false);
166: // if (httpSession == null) {
167: // final Application application = getApplicationForContextPath(request.getContextPath());
168: // if (application != null) {
169: // application.requestInitialized(request, servletContext);
170: // }
171: // return;
172: // } // else
173: try {
174: final Application application = getApplication(servletContext);
175: application.requestInitialized(request, servletContext);
176: } catch (IllegalStateException ise) {
177: // session is invalidated
178: }
179: }
180:
181: /** {@inheritDoc} */
182: public void requestDestroyed(final MessAdminRequestWrapper request,
183: final MessAdminResponseWrapper response,
184: final ServletContext servletContext) {
185: if (request == null || response == null
186: || servletContext == null) {
187: return;
188: }
189: // final HttpSession httpSession = request.getSession(false);
190: // if (httpSession == null) {
191: // final Application application = getApplicationForContextPath(request.getContextPath());
192: // if (application != null) {
193: // application.requestDestroyed(request, response, servletContext);
194: // }
195: // return;
196: // } // else
197: try {
198: final Application application = getApplication(servletContext);
199: // registerContextPath(request, application); // in case HttpSession has been created after request
200: application.requestDestroyed(request, response,
201: servletContext);
202: } catch (IllegalStateException ise) {
203: // session is invalidated
204: }
205: }
206:
207: /** {@inheritDoc} */
208: public void requestException(Exception e,
209: MessAdminRequestWrapper request,
210: MessAdminResponseWrapper response,
211: ServletContext servletContext) {
212: if (request == null || response == null
213: || servletContext == null) {
214: return;
215: }
216: try {
217: final Application application = getApplication(servletContext);
218: // registerContextPath(request, application); // in case HttpSession has been created after request
219: application.requestException(e, request, response,
220: servletContext);
221: } catch (IllegalStateException ise) {
222: // session is invalidated
223: }
224: }
225:
226: /*************************************/
227: /** ServletContextListener methods **/
228: /*************************************/
229:
230: /**
231: * {@inheritDoc}
232: */
233: public void contextInitialized(final ServletContextEvent sce) {
234: String internalContext = getInternalContext(sce
235: .getServletContext());
236: Application application = new Application(sce
237: .getServletContext());
238: webApps.put(internalContext, application);
239: }
240:
241: /**
242: * {@inheritDoc}
243: */
244: public void contextDestroyed(final ServletContextEvent sce) {
245: String internalContext = getInternalContext(sce
246: .getServletContext());
247: /*Application application = (Application)*/webApps
248: .remove(internalContext);
249: // if (application.getApplicationInfo().getContextPath() != null) {
250: // webAppsByContextPath.remove(application.getApplicationInfo().getContextPath());
251: // }
252: }
253:
254: /*****************************/
255: /** ServerListener methods **/
256: /*****************************/
257:
258: private boolean isInitialized = false;
259:
260: /**
261: * @see ServerLifeCycleProvider#serverInitialized()
262: */
263: public synchronized void serverInitialized() {
264: if (isInitialized || !webApps.isEmpty()) {
265: // don't initialize twice, or while running
266: return;
267: }
268: isInitialized = true;
269: // Put Server-specific actions here
270: Iterator iter = ProviderUtils.getProviders(
271: ServerLifeCycleProvider.class).iterator();
272: while (iter.hasNext()) {
273: ServerLifeCycleProvider lc = (ServerLifeCycleProvider) iter
274: .next();
275: try {
276: lc.serverInitialized();
277: } catch (RuntimeException rte) {
278: }
279: }
280: }
281:
282: /**
283: * @see ServerLifeCycleProvider#serverDestroyed()
284: */
285: public synchronized void serverDestroyed() {
286: if (!isInitialized || !webApps.isEmpty()) {
287: // don't destroy if not initialized, or if running
288: return;
289: }
290: isInitialized = false;
291: List providers = new ArrayList(ProviderUtils
292: .getProviders(ServerLifeCycleProvider.class));
293: Collections.reverse(providers);
294: Iterator iter = providers.iterator();
295: while (iter.hasNext()) {
296: ServerLifeCycleProvider lc = (ServerLifeCycleProvider) iter
297: .next();
298: try {
299: lc.serverDestroyed();
300: } catch (RuntimeException rte) {
301: }
302: }
303: // Put Server-specific actions here
304: ProviderUtils.reload();
305: }
306: }
|