001: package org.jacorb.notification;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2003 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.Properties;
024: import java.util.StringTokenizer;
025:
026: import org.jacorb.notification.conf.Attributes;
027:
028: /**
029: * @author Alphonse Bendt
030: * @version $Id: ConsoleMain.java,v 1.6 2006/01/31 21:05:33 alphonse.bendt Exp $
031: */
032:
033: public class ConsoleMain {
034: public static void help() {
035: System.out
036: .println("valid arguments: [-printIOR] [-printCorbaloc] "
037: + "[-writeIOR <filename>] "
038: + "[-registerName <nameId>[.<nameKind>]] "
039: + "[-port <oaPort>] [-channels <channels>] [-help]");
040: }
041:
042: private static class CmdLineParser {
043: private Properties props = new Properties();
044:
045: public Properties getProps() {
046: return props;
047: }
048:
049: public CmdLineParser(String[] args) {
050: perform(args);
051: }
052:
053: private void perform(String[] args)
054: throws IllegalArgumentException {
055: try {
056: // process arguments
057: for (int i = 0; i < args.length; i++) {
058: if (args[i].equals("-printIOR")) {
059: props.put(Attributes.PRINT_IOR, "on");
060: } else if (args[i].equals("-printCorbaloc")) {
061: props.put(Attributes.PRINT_CORBALOC, "on");
062: } else if (args[i].equals("-help")) {
063: // TODO exception is used as goto here.
064: throw new IllegalArgumentException("usage");
065: } else if (args[i].equals("-port")) {
066: props.put("OAPort", args[++i]);
067: } else if (args[i].equals("-channels")) {
068: props.put(Attributes.START_CHANNELS, args[++i]);
069: } else if (args[i].equals("-writeIOR")) {
070: props.put(Attributes.IOR_FILE, args[++i]);
071: } else if (args[i].equals("-registerName")) {
072: String name = args[++i];
073:
074: addCOSNamingName(props, name);
075: } else if (args[i].equals("-typed")) {
076: props
077: .put(Attributes.ENABLE_TYPED_CHANNEL,
078: "on");
079: } else {
080: throw new IllegalArgumentException(
081: "unknown Argument: " + args[i]);
082: }
083: }
084: } catch (ArrayIndexOutOfBoundsException e) {
085: throw new IllegalArgumentException(
086: "illegal number of arguments");
087: }
088: }
089: }
090:
091: public static void addCOSNamingName(Properties props, String name) {
092: if (name == null || name.length() == 0) {
093: props.remove(Attributes.REGISTER_NAME_ID);
094: props.remove(Attributes.REGISTER_NAME_KIND);
095: return;
096: }
097:
098: int index = name.indexOf(".");
099: if (name.lastIndexOf(".") != index) {
100: throw new IllegalArgumentException(name
101: + ": argument to -registerName should be "
102: + "<nameId> or <nameId>.<nameKind>");
103: }
104: if (index != -1) {
105: props.put(Attributes.REGISTER_NAME_ID, name.substring(0,
106: index));
107:
108: props.put(Attributes.REGISTER_NAME_KIND, name
109: .substring(index + 1));
110: } else {
111: props.put(Attributes.REGISTER_NAME_ID, name);
112: }
113: }
114:
115: public static AbstractChannelFactory newFactory(String[] args)
116: throws Exception {
117: Properties props = parseProperties(args);
118:
119: return AbstractChannelFactory.newFactory(props);
120: }
121:
122: public static Properties parseProperties(String[] args) {
123: if (args == null) {
124: return new Properties();
125: }
126:
127: CmdLineParser _cmdLineParser = new CmdLineParser(args);
128:
129: return _cmdLineParser.getProps();
130: }
131:
132: public static String[] splitArgs(String argString) {
133: if (argString == null) {
134: return new String[0];
135: }
136:
137: StringTokenizer tokenizer = new StringTokenizer(argString, " ");
138: String[] result = new String[tokenizer.countTokens()];
139:
140: for (int i = 0; i < result.length; ++i) {
141: result[i] = tokenizer.nextToken();
142: }
143:
144: return result;
145: }
146:
147: public static final void main(String[] args) throws Exception {
148: try {
149: newFactory(args);
150: } catch (IllegalArgumentException e) {
151: System.out.println(e.getMessage());
152:
153: help();
154: }
155: }
156: }
|