View Javadoc

1   /**
2    * Copyright (C) 2010-2012 Joerg Bellmann <joerg.bellmann@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * TODO Comment.
28   * 
29   * @author Joerg Bellmann
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  }