go4it

just do it

Jive学习(三)ForumFactory的工厂、代理模式和单态模式

1.ForumFactory工厂

/**
 * $RCSfile: ForumFactory.java,v $
 * $Revision: 1.9 $
 * $Date: 2001/09/30 20:17:06 $
 *
 * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.jivesoftware.forum;

import java.lang.reflect.*;
import java.util.*;

/**
 * A ForumFactory provides access to and management of Forums. It is the point
 * of entry for the entire Jive system.
 * <p>
 * A concrete instance of ForumFactory can be obtained by calling the getInstance()
 * method with an Authorization token. The Authorization token determines with
 * what permissions the rest of the objects in the system will be accessed with.
 * <p>
 * Usually the first steps of any program interacting with the Jive system are:
 * <ul>
 *   <li> Obtain an authorization token by calling
 *    AuthorizationFactory.getInstance().getAuthorization(username, password);
 *   <li> Use that authorization to get a ForumFactory instance.
 *   <li> Use the forum factory to access forums and other Jive content.
 * </ul>
 * It is also possible to access Jive content with anonymous permissions. See
 * the AuthorizationFactory class for more information.
 * <p>
 * ForumFactory is an abstract class so that the actual implementation is
 * pluggable. For example, the default Jive implementation uses a database
 * backend. You can optionally plug in your own backend that might use the
 * filesystem, for example. When first creating the forum factory, Jive will
 * look for the Jive property "ForumFactory.className". If it fails to find
 * that property, it will use the default class.
 *
 * @see AuthorizationFactory
 */
public abstract class ForumFactory {

    private static Object initLock = new Object();
    private static String className = "com.jivesoftware.forum.database.DbForumFactory";
    private static ForumFactory factory = null;

    /**
     * Returns a concrete ForumFactory instance. Permissions corresponding
     * to the Authorization will be used. If getting the factory fails, null
     * will be returned.
     *
     * @param authorization the auth token for the user.
     * @return a concrete ForumFactory instance.
     */
    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("Jive Forums Basic", "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 normally.");
                        e.printStackTrace();
                        return null;
                    }
                }
            }
        }
        //Now, create a forum factory proxy.
        return new ForumFactoryProxy(authorization, factory,
                factory.getPermissions(authorization));
    }
   。。。。。。
}
 
代理工厂----实现访问权限控制
/**
 * $RCSfile: ForumFactoryProxy.java,v $
 * $Revision: 1.5 $
 * $Date: 2001/08/16 18:35:28 $
 *
 * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.jivesoftware.forum;

import java.util.Iterator;
import java.util.ArrayList;

/**
 * A protection proxy for ForumFactory. It ensures that only authorized users
 * are allowed to access restricted methods.
 */
public class ForumFactoryProxy extends ForumFactory {

    protected ForumFactory factory;
    protected Authorization authorization;
    protected ForumPermissions permissions;

    public ForumFactoryProxy(Authorization authorization, ForumFactory factory,
            ForumPermissions permissions)
    {
        this.factory = factory;
        this.authorization = authorization;
        this.permissions = permissions;
    }

    public Forum createForum(String name, String description)
            throws UnauthorizedException, ForumAlreadyExistsException
    {
        if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
            Forum newForum = factory.createForum(name, description);
            return new ForumProxy(newForum, authorization, permissions);
        }
        else {
            throw new UnauthorizedException();
        }
    }
  /*******************************DBForumFactory里的方法:****************************
 public ForumPermissions getPermissions(Authorization authorization) {
        long userID = authorization.getUserID();

        return permissionsManager.getFinalUserPerms(-1, userID);
    }
************************************************************************************

   。。。。。。   
}
 
ForumFactory进一步对权限细化:
/**
 * $RCSfile: ForumProxy.java,v $
 * $Revision: 1.5 $
 * $Date: 2001/08/15 09:42:57 $
 *
 * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.jivesoftware.forum;

import com.jivesoftware.forum.gateway.GatewayManager;

import java.util.Date;
import java.util.Iterator;
import java.util.Enumeration;

/**
 * A protection proxy for Forums. A proxy has a set of permissions that are
 * specified at creation time of the proxy. Subsequently, those permissions
 * are use to restrict access to protected Forum methods. If a user does
 * not have the right to execute a particular method, an UnauthorizedException
 * is thrown.
 *
 * @see Forum
 * @see ForumPermissions
 * @see UnauthorizedException
 */
