1 43 package net.jforum; 44 45 import java.io.IOException ; 46 import java.sql.Connection ; 47 48 import javax.servlet.http.HttpServletResponse ; 49 50 import net.jforum.util.preferences.ConfigKeys; 51 import net.jforum.util.preferences.SystemGlobals; 52 53 import org.apache.log4j.Logger; 54 55 import freemarker.template.Configuration; 56 import freemarker.template.ObjectWrapper; 57 import freemarker.template.SimpleHash; 58 59 65 public class JForumExecutionContext 66 { 67 private static ThreadLocal userData = new ThreadLocal (); 68 private static Logger logger = Logger.getLogger(JForumExecutionContext.class); 69 private static Configuration templateConfig; 70 71 private Connection conn; 72 private ActionServletRequest request; 73 private HttpServletResponse response; 74 private SimpleHash context = new SimpleHash(ObjectWrapper.BEANS_WRAPPER); 75 private String redirectTo; 76 private String contentType; 77 private boolean isCustomContent; 78 private boolean enableRollback; 79 80 84 public static JForumExecutionContext get() 85 { 86 JForumExecutionContext ex = (JForumExecutionContext)userData.get(); 87 88 if (ex == null) { 89 ex = new JForumExecutionContext(); 90 userData.set(ex); 91 } 92 93 return ex; 94 } 95 96 101 public static boolean exists() 102 { 103 return (userData.get() != null); 104 } 105 106 110 public static void setTemplateConfig(Configuration config) 111 { 112 templateConfig = config; 113 } 114 115 119 public static Configuration templateConfig() 120 { 121 return templateConfig; 122 } 123 124 128 public static void set(JForumExecutionContext ex) 129 { 130 userData.set(ex); 131 } 132 133 137 public void setConnection(Connection conn) 138 { 139 this.conn = conn; 140 } 141 142 146 public static Connection getConnection() 147 { 148 return getConnection(true); 149 } 150 151 public static Connection getConnection(boolean validate) 152 { 153 JForumExecutionContext ex = get(); 154 Connection c = ex.conn; 155 156 if (validate && c == null) { 157 c = DBConnection.getImplementation().getConnection(); 158 159 logger.debug("Getting a connection for the request: " + c); 160 161 try { 162 c.setAutoCommit(!SystemGlobals.getBoolValue(ConfigKeys.DATABASE_USE_TRANSACTIONS)); 163 } 164 catch (Exception e) {} 165 166 ex.setConnection(c); 167 set(ex); 168 } 169 170 return c; 171 } 172 173 177 public void setRequest(ActionServletRequest request) 178 { 179 this.request = request; 180 } 181 182 186 public static ActionServletRequest getRequest() { 187 return ((JForumExecutionContext)userData.get()).request; 188 } 189 190 194 public void setResponse(HttpServletResponse response) 195 { 196 this.response = response; 197 } 198 199 203 public static HttpServletResponse getResponse() { 204 return ((JForumExecutionContext)userData.get()).response; 205 } 206 207 211 public static SimpleHash getTemplateContext() { 212 return ((JForumExecutionContext)userData.get()).context; 213 } 214 215 219 public static void setRedirect(String redirect) { 220 ((JForumExecutionContext)userData.get()).redirectTo = redirect; 221 } 222 223 227 public static void setContentType(String contentType) { 228 ((JForumExecutionContext)userData.get()).contentType = contentType; 229 } 230 231 235 public static String getContentType() 236 { 237 return ((JForumExecutionContext)userData.get()).contentType; 238 } 239 240 244 public static String getRedirectTo() 245 { 246 JForumExecutionContext ex = (JForumExecutionContext)userData.get(); 247 return (ex != null ? ex.redirectTo : null); 248 } 249 250 254 public static void enableCustomContent(boolean enable) { 255 ((JForumExecutionContext)userData.get()).isCustomContent = enable; 256 } 257 258 263 public static boolean isCustomContent() 264 { 265 return ((JForumExecutionContext)userData.get()).isCustomContent; 266 } 267 268 271 public static void enableRollback() { 272 ((JForumExecutionContext)userData.get()).enableRollback = true; 273 } 274 275 279 public static boolean shouldRollback() { 280 return ((JForumExecutionContext)userData.get()).enableRollback; 281 } 282 283 288 public static void requestBasicAuthentication() throws IOException { 289 getResponse().addHeader("WWW-Authenticate", "Basic realm=\"JForum\""); 290 getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED); 291 enableCustomContent(true); 292 } 293 294 297 public static void finish() 298 { 299 Connection conn = JForumExecutionContext.getConnection(false); 300 301 if (conn != null) { 302 if (SystemGlobals.getBoolValue(ConfigKeys.DATABASE_USE_TRANSACTIONS)) { 303 if (JForumExecutionContext.shouldRollback()) { 304 try { 305 conn.rollback(); 306 } 307 catch (Exception e) { 308 logger.error("Error while rolling back a transaction", e); 309 } 310 } 311 else { 312 try { 313 conn.commit(); 314 } 315 catch (Exception e) { 316 logger.error("Error while commiting a transaction", e); 317 } 318 } 319 } 320 321 try { 322 DBConnection.getImplementation().releaseConnection(conn); 323 } 324 catch (Exception e) { 325 logger.error("Error while releasing the connection : " + e, e); 326 } 327 } 328 329 userData.set(null); 330 } 331 } 332 | Popular Tags |