View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * JettyStopTask
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       * @see org.apache.tools.ant.Task#execute()
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 }