AuthProviderFactory.java 4.93 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18
package org.jivesoftware.messenger.auth;

import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.JiveGlobals;

/**
Matt Tucker's avatar
Matt Tucker committed
19 20 21
 * Provides a centralized source of the various auth providers.<p>
 *
 * The auth system has one provider. The provider allows you to
Matt Tucker's avatar
Matt Tucker committed
22 23 24 25 26 27 28
 * integrate Messenger with various backend authenication systems without
 * necessarily replacing the Messenger user management system.
 * In other words, you can verify usernames and passwords against a common
 * user directory, but allow messenger to manage copies of the user account
 * data in it's own database. This results in a redundant storage of data
 * and can cause 'data skew' where values are not updated in sync. However,
 * it is the simplest and least amount of work to integrate Messenger with
Matt Tucker's avatar
Matt Tucker committed
29 30 31
 * existing authentication systems.<p>
 *
 * Users of Jive that wish to change the AuthProvider implementation
Matt Tucker's avatar
Matt Tucker committed
32 33 34 35 36
 * used to generate users can set the <code>AuthProvider.className</code>
 * Jive property. For example, if you have altered Jive to use LDAP for
 * user information, you'd want to send a custom implementation of
 * AuthProvider classes to make LDAP authetnication queries. After changing the
 * <code>AuthProvider.className</code> Jive property, you must restart
Matt Tucker's avatar
Matt Tucker committed
37 38 39
 * Messenger. The valid properties are:<ul>
 *      <li>AuthProvider.className - specifies an AuthProvider class.</li>
 *      <li>GroupProvider.className - specifies a GroupProvider class.</li>
Matt Tucker's avatar
Matt Tucker committed
40 41 42 43 44
 * </ul>
 *
 * @author Iain Shigeoka
 */
public class AuthProviderFactory {
Matt Tucker's avatar
Matt Tucker committed
45

Matt Tucker's avatar
Matt Tucker committed
46 47 48 49
    /**
     * The default class to instantiate is database implementation.
     */
    private static String authClassName =
50
            "org.jivesoftware.messenger.auth.spi.DbAuthProvider";
Matt Tucker's avatar
Matt Tucker committed
51
    private static String groupClassName =
52
            "org.jivesoftware.messenger.auth.spi.DbGroupProvider";
Matt Tucker's avatar
Matt Tucker committed
53 54 55 56 57

    private static AuthProvider authProvider = null;
    private static GroupProvider groupProvider = null;

    /**
Matt Tucker's avatar
Matt Tucker committed
58 59 60 61 62 63 64
     * Returns a concrete AuthProvider. By default, the implementation
     * used will be an instance of DbAuthProvider -- the standard database
     * implementation that uses the Jive user table. A different authProvider
     * can be specified by setting the Jive property "AuthProvider.className".
     * However, you must restart Jive for any change to take effect.
     *
     * @return an AuthProvider instance.
Matt Tucker's avatar
Matt Tucker committed
65 66 67 68 69 70 71 72
     */
    public static AuthProvider getAuthProvider() {
        if (authProvider == null) {
            // Use className as a convenient object to get a lock on.
            synchronized (authClassName) {
                if (authProvider == null) {
                    //See if the classname has been set as a Jive property.
                    String classNameProp =
73
                            JiveGlobals.getProperty("AuthProvider.className");
Matt Tucker's avatar
Matt Tucker committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                    if (classNameProp != null) {
                        authClassName = classNameProp;
                    }
                    try {
                        Class c = ClassUtils.forName(authClassName);
                        authProvider = (AuthProvider)c.newInstance();
                    }
                    catch (Exception e) {
                        Log.error("Exception loading class: " + authClassName, e);
                    }
                }
            }
        }
        return authProvider;
    }

    /**
     * <p>Obtains a concrete GroupProvider.<p>
     * <p/>
     * <p>By default, the implementation used will be an instance
     * of DbGroupProvider -- the standard database implementation
     * that uses the Jive group table. A different GroupProvider can be
     * specified by setting the Jive property "GroupProvider.className".
     * However, you must restart Jive for any change to take effect.</p>
     */
    public static GroupProvider getGroupProvider() {
        if (groupProvider == null) {
            // Use className as a convenient object to get a lock on.
            synchronized (groupClassName) {
                if (groupProvider == null) {
                    //See if the classname has been set as a Jive property.
                    String classNameProp =
106
                            JiveGlobals.getProperty("GroupProvider.className");
Matt Tucker's avatar
Matt Tucker committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                    if (classNameProp != null) {
                        groupClassName = classNameProp;
                    }
                    try {
                        Class c = ClassUtils.forName(groupClassName);
                        groupProvider = (GroupProvider)c.newInstance();
                    }
                    catch (Exception e) {
                        Log.error("Exception loading class: " + groupClassName, e);
                    }
                }
            }
        }
        return groupProvider;
    }
}