JIVE source code explores the exploration of the JIVE source code, has always been engaged in many friends, although many people have also put forward different criticisms on JIVE itself, but individuals think that learning Jive's source code is a shortcut to Java programming, due to I have limited great levels of JIVE research, so I hope that all of you want to make different opinions. Here is the analysis of part of the code, including three common design patterns of Java: Factory Proxy and SINGLTON hopes that the following analysis has a certain help for your study. Next, first need to understand the global configuration page: global.jsp
<% @ Page Import = "java.util. *, com.jivesoftware.util. *, com.jivesoftware.forum. *, com.jivesoftware.forum.util. *"%>
<% // check to see if a jive authorization token exissrs boolean isguest = false; authorization authtoken = Skinutils.getuserauthorization (Request, Response); -------------------------------------------------------------------------------- ------------ (2) IF (authtoken == null) {authtoken = authorizationFactory.GetanonymousAuthorization (); ------------------- ------------- (3) ISGUEST = true;} // init forumfactory and pageuser myenv.registeuserinit (authtoken); ---------------- ----------------- (4)
User Pageuser = myenv.getpageuseuser ();
// the last time the user visited this page date (Skinutils.getlastvisited (Request, Response)); ------------------------ ---------- (5)
// The number of message a user wants to show per page int userMessageRange = MyEnv.du.getMessageRange (Request, Response, Pageuser); --------------------- ----- (6)
%> (1) JavaBean: MyEnv will call each page during the entire forum run, so it is defined here to define it in Application. (2) The getUserauThorization method in skinutils is used. If you enter its source code, it will find that it first obtains user information in the session. If the user is just entering the forum, verify the user through the user's cookie. (3) If the authtoken returns, this user is a visit, then assigns the corresponding validation level to the user through the GetAnonymousAuthorization method, and the ISGUEST flag is TRUE. (4) in registeUserInit () method will be initialized forumFactory wherein obtain an instance of an object of ForumFactoryProxy, public void registeUserInit (Authorization authToken) {try {this.forumFactory = ForumFactory.getInstance (authToken); ---> where in fact, the object is returned ForumFactoryProxy this.pageUser = this.forumFactory.getUserManager () getUser (authToken.getUserID ());.} catch (Exception ex) {}} registeUserInit can be seen on the inside by a method to obtain a getInstance method ForumFactoryProxy Objects, in fact, the Factory parameter in the ForumFactoryProxy is a COM.JIVESoftware.forum.Database.dbForumFactory type under default. Therefore, the Factory operating in the getUserManager () in ForumFacrotyProxy is the object of the default dbforumFactory. The GetUserManager () method will return a userManagerProxy object and then return a UserProxy object through its getUser () method to the Pageuser, and finally pass this pageUser to the Pageuser in Global.jsp by myenv.getpageuser (). (5) You can get the last time you visit by saving information in Skinutils.GetlastVisInd (Request, Response).
(6) You can customize the number of Message displayed per page in MyEnv.du.getMessageRange (Request, Response, Pageuser).
Analysis: In this study, we will find three design modes here: 1. Factory model 2, proxy mode 3, single mode
The factory model mentioned here is a general concept (the abstract factory and factory method are classified). In order to clarify, we will analyze the Myenv.registeuserinit (Authtokeen) of the comment (4) as an entrance. In the registeuserinit method, we saw a ForumFactory.getInstance (Authtoken) method for returning a ForumFactory, so we would like to analyze it: We know that ForumFactory is an Abstract class, and the only way to make specific implementations. The static GetInstance method. public static ForumFactory getInstance (Authorization authorization) {// If no valid authorization passed in, return null if (authorization == null) {return null;.} if (factory == null) {synchronized (initLock) {if (factory = = NULL) {// Note, The Software License Expressely forbids // Tampering with this check. //licenseManager.validateLicense, "2.0");
String classNameProp = JiveGlobals.getJiveProperty ( "ForumFactory.className"); if (classNameProp = null!) {ClassName = classNameProp;}. Try {// Load the class and create an instance Class c = Class.forName (className); factory = (Forumfactory) c.newinstance ();} catch (exception e) {system.err.println ("Failed to load forumfactory class" classname ". Jive Cannot Function Normal."); E.PrintStackTrace (); return NULL;}}}} // NOW, CREATE A Forum Factory Proxy. Return New ForumFactoryProxy (Authorization, Factory, Factory.getPermissions);} The above-mentioned 3 models mentioned above, first GetInstanc The E method itself is in the factory mode, and the ForumFactory class is through this GetInstance method to get a proxy factory. In the method, we also found synchronous object initlock Since its presence makes the following creation of the Factory instance process, only the synchronous access mode of one person is allowed to access by one person, and the single-state mode is also reflected here. Next, we will enter this synchronization module to see, first we see JIVEGLOBALS.GETJIVEPROPERTY ("forumfactory.classname") In order to be able to explore what we have to enter JIVEGLOBALS to see how the getJiveProperty method is how Get these jive's Property.
First, we see there are a loadProperties inside getJiveProperty method () method: private synchronized static void loadProperties () {// If jiveHome is still null, no outside process has set it and // we have to attempt to load the value from jive_init.properties, // which must be in the classpath if (jiveHome == null) {jiveHome = new InitPropLoader () getJiveHome ();.}. // Create a manager with the full path to the xml config file properties. = New XMLProperties (JiveHome File.seParetor Jive_Config_FileName);
} First GetJiveHome () in the loadproperties method, get the path to the corresponding configuration file, and then get the properties within the corresponding configuration file by creating an object of the XMLProperties class. (Methods of various SET, GET, and DELETE) of the attributes existing in the XML document are provided in the XMLProperties class. Back to the synchronization module! The next thing is clearer, first pass class.Forname () to get the corresponding class of the corresponding classes from the configuration, then shape it to ForumFactory, finally jump out of the synchronization module, see Return a ForumFactoryProxy tape The object of participation. Note the second parameter Factory, in fact, this factory has been mentioned above (4) above, in its default, actually a DBForumFactory type object. When you need product forum in the ForumFactoryProxy class, you will call Method CreateForum (), in this method we only pay attention to: if (permissions.get (forumpermissions.system_admin) {forum newforum = factory.createforum (name, description); Return New ForumProxy (Newforum, Authorization, Permissions); In this IF, Factory.CreateForum (Name, Description) returns a manufactured Forum. Note: We have found this familiar Factory Yes here! This factory is the Factory that has just been mentioned above, and the default, it is an object that repeatedly mentioned DBForumFactory types. At this time we were surprised, found that the Forum we operated now is DBForumFactory. (The DBForumFactory class is a class that inherits the self-ABSTRACT ForumFactory class to perform direct operations in the forum relative to the database, including various methods such as finding Forum, Message. OK! In the code, you can always find the direction, but fainting. Now we jump out of the design mode perspective: Factory mode: We have found a lot of Factory, including ForumFactory, ForumFactoryProxy, DBforumFactory; here forumfactory as an abstract parent class by ForumFactoryProxy and DBForumFactory, so ForumFactory is actually an abstract factory, which is actually two roles: 1, declaration method 2, through the unique implementation method GetInstance () to obtain a real work; Specific implementation, the last product (such as Forum, Message) will be produced in these real work plants. In fact, in the JIVE design, it is not only here, like AuthorizationFactory, DbauthorizationFactory These are also the use of factory models in JIVE.