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.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      
68      
69      
70  
71      public void execute() throws MojoExecutionException, MojoFailureException 
72      {
73          if (stopPort <= 0)
74              throw new MojoExecutionException("Please specify a valid port"); 
75          if (stopKey == null)
76              throw new MojoExecutionException("Please specify a valid stopKey");  
77  
78          //Ensure jetty Server instance stops. Whether or not the remote process
79          //also stops depends whether or not it was started with ShutdownMonitor.exitVm=true
80          String command = "forcestop"; 
81         
82          try
83          {        
84              Socket s=new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
85              s.setSoLinger(false, 0);
86  
87              OutputStream out=s.getOutputStream();
88              out.write((stopKey+"\r\n"+command+"\r\n").getBytes());
89              out.flush();
90  
91              if (stopWait > 0)
92              {      
93                  s.setSoTimeout(stopWait * 1000);
94                  s.getInputStream();
95  
96                  getLog().info("Waiting "+stopWait+" seconds for jetty to stop");
97                  LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
98                  String response;
99                  boolean stopped = false;
100                 while (!stopped && ((response = lin.readLine()) != null))
101                 {
102                     if ("Stopped".equals(response))
103                     {
104                         stopped = true;
105                         getLog().info("Server reports itself as stopped");
106                     }                
107                 }
108             }
109             s.close();
110         }
111         catch (ConnectException e)
112         {
113             getLog().info("Jetty not running!");
114         }
115         catch (Exception e)
116         {
117             getLog().error(e);
118         }
119     }
120 
121     public int getStopPort() {
122         return stopPort;
123     }
124 
125     public void setStopPort(int stopPort) {
126         this.stopPort = stopPort;
127     }
128 
129     public String getStopKey() {
130         return stopKey;
131     }
132 
133     public void setStopKey(String stopKey) {
134         this.stopKey = stopKey;
135     }
136 }