1 13 package com.tonbeller.wcf.controller; 14 15 import javax.servlet.ServletContext ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.servlet.http.HttpSession ; 19 20 import org.apache.log4j.Logger; 21 22 import com.tonbeller.wcf.utils.SoftException; 23 24 29 public class RequestContextFactoryFinder { 30 private static Logger logger = Logger.getLogger(RequestContextFactoryFinder.class); 31 32 35 private static final String SESSION_KEY = RequestContextFactory.class.getName(); 36 37 41 private static final String CONTEXT_KEY = "com.tonbeller.wcf.controller.RequestContextFactory"; 42 43 public static RequestContextFactory findFactory(HttpSession session) { 44 RequestContextFactory rcf; 45 try { 46 rcf = (RequestContextFactory) session.getAttribute(SESSION_KEY); 47 if (rcf == null) { 48 ServletContext context = session.getServletContext(); 49 String className = (String ) 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 e) { 56 logger.error(null, e); 57 throw new SoftException(e); 58 } catch (IllegalAccessException e) { 59 logger.error(null, e); 60 throw new SoftException(e); 61 } catch (ClassNotFoundException e) { 62 logger.error(null, e); 63 throw new SoftException(e); 64 } 65 return rcf; 66 } 67 68 78 public static RequestContext createContext(HttpServletRequest request, HttpServletResponse response, boolean threadLocal) { 79 HttpSession 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 |