KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > RequestContextFactoryFinder


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.controller;
14
15 import javax.servlet.ServletContext JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import javax.servlet.http.HttpSession JavaDoc;
19
20 import org.apache.log4j.Logger;
21
22 import com.tonbeller.wcf.utils.SoftException;
23
24 /**
25  * finds the RequestContextFactory
26  * @task the factory should be thread safe and part of the environment
27  * @author av
28  */

29 public class RequestContextFactoryFinder {
30   private static Logger logger = Logger.getLogger(RequestContextFactoryFinder.class);
31
32   /**
33    * an instance of RequestContextFactory ist found in the users session.
34    */

35   private static final String JavaDoc SESSION_KEY = RequestContextFactory.class.getName();
36
37   /**
38    * The name of the concrete class is a configurable context attribute with this
39    * name.
40    */

41   private static final String JavaDoc CONTEXT_KEY = "com.tonbeller.wcf.controller.RequestContextFactory";
42
43   public static RequestContextFactory findFactory(HttpSession JavaDoc session) {
44     RequestContextFactory rcf;
45     try {
46       rcf = (RequestContextFactory) session.getAttribute(SESSION_KEY);
47       if (rcf == null) {
48         ServletContext JavaDoc context = session.getServletContext();
49         String JavaDoc className = (String JavaDoc) context.getInitParameter(CONTEXT_KEY);
50         if (className == null)
51           className = RequestContextFactoryImpl.class.getName();
52         rcf = (RequestContextFactory) Class.forName(className).newInstance();
53         session.setAttribute(SESSION_KEY, rcf);
54       }
55     } catch (InstantiationException JavaDoc e) {
56       logger.error(null, e);
57       throw new SoftException(e);
58     } catch (IllegalAccessException JavaDoc e) {
59       logger.error(null, e);
60       throw new SoftException(e);
61     } catch (ClassNotFoundException JavaDoc e) {
62       logger.error(null, e);
63       throw new SoftException(e);
64     }
65     return rcf;
66   }
67
68   /**
69    * @param request
70    * @param response
71    * @param threadLocal if true, a thread local RequestContext variable is initialized. The thread
72    * local may be accessed via RequestContext.instance() and must be cleaned up by the caller
73    * via RequestContext.invalidate().
74    *
75    * @see RequestContext#instance()
76    * @see RequestContext#invalidate()
77    */

78   public static RequestContext createContext(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, boolean threadLocal) {
79     HttpSession JavaDoc session = request.getSession(true);
80     RequestContextFactory rcf = findFactory(session);
81     RequestContext context = rcf.createContext(request, response);
82     if (threadLocal)
83       RequestContext.setInstance(context);
84     return context;
85   }
86 }
87
Popular Tags