alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

jforum example source code file (ExecutionContext.txt)

This example jforum source code file (ExecutionContext.txt) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - jforum tags/keywords

a, a, by, forumcontext, forumcontext, http, if, in, jdbc, jdbc, jforum, jforum, the, to

The jforum ExecutionContext.txt source code

!!! Integrating JForum
JForum uses [ThreadLocal|http://www.ibm.com/developerworks/java/library/j-threads3.html] 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. 

[{Java2HtmlPlugin 

import net.jforum.JForumExecutionContext;
import net.jforum.context.ForumContext;
import net.jforum.context.JForumContext;
import net.jforum.context.web.WebRequestContext;
import net.jforum.context.web.WebResponseContext;

// .....

try {
	// Retrieve an existing or create a new execution context
	JForumExecutionContext executionContext = JForumExecutionContext.get();
	
	// Create a ForumContext
	ForumContext forumContext = new JForumContext(request.getContextPath(), 
		".page", 
		new WebRequestContext(request), 
		new WebResponseContext(response));
	
	executionContext.setForumContext(forumContext);
	
	// If you have a valid JDBC connection at this point, you can 
	// set it as well
	executionContext.setConnection(connection);
	
	// Set back the new execution context, so we can use it
	JForumExecutionContext.set(executionContext);
	
	// ....
	// Execute all your JForum code here
	// ....
}
finally {
	// Release the resources
	JForumExecutionContext.finish();
}
}]

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:

[{Java2HtmlPlugin 

// ....
// By setting the connection to null, we prevent JForum of trying
// to release it when finish() is called
executionContext.setConnection(null);
JForumExecutionContext.set(executionContext);

// Now, when calling JForumExecutionContext.finish(), JForum will not try
// to release that connection
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

Other jforum examples (source code examples)

Here is a short list of links related to this jforum ExecutionContext.txt source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.