View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.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   * JettyStopMojo - stops a running instance of jetty.
34   * The ff are required:
35   * -DstopKey=someKey
36   * -DstopPort=somePort
37   * 
38   * @goal stop
39   * @description Stops jetty that is configured with <stopKey> and <stopPort>.
40   */
41  
42  public class JettyStopMojo extends AbstractMojo
43  {
44      
45      /**
46       * Port to listen to stop jetty on sending stop command
47       * @parameter
48       * @required
49       */
50      protected int stopPort;
51      
52      /**
53       * Key to provide when stopping jetty on executing java -DSTOP.KEY=<stopKey> 
54       * -DSTOP.PORT=<stopPort> -jar start.jar --stop
55       * @parameter
56       * @required
57       */
58      protected String stopKey;
59      
60      /**
61       * Max time in seconds that the plugin will wait for confirmation that jetty has stopped.
62       * @parameter
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 }