001: /*
002: * $Id: Concern.java 674 2006-10-06 12:15:59Z hengels $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.sf.net).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.client.commandline;
015:
016: import org.concern.Controller;
017: import org.concern.client.remote.JMXConnector;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Iterator;
022:
023: /**
024: * @author hengels
025: */
026: public class Concern {
027: public static Map commands = new HashMap();
028:
029: static {
030: commands.put("log",
031: org.concern.client.commandline.LogCommand.class);
032: commands.put("archive",
033: org.concern.client.commandline.ArchiveCommand.class);
034: commands.put("list",
035: org.concern.client.commandline.ListCommand.class);
036: commands.put("create",
037: org.concern.client.commandline.CreateCommand.class);
038: commands.put("destroy",
039: org.concern.client.commandline.DestroyCommand.class);
040: commands.put("touch",
041: org.concern.client.commandline.TouchCommand.class);
042: commands.put("process",
043: org.concern.client.commandline.ProcessCommand.class);
044: //commands.put("load", org.concern.client.commandline.LoadCommand.class);
045: commands
046: .put(
047: "check",
048: org.concern.client.commandline.MatchConditionCommand.class);
049: commands
050: .put(
051: "pre",
052: org.concern.client.commandline.MatchPreconditionCommand.class);
053: commands
054: .put(
055: "post",
056: org.concern.client.commandline.MatchPostconditionCommand.class);
057: commands.put("complete",
058: org.concern.client.commandline.CompleteCommand.class);
059: //commands.put("xml", org.concern.client.commandline.XMLCommand.class);
060: commands.put("describe",
061: org.concern.client.commandline.DescribeCommand.class);
062: }
063:
064: public static void main(String[] args) {
065: int index = 0;
066:
067: try {
068: String process = args[index++];
069: String commandName = args[index++];
070: Command command = getCommand(commandName);
071: String[] commandArgs = new String[args.length - index];
072: System.arraycopy(args, index, commandArgs, 0,
073: commandArgs.length);
074: command.setController(getController(process));
075: command.setArguments(commandArgs);
076: command.run();
077: } catch (Exception e) {
078: fail();
079: }
080: }
081:
082: public static Command getCommand(String commandName)
083: throws InstantiationException, IllegalAccessException {
084: Class commandClass = (Class) commands.get(commandName);
085: if (commandClass == null) {
086: System.err.println("unknown command " + commandName);
087: fail();
088: }
089: Command command = (Command) commandClass.newInstance();
090: return command;
091: }
092:
093: private static void fail() {
094: printUsage();
095: System.exit(1);
096: }
097:
098: private static void printUsage() {
099: try {
100: StringBuffer buffer = new StringBuffer(
101: "usage: concern PROCESS COMMAND ...\n");
102: buffer
103: .append(" where COMMAND ... is one of the following:\n\n");
104: for (Iterator iterator = commands.entrySet().iterator(); iterator
105: .hasNext();) {
106: Map.Entry entry = (Map.Entry) iterator.next();
107: String name = (String) entry.getKey();
108: Command command = getCommand(name);
109: buffer.append(name);
110: buffer.append(" ");
111: buffer.append(command.usage());
112: buffer.append("\n");
113: }
114: System.err.println(buffer.toString());
115: } catch (Exception e) {
116: }
117: }
118:
119: private static Controller getController(String name) {
120: try {
121: return new JMXConnector().connect(name, null);
122: //return new JBossConnector().connect(name);
123: } catch (Exception e) {
124: System.out.println(e.getMessage());
125: e.printStackTrace();
126: }
127: return null;
128: }
129: }
|