1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.maven.plugin;
20
21 import java.io.InputStreamReader;
22 import java.io.LineNumberReader;
23 import java.io.OutputStream;
24 import java.net.ConnectException;
25 import java.net.InetAddress;
26 import java.net.Socket;
27
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31
32
33
34
35
36
37
38
39
40
41
42 public class JettyStopMojo extends AbstractMojo
43 {
44
45
46
47
48
49
50 protected int stopPort;
51
52
53
54
55
56
57
58 protected String stopKey;
59
60
61
62
63
64 protected int stopWait;
65
66
67
68
69
70
71 public void execute() throws MojoExecutionException, MojoFailureException
72 {
73 if (stopPort <= 0)
74 throw new MojoExecutionException("Please specify a valid port");
75 if (stopKey == null)
76 throw new MojoExecutionException("Please specify a valid stopKey");
77
78
79
80 String command = "forcestop";
81
82 try
83 {
84 Socket s=new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
85 s.setSoLinger(false, 0);
86
87 OutputStream out=s.getOutputStream();
88 out.write((stopKey+"\r\n"+command+"\r\n").getBytes());
89 out.flush();
90
91 if (stopWait > 0)
92 {
93 s.setSoTimeout(stopWait * 1000);
94 s.getInputStream();
95
96 getLog().info("Waiting "+stopWait+" seconds for jetty to stop");
97 LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
98 String response;
99 boolean stopped = false;
100 while (!stopped && ((response = lin.readLine()) != null))
101 {
102 if ("Stopped".equals(response))
103 {
104 stopped = true;
105 getLog().info("Server reports itself as stopped");
106 }
107 }
108 }
109 s.close();
110 }
111 catch (ConnectException e)
112 {
113 getLog().info("Jetty not running!");
114 }
115 catch (Exception e)
116 {
117 getLog().error(e);
118 }
119 }
120
121 public int getStopPort() {
122 return stopPort;
123 }
124
125 public void setStopPort(int stopPort) {
126 this.stopPort = stopPort;
127 }
128
129 public String getStopKey() {
130 return stopKey;
131 }
132
133 public void setStopKey(String stopKey) {
134 this.stopKey = stopKey;
135 }
136 }