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 public void execute() throws MojoExecutionException, MojoFailureException
68 {
69 if (stopPort <= 0)
70 throw new MojoExecutionException("Please specify a valid port");
71 if (stopKey == null)
72 throw new MojoExecutionException("Please specify a valid stopKey");
73
74 try
75 {
76 Socket s=new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
77 s.setSoLinger(false, 0);
78
79 OutputStream out=s.getOutputStream();
80 out.write((stopKey+"\r\nstop\r\n").getBytes());
81 out.flush();
82
83 if (stopWait > 0)
84 {
85 s.setSoTimeout(stopWait * 1000);
86 s.getInputStream();
87
88 System.err.printf("Waiting %d seconds for jetty to stop%n",stopWait);
89 LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
90 String response;
91 boolean stopped = false;
92 while (!stopped && ((response = lin.readLine()) != null))
93 {
94 if ("Stopped".equals(response))
95 {
96 stopped = true;
97 System.err.println("Server reports itself as Stopped");
98 }
99 }
100 }
101 s.close();
102 }
103 catch (ConnectException e)
104 {
105 getLog().info("Jetty not running!");
106 }
107 catch (Exception e)
108 {
109 getLog().error(e);
110 }
111 }
112
113 public int getStopPort() {
114 return stopPort;
115 }
116
117 public void setStopPort(int stopPort) {
118 this.stopPort = stopPort;
119 }
120
121 public String getStopKey() {
122 return stopKey;
123 }
124
125 public void setStopKey(String stopKey) {
126 this.stopKey = stopKey;
127 }
128 }