Integrating JForum
JForum uses
ThreadLocal
for its execution contexts, which is where all the action occurs. In order to execute anything related to JForum, you have to setup the execution context, represented by the class
net.jforum.JForumExecutionContext. The most common used methods are:
- get()
- set()
- setConnection(),
- setForumContext()
This last being an instance of net.jforum.context.ForumContext, usually net.jforum.context.JForumContext.
A ForumContext contains the HTTP Request and Response, as well the context path' and JForum's servlet extension.
Creating a complete execution context
By
complete we mean an execution context that has a request, response and JDBC connection. Not every time you will need all this information - it depends of what you are trying to do -, but it is very likely that at least the HTTP request will be necessary.
01 import net.jforum.JForumExecutionContext;
02 import net.jforum.context.ForumContext;
03 import net.jforum.context.JForumContext;
04 import net.jforum.context.web.WebRequestContext;
05 import net.jforum.context.web.WebResponseContext;
06
07 // .....
08
09 try {
10 // Retrieve an existing or create a new execution context
11 JForumExecutionContext executionContext = JForumExecutionContext.get();
12
13 // Create a ForumContext
14 ForumContext forumContext = new JForumContext(request.getContextPath(),
15 ".page",
16 new WebRequestContext(request),
17 new WebResponseContext(response));
18
19 executionContext.setForumContext(forumContext);
20
21 // If you have a valid JDBC connection at this point, you can
22 // set it as well
23 executionContext.setConnection(connection);
24
25 // Set back the new execution context, so we can use it
26 JForumExecutionContext.set(executionContext);
27
28 // ....
29 // Execute all your JForum code here
30 // ....
31 }
32 finally {
33 // Release the resources
34 JForumExecutionContext.finish();
35 }
|
In lines 16 and 17 we create new instance of WebRequestContext and WebResponseContext, passing a valid instance of a HttpServletRequest and HttpServletResponse respectively. It is supposed you already have such instances (e.g, from your Servlet / JSP).
A note about the JDBC Connection
In line 23 we manually set a JDBC connection. If we don't do that, JForum will make a call to the method
getConnection() of the class
JForumExecutionContext, trying to get a connection from JForum's configuration.
However, the example code doesn't tell the whole history. By calling JForumExecutionContext.finish(), JForum will also try to release such connection, even if it was manually set. To prevent JForum of doing that - for example, if you have your own connection poll and / or want to use that connection besides JForum execution context, you have to set it to null, as shown in the following code:
1 // ....
2 // By setting the connection to null, we prevent JForum of trying
3 // to release it when finish() is called
4 executionContext.setConnection(null);
5 JForumExecutionContext.set(executionContext);
6
7 // Now, when calling JForumExecutionContext.finish(), JForum will not try
8 // to release that connection
9 JForumExecutionContext.finish();
|
Resuming
To resume the steps:
- Always use the execution context inside a try-finally block;
- Never forget to fall JForumExecutionContext.finish()
- Retrieve the execution context by calling 'JForumExecutionContext.get()
- A ForumContext holds the request and response
- The default implementation of ForumContext is JForumContext
- You can manually set a JDBC connection using the method setConnection()
- If not using setConnection(), JForum will try to retrieve one from its connection poll