1 //========================================================================
2 //$Id: JettyStopMojo.java 5222 2009-05-29 07:34:32Z dyu $
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 * JettyStopMojo - stops a running instance of jetty.
29 * The ff are required:
30 * -DstopKey=someKey
31 * -DstopPort=somePort
32 *
33 * @author David Yu
34 *
35 * @goal stop
36 * @description Stops jetty that is configured with <stopKey> and <stopPort>.
37 */
38
39 public class JettyStopMojo extends AbstractMojo
40 {
41
42 /**
43 * Port to listen to stop jetty on sending stop command
44 * @parameter
45 * @required
46 */
47 protected int stopPort;
48
49 /**
50 * Key to provide when stopping jetty on executing java -DSTOP.KEY=<stopKey>
51 * -DSTOP.PORT=<stopPort> -jar start.jar --stop
52 * @parameter
53 * @required
54 */
55 protected String stopKey;
56
57 public void execute() throws MojoExecutionException, MojoFailureException
58 {
59 if (stopPort <= 0)
60 throw new MojoExecutionException("Please specify a valid port");
61 if (stopKey == null)
62 throw new MojoExecutionException("Please specify a valid stopKey");
63
64 try
65 {
66 Socket s=new Socket(InetAddress.getByName("127.0.0.1"),stopPort);
67 s.setSoLinger(false, 0);
68
69 OutputStream out=s.getOutputStream();
70 out.write((stopKey+"\r\nstop\r\n").getBytes());
71 out.flush();
72 s.close();
73 }
74 catch (ConnectException e)
75 {
76 getLog().info("Jetty not running!");
77 }
78 catch (Exception e)
79 {
80 getLog().error(e);
81 }
82 }
83
84 }