KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > common > ShutdownHook


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.jfox.ioc.common;
5
6 import org.jfox.ioc.ComponentContext;
7 import org.jfox.ioc.Registry;
8 import org.jfox.ioc.annotation.Managable;
9 import org.jfox.ioc.ext.ActiveComponent;
10 import org.jfox.ioc.ext.ManagableComponent;
11
12 /**
13  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
14  */

15
16 public class ShutdownHook extends AbstractService implements ManagableComponent, ActiveComponent{
17     private Registry registry = null;
18     private Thread JavaDoc shutdownThread = null;
19
20     protected void doInit() throws Exception JavaDoc {
21         shutdownThread = new Thread JavaDoc(this);
22         shutdownThread.setName(getName());
23
24     }
25
26     protected void doStart() throws Exception JavaDoc {
27         Runtime.getRuntime().addShutdownHook(shutdownThread);
28     }
29
30     protected void doStop() throws Exception JavaDoc {
31         // if shutdown thread is working, can not remove it
32
if(!shutdownThread.isAlive()) {
33             Runtime.getRuntime().removeShutdownHook(shutdownThread);
34         }
35     }
36
37     protected void doDestroy() throws Exception JavaDoc {
38         shutdownThread = null;
39     }
40
41     public void run() {
42         logger.info("stopping system.");
43         try {
44             registry.stop();
45         }
46         catch(Exception JavaDoc e) {
47             e.printStackTrace();
48         }
49
50     }
51
52     @Managable
53     public void stopSystem() {
54         Thread JavaDoc t = new Thread JavaDoc(this);
55         t.start();
56
57         try {
58             t.join();
59 // Thread.sleep(2000L);
60
// System.exit(0);
61
}
62         catch(Exception JavaDoc e){
63
64         }
65
66     }
67
68     public void setComponentContext(ComponentContext ctx) {
69         super.setComponentContext(ctx);
70         registry = ctx.getRegistry();
71     }
72
73     public static void main(String JavaDoc[] args) {
74
75     }
76 }
77
78
Popular Tags