001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of Granite Data Services.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or modify
008     *   it under the terms of the GNU Library General Public License as published by
009     *   the Free Software Foundation; either version 2 of the License, or (at your
010     *   option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful, but
013     *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014     *   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015     *   for more details.
016     *
017     *   You should have received a copy of the GNU Library General Public License
018     *   along with this library; if not, see <http://www.gnu.org/licenses/>.
019     */
020    package org.granite.client.messaging.transport;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.granite.client.configuration.Configuration;
026    import org.granite.client.messaging.transport.TransportStatusHandler.LogEngineStatusHandler;
027    import org.granite.client.messaging.transport.TransportStatusHandler.NoopEngineStatusHandler;
028    
029    /**
030     * @author Franck WOLFF
031     */
032    public abstract class AbstractTransport<C> implements Transport {
033    
034            private volatile C context;
035            private volatile Configuration config;
036            private volatile TransportStatusHandler statusHandler = new LogEngineStatusHandler();
037            
038            protected final List<TransportStopListener> stopListeners = new ArrayList<TransportStopListener>();
039            
040            @SuppressWarnings("unchecked")
041            @Override
042            public void setContext(Object context) {
043                    this.context = (C)context;
044            }
045    
046            @Override
047            public C getContext() {
048                    return context;
049            }
050    
051            @Override
052            public void setConfiguration(Configuration config) {
053                    this.config = config;
054            }
055    
056            @Override
057            public Configuration getConfiguration() {
058                    return config;
059            }
060    
061            @Override
062            public void setStatusHandler(TransportStatusHandler statusHandler) {
063                    if (statusHandler == null)
064                            statusHandler = new NoopEngineStatusHandler();
065                    this.statusHandler = statusHandler;
066            }
067    
068            @Override
069            public TransportStatusHandler getStatusHandler() {
070                    return statusHandler;
071            }
072    
073            @Override
074            public void addStopListener(TransportStopListener listener) {
075                    synchronized (stopListeners) {
076                            if (!stopListeners.contains(listener))
077                                    stopListeners.add(listener);
078                    }
079            }
080    
081            @Override
082            public boolean removeStopListener(TransportStopListener listener) {
083                    synchronized (stopListeners) {
084                            return stopListeners.remove(listener);
085                    }
086            }
087    
088            @Override
089            public void stop() {
090                    synchronized (stopListeners) {
091                            for (TransportStopListener listener : stopListeners) {
092                                    try {
093                                            listener.onStop(this);
094                                    }
095                                    catch (Exception e) {
096                                    }
097                            }
098                            stopListeners.clear();
099                    }
100            }
101    }