1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.googlecode.t7mp;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.catalina.startup.Bootstrap;
22
23 import com.googlecode.t7mp.scanner.Scanner;
24 import com.googlecode.t7mp.util.CatalinaOutPrintStream;
25
26
27
28
29
30
31
32 public final class TomcatShutdownHook extends Thread implements ShutdownHook {
33
34 private static final int SLEEPTIME = 1500;
35 private Bootstrap bootstrap;
36 private final List<Scanner> scanners = new ArrayList<Scanner>();
37 private final CatalinaOutPrintStream catalinaOutPrintStream;
38 private final PluginLog log;
39
40 public TomcatShutdownHook(Bootstrap bootstrap, CatalinaOutPrintStream catalinaOutPrintStream, PluginLog log) {
41 this.bootstrap = bootstrap;
42 this.catalinaOutPrintStream = catalinaOutPrintStream;
43 this.log = log;
44 }
45
46 @Override
47 public void addScanner(Scanner scanner) {
48 this.scanners.add(scanner);
49 }
50
51 @Override
52 public void run() {
53 log.info("Stopping Tomcat ...");
54 stopScanners();
55 if (bootstrap != null) {
56 try {
57 bootstrap.stop();
58 bootstrap = null;
59 log.info("Tomcat stopped");
60 Thread.sleep(SLEEPTIME);
61 } catch (Exception e) {
62 log.error(e.getMessage(), e);
63 }
64 }
65 if (catalinaOutPrintStream != null) {
66 catalinaOutPrintStream.flush();
67 catalinaOutPrintStream.close();
68 System.setErr(catalinaOutPrintStream.getOriginalSystemErr());
69 }
70 }
71
72 @Override
73 public void stopScanners() {
74 for (Scanner scanner : scanners) {
75 scanner.stop();
76 }
77 }
78 }