001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 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    
021    package org.granite.client.messaging.transport;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.granite.client.configuration.Configuration;
027    import org.granite.client.messaging.transport.TransportStatusHandler.LogEngineStatusHandler;
028    import org.granite.client.messaging.transport.TransportStatusHandler.NoopEngineStatusHandler;
029    
030    /**
031     * @author Franck WOLFF
032     */
033    public abstract class AbstractTransport implements Transport {
034    
035            private Configuration config;
036            private TransportStatusHandler statusHandler = new LogEngineStatusHandler();
037            
038            private final List<TransportStopListener> stopListeners = new ArrayList<TransportStopListener>();
039            
040            @Override
041            public void setConfiguration(Configuration config) {
042                    this.config = config;
043            }
044    
045            @Override
046            public Configuration getConfiguration() {
047                    return config;
048            }
049    
050            @Override
051            public void setStatusHandler(TransportStatusHandler statusHandler) {
052                    if (statusHandler == null)
053                            statusHandler = new NoopEngineStatusHandler();
054                    this.statusHandler = statusHandler;
055            }
056    
057            @Override
058            public TransportStatusHandler getStatusHandler() {
059                    return statusHandler;
060            }
061    
062            @Override
063            public void addStopListener(TransportStopListener listener) {
064                    synchronized (stopListeners) {
065                            if (!stopListeners.contains(listener))
066                                    stopListeners.add(listener);
067                    }
068            }
069    
070            @Override
071            public boolean removeStopListener(TransportStopListener listener) {
072                    synchronized (stopListeners) {
073                            return stopListeners.remove(listener);
074                    }
075            }
076    
077            @Override
078            public void stop() {
079                    synchronized (stopListeners) {
080                            for (TransportStopListener listener : stopListeners) {
081                                    try {
082                                            listener.onStop(this);
083                                    }
084                                    catch (Exception e) {
085                                    }
086                            }
087                            stopListeners.clear();
088                    }
089            }
090    }