public class ForumProxy implements Forum {

    private Forum forum;
    private Authorization authorization;
    private ForumPermissions permissions;

    /**
     * Creates a new ForumProxy object.
     *
     * @param forum the forum to protect by proxy
     * @param authorization the user's authorization token
     * @param permissions the permissions to use with this proxy.
     */
    public ForumProxy(Forum forum, Authorization authorization,
            ForumPermissions permissions)
    {
        this.forum = forum;
        this.authorization = authorization;
        this.permissions = permissions;
    }
。。。。。。
      public void setCreationDate(Date creationDate)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin()) {
            forum.setCreationDate(creationDate);
        }
        else {
            throw new UnauthorizedException();
        }
    }

   
    public void setModerationDefaultThreadValue(int value)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.MODERATE_THREADS))
        {
            forum.setModerationDefaultThreadValue(value);
        }
        else {
            throw new UnauthorizedException();
        }
    }

       public void setModerationDefaultMessageValue(int value)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.MODERATE_MESSAGES))
        {
            forum.setModerationDefaultMessageValue(value);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public void setModerationMinThreadValue(int value)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.MODERATE_THREADS))
        {
            forum.setModerationMinThreadValue(value);
        }
        else {
            throw new UnauthorizedException();
        }
    }

       public void setModerationMinMessageValue(int value)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.MODERATE_MESSAGES))
        {
            forum.setModerationMinMessageValue(value);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public void setProperty(String name, String value)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin()) {
            forum.setProperty(name, value);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public void deleteProperty(String name)
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin()) {
            forum.deleteProperty(name);
        }
        else {
            throw new UnauthorizedException();
        }
    }

       public void deleteThread(ForumThread thread) throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.MODERATE_THREADS))
        {
            forum.deleteThread(thread);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public void moveThread(ForumThread thread, Forum newForum) throws
            UnauthorizedException, IllegalArgumentException
    {
        // If the user is an admin or moderator of both forums
        boolean forum1Perm = permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.MODERATE_THREADS);
        boolean forum2Perm = newForum.hasPermission(ForumPermissions.SYSTEM_ADMIN) ||
                newForum.hasPermission(ForumPermissions.FORUM_ADMIN) ||
                newForum.hasPermission(ForumPermissions.MODERATE_THREADS);
        if (forum1Perm && forum2Perm)
        {
            forum.moveThread(thread, newForum);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public void addThread(ForumThread thread) throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin() ||
                permissions.get(ForumPermissions.CREATE_THREAD))
        {
            forum.addThread(thread);
        }
        else {
            throw new UnauthorizedException("Create thread permissions are " +
                "necessary to perform this task"
            );
        }
    }

 
    public Iterator popularThreads() {
        Iterator iterator = forum.popularThreads();
        return new IteratorProxy(JiveGlobals.THREAD, iterator, authorization, permissions);
    }

    public Iterator messages() {
        Iterator iterator = forum.messages();
        return new IteratorProxy(JiveGlobals.MESSAGE, iterator, authorization, permissions);
    }

    public Iterator messages(ResultFilter resultFilter) {
        Iterator iterator = forum.messages(resultFilter);
        return new IteratorProxy(JiveGlobals.MESSAGE, iterator, authorization, permissions);
    }

   
    public Query createQuery() {
        return new QueryProxy(forum.createQuery(), authorization, permissions);
    }

    public FilterManager getFilterManager() {
        return new FilterManagerProxy(forum.getFilterManager(), authorization,
                permissions);
    }

    public GatewayManager getGatewayManager() throws UnauthorizedException {
        if (permissions.isSystemOrForumAdmin()) {
            return forum.getGatewayManager();
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public PermissionsManager getPermissionsManager()
            throws UnauthorizedException
    {
        if (permissions.isSystemOrForumAdmin()) {
            return new PermissionsManagerProxy(forum.getPermissionsManager(),
                authorization, permissions);
        }
        else {
            throw new UnauthorizedException();
        }
    }
 }

posted on 2009-04-11 23:42  cxccbv  阅读(495)  评论(0)    收藏  举报

导航