You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2010/10/13 01:57:57 UTC
svn commit: r1021955 - in
/openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client:
CommandParser.java MulticastTool.java Options.java
Author: dblevins
Date: Tue Oct 12 23:57:57 2010
New Revision: 1021955
URL: http://svn.apache.org/viewvc?rev=1021955&view=rev
Log:
OPENEJB-1369: Help, expanded options and cleaner output for MulticastTool
Added:
openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/CommandParser.java (with props)
openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/Options.java
- copied, changed from r1021903, openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java
Modified:
openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastTool.java
Added: openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/CommandParser.java
URL: http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/CommandParser.java?rev=1021955&view=auto
==============================================================================
--- openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/CommandParser.java (added)
+++ openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/CommandParser.java Tue Oct 12 23:57:57 2010
@@ -0,0 +1,364 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.client;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class CommandParser {
+
+ private final Map<String, Option> opts = new LinkedHashMap<String, Option>();
+
+ public CommandParser() {
+ init();
+ }
+
+ protected void init(){}
+
+ protected List<String> validate(Arguments arguments) {
+ return new ArrayList<String>();
+ }
+
+ protected List<String> usage() {
+ return new ArrayList<String>();
+ }
+
+ public void category(String name) {
+ opts.put("-@" + name, new Category(name));
+ }
+
+ public Option opt(String _long) {
+ return opt(new Option(_long));
+ }
+
+ public Option opt(char _short, String _long) {
+ return opt(new Option(_short, _long));
+ }
+
+ private Option opt(Option o) {
+ opts.put(o.getName(), o);
+ if (o.getMini() != null) opts.put(o.getMini(), o);
+ return o;
+ }
+
+ private void help() {
+
+ final Iterator<String> usage = usage().iterator();
+
+ if (usage.hasNext()) {
+ System.out.println("Usage: " + usage.next());
+ }
+
+ while (usage.hasNext()) {
+ System.out.println(" or " + usage.next());
+ }
+
+ List<Option> seen = new ArrayList<Option>();
+
+ for (Option option : opts.values()) {
+ if (seen.contains(option)) continue;
+ seen.add(option);
+
+ if (option instanceof Category) {
+ System.out.println("");
+ System.out.println(option.getName() + ":");
+ continue;
+ }
+
+ StringBuilder s = new StringBuilder();
+ if (option.getMini() != null) {
+ s.append(" -");
+ s.append(option.getMini());
+ s.append(", ");
+ } else {
+ s.append(" ");
+ }
+
+ s.append("--");
+ s.append(option.getName());
+ if (option.getType() != null) {
+ s.append("=");
+ s.append(option.getType().getSimpleName());
+ }
+
+ if (option.getDescription() != null || option.getValue() != null) {
+ for (int i = s.length(); i < 29; i++) {
+ s.append(' ');
+ }
+
+ s.append(' ');
+ s.append(' ');
+
+ if (option.getDescription() != null) {
+ s.append(option.getDescription());
+ }
+
+ if (option.getValue() != null) {
+ if (option.getDescription() != null) s.append(" ");
+ s.append("Default is ");
+ s.append(option.getValue());
+ }
+ }
+
+ System.out.println(s.toString());
+ }
+ }
+
+ public Arguments parse(String[] args) throws HelpException, InvalidOptionsException {
+ List<String> list = new ArrayList<String>(Arrays.asList(args));
+
+ ListIterator<String> items = list.listIterator();
+
+ Properties props = new Properties();
+
+ while (items.hasNext()) {
+ String arg = items.next();
+
+ if (!arg.startsWith("-")) break;
+
+ items.remove();
+
+ boolean longOption = arg.startsWith("--");
+
+ arg = arg.replaceFirst("-+", "");
+
+ if (longOption) {
+ String name, value = "";
+
+ if (arg.equals("help")) {
+ props.put("help", "");
+ continue;
+ } else if (arg.indexOf("=") > -1) {
+ name = arg.substring(0, arg.indexOf("="));
+ value = arg.substring(arg.indexOf("=") + 1);
+ } else {
+ name = arg;
+
+ final Option option = opts.get(name);
+
+ value = value(items, option);
+ }
+
+ Option option = opts.get(name);
+
+ checkOption("--" + name, option);
+ checkValue("--" + name, value, option);
+ props.put(option.getName(), value);
+ } else {
+ char[] chars = new char[arg.length()];
+ arg.getChars(0, chars.length, chars, 0);
+
+ for (int i = 0; i < chars.length; i++) {
+ char c = chars[i];
+
+ String name = c + "";
+
+ Option option = opts.get(name);
+
+ checkOption("-" + name, option);
+
+ String value = "";
+
+ if (i + 1 >= chars.length) {
+ // Last char so, next item could be a value
+ value = value(items, option);
+ }
+
+ checkValue("-" + c, value, option);
+ props.put(option.getName(), value);
+ }
+ }
+ }
+
+ final Arguments arguments = new Arguments(new Options(props), list);
+
+ if (arguments.options().has("help")) {
+
+ help();
+
+ throw new HelpException();
+ }
+
+ final List<String> issues = validate(arguments);
+
+ if (issues.size() > 0) {
+
+ for (String issue : issues) {
+ System.err.println(issue);
+ }
+
+ System.err.println();
+
+ help();
+
+ throw new InvalidOptionsException();
+ }
+
+ return arguments;
+ }
+
+ private void checkValue(String optString, String value, Option option) {
+ checkOption(optString, option);
+
+ if (option.getType() == null) return;
+
+ if (value == null || value.equals("")) {
+ System.err.println("Missing param " + optString + " <" + option.getType().getSimpleName() + ">");
+ System.exit(1);
+ }
+ }
+
+ private void checkOption(String invalid, Option option) {
+ if (option != null) return;
+
+ System.err.println("Unknown option: " + invalid);
+
+ help();
+
+ System.exit(1);
+ }
+
+ private String value(ListIterator<String> items, Option option) {
+ if (option.getType() != null && items.hasNext()) {
+
+ String possibleValue = items.next();
+
+ if (!possibleValue.startsWith("-")) {
+ items.remove();
+ // next item is the value
+ return possibleValue;
+ } else {
+ // next item is an option
+ items.previous();
+ }
+ }
+ return "";
+ }
+
+ public static final class Arguments {
+ private final Options options;
+ private final List<String> args;
+
+ public Arguments(Options options, List<String> args) {
+ this.options = options;
+ this.args = args;
+ }
+
+ public Options options() {
+ return options;
+ }
+
+ public List<String> get() {
+ return args;
+ }
+ }
+
+ public static class Category extends Option {
+ public Category(String name) {
+ super(name);
+ }
+ }
+
+ public static class Option {
+ private final String mini;
+ private final String name;
+ private String description;
+ private Class type;
+ private Object value;
+
+ Option(char mini, String name) {
+ this(mini + "", name);
+ }
+
+ Option(String name) {
+ this(null, name);
+ }
+
+ Option(String mini, String name) {
+ this.mini = mini;
+ this.name = name;
+ }
+
+ public Option description(String description) {
+ if (!description.endsWith(".")) description += ".";
+ this.setDescription(description);
+ return this;
+ }
+
+ public Option type(Class type) {
+ this.setType(type);
+ return this;
+ }
+
+ public Option value(Object value) {
+ this.setValue(value);
+ if (getType() == null) this.setType(value.getClass());
+ return this;
+ }
+
+ public String getMini() {
+ return mini;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public Class getType() {
+ return type;
+ }
+
+ public void setType(Class type) {
+ this.type = type;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ public void setValue(Object value) {
+ this.value = value;
+ }
+ }
+
+
+ public static class HelpException extends Exception {
+
+ }
+
+ public static class InvalidOptionsException extends Exception {
+
+ }
+}
Propchange: openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/CommandParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastTool.java
URL: http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastTool.java?rev=1021955&r1=1021954&r2=1021955&view=diff
==============================================================================
--- openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastTool.java (original)
+++ openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastTool.java Tue Oct 12 23:57:57 2010
@@ -24,9 +24,8 @@ import java.net.MulticastSocket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
-import java.util.Arrays;
import java.util.Date;
-import java.util.Iterator;
+import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@@ -35,80 +34,126 @@ import java.util.TimerTask;
*/
public class MulticastTool {
- private static final int BUFF_SIZE = 8192;
+ private static final CommandParser cmd = new CommandParser() {
+ @Override
+ protected void init() {
+ category("Options");
+ opt('h', "host").type(String.class).value("239.255.3.2")
+ .description("Address of the multicast channel");
- public static void main(String[] array) throws Exception {
+ opt('p', "port").type(int.class).value(6142)
+ .description("Port of the multicast channel");
- SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
+ opt("date-format").type(String.class).value("HH:mm:ss")
+ .description("Date format to use for log lines");
- String send = null;
+ category("Sending");
- long rate = 1000;
+ opt('s', "send").type(String.class)
+ .description("Optional message to broadcast to the channel");
+ opt('r', "rate").type(long.class).value(1000)
+ .description("Resend every N milliseconds. Zero sends just once");
- String host = "239.255.3.2";
- int port = 6142;
- Integer ttl = null;
- Boolean loopbackmode = null;
- Integer socketTimeout = null;
-
- Iterator<String> args = Arrays.asList(array).iterator();
- while (args.hasNext()) {
- String arg = args.next();
-
- if (arg.equals("--host") || arg.equals("-h")) {
- host = args.next();
- } else if (arg.equals("--port") || arg.equals("-p")) {
- port = Integer.parseInt(args.next());
- } else if (arg.equals("--rate") || arg.equals("-r")) {
- rate = new Long(args.next());
- } else if (arg.equals("--ttl")) {
- ttl = new Integer(args.next());
- } else if (arg.equals("--send") || arg.equals("-s")) {
- send = args.next();
- } else if (arg.equals("--timeout") || arg.equals("-t")) {
- socketTimeout = new Integer(args.next());
- } else if (arg.equals("--loopback") || arg.equals("-l")) {
- loopbackmode = new Boolean(args.next());
- } else {
- throw new IllegalArgumentException(arg);
- }
+ category("Advanced");
+
+ opt("broadcast").type(boolean.class).description("java.net.MulticastSocket#setBroadcast");
+ opt("loopback-mode").type(boolean.class).description("java.net.MulticastSocket#setLoopbackMode");
+ opt("receive-buffer-size").type(int.class).description("java.net.MulticastSocket#setReceiveBufferSize");
+ opt("reuse-address").type(boolean.class).description("java.net.MulticastSocket#setReuseAddress");
+ opt("send-buffer-size").type(int.class).description("java.net.MulticastSocket#setSendBufferSize");
+ opt("so-timeout").type(int.class).description("java.net.MulticastSocket#setSoTimeout");
+ opt("time-to-live").type(int.class).description("java.net.MulticastSocket#setTimeToLive");
+ opt("traffic-class").type(int.class).description("java.net.MulticastSocket#setTrafficClass");
}
- InetAddress inetAddress = InetAddress.getByName(host);
+ @Override
+ protected List<String> validate(Arguments arguments) {
+ return super.validate(arguments);
+ }
- InetSocketAddress address = new InetSocketAddress(inetAddress, port);
+ @Override
+ protected List<String> usage() {
+ return super.usage();
+ }
+ };
- MulticastSocket multicast = new MulticastSocket(port);
- multicast.joinGroup(inetAddress);
+ private static final int BUFF_SIZE = 8192;
- if (ttl != null) {
- multicast.setTimeToLive(ttl);
- }
+ public static void main(String[] array) throws Exception {
- if (socketTimeout != null) {
- multicast.setSoTimeout(socketTimeout);
+ final CommandParser.Arguments arguments;
+ try {
+ arguments = cmd.parse(array);
+ } catch (CommandParser.HelpException e) {
+ System.exit(0);
+ throw new Exception(); // never reached, but keeps compiler happy
+ } catch (CommandParser.InvalidOptionsException e) {
+ System.exit(1);
+ throw new Exception(); // never reached, but keeps compiler happy
}
- if (loopbackmode != null) {
- multicast.setLoopbackMode(loopbackmode);
- }
+ final Options options = arguments.options();
+
+ SimpleDateFormat format = new SimpleDateFormat(options.get("date-format", "HH:mm:ss"));
+
+ final String host = options.get("host", "239.255.3.2");
+ final int port = options.get("port", 6142);
+
+ InetAddress inetAddress = InetAddress.getByName(host);
- System.out.print("Connecting to multicast group: ");
- System.out.print(host);
- System.out.print(":");
- System.out.println(multicast.getLocalPort());
+ InetSocketAddress address = new InetSocketAddress(inetAddress, port);
- print("LoopbackMode", multicast.getLoopbackMode());
- print("TimeToLive", multicast.getTimeToLive());
- print("SoTimeout", multicast.getSoTimeout());
+ MulticastSocket multicast = new MulticastSocket(port);
+ multicast.joinGroup(inetAddress);
- System.out.println("-------------------------------");
- if (send != null) {
- Timer timer = new Timer("Multicast Send", true);
- timer.scheduleAtFixedRate(new Send(address, multicast, send), 0, rate);
+ final MulticastSocket s = multicast;
+ if (options.has("reuse-address")) s.setReuseAddress(options.get("reuse-address", false));
+ if (options.has("broadcast")) s.setBroadcast(options.get("broadcast", false));
+ if (options.has("loopback-mode")) s.setLoopbackMode(options.get("loopback-mode", false));
+ if (options.has("send-buffer-size")) s.setSendBufferSize(options.get("send-buffer-size", 0));
+ if (options.has("receive-buffer-size")) s.setReceiveBufferSize(options.get("receive-buffer-size", 0));
+ if (options.has("so-timeout")) s.setSoTimeout(options.get("so-timeout", 0));
+ if (options.has("time-to-live")) s.setTimeToLive(options.get("time-to-live", 0));
+ if (options.has("traffic-class")) s.setTrafficClass(options.get("traffic-class", 0));
+
+ System.out.println("Connected");
+ print("host", host);
+ print("port", port);
+ System.out.println();
+
+ System.out.println("Socket");
+ print("broadcast", s.getBroadcast());
+ print("loopback-mode", s.getLoopbackMode());
+ print("receive-buffer-size", s.getReceiveBufferSize());
+ print("reuse-address", s.getReuseAddress());
+ print("send-buffer-size", s.getSendBufferSize());
+ print("so-timeout", s.getSoTimeout());
+ print("time-to-live", s.getTimeToLive());
+ print("traffic-class", s.getTrafficClass());
+ System.out.println();
+
+ if (options.has("send")) {
+ String send = options.get("send", "");
+ long rate = options.get("rate", 1000);
+
+ System.out.println("Sending");
+ print("send", send);
+ print("rate", rate);
+ System.out.println();
+
+ final Send message = new Send(address, multicast, send);
+
+ if (rate >0) {
+ Timer timer = new Timer("Multicast Send", true);
+ timer.scheduleAtFixedRate(message, 0, rate);
+ } else {
+ message.run();
+ }
}
+ System.out.println("Listening....");
+
byte[] buf = new byte[BUFF_SIZE];
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
@@ -116,23 +161,30 @@ public class MulticastTool {
try {
multicast.receive(packet);
if (packet.getLength() > 0) {
- InetAddress a = packet.getAddress();
- System.out.print(format.format(new Date()));
- System.out.print(" - ");
- System.out.print(a.getHostAddress());
- System.out.print(" - ");
+ final StringBuilder sb = new StringBuilder();
+ sb.append(format.format(new Date()));
+ sb.append(" - ");
+ sb.append(packet.getAddress().getHostAddress());
+ sb.append(" - ");
String str = new String(packet.getData(), packet.getOffset(), packet.getLength());
- System.out.println(str);
+ sb.append(str);
+ System.out.println(sb.toString());
}
} catch (SocketTimeoutException e) {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(format.format(new Date()));
+ sb.append(" - ");
+ sb.append("ERROR");
+ sb.append(" - ");
+ sb.append(e.getMessage());
+ System.out.println(sb.toString());
}
}
}
private static void print(String name, Object value) {
- System.out.print(name);
- System.out.print(":");
- System.out.println(value);
+ System.out.printf(" %-20s: %s", name, value);
+ System.out.println();
}
static class Send extends TimerTask {
Copied: openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/Options.java (from r1021903, openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java)
URL: http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/Options.java?p2=openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/Options.java&p1=openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java&r1=1021903&r2=1021955&rev=1021955&view=diff
==============================================================================
--- openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java (original)
+++ openejb/branches/openejb-3.1.x/server/openejb-client/src/main/java/org/apache/openejb/client/Options.java Tue Oct 12 23:57:57 2010
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.openejb.loader;
+package org.apache.openejb.client;
import java.util.Arrays;
import java.util.Collection;