KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > admin > Stop


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Stop.java 2230 2005-10-11 03:14:03Z jcscoobyrs $
44  */

45 package org.openejb.server.admin;
46
47 import java.io.InputStream JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.net.ConnectException JavaDoc;
50 import java.net.Socket JavaDoc;
51 import java.net.URL JavaDoc;
52 import java.util.Properties JavaDoc;
53
54 import org.openejb.util.JarUtils;
55
56 /**
57  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
58  */

59 public class Stop implements org.openejb.client.RequestMethods {
60     private static final String JavaDoc helpBase = "META-INF/org.openejb.cli/";
61     
62     /**
63      *
64      * @param host The ip address the Remote Server is running on
65      * @param port The port the Remote Server is running on
66      */

67     public static void stop(String JavaDoc host, int port) {
68         try{
69             
70         Socket JavaDoc socket = new Socket JavaDoc(host, port);
71         OutputStream JavaDoc out = socket.getOutputStream();
72         
73         out.write( STOP_REQUEST_Stop );
74                 
75         } catch (ConnectException JavaDoc e){
76             System.out.println(":: server not running ::");
77         } catch (Exception JavaDoc e){
78             e.printStackTrace();
79         }
80     }
81
82     public void stop() {
83         stop("localhost",4200);
84     }
85         
86     public static void main(String JavaDoc [] args) {
87         try {
88
89             // The ip address the Remote Server is running on
90
String JavaDoc host = "localhost";
91
92             // The port the Remote Server is running on
93
int port = 4200;
94
95             for (int i=0; i < args.length; i++){
96                 if (args[i].equals("-h")){
97                     if (args.length > i+1 ) {
98                         host = args[++i];
99                     }
100                 } else if (args[i].equals("-p")){
101                     if (args.length > i+1 ) {
102                         port = Integer.parseInt( args[++i] );
103                     }
104                 } else if (args[i].equals("--help")){
105                     printHelp();
106                     return;
107                 } else if (args[i].equals("-examples")){
108                     printExamples();
109                     return;
110                 }
111             }
112
113             stop( host, port );
114         } catch ( Exception JavaDoc re ) {
115             System.err.println("[EJB Server] FATAL ERROR: "+ re.getMessage());
116             re.printStackTrace();
117         }
118     }
119
120     private static void printHelp() {
121         String JavaDoc header = "OpenEJB Remote Server ";
122         try {
123             JarUtils.setHandlerSystemProperty();
124             Properties JavaDoc versionInfo = new Properties JavaDoc();
125             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
126             header += versionInfo.get( "version" );
127         } catch (java.io.IOException JavaDoc e) {
128         }
129
130         System.out.println( header );
131
132         // Internationalize this
133
try {
134             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResource(helpBase + "stop.help").openConnection().getInputStream();
135
136             int b = in.read();
137             while (b != -1) {
138                 System.out.write( b );
139                 b = in.read();
140             }
141         } catch (java.io.IOException JavaDoc e) {
142         }
143     }
144
145     private static void printExamples() {
146         String JavaDoc header = "OpenEJB Remote Server ";
147         try {
148             JarUtils.setHandlerSystemProperty();
149             Properties JavaDoc versionInfo = new Properties JavaDoc();
150             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
151             header += versionInfo.get( "version" );
152         } catch (java.io.IOException JavaDoc e) {
153         }
154
155         System.out.println( header );
156
157         // Internationalize this
158
try {
159             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResource(helpBase + "stop.examples").openConnection().getInputStream();
160
161             int b = in.read();
162             while (b != -1) {
163                 System.out.write( b );
164                 b = in.read();
165             }
166         } catch (java.io.IOException JavaDoc e) {
167         }
168     }
169 }
170
Popular Tags