KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > core > CleanupClass


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

18 package freecs.core;
19
20 import java.util.Iterator JavaDoc;
21 import freecs.*;
22
23 public class CleanupClass extends Thread JavaDoc {
24
25    public CleanupClass () {
26    }
27
28    public void run () {
29        this.setName("CleanupClass");
30        Server.log (this, "starting to clean up", Server.MSG_STATE, Server.LVL_MAJOR);
31        Server.srv.startShutdown ();
32        MessageParser mpr = new MessageParser ();
33        mpr.setMessageTemplate ("message.server.shutdown");
34        Server.log (this, "sending shutdownmessages...", Server.MSG_STATE, Server.LVL_MAJOR);
35        Server.log (this, "Logout users...", Server.MSG_STATE, Server.LVL_MAJOR);
36        UserManager.mgr.sendMessage(mpr);
37        Server.log (this, "Users logged out.", Server.MSG_STATE, Server.LVL_MAJOR);
38        Server.log (this, "Closing all connections...", Server.MSG_STATE, Server.LVL_MAJOR);
39
40        // 30000 millis before the CentralSelector will be forced to stop
41
long killTime = System.currentTimeMillis() + 60000;
42        while (!CentralSelector.stopped) try {
43            if (killTime < System.currentTimeMillis()) {
44                Server.log (this, "CentralSelector didn't shutdown within 60000 millis", Server.MSG_STATE, Server.LVL_VERBOSE);
45                break;
46            }
47            System.out.print (".");
48            Thread.sleep (1000);
49        } catch (InterruptedException JavaDoc ie) {}
50        synchronized (UserManager.mgr.ustr) {
51            for (Iterator JavaDoc i = UserManager.mgr.users (); i.hasNext (); ) try {
52                User u = (User) i.next ();
53                i.remove();
54                u.removeNow();
55            } catch (Exception JavaDoc e) {
56                Server.debug (this, "caused Exception while removing user: ", e, Server.MSG_STATE, Server.LVL_MAJOR);
57            }
58        }
59        Server.log (this, "Shutting down authentication", Server.MSG_STATE, Server.LVL_MAJOR);
60        try {
61            Server.srv.auth.shutdown ();
62        } catch (Exception JavaDoc e) {
63            Server.debug (this, "caused Exception while shutting down authentication: ", e, Server.MSG_STATE, Server.LVL_MAJOR);
64        }
65        Server.log (this, "Final cleanup done. Exiting JVM.", Server.MSG_STATE, Server.LVL_MAJOR);
66    }
67     
68    public String JavaDoc toString () {
69        return "[CleanupClass]";
70    }
71 }
Popular Tags