View Javadoc

1   //========================================================================
2   //$Id: JettyStopMojo.java 4005 2008-11-06 22:31:53Z janb $
3   //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mortbay.jetty.plugin;
17  
18  import java.io.OutputStream;
19  import java.net.ConnectException;
20  import java.net.InetAddress;
21  import java.net.Socket;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  
27  /**
28   * 
29   * @author David Yu
30   * 
31   * @goal stop
32   * @description Stops jetty that is configured with <stopKey> and <stopPort>.
33   */
34  
35  public class JettyStopMojo extends AbstractMojo
36  {
37      
38      /**
39       * Port to listen to stop jetty on sending stop command
40       * @parameter
41       * @required
42       */
43      protected int stopPort;
44      
45      /**
46       * Key to provide when stopping jetty on executing java -DSTOP.KEY=<stopKey> 
47       * -DSTOP.PORT=<stopPort> -jar start.jar --stop
48       * @parameter
49       * @required
50       */
51      protected String stopKey;
52  
53      public void execute() throws MojoExecutionException, MojoFailureException 
54      {
55          if (stopPort <= 0)
56              throw new MojoExecutionException("Please specify a valid port"); 
57          if (stopKey == null)
58              throw new MojoExecutionException("Please specify a valid stopKey");  
59  
60          try
61          {        
62              Socket s=new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
63              s.setSoLinger(false, 0);
64              
65              OutputStream out=s.getOutputStream();
66              out.write((stopKey+"\r\nstop\r\n").getBytes());
67              out.flush();
68              s.close();
69          }
70          catch (ConnectException e)
71          {
72              getLog().info("Jetty not running!");
73          }
74          catch (Exception e)
75          {
76              getLog().error(e);
77          }
78      }
79  
80  }