All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src-html.com.pusher.client.util.Factory.html Maven / Gradle / Ivy




Source code



001package com.pusher.client.util;
002
003import java.net.Proxy;
004import java.net.URI;
005import java.net.URISyntaxException;
006import java.util.concurrent.ExecutorService;
007import java.util.concurrent.Executors;
008import java.util.concurrent.ScheduledExecutorService;
009import java.util.concurrent.ThreadFactory;
010
011import javax.net.ssl.SSLException;
012
013import org.java_websocket.client.WebSocketClient;
014
015import com.pusher.client.Authorizer;
016import com.pusher.client.PusherOptions;
017import com.pusher.client.channel.impl.ChannelImpl;
018import com.pusher.client.channel.impl.ChannelManager;
019import com.pusher.client.channel.impl.PresenceChannelImpl;
020import com.pusher.client.channel.impl.PrivateChannelImpl;
021import com.pusher.client.connection.impl.InternalConnection;
022import com.pusher.client.connection.websocket.WebSocketClientWrapper;
023import com.pusher.client.connection.websocket.WebSocketConnection;
024import com.pusher.client.connection.websocket.WebSocketListener;
025
026/**
027 * This is a lightweight way of doing dependency injection and enabling classes
028 * to be unit tested in isolation. No class in this library instantiates another
029 * class directly, otherwise they would be tightly coupled. Instead, they all
030 * call the factory methods in this class when they want to create instances of
031 * another class.
032 *
033 * An instance of Factory is provided on construction to each class which may
034 * require it, the initial factory is instantiated in the Pusher constructor,
035 * the only constructor which a library consumer should need to call directly.
036 *
037 * Conventions:
038 *
039 * - any method that starts with "new", such as
040 * {@link #newPublicChannel(String)} creates a new instance of that class every
041 * time it is called.
042 *
043 * - any method that starts with "get", such as {@link #getEventQueue()} returns
044 * a singleton. These are lazily constructed and their access methods should be
045 * synchronized for this reason.
046 */
047public class Factory {
048
049    private InternalConnection connection;
050    private ChannelManager channelManager;
051    private ExecutorService eventQueue;
052    private ScheduledExecutorService timers;
053
054    public synchronized InternalConnection getConnection(final String apiKey, final PusherOptions options) {
055        if (connection == null) {
056            try {
057                connection = new WebSocketConnection(options.buildUrl(apiKey), options.getActivityTimeout(),
058                        options.getPongTimeout(), options.getProxy(), this);
059            }
060            catch (final URISyntaxException e) {
061                throw new IllegalArgumentException("Failed to initialise connection", e);
062            }
063        }
064        return connection;
065    }
066
067    public WebSocketClient newWebSocketClientWrapper(final URI uri, final Proxy proxy, final WebSocketListener webSocketListener) throws SSLException {
068        return new WebSocketClientWrapper(uri, proxy, webSocketListener);
069    }
070
071    public synchronized ExecutorService getEventQueue() {
072        if (eventQueue == null) {
073            eventQueue = Executors.newSingleThreadExecutor(new DaemonThreadFactory("eventQueue"));
074        }
075        return eventQueue;
076    }
077
078    public synchronized ScheduledExecutorService getTimers() {
079        if (timers == null) {
080            timers = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory("timers"));
081        }
082        return timers;
083    }
084
085    public ChannelImpl newPublicChannel(final String channelName) {
086        return new ChannelImpl(channelName, this);
087    }
088
089    public PrivateChannelImpl newPrivateChannel(final InternalConnection connection, final String channelName,
090            final Authorizer authorizer) {
091        return new PrivateChannelImpl(connection, channelName, authorizer, this);
092    }
093
094    public PresenceChannelImpl newPresenceChannel(final InternalConnection connection, final String channelName,
095            final Authorizer authorizer) {
096        return new PresenceChannelImpl(connection, channelName, authorizer, this);
097    }
098
099    public synchronized ChannelManager getChannelManager() {
100        if (channelManager == null) {
101            channelManager = new ChannelManager(this);
102        }
103        return channelManager;
104    }
105
106    public synchronized void shutdownThreads() {
107        if (eventQueue != null) {
108            eventQueue.shutdown();
109            eventQueue = null;
110        }
111        if (timers != null) {
112            timers.shutdown();
113            timers = null;
114        }
115    }
116
117    private static class DaemonThreadFactory implements ThreadFactory {
118        private final String name;
119
120        public DaemonThreadFactory(final String name) {
121            this.name = name;
122        }
123
124        @Override
125        public Thread newThread(final Runnable r) {
126            final Thread t = new Thread(r);
127            t.setDaemon(true);
128            t.setName("pusher-java-client " + name);
129            return t;
130        }
131    }
132}
































































© 2015 - 2025 Weber Informatics LLC | Privacy Policy