001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.admin;
017:
018: import java.io.InputStream;
019: import java.io.OutputStream;
020: import java.net.ConnectException;
021: import java.net.Socket;
022: import java.net.URL;
023: import java.util.Properties;
024:
025: import org.apache.openejb.client.RequestMethodConstants;
026:
027: public class Stop {
028:
029: private static final String helpBase = "META-INF/org.apache.openejb.cli/";
030:
031: public static void stop(String host, int port) {
032: try {
033:
034: Socket socket = new Socket(host, port);
035: OutputStream out = socket.getOutputStream();
036:
037: out.write(RequestMethodConstants.STOP_REQUEST_Stop);
038:
039: } catch (ConnectException e) {
040: System.out.println(":: server not running ::");
041: } catch (Exception e) {
042: e.printStackTrace();
043: }
044: }
045:
046: public void stop() {
047: stop("localhost", 4200);
048: }
049:
050: public static void main(String[] args) {
051: try {
052:
053: String host = "localhost";
054:
055: int port = 4200;
056:
057: for (int i = 0; i < args.length; i++) {
058: if (args[i].equals("-h")) {
059: if (args.length > i + 1) {
060: host = args[++i];
061: }
062: } else if (args[i].equals("-p")) {
063: if (args.length > i + 1) {
064: port = Integer.parseInt(args[++i]);
065: }
066: } else if (args[i].equals("--help")) {
067: printHelp();
068: return;
069: } else if (args[i].equals("-examples")) {
070: printExamples();
071: return;
072: }
073: }
074:
075: stop(host, port);
076: } catch (Exception re) {
077: System.err.println("[EJB Server] FATAL ERROR: "
078: + re.getMessage());
079: re.printStackTrace();
080: }
081: }
082:
083: private static void printHelp() {
084: String header = "OpenEJB Remote Server ";
085: try {
086: Properties versionInfo = new Properties();
087: versionInfo.load(new URL(
088: "resource:/openejb-version.properties")
089: .openConnection().getInputStream());
090: header += versionInfo.get("version");
091: } catch (java.io.IOException e) {
092: }
093:
094: System.out.println(header);
095:
096: try {
097: InputStream in = Thread.currentThread()
098: .getContextClassLoader().getResource(
099: helpBase + "stop.help").openConnection()
100: .getInputStream();
101:
102: int b = in.read();
103: while (b != -1) {
104: System.out.write(b);
105: b = in.read();
106: }
107: } catch (java.io.IOException e) {
108: }
109: }
110:
111: private static void printExamples() {
112: String header = "OpenEJB Remote Server ";
113: try {
114: Properties versionInfo = new Properties();
115: versionInfo.load(new URL(
116: "resource:/openejb-version.properties")
117: .openConnection().getInputStream());
118: header += versionInfo.get("version");
119: } catch (java.io.IOException e) {
120: }
121:
122: System.out.println(header);
123:
124: try {
125: InputStream in = Thread.currentThread()
126: .getContextClassLoader().getResource(
127: helpBase + "stop.examples")
128: .openConnection().getInputStream();
129:
130: int b = in.read();
131: while (b != -1) {
132: System.out.write(b);
133: b = in.read();
134: }
135: } catch (java.io.IOException e) {
136: }
137: }
138: }
|