KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > ConnectInfoManager


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.server;
21
22 import java.util.*;
23
24 import org.lucane.common.*;
25 import org.lucane.common.net.ObjectConnection;
26
27 public class ConnectInfoManager
28 {
29     //-- instance management
30
private static ConnectInfoManager instance = null;
31     public static ConnectInfoManager getInstance()
32     {
33         if(instance == null)
34             instance = new ConnectInfoManager();
35         
36         return instance;
37     }
38     
39     //-- attributes
40
private ArrayList connections;
41     private ConnectInfo serverInfo;
42     
43     /**
44      * Constructor
45      */

46     private ConnectInfoManager()
47     {
48         this.connections = new ArrayList();
49     }
50     
51     /**
52      * Set the server's ConnectInfo
53      *
54      * @param info the server info
55      */

56     public void setServerInfo(ConnectInfo info)
57     {
58         this.serverInfo = info;
59     }
60     
61     /**
62      * Get the server's ConnectInfo
63      *
64      * @return the server info
65      */

66     public ConnectInfo getServerInfo()
67     {
68         return this.serverInfo;
69     }
70     
71     /**
72      * Add a connect info to the list
73      *
74      * @param info the info to add
75      */

76     public void addConnectInfo(ConnectInfo info)
77     {
78         if(info.isWebClient())
79             return;
80         
81         Logging.getLogger().finer("Added ConnectInfo into ConnectInfoManager : " + info);
82         this.connections.add(info);
83     }
84     
85     /**
86      * Remove a ConnectionInfo
87      *
88      * @param info the info to remove
89      */

90     public void removeConnectInfo(ConnectInfo info)
91     {
92         removeConnectInfo(info.getName());
93     }
94     
95     /**
96      * Remove a ConnectionInfo
97      *
98      * @param user the user name to remove
99      */

100     public void removeConnectInfo(String JavaDoc user)
101     {
102         ConnectInfo info = getConnectInfo(user);
103         if(info == null)
104             return;
105         
106         Logging.getLogger().finer("Removed ConnectInfo from ConnectInfoManager : " + info);
107         this.connections.remove(info);
108         
109         //refresh the user list if needed
110
if(info.isClient())
111             Server.getInstance().sendUserListToEveryone();
112     }
113     
114     /**
115      * Get all known connectinfos
116      * @return an iterator of ConnectInfos
117      */

118     public Iterator getAllConnectInfos()
119     {
120         return this.connections.iterator();
121     }
122     
123     /**
124      * Get all client connectinfos
125      *
126      * @return an iterator of ConnectInfos with "Client" as a type
127      */

128     public Iterator getClientConnectInfos()
129     {
130         ArrayList clients = new ArrayList();
131         
132         Iterator i = getAllConnectInfos();
133         while(i.hasNext())
134         {
135             ConnectInfo ci = (ConnectInfo)i.next();
136             if(ci.isClient())
137                 clients.add(ci);
138         }
139         
140         return clients.iterator();
141     }
142     
143     
144     /**
145      * Check if a user is already known
146      *
147      * @param userName the user
148      * @return true or false
149      */

150     public boolean isConnected(String JavaDoc userName)
151     {
152         ConnectInfo ci = getConnectInfo(userName);
153         return ci != null;
154     }
155     
156     
157     /**
158      * Check if a user is already known
159      *
160      * @param ci the user
161      * @return true or false
162      */

163     public boolean isConnected(ConnectInfo ci)
164     {
165         return isConnected(ci.getName());
166     }
167     
168     
169     /**
170      * Get a user connection infos
171      *
172      * @param userName the user
173      * @return the ConnectInfo
174      */

175     public ConnectInfo getConnectInfo(String JavaDoc userName)
176     {
177         Iterator infos = this.connections.iterator();
178         while(infos.hasNext())
179         {
180             ConnectInfo info = (ConnectInfo)infos.next();
181             if (info.getName().equals(userName))
182                 return info;
183         }
184         
185         Logging.getLogger().finer("Unknown ConnectInfo from ConnectInfoManager : " + userName);
186         
187         return null;
188     }
189     
190     /**
191      * Get the complete ConnectInfo
192      *
193      * @param info the basic ConnectInfo
194      * @return the complete ConnectInfo
195      */

196     public ConnectInfo getCompleteConnectInfo(ConnectInfo info)
197     {
198         ConnectInfo complete = getConnectInfo(info.getName());
199         return complete == null ? info : complete;
200     }
201
202     /**
203      * Disconnect every user
204      */

205     public void kickAllUsers()
206     {
207         Iterator clients = getClientConnectInfos();
208         while(clients.hasNext())
209         {
210             ConnectInfo client = (ConnectInfo)clients.next();
211             try {
212                 Map action = new HashMap();
213                 action.put("command", "DISCONNECT");
214                 ObjectConnection oc = Server.getInstance().sendMessageTo(client, "Client", action);
215                 oc.close();
216             } catch (Exception JavaDoc e) {
217                 Logging.getLogger().warning("Unable to kick : " + client.getName());
218             }
219         }
220     }
221 }
Popular Tags