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.OutputStream;
22  import java.net.ConnectException;
23  import java.net.InetAddress;
24  import java.net.Socket;
25  
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  
30  /**
31   * JettyStopMojo - stops a running instance of jetty.
32   * The ff are required:
33   * -DstopKey=someKey
34   * -DstopPort=somePort
35   * 
36   * @goal stop
37   * @description Stops jetty that is configured with <stopKey> and <stopPort>.
38   */
39  
40  public class JettyStopMojo extends AbstractMojo
41  {
42      
43      /**
44       * Port to listen to stop jetty on sending stop command
45       * @parameter
46       * @required
47       */
48      protected int stopPort;
49      
50      /**
51       * Key to provide when stopping jetty on executing java -DSTOP.KEY=<stopKey> 
52       * -DSTOP.PORT=<stopPort> -jar start.jar --stop
53       * @parameter
54       * @required
55       */
56      protected String stopKey;
57  
58      public void execute() throws MojoExecutionException, MojoFailureException 
59      {
60          if (stopPort <= 0)
61              throw new MojoExecutionException("Please specify a valid port"); 
62          if (stopKey == null)
63              throw new MojoExecutionException("Please specify a valid stopKey");  
64  
65          try
66          {        
67              Socket s=new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
68              s.setSoLinger(false, 0);
69              
70              OutputStream out=s.getOutputStream();
71              out.write((stopKey+"\r\nstop\r\n").getBytes());
72              out.flush();
73              s.close();
74          }
75          catch (ConnectException e)
76          {
77              getLog().info("Jetty not running!");
78          }
79          catch (Exception e)
80          {
81              getLog().error(e);
82          }
83      }
84  
85      public int getStopPort() {
86          return stopPort;
87      }
88  
89      public void setStopPort(int stopPort) {
90          this.stopPort = stopPort;
91      }
92  
93      public String getStopKey() {
94          return stopKey;
95      }
96  
97      public void setStopKey(String stopKey) {
98          this.stopKey = stopKey;
99      }
100 }