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;
023
024import de.novanic.eventservice.config.ConfigurationDependentFactory;
025import de.novanic.eventservice.config.EventServiceConfiguration;
026import de.novanic.eventservice.service.connection.id.ConnectionIdGenerator;
027import de.novanic.eventservice.service.registry.EventRegistryFactory;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpSession;
031
032/**
033 * The EventExecutorServiceFactory is used to create the EventExecutorService and to ensure that only one instance of
034 * EventExecutorServiceFactory and EventExecutorService exists (singleton).
035 * @see EventExecutorService
036 *
037 * @author sstrohschein
038 * <br>Date: 20.07.2008
039 * <br>Time: 14:26:03
040 */
041public class EventExecutorServiceFactory
042{
043    /**
044     * The EventExecutorServiceFactory should be created via the getInstance method.
045     * @see EventExecutorServiceFactory#getInstance()
046     */
047    private EventExecutorServiceFactory() {}
048
049    /**
050     * Factory-Holder class to ensure thread-safe lazy-loading with IODH.
051     */
052    private static class EventExecutorServiceFactoryHolder {
053        private static final EventExecutorServiceFactory INSTANCE = new EventExecutorServiceFactory();
054    }
055
056    /**
057     * This method should be used to create an instance of EventExecutorServiceFactory.
058     * EventExecutorServiceFactory is a singleton, so this method returns always the same instance of
059     * EventExecutorServiceFactory.
060     * @return EventExecutorServiceFactory (singleton)
061     */
062    public static EventExecutorServiceFactory getInstance() {
063        return EventExecutorServiceFactoryHolder.INSTANCE;
064    }
065
066    /**
067     * This method should be used to create an instance of EventExecutorService.
068     * The session is needed to generate the client/user id.
069     *
070     * @deprecated Please use {@link de.novanic.eventservice.service.EventExecutorServiceFactory#getEventExecutorService(javax.servlet.http.HttpServletRequest)} instead
071     * because a request is necessary instead of a session to support multiple sessions. This method will work like before, but multiple sessions will not be support
072     * when it is configured.
073     *
074     * @param aHttpSession the session is needed to generate the client/user id
075     * @return EventExecutorService
076     */
077    public EventExecutorService getEventExecutorService(final HttpSession aHttpSession) {
078        String theClientId = null;
079        if(aHttpSession != null) {
080            theClientId = aHttpSession.getId();
081        }
082        return getEventExecutorService(theClientId);
083    }
084
085    /**
086     * This method should be used to create an instance of EventExecutorService.
087     * The session is needed to generate the client/user id.
088     * @param aRequest a request / session is needed to generate the client/user id
089     * @return EventExecutorService
090     */
091    public EventExecutorService getEventExecutorService(HttpServletRequest aRequest) {
092        String theConnectionId = null;
093        if(aRequest != null) {
094            EventServiceConfiguration theConfiguration = EventRegistryFactory.getInstance().getEventRegistry().getConfiguration();
095
096            ConnectionIdGenerator theConnectionIdGenerator = ConfigurationDependentFactory.getInstance(theConfiguration).getConnectionIdGenerator();
097            theConnectionId = theConnectionIdGenerator.getConnectionId(aRequest);
098        }
099        return getEventExecutorService(theConnectionId);
100    }
101
102    /**
103     * This method should be used to create an instance of EventExecutorService.
104     * The EventExecutorService can also be created with a request.
105     * @see EventExecutorServiceFactory#getEventExecutorService(javax.servlet.http.HttpServletRequest)
106     * @param aClientId the client/user id
107     * @return EventExecutorService
108     */
109    public EventExecutorService getEventExecutorService(String aClientId) {
110        return new DefaultEventExecutorService(aClientId);
111    }
112}