Jive talkin: open source Java forum software

Source: Internet
Author: User
Tags forum software

Soon, we decided to adopt Jive. Just like lockerjock.com, Jive adopts server-side Java, supports our database (mySQL) and Application Server (JRun), and uses JSP file representation. We have not only one forum, but also many personalized forums. All forums can be well integrated with lockerjock.com.

Installing Jive is as easy as creating a Jive table. Copy the jive. jar file to the classpath directory, place the Forum and Management JSP file in the appropriate location of the site, and run the Jive installation page. The only problem is that our JSP version is 0.92, and Jive requires JSP 1.0 or a later version. So it took me a few days to upgrade JSP to the latest version.

How does Jive work?

Jive consists of a series of tables in the SQL database and a series of class libraries. These tables store Forum data, class library tables, JSP files, and display Forum data. The two sets of JSP files to be used are provided together with Jive. They share the same functions, display the forum, and allow users to interact with the Forum. Lockerjock.com uses viewForum. jsp, viewMessage. jsp, and post. jsp. The functions of these JSP files are the same as those of their names. If you want to use the Jive authentication scheme when sending messages and accessing the forum, you can use these files.

Back to Top

Example 1: A simple Jive Application

The JSP file listed in Listing 1 uses the main Jive object to display the name of a thread in a given forum. Each object and method will be discussed in detail later, but the concept provided here is very important for understanding how Jive works.

  • Before starting any Jive operation, create a Jive authorization token. In this example, create an anonymous authorization token.
  • Use the authorization token to create a specific forum factory.
  • Create a forum from the Forum factory. If the authorization token license does not match the Forum license set, an exception is thrown and the user cannot access the forum.
  • Print the name of each thread for every thread in the Forum.


Listing 1: print the name of each thread in the Forum

<%@ page import="java.util.Iterator,com.coolservlets.forum.*" %> <%   //JSP to print out the names of all the threads in a forum.   //Get an anonymous authorization object.   Authorization auth = AuthorizationFactory.getAnonymousAuthorization();   ForumFactory factory = ForumFactory.getInstance(auth);   //Loading a forum object throws an Unauthorized exception if the   //the permissions set on the forum don't correspond to your access level.   try {       //Load the forum named myForum       Forum forum = factory.getForum("myForum");              //Get an iterator for all the threads in myForum       Iterator threads = forum.threads();       while (threads.hasNext() ) {           ForumThread thread = (ForumThread)threads.next(); %>       <%= thread.getName() %>  <%       }   }   catch (UnauthorizedException ue) {     System.err.println("You do not have permission to read this forum.");   } %>


Back to Top

Jive Example 2: Custom Jive

At lockerjock, we already have some customers and their own customer authorization schemes. Therefore, if we do not modify the Jive authorization scheme, we will not be able to use it. We need to integrate the Jive user authorization solution with our user authorization solution. Before I modify the SQL statement in the Jive Code so that it can be queried from our user data table instead of the Jive table, I think it will be very simple to use the Jive user data table and will require little maintenance in the future, I decided to keep two sets of user data tables at the same time. When a lockerjock user is created, I create a Jive user at the same time. When a user changes its data,
I will update the data in the Jive table. To encapsulate all the Jive methods used, I created the Jivehelp class.

A Jive authorization object is required to manipulate the Jive user. Before an authorized object creates any user, only authorized objects created by the Jive Administrator (user with ID 1) Have the right to manipulate other users. Listing 2 shows how to create a helper class of the Jive authorization object.


Listing 2: Create an administrator authorization object

public static Authorization getLJJiveAdminAuthorization(){  if (m_adminAuth != null)   {     return m_adminAuth;    }  else   {     try      { m_adminAuth =          AuthorizationFactory.getAuthorization( ADMIN_ID, ADMIN_PWD );}     catch( UnauthorizedException ue ) {     System.out.println("Error in getLJJiveAdminAuthorization: "                            + ue.toString());     }  }  return m_adminAuth;}


The code first checks whether the Administrator authorization object already exists, and thenAuthorizationFactoryCreate an administrator authorization object. The Administrator authorization token will be widely used.

The next step is to create a helper class method for a Jive user (see
Listing 3: This code is called using the Administrator authorization tokengetInstance()ObtainForumFactoryInstance. The authorization token passed to the Forum factory determines the access level of the created object in the system. Because the Administrator authorization token is used, all the Jive objects can be fully accessed. The proxy manages all users and groups. In this example, it is used to create users and set user attributes. And return the newly created user object.


Listing 3: Create a Jive user

public User createForumUser(String name, String username,                              String password, String email) {    ProfileManager profileManager = null;    ForumFactory forumFactory = null;    User newUser = null;    forumFactory =       ForumFactory.getInstance(ForumHelper.getLJJiveAdminAuthorization());     try {              if (forumFactory != null)       {         profileManager = forumFactory.getProfileManager();         newUser = profileManager.createUser(username,password,email);         newUser.setName( name );         newUser.setEmailVisible(true);         newUser.setNameVisible(true);       }      }    catch( UserAlreadyExistsException uaee )     {           System.out.println("Error in createUser: " + uaee.toString());       System.out.println("User is: " + username);    }    catch( UnauthorizedException ue )     {       System.out.println("Error in createUser: " + ue.toString());    }    return newUser; }


Listing 4 shows the helper method for getting a Jiv e user. This Code creates an authorization token for the retrieved user. This authorization token contains a user ID because the proxy uses this ID to retrieve the Jive user object. Note that this method contains a proxy that uses the Administrator authorization token.


Listing 4: Get a Jive user

public User getUser(String userName, String password) {    ProfileManager profileManager = null;    ForumFactory forumFactory = null;    User user = null;          Authorization userAuth = getUserAuthorization(userName, password);    if (userAuth == null)    {       return null;    }    forumFactory =       ForumFactory.getInstance(ForumHelper.getLJJiveAdminAuthorization());    try {              if (forumFactory != null)       {          profileManager = forumFactory.getProfileManager();          user = profileManager.getUser( userAuth.getUserID() );       }      }    catch( UserNotFoundException unfe ) {      System.out.println("Error in getUser: " + unfe.toString());    }          return user; }


Back to Top

Combine them

At lockerjock, we use servlets to manage applications, data beans to store data, and JSP file display data.

In the log on to the server programForumHelperAnd call
getUser()
To pass the name and password obtained from the lockerjock user. If the return value of the Jive user object is not empty, it stores other data beans in the session process, as shown in listing 5.


Listing 5: Add a Jive user to a session

ForumHelper forumHelper = new ForumHelper(); User user = forumHelper.getUser(userBean.getScreenName(),                                 userBean.getPassword());if (user != null){   session.putValue(LockerjockConstants.SESSION_JIVE_USER, user);}


I used three modified Jive JSP files:viewForum.jsp,
viewMessage.jspAndpost.jsp. Because the default JSP file provided by Jive allows unauthorized users to access it as a special anonymous user. Because we require all Forum users to be lockerjock users, I modified the JSPs of Jive to check sessions and confirm that both Jive users and lockerjock exist. Only when two users exist at the same time can the user access the forum. This code is provided in the six types in the list.


Listing 6: controlling access from the JSP file of the Jive Forum

<% User jiveUser     = (User)session.getValue(LockerjockConstants.LOCKERJOCK_JIVE_USER); UserBean userBean     = (UserBean)session.getValue(LockerjockConstants.LOCKERJOCK_USER_BEAN);     if (jiveUser != null && userBean != null) {   // do forum stuff, yadayadayada }%>


Unlike the default Jive JSP file, we control the access to the Forum based on the part that lockerjock users can access, rather than the access permission of Forum users. For example, a user can access a forum in a community only when a user is a member of a community. The user's Jive license is subject to the lockerjock license. Therefore, the expected Forum object is based on our administrator authorization rather than user authorization. At this point, if the user is not authorized to the Forum, the default JiveJSP file will be redirected to an error page. However, we do not need to redirect because management-level users are always authorized. If not
The authorized users of lockerjock are running
The code in listing 7 is redirected. This code retrieves the user name from the Forum object.


Listing 7: getting the Forum name

<%   String pForumID = request.getParameter("forum");      // get the forumID, create the forum object   int forumID =     ((pForumID!=null&&!pForumID.equals(""))?(Integer.parseInt(pForumID)):-1);   ForumFactory forumFactory =      ForumHelper.getForumFactory(ForumHelper.getLJJiveAdminAuthorization());    Forum forum = null;   String forumName = null;   try {        if (forumFactory != null)           forum = forumFactory.getForum(forumID);    }    catch( UnauthorizedException ue )     {      //default Jive forum JSPs redirect to an error page if      //the forum factory doesn't have permission to get this forum    }    catch( ForumNotFoundException fnfe )     {     }   if (forum != null)     forumName = forum.getName();%>


Jive also fascinated me by the cool feature of creating forums at any time.
Listing 8 shows a simple and surprising way to create a forum. This method only obtains a forum factory with an administrator authorization token, and then callscreateForum()Method. When a user creates a new topic community, this method can be used to create a new community forum.


Listing 8: creating a forum at any time

public Forum createForum(String name, String description) {    ForumFactory forumFactory = null;    Forum newForum = null;           try     {       if( description == null )         description = "";                    forumFactory =          ForumFactory.getInstance(ForumHelper.getLJJiveAdminAuthorization());              if (forumFactory != null)         newForum = forumFactory.createForum( name, description );    }    catch( UnauthorizedException ue ) {       System.out.println("Error in createForum: " + ue.toString());    }    catch( ForumAlreadyExistsException fae ) {       System.out.println("Error in createForum: " + fae.toString());    }    return newForum; }


Back to Top

Summary

Jive provides an open source code and 100% Java forum software package for the Web community. As shown, it can be applied directly or modified and expanded to suit various needs. Jive developers are so friendly, beneficial, and efficient. In fact, many new features, such as search, have been introduced since I used Jive. The only problem is to spend some time learning and using them. I encourage you to use Jive!



References

  • You can refer to
    Original English.

  • Learn more about
    Knowledge of Jive and download the code.

  • For more information about JSP encoding, see
    JSP technology-friends or enemies?

  • For more information about JSP technology, see the tutorial provided by developerWorks.
    JSP technology entry.

  • Tutorials provided by developerWorks
    Create a Java HTTP Servlet and learn how to compile a simple HTTP Servlet.

About the author

Jay Allen is a software engineer at a large computer company.
Lockerjock.com works with several other websites. You can use
Allenj@us.ibm.com contact Jay.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.