Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23b68a1c4c5808164e2a2279832a7453998cb614 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.ant;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.eclipse.jetty.ant.utils.TaskLog;

/**
 * JettyStopTask
 *
 *
 */
public class JettyStopTask extends Task
{

    private int stopPort;
    
    private String stopKey;
    
    private int stopWait;
    
    
    
    /**
     * 
     */
    public JettyStopTask()
    {
        TaskLog.setTask(this);
    }

    /** 
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException
    {
        try
        {        
            Socket s = new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
            if (stopWait > 0)
                s.setSoTimeout(stopWait*1000);
            try
            {
                OutputStream out = s.getOutputStream();
                out.write((stopKey + "\r\nstop\r\n").getBytes());
                out.flush();

                if (stopWait > 0)
                {
                    TaskLog.log("Waiting"+(stopWait > 0 ? (" "+stopWait+"sec") : "")+" for jetty to stop");
                    LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
                    String response=lin.readLine();
                    if ("Stopped".equals(response))
                        System.err.println("Stopped");
                }
            }
            finally
            {
                s.close();
            }  
        }
        catch (ConnectException e)
        {
            TaskLog.log("Jetty not running!");
        }
        catch (Exception e)
        {
            TaskLog.log(e.getMessage());
        }
    }

    public int getStopPort() 
    {
        return stopPort;
    }

    public void setStopPort(int stopPort) 
    {
        this.stopPort = stopPort;
    }

    public String getStopKey() 
    {
        return stopKey;
    }

    public void setStopKey(String stopKey) 
    {
        this.stopKey = stopKey;
    }

    public int getStopWait()
    {
        return stopWait;
    }

    public void setStopWait(int stopWait)
    {
        this.stopWait = stopWait;
    }
    
    
}

Back to the top