1 43 package net.jforum; 44 45 import java.io.BufferedWriter ; 46 import java.io.IOException ; 47 import java.io.OutputStreamWriter ; 48 import java.io.Writer ; 49 import java.sql.Connection ; 50 51 import javax.servlet.ServletConfig ; 52 import javax.servlet.ServletException ; 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 56 import net.jforum.dao.DatabaseWorkarounder; 57 import net.jforum.exceptions.ExceptionWriter; 58 import net.jforum.exceptions.ForumStartupException; 59 import net.jforum.repository.ModulesRepository; 60 import net.jforum.repository.RankingRepository; 61 import net.jforum.repository.SecurityRepository; 62 import net.jforum.repository.SmiliesRepository; 63 import net.jforum.util.I18n; 64 import net.jforum.util.MD5; 65 import net.jforum.util.preferences.ConfigKeys; 66 import net.jforum.util.preferences.SystemGlobals; 67 68 import org.apache.log4j.Logger; 69 70 import freemarker.template.SimpleHash; 71 import freemarker.template.Template; 72 73 79 public class JForum extends JForumBaseServlet 80 { 81 private static boolean isDatabaseUp; 82 private static Logger logger = Logger.getLogger(JForum.class); 83 84 87 public void init(ServletConfig config) throws ServletException 88 { 89 super.init(config); 90 super.startFrontController(); 91 92 isDatabaseUp = ForumStartup.startDatabase(); 94 95 try { 96 Connection conn = DBConnection.getImplementation().getConnection(); 97 conn.setAutoCommit(!SystemGlobals.getBoolValue(ConfigKeys.DATABASE_USE_TRANSACTIONS)); 98 99 DatabaseWorkarounder dw = new DatabaseWorkarounder(); 101 dw.handleWorkarounds(conn); 102 103 JForumExecutionContext ex = JForumExecutionContext.get(); 105 ex.setConnection(conn); 106 JForumExecutionContext.set(ex); 107 108 ForumStartup.startForumRepository(); 110 RankingRepository.loadRanks(); 111 SmiliesRepository.loadSmilies(); 112 } 113 catch (Exception e) { 114 throw new ForumStartupException("Error while starting jforum", e); 115 } 116 finally { 117 JForumExecutionContext.finish(); 118 } 119 } 120 121 124 public void service(HttpServletRequest req, HttpServletResponse response) throws IOException , ServletException 125 { 126 Writer out = null; 127 ActionServletRequest request = null; 128 String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING); 129 130 try { 131 request = new ActionServletRequest(req); 133 134 JForumExecutionContext ex = JForumExecutionContext.get(); 136 ex.setRequest(request); 137 ex.setResponse(response); 138 139 if (!isDatabaseUp) { 140 ForumStartup.startDatabase(); 141 } 142 143 JForumExecutionContext.set(ex); 144 145 SimpleHash context = JForumExecutionContext.getTemplateContext(); 147 148 ControllerUtils utils = new ControllerUtils(); 149 utils.refreshSession(); 150 151 context.put("logged", SessionFacade.isLogged()); 152 153 SecurityRepository.load(SessionFacade.getUserSession().getUserId()); 155 156 request.setJForumContext(new JForumContext(request.getContextPath(), 157 SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), 158 request, 159 response, 160 SessionFacade.getUserSession().isBot())); 161 162 utils.prepareTemplateContext(context, request.getJForumContext()); 163 164 String module = request.getModule(); 165 166 String moduleClass = module != null 168 ? ModulesRepository.getModuleClass(module) 169 : null; 170 171 context.put("moduleName", module); 172 context.put("action", request.getAction()); 173 context.put("language", I18n.getUserLanguage()); 174 context.put("securityHash", MD5.crypt(request.getSession().getId())); 175 context.put("session", SessionFacade.getUserSession()); 176 context.put("request", req); 177 context.put("response", response); 178 179 if (moduleClass != null) { 180 Command c = (Command)Class.forName(moduleClass).newInstance(); 182 Template template = c.process(request, response, context); 183 184 if (JForumExecutionContext.getRedirectTo() == null) { 185 String contentType = JForumExecutionContext.getContentType(); 186 187 if (contentType == null) { 188 contentType = "text/html; charset=" + encoding; 189 } 190 191 response.setContentType(contentType); 192 193 if (!JForumExecutionContext.isCustomContent()) { 197 out = new BufferedWriter (new OutputStreamWriter (response.getOutputStream(), encoding)); 198 template.process(JForumExecutionContext.getTemplateContext(), out); 199 out.flush(); 200 } 201 } 202 } 203 else { 204 response.sendError(HttpServletResponse.SC_NOT_FOUND); 206 } 207 } 208 catch (Exception e) { 209 JForumExecutionContext.enableRollback(); 210 211 if (e.toString().indexOf("ClientAbortException") == -1) { 212 response.setContentType("text/html; charset=" + encoding); 213 if (out != null) { 214 new ExceptionWriter().handleExceptionData(e, out); 215 } 216 else { 217 new ExceptionWriter().handleExceptionData(e, new BufferedWriter (new OutputStreamWriter (response.getOutputStream()))); 218 } 219 } 220 } 221 finally { 222 try { 223 if (out != null) { out.close(); } 224 } 225 catch (Exception e) {} 226 227 String redirectTo = JForumExecutionContext.getRedirectTo(); 228 JForumExecutionContext.finish(); 229 230 if (redirectTo != null) { 231 if(request.getJForumContext().isEncodingDisabled()) { 232 response.sendRedirect(redirectTo); 233 } 234 else { 235 response.sendRedirect(response.encodeRedirectURL(redirectTo)); 236 } 237 } 238 } 239 } 240 241 244 public void destroy() { 245 super.destroy(); 246 System.out.println("Destroying JForum..."); 247 248 try { 249 DBConnection.getImplementation().realReleaseAllConnections(); 250 ConfigLoader.stopCacheEngine(); 251 } 252 catch (Exception e) {} 253 } 254 } 255 | Popular Tags |