1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.ant;
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.tools.ant.BuildException;
29 import org.apache.tools.ant.Task;
30 import org.eclipse.jetty.ant.utils.TaskLog;
31
32
33
34
35
36
37 public class JettyStopTask extends Task
38 {
39
40 private int stopPort;
41
42 private String stopKey;
43
44 private int stopWait;
45
46
47
48
49
50
51 public JettyStopTask()
52 {
53 TaskLog.setTask(this);
54 }
55
56
57
58
59 public void execute() throws BuildException
60 {
61 try
62 {
63 Socket s = new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
64 if (stopWait > 0)
65 s.setSoTimeout(stopWait*1000);
66 try
67 {
68 OutputStream out = s.getOutputStream();
69 out.write((stopKey + "\r\nstop\r\n").getBytes());
70 out.flush();
71
72 if (stopWait > 0)
73 {
74 TaskLog.log("Waiting"+(stopWait > 0 ? (" "+stopWait+"sec") : "")+" for jetty to stop");
75 LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
76 String response=lin.readLine();
77 if ("Stopped".equals(response))
78 System.err.println("Stopped");
79 }
80 }
81 finally
82 {
83 s.close();
84 }
85 }
86 catch (ConnectException e)
87 {
88 TaskLog.log("Jetty not running!");
89 }
90 catch (Exception e)
91 {
92 TaskLog.log(e.getMessage());
93 }
94 }
95
96 public int getStopPort()
97 {
98 return stopPort;
99 }
100
101 public void setStopPort(int stopPort)
102 {
103 this.stopPort = stopPort;
104 }
105
106 public String getStopKey()
107 {
108 return stopKey;
109 }
110
111 public void setStopKey(String stopKey)
112 {
113 this.stopKey = stopKey;
114 }
115
116 public int getStopWait()
117 {
118 return stopWait;
119 }
120
121 public void setStopWait(int stopWait)
122 {
123 this.stopWait = stopWait;
124 }
125
126
127 }