View Javadoc

1   package com.googlecode.t7mp;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.PrintStream;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.apache.catalina.startup.Bootstrap;
10  
11  import com.google.common.collect.Lists;
12  import com.googlecode.t7mp.scanner.Scanner;
13  import com.googlecode.t7mp.scanner.ScannerSetup;
14  import com.googlecode.t7mp.steps.CopyJuliJarStep;
15  import com.googlecode.t7mp.steps.StepSequence;
16  import com.googlecode.t7mp.util.CatalinaOutPrintStream;
17  
18  /**
19   * Holds a {@link Bootstrap} instance (Tomcat-Instance).
20   * 
21   * @author Joerg Bellmann
22   *
23   */
24  public class BootstrapHolder {
25  
26      // the tomcat
27      private Bootstrap bootstrap;
28  
29      private CatalinaOutPrintStream catalinaOutPrintStream;
30      private PluginLog log;
31      private T7Configuration configuration;
32  
33      public void startBootstrapInstance(MavenPluginContext pluginContext) {
34          this.log = pluginContext.getLog();
35          this.configuration = pluginContext.getConfiguration();
36          List<Stoppable> stoppables = Lists.newArrayList();
37          getSetupStepSequence().execute(pluginContext);
38  
39          PrintStream originalSystemErr = System.err;
40  
41          bootstrap = getBootstrap();
42          log.info("Starting Tomcat ...");
43          try {
44              File catalinaout = new File(configuration.getCatalinaBase(), "/logs/catalina.out");
45              CatalinaOutPrintStream catalinaOutputStream = new CatalinaOutPrintStream(originalSystemErr, new FileOutputStream(catalinaout),
46                      configuration.isSuspendConsoleOutput());
47              System.setErr(catalinaOutputStream);
48              bootstrap.init();
49              final BootstrapShutdownHook shutdownHook = new BootstrapShutdownHook();
50              List<Stoppable> stoppableScanner = ScannerSetup.configureScanners(shutdownHook, configuration, log);
51              stoppables.addAll(stoppableScanner);
52                  bootstrap.start();
53                  Runtime.getRuntime().addShutdownHook(shutdownHook);
54                  log.info("Tomcat started");
55          } catch (Exception e) {
56              throw new RuntimeException(e.getMessage(), e);
57          }
58          stoppables.add(new StoppableBootstrapAdapter(bootstrap));
59          pluginContext.getMojo().getPluginContext().put(AbstractT7BaseMojo.T7_BOOTSTRAP_CONTEXT_ID, stoppables);
60      }
61      
62      protected StepSequence getSetupStepSequence() {
63          StepSequence seq = new MavenTomcatSetupSequence();
64          seq.add(new CopyJuliJarStep());
65          return seq;
66      }
67  
68      protected Bootstrap getBootstrap() {
69          return new Bootstrap();
70      }
71  
72      
73      
74      /**
75       * ShutdownHook for stopping the {@link Bootstrap} instance.
76       * 
77       * @author jbellmann
78       *
79       */
80      final class BootstrapShutdownHook extends Thread implements ShutdownHook {
81  
82          private static final int SLEEPTIME = 1000;
83          private final List<Scanner> scanners = new ArrayList<Scanner>();
84  
85          @Override
86          public void addScanner(Scanner scanner) {
87              this.scanners.add(scanner);
88          }
89  
90          @Override
91          public void run() {
92              log.info("Stopping Tomcat ...");
93              stopScanners();
94              if (bootstrap != null) {
95                  try {
96                      bootstrap.stop();
97                      bootstrap = null;
98                      log.info("Tomcat stopped");
99                      Thread.sleep(SLEEPTIME);
100                 } catch (Exception e) {
101                     log.error(e.getMessage(), e);
102                 }
103             }
104             if (catalinaOutPrintStream != null) {
105                 catalinaOutPrintStream.flush();
106                 catalinaOutPrintStream.close();
107                 System.setErr(catalinaOutPrintStream.getOriginalSystemErr());
108             }
109         }
110 
111         @Override
112         public void stopScanners() {
113             for (Scanner scanner : scanners) {
114                 scanner.stop();
115             }
116         }
117     }
118 
119 }