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.io.BufferedReader;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  
28  import com.googlecode.t7mp.steps.Context;
29  import com.googlecode.t7mp.util.SystemUtil;
30  import com.googlecode.t7mp.util.TomcatUtil;
31  
32  /**
33   * 
34   * Stops the forked Tomcat instance.
35   * 
36   * @goal stop-forked
37   * @requiresDependencyResolution runtime
38   * 
39   *
40   */
41  public class StopForkedMojo extends AbstractT7TomcatMojo {
42  
43      private static final long SLEEPTIME = 3000;
44  
45      private PluginLog log;
46      private Context context;
47  
48      @Override
49      public void execute() throws MojoExecutionException, MojoFailureException {
50          context = buildParentContext();
51          this.log = new MavenPluginLog(this.getLog());
52          log.info("running t7:stop-forked ...");
53  
54          DefaultMavenPluginContext mavenPluginContext = new DefaultMavenPluginContext(context, this);
55  
56          List<InstanceConfiguration> configurations = InstanceConfigurationUtil.createInstanceConfigurations(mavenPluginContext);
57  
58          for (InstanceConfiguration configuration : configurations) {
59              setStartScriptPermissions(TomcatUtil.getBinDirectory(new File(configuration.getCatalinaBasePath())));
60              ProcessBuilder processBuilder = new ProcessBuilder(getStopSkriptCommand());
61              int exitValue = -1;
62              try {
63                  File binDirectory = TomcatUtil.getBinDirectory(new File(configuration.getCatalinaBasePath()));
64                  Process p = processBuilder.directory(binDirectory).start();
65                  InputStream is = p.getInputStream();
66                  BufferedReader br = new BufferedReader(new InputStreamReader(is));
67                  String line;
68                  while ((line = br.readLine()) != null) {
69                      log.info(line);
70                  }
71                  exitValue = p.waitFor();
72              } catch (InterruptedException e) {
73                  e.printStackTrace();
74              } catch (IOException e) {
75                  e.printStackTrace();
76              }
77              try {
78                  Thread.sleep(SLEEPTIME);
79              } catch (InterruptedException e) {
80                  log.error(e.getMessage(), e);
81              }
82              log.info("Exit-Value ForkedTomcatShutdownHook " + exitValue);
83          }
84      }
85  
86      protected String[] getStopSkriptCommand() {
87          if (SystemUtil.isWindowsSystem()) {
88              return new String[] {"cmd", "/c", "catalina.bat", "stop"};
89          } else {
90              return new String[] {"./catalina.sh", "stop"};
91          }
92      }
93  
94      protected void setStartScriptPermissions(File binDirectory) {
95          log.debug("set permissions ...");
96          if (SystemUtil.isWindowsSystem()) {
97              // do we have filepermissions on windows
98              return;
99          }
100         ProcessBuilder processBuilder = new ProcessBuilder("chmod", "755", "catalina.sh", "setclasspath.sh", "startup.sh", "shutdown.sh");
101         processBuilder.directory(binDirectory);
102         processBuilder.redirectErrorStream(true);
103         int exitValue = -1;
104         try {
105             Process p = processBuilder.start();
106             exitValue = p.waitFor();
107         } catch (InterruptedException e) {
108             getLog().error(e.getMessage(), e);
109             throw new TomcatSetupException(e.getMessage(), e);
110         } catch (IOException e) {
111             getLog().error(e.getMessage(), e);
112             throw new TomcatSetupException(e.getMessage(), e);
113         }
114         log.debug("SetStartScriptPermission return value " + exitValue);
115     }
116 
117 }