001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.service.registry.user;
023
024import de.novanic.eventservice.config.EventServiceConfiguration;
025import de.novanic.eventservice.client.config.ConfigurationException;
026
027/**
028 * The UserManagerFactory is used to create the UserManager and to ensure that only one instance of
029 * UserManagerFactory and UserManager exists (singleton).
030 * @see de.novanic.eventservice.service.registry.user.UserManagerFactory
031 *
032 * @author sstrohschein
033 *         <br>Date: 01.02.2009
034 *         <br>Time: 18:17:58
035 */
036public class UserManagerFactory
037{
038    private volatile UserManager myUserManager;
039
040    private UserManagerFactory() {}
041
042    /**
043     * Factory-Holder class to ensure thread-safe lazy-loading with IODH.
044     */
045    private static class UserManagerFactoryHolder {
046        private static UserManagerFactory INSTANCE = new UserManagerFactory();
047    }
048
049    /**
050     * This method should be used to create an instance of UserManagerFactory.
051     * UserManagerFactory is a singleton, so this method returns always the same instance of UserManagerFactory.
052     * @return UserManagerFactory (singleton)
053     */
054    public static UserManagerFactory getInstance() {
055        return UserManagerFactoryHolder.INSTANCE;
056    }
057
058    /**
059     * Returns the {@link de.novanic.eventservice.service.registry.user.UserManager} as a singleton. It is important that
060     * the {@link de.novanic.eventservice.service.registry.user.UserManager} must be initiated first. That can be done by
061     * creating a {@link de.novanic.eventservice.service.registry.user.UserManager} with a configuration:
062     * {@link de.novanic.eventservice.service.registry.user.UserManagerFactory#getUserManager(de.novanic.eventservice.config.EventServiceConfiguration)}
063     * or {@link de.novanic.eventservice.service.registry.user.UserManagerFactory#getUserManager(long)}. When the
064     * {@link de.novanic.eventservice.service.registry.user.UserManager} isn't initiated the method throws a
065     * {@link de.novanic.eventservice.client.config.ConfigurationException}.
066     * @return {@link de.novanic.eventservice.service.registry.user.UserManager} (singleton)
067     * @throws ConfigurationException thrown when the {@link de.novanic.eventservice.service.registry.user.UserManager} isn't
068     * initiated first with {@link de.novanic.eventservice.service.registry.user.UserManagerFactory#getUserManager(de.novanic.eventservice.config.EventServiceConfiguration)}
069     * or {@link de.novanic.eventservice.service.registry.user.UserManagerFactory#getUserManager(long)}
070     */
071    public UserManager getUserManager() {
072        if(myUserManager != null) {
073            return myUserManager;
074        }
075        throw new ConfigurationException("The UserManager isn't configured! It is necessary to create an instance of " +
076                "UserManager first. That can be done by calling a factory method of UserManagerFactory with a configuration.");
077    }
078
079    /**
080     * Returns the {@link de.novanic.eventservice.service.registry.user.UserManager} as a singleton.
081     * @param aConfiguration {@link EventServiceConfiguration} used to read the timeout time/interval.
082     * @return {@link de.novanic.eventservice.service.registry.user.UserManager} (singleton)
083     */
084    public UserManager getUserManager(EventServiceConfiguration aConfiguration) {
085        return getUserManager(aConfiguration.getTimeoutTime());
086    }
087
088    /**
089     * Returns the {@link de.novanic.eventservice.service.registry.user.UserManager} as a singleton.
090     * @param aTimeoutInterval timeout interval (is only required if the {@link de.novanic.eventservice.service.registry.user.UserActivityScheduler}
091     * needs to be started).
092     * @return {@link de.novanic.eventservice.service.registry.user.UserManager} (singleton)
093     */
094    public UserManager getUserManager(long aTimeoutInterval) {
095        if(myUserManager == null) {
096            synchronized(this) {
097                if(myUserManager == null) {
098                    myUserManager = new DefaultUserManager(aTimeoutInterval);
099                }
100            }
101        }
102        return myUserManager;
103    }
104}