1 16 package javax.faces.webapp; 17 18 import javax.faces.FactoryFinder; 19 import javax.faces.context.FacesContext; 20 import javax.faces.context.FacesContextFactory; 21 import javax.faces.lifecycle.Lifecycle; 22 import javax.faces.lifecycle.LifecycleFactory; 23 import javax.servlet.*; 24 import java.io.IOException ; 25 26 45 public class FacesServlet 46 implements Servlet 47 { 48 public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES"; 49 public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID"; 50 51 private static final String SERVLET_INFO = "FacesServlet of the MyFaces API implementation"; 52 private ServletConfig _servletConfig; 53 private FacesContextFactory _facesContextFactory; 54 private Lifecycle _lifecycle; 55 56 public FacesServlet() 57 { 58 super(); 59 } 60 61 public void destroy() 62 { 63 _servletConfig = null; 64 _facesContextFactory = null; 65 _lifecycle = null; 66 } 67 68 public ServletConfig getServletConfig() 69 { 70 return _servletConfig; 71 } 72 73 public String getServletInfo() 74 { 75 return SERVLET_INFO; 76 } 77 78 private String getLifecycleId() 79 { 80 String lifecycleId = _servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR); 81 return lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE; 82 } 83 84 public void init(ServletConfig servletConfig) 85 throws ServletException 86 { 87 _servletConfig = servletConfig; 88 _facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); 89 91 LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 94 _lifecycle = lifecycleFactory.getLifecycle(getLifecycleId()); 95 } 96 97 public void service(ServletRequest request, 98 ServletResponse response) 99 throws IOException , 100 ServletException 101 { 102 FacesContext facesContext 103 = _facesContextFactory.getFacesContext(_servletConfig.getServletContext(), 104 request, 105 response, 106 _lifecycle); 107 try 108 { 109 _lifecycle.execute(facesContext); 110 _lifecycle.render(facesContext); 111 } 112 catch (Throwable e) 113 { 114 logException(e, null); 115 if (e instanceof IOException ) 116 { 117 throw (IOException )e; 118 } 119 else if (e instanceof ServletException) 120 { 121 throw (ServletException)e; 122 } 123 else if (e.getMessage() != null) 124 { 125 throw new ServletException(e.getMessage(), e); 126 } 127 else 128 { 129 throw new ServletException(e); 130 } 131 } 132 finally 133 { 134 facesContext.release(); 135 } 136 } 137 138 private void logException(Throwable e, String msgPrefix) 139 { 140 String msg; 141 if (msgPrefix == null) 142 { 143 if (e.getMessage() == null) 144 { 145 msg = "Exception in FacesServlet"; 146 } 147 else 148 { 149 msg = e.getMessage(); 150 } 151 } 152 else 153 { 154 if (e.getMessage() == null) 155 { 156 msg = msgPrefix; 157 } 158 else 159 { 160 msg = msgPrefix + ": " + e.getMessage(); 161 } 162 } 163 164 _servletConfig.getServletContext().log(msg, e); 165 e.printStackTrace(); 166 167 Throwable cause = e.getCause(); 168 if (cause != null && cause != e) 169 { 170 logException(cause, "Root cause"); 171 } 172 173 if(e instanceof ServletException) 174 { 175 cause = ((ServletException) e).getRootCause(); 176 177 if(cause != null && cause != e) 178 { 179 logException(cause, "Root cause of ServletException"); 180 } 181 } 182 } 183 184 185 } 186 | Popular Tags |