View Javadoc

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