KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > connectionstate > ConnectionStateImpl


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.connectionstate;
19
20 import java.net.Socket JavaDoc;
21
22 import javax.swing.SwingUtilities JavaDoc;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import javax.swing.event.EventListenerList JavaDoc;
26
27 /**
28  * Default implementation for ConnectionState.
29  *
30  * @author javaprog
31  * @author fdietz
32  */

33 public class ConnectionStateImpl implements ConnectionState {
34     protected boolean online = false;
35
36     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
37
38     protected ChangeEvent JavaDoc e;
39
40     private static ConnectionStateImpl instance;
41
42     protected String JavaDoc connectionName;
43
44     protected int connectionPort;
45
46     public ConnectionStateImpl() {
47         e = new ChangeEvent JavaDoc(this);
48     }
49
50     public void checkPhysicalState() {
51         if (connectionName != null) {
52             try {
53                 final Socket JavaDoc testSocket = new Socket JavaDoc(connectionName, connectionPort);
54                 testSocket.close();
55                 setOnline(true);
56             } catch (final Exception JavaDoc exception) {
57                 setOnline(false);
58             }
59         }
60     }
61
62     public static ConnectionStateImpl getInstance() {
63         if (instance == null) {
64             instance = new ConnectionStateImpl();
65         }
66
67         return instance;
68     }
69
70     public void addChangeListener(final ChangeListener JavaDoc l) {
71         listenerList.add(ChangeListener JavaDoc.class, l);
72     }
73
74     public synchronized boolean isOnline() {
75         return online;
76     }
77
78     public void removeChangeListener(final ChangeListener JavaDoc l) {
79         listenerList.remove(ChangeListener JavaDoc.class, l);
80     }
81
82     public synchronized void setOnline(final boolean b) {
83         if (online != b) {
84             online = b;
85             SwingUtilities.invokeLater(new Runnable JavaDoc() {
86                 public void run() {
87                     notifyListener();
88                 }
89              });
90         }
91     }
92
93         void notifyListener() {
94             final Object JavaDoc[] listeners = listenerList.getListenerList();
95             
96             // Process the listeners last to first, notifying
97
// those that are interested in this event
98
for (int i = listeners.length - 2; i >= 0; i -= 2) {
99                 if (listeners[i] == ChangeListener JavaDoc.class) {
100                     ((ChangeListener JavaDoc) listeners[i + 1]).stateChanged(e);
101                 }
102             }
103             
104             
105         }
106         
107     public void setTestConnection(final String JavaDoc name, final int port) {
108         connectionName = name;
109         connectionPort = port;
110     }
111 }
Popular Tags