UserProviderFactory.java 4.72 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package org.jivesoftware.messenger.user;

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

/**
 * <p>Provides a centralized source of the various user providers.</p>
 * <p/>
 * <p>The user system has many providers. These providers allow you to
 * integrate Messenger with various backend user management systems on a
 * 'pay as you go' basis. In otherwords, a simple integration only requires
 * a very small amount of customization (modifying a few providers) while
 * a more complex integration can modify many providers.</p>
 * <p/>
 * <p>Users of Jive that wish to change the User*Provider implementation used to generate
 * users can set the <code>UserProvider.*.className</code> Jive properties. For example, if
 * you have altered Jive to use LDAP for user information, you'd want to send a custom
 * implementation of User*Provider classes to make LDAP user queries. After changing the
 * <code>UserProvider.*.className</code> Jive properties, you must restart Messenger. The
 * valid properties are:<p>
 * <p/>
 * <ul>
 * <li>UserProvider.id.className - specifies a UserIDProvider class.</li>
 * <li>UserProvider.properties.className - specifies a UserPropertiesProvider class.</li>
 * <li>UserProvider.info.className - specifies a UserInfoProvider class.</li>
 * <li>UserProvider.account.className - specifies a UserAccountProvider class.</li>
 * </ul>
 *
 * @author Iain Shigeoka
 */
public class UserProviderFactory {

    private static UserPropertiesProvider userPropertiesProvider;
    private static UserInfoProvider userInfoProvider;
    private static RosterItemProvider rosterItemProvider;

    /**
     * The default class to instantiate is database implementation.
     */
52
    private static String[] classNames = {"org.jivesoftware.messenger.user.spi.DbUserPropertiesProvider",
53 54
                                          "org.jivesoftware.messenger.user.spi.DbUserInfoProvider",
                                          "org.jivesoftware.messenger.user.spi.DbRosterItemProvider"};
Matt Tucker's avatar
Matt Tucker committed
55 56 57 58 59 60 61 62

    private static String[] propNames = {"UserProvider.id.className",
                                         "UserProvider.properties.className",
                                         "UserProvider.info.className",
                                         "UserProvider.account.className",
                                         "UserProvider.roster.className"};

    private static void setProviders(Class[] providers) throws IllegalAccessException, InstantiationException {
63 64 65
        userPropertiesProvider = (UserPropertiesProvider)providers[0].newInstance();
        userInfoProvider = (UserInfoProvider)providers[1].newInstance();
        rosterItemProvider = (RosterItemProvider)providers[2].newInstance();
Matt Tucker's avatar
Matt Tucker committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    }

    public static UserPropertiesProvider getUserPropertiesProvider() {
        loadProviders();
        return userPropertiesProvider;
    }

    public static UserInfoProvider getUserInfoProvider() {
        loadProviders();
        return userInfoProvider;
    }

    public static RosterItemProvider getRosterItemProvider() {
        loadProviders();
        return rosterItemProvider;
    }

    private static void loadProviders() {
84
        if (userInfoProvider == null) {
Matt Tucker's avatar
Matt Tucker committed
85 86
            // Use className as a convenient object to get a lock on.
            synchronized (classNames) {
87
                if (userInfoProvider == null) {
Matt Tucker's avatar
Matt Tucker committed
88 89 90 91 92
                    try {
                        Class[] providers = new Class[classNames.length];
                        for (int i = 0; i < classNames.length; i++) {
                            String className = classNames[i];
                            //See if the classname has been set as a Jive property.
93
                            String classNameProp = JiveGlobals.getXMLProperty(propNames[i]);
Matt Tucker's avatar
Matt Tucker committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                            if (classNameProp != null) {
                                className = classNameProp;
                            }
                            try {
                                providers[i] = ClassUtils.forName(className);
                            }
                            catch (Exception e) {
                                Log.error("Exception loading class: " + className, e);
                            }
                        }
                        setProviders(providers);
                    }
                    catch (Exception e) {
                        Log.error("Exception loading class: " + classNames, e);
                    }
                }
            }
        }
    }
}