1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
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
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 }