JForumExecutionContext.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 8k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * This file creation date: 29/01/2006 - 12:19:11
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum;
  44. import java.io.IOException;
  45. import java.sql.Connection;
  46. import javax.servlet.http.HttpServletResponse;
  47. import net.jforum.context.RequestContext;
  48. import net.jforum.context.ResponseContext;
  49. import net.jforum.context.ForumContext;
  50. import net.jforum.exceptions.ForumException;
  51. import net.jforum.util.preferences.ConfigKeys;
  52. import net.jforum.util.preferences.SystemGlobals;
  53. import org.apache.log4j.Logger;
  54. import freemarker.template.Configuration;
  55. import freemarker.template.ObjectWrapper;
  56. import freemarker.template.SimpleHash;
  57. /**
  58.  * Data execution context. 
  59.  * 
  60.  * @author Rafael Steil
  61.  * @version $Id: JForumExecutionContext.java,v 1.10 2006/10/10 01:59:55 rafaelsteil Exp $
  62.  */
  63. public class JForumExecutionContext
  64. {
  65.     private static ThreadLocal userData = new ThreadLocal();
  66. private static Logger logger = Logger.getLogger(JForumExecutionContext.class);
  67. private static Configuration templateConfig;
  68. private Connection conn;
  69.     private ForumContext forumContext;
  70.     private SimpleHash context = new SimpleHash(ObjectWrapper.BEANS_WRAPPER);
  71.     private String redirectTo;
  72.     private String contentType;
  73.     private boolean isCustomContent;
  74.     private boolean enableRollback;
  75. /**
  76.  * Gets the execution context.
  77.  * @return JForumExecutionContext
  78.  */
  79. public static JForumExecutionContext get()
  80. {
  81. JForumExecutionContext ex = (JForumExecutionContext)userData.get();
  82. if (ex == null) {
  83. ex = new JForumExecutionContext();
  84. userData.set(ex);
  85. }
  86. return ex;
  87. }
  88. /**
  89.  * Checks if there is an execution context already set
  90.  * @return <code>true</code> if there is an execution context
  91.  * @see #get()
  92.  */
  93. public static boolean exists()
  94. {
  95. return (userData.get() != null);
  96. }
  97. /**
  98.  * Sets the default template configuration 
  99.  * @param config The template configuration to set
  100.  */
  101. public static void setTemplateConfig(Configuration config)
  102. {
  103. templateConfig = config;
  104. }
  105. /**
  106.  * Gets a reference to the default template configuration settings.
  107.  * @return The template configuration instance
  108.  */
  109. public static Configuration templateConfig()
  110. {
  111. return templateConfig;
  112. }
  113. /**
  114.  * Sets the execution context
  115.  * @param ex JForumExecutionContext
  116.  */
  117. public static void set(JForumExecutionContext ex)
  118. {
  119. userData.set(ex);
  120. }
  121. /**
  122.  * Sets a connection
  123.  * @param conn The connection to use
  124.  */
  125. public void setConnection(Connection conn)
  126. {
  127. this.conn = conn;
  128. }
  129. /**
  130.  * Gets the current thread's connection
  131.  * @return Connection
  132.  */
  133. public static Connection getConnection() 
  134. {
  135. return getConnection(true);
  136. }
  137. public static Connection getConnection(boolean validate)
  138. {
  139. JForumExecutionContext ex = get();
  140. Connection c =  ex.conn;
  141. if (validate && c == null) {
  142. c = DBConnection.getImplementation().getConnection();
  143. try {
  144. c.setAutoCommit(!SystemGlobals.getBoolValue(ConfigKeys.DATABASE_USE_TRANSACTIONS));
  145. }
  146. catch (Exception e) {
  147.                 //catch error autocommit
  148.             }
  149. ex.setConnection(c);
  150. set(ex);
  151. }
  152.     
  153. return c; 
  154. }
  155.     public static ForumContext getForumContext()
  156.     {
  157.         return ((JForumExecutionContext)userData.get()).forumContext;
  158.     }
  159.     public void setForumContext(ForumContext forumContext)
  160.     {
  161.         this.forumContext = forumContext;
  162.     }
  163.     /**
  164.  * Gets the current thread's request
  165.  * @return WebContextRequest
  166.  */
  167. public static RequestContext getRequest() {
  168. return getForumContext().getRequest();
  169. }
  170. /**
  171.  * Gets the current thread's response
  172.  * @return HttpServletResponse
  173.  */
  174. public static ResponseContext getResponse() {
  175. return getForumContext().getResponse();
  176. }
  177. /**
  178.  * Gets the current thread's template context
  179.  * @return SimpleHash
  180.  */
  181. public static SimpleHash getTemplateContext() {
  182. return ((JForumExecutionContext)userData.get()).context;
  183. }
  184. /**
  185.  * Gets the current thread's <code>DataHolder</code> instance
  186.      * @param redirect String
  187.      */
  188. public static void setRedirect(String redirect) {
  189. ((JForumExecutionContext)userData.get()).redirectTo = redirect;
  190. }
  191. /**
  192.  * Sets the content type for the current http response.
  193.  * @param contentType String
  194.  */
  195. public static void setContentType(String contentType) {
  196. ((JForumExecutionContext)userData.get()).contentType = contentType;
  197. }
  198. /**
  199.  * Gets the content type for the current request.
  200.  * @return String
  201.  */
  202. public static String getContentType()
  203. {
  204. return ((JForumExecutionContext)userData.get()).contentType;
  205. }
  206. /**
  207.  * Gets the URL to redirect to, if any.
  208.  * @return The URL to redirect, of <code>null</code> if none.
  209.  */
  210. public static String getRedirectTo()
  211. {
  212. JForumExecutionContext ex = (JForumExecutionContext)userData.get();
  213. return (ex != null ? ex.redirectTo : null);
  214. }
  215. /**
  216.  * Marks the request to use a binary content-type.
  217.  * @param enable boolean
  218.  */
  219. public static void enableCustomContent(boolean enable) {
  220. ((JForumExecutionContext)userData.get()).isCustomContent = enable;
  221. }
  222. /**
  223.  * Checks if the current request is binary
  224.  * @return <code>true</code> if the content tyee for the current request is 
  225.  * any binary data.
  226.  */
  227. public static boolean isCustomContent()
  228. {
  229. return ((JForumExecutionContext)userData.get()).isCustomContent;
  230. }
  231. /**
  232.  * Forces the request to not commit the connection.
  233.  */
  234. public static void enableRollback() {
  235. ((JForumExecutionContext)userData.get()).enableRollback = true;
  236. }
  237. /**
  238.  * Check if commit is disabled or not for the current request.
  239.  * @return <code>true</code> if a commit should NOT be made
  240.  */
  241. public static boolean shouldRollback() {
  242. return ((JForumExecutionContext)userData.get()).enableRollback;
  243. }
  244.     /**
  245.      * Send UNAUTHORIZED to the browser and ask user to login via basic authentication
  246.      */
  247. public static void requestBasicAuthentication()  
  248. {
  249. getResponse().addHeader("WWW-Authenticate", "Basic realm="JForum"");
  250. try {
  251. getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED);
  252. }
  253. catch (IOException e) {
  254. throw new ForumException(e);
  255. }
  256. enableCustomContent(true);
  257.     }
  258. /**
  259.  * Finishes the execution context
  260.  */
  261. public static void finish()
  262. {
  263. Connection conn = JForumExecutionContext.getConnection(false);
  264. if (conn != null) {
  265. if (SystemGlobals.getBoolValue(ConfigKeys.DATABASE_USE_TRANSACTIONS)) {
  266. if (JForumExecutionContext.shouldRollback()) {
  267. try {
  268. conn.rollback();
  269. }
  270. catch (Exception e) {
  271. logger.error("Error while rolling back a transaction", e);
  272. }
  273. }
  274. else {
  275. try {
  276. conn.commit();
  277. }
  278. catch (Exception e) {
  279. logger.error("Error while commiting a transaction", e);
  280. }
  281. }
  282. }
  283. try {
  284. DBConnection.getImplementation().releaseConnection(conn);
  285. }
  286. catch (Exception e) {
  287. logger.error("Error while releasing the connection : " + e, e);
  288. }
  289. }
  290. userData.set(null);
  291. }
  292. }