KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jtests > conform > util > ProcessStopper


1 /**
2  * Copyright (C) 2005 - Red Hat, Inc. All rights reserved.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: ProcessStopper.java,v 1.3 2005/02/11 10:12:59 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package org.objectweb.carol.jtests.conform.util;
30
31 import java.io.BufferedWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStreamWriter JavaDoc;
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.net.Socket JavaDoc;
36 import java.net.UnknownHostException JavaDoc;
37 import java.util.Arrays JavaDoc;
38
39 /**
40  * @author Vadim Nasardinov (vadimn@redhat.com)
41  * @since 2005-01-04
42  * @see ProcessStopper
43  */

44 public class ProcessStopper {
45
46     /**
47      * No default constructor, utility class
48      */

49     private ProcessStopper() {
50
51     }
52
53     /**
54      * Main method
55      * @param args arguments of the program
56      * @throws IOException
57      */

58     public static void main(String JavaDoc[] args) {
59         if (args.length != 1) {
60             throw new IllegalArgumentException JavaDoc("expected the port number, but got: " + Arrays.asList(args));
61         }
62         final int port = Integer.parseInt(args[0]);
63         final String JavaDoc host = "localhost";
64
65         final Socket JavaDoc socket;
66         try {
67             socket = new Socket JavaDoc(host, port);
68         } catch (UnknownHostException JavaDoc ex) {
69             throw new RuntimeException JavaDoc("no such host: " + host, ex);
70         } catch (IOException JavaDoc ex) {
71             System.err.println("ProcessStopper: couldn't connect to " + host + ":" + port);
72             return;
73         }
74
75         final BufferedWriter JavaDoc writer;
76
77         try {
78             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(socket.getOutputStream(), ProcessRunner.STREAM_ENCODING));
79         } catch (UnsupportedEncodingException JavaDoc ex) {
80             throw new RuntimeException JavaDoc("can't happen", ex);
81         } catch (IOException JavaDoc ex) {
82             throw new RuntimeException JavaDoc(ex);
83         }
84
85         try {
86             writer.write(ProcessRunner.SHUTDOWN_COMMAND);
87             writer.newLine();
88             writer.flush();
89             socket.close();
90         } catch (IOException JavaDoc ex) {
91             throw new RuntimeException JavaDoc("Error sending a shutdown command", ex);
92         }
93     }
94 }
Popular Tags