001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.connectionstate;
019:
020: import java.net.Socket;
021:
022: import javax.swing.SwingUtilities;
023: import javax.swing.event.ChangeEvent;
024: import javax.swing.event.ChangeListener;
025: import javax.swing.event.EventListenerList;
026:
027: /**
028: * Default implementation for ConnectionState.
029: *
030: * @author javaprog
031: * @author fdietz
032: */
033: public class ConnectionStateImpl implements ConnectionState {
034: protected boolean online = false;
035:
036: protected EventListenerList listenerList = new EventListenerList();
037:
038: protected ChangeEvent e;
039:
040: private static ConnectionStateImpl instance;
041:
042: protected String connectionName;
043:
044: protected int connectionPort;
045:
046: public ConnectionStateImpl() {
047: e = new ChangeEvent(this );
048: }
049:
050: public void checkPhysicalState() {
051: if (connectionName != null) {
052: try {
053: final Socket testSocket = new Socket(connectionName,
054: connectionPort);
055: testSocket.close();
056: setOnline(true);
057: } catch (final Exception exception) {
058: setOnline(false);
059: }
060: }
061: }
062:
063: public static ConnectionStateImpl getInstance() {
064: if (instance == null) {
065: instance = new ConnectionStateImpl();
066: }
067:
068: return instance;
069: }
070:
071: public void addChangeListener(final ChangeListener l) {
072: listenerList.add(ChangeListener.class, l);
073: }
074:
075: public synchronized boolean isOnline() {
076: return online;
077: }
078:
079: public void removeChangeListener(final ChangeListener l) {
080: listenerList.remove(ChangeListener.class, l);
081: }
082:
083: public synchronized void setOnline(final boolean b) {
084: if (online != b) {
085: online = b;
086: SwingUtilities.invokeLater(new Runnable() {
087: public void run() {
088: notifyListener();
089: }
090: });
091: }
092: }
093:
094: void notifyListener() {
095: final Object[] listeners = listenerList.getListenerList();
096:
097: // Process the listeners last to first, notifying
098: // those that are interested in this event
099: for (int i = listeners.length - 2; i >= 0; i -= 2) {
100: if (listeners[i] == ChangeListener.class) {
101: ((ChangeListener) listeners[i + 1]).stateChanged(e);
102: }
103: }
104:
105: }
106:
107: public void setTestConnection(final String name, final int port) {
108: connectionName = name;
109: connectionPort = port;
110: }
111: }
|