/* * Copyright 2007 Kevin Roe, Daniel Mccarney * This file is part of Jriver. * * Jriver is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Jriver is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.jriver.lib.daemon; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /* import org.apache.log4j.Logger; */ import org.jriver.lib.std.Command; import org.jriver.lib.Player; /** * A command parser implementation * The CommandD takes our raw input string and parses it down into * several tokens including the command to execute and the commands * argument. It is the responsibility of this class to locate the * required command and invoke the entry point of this class with the * correct arguments. * @author Zanz * @author Enigma */ public class CommandD { /* private static Logger m_log = Logger.getLogger("org.jriver.lib.daemon.CommandD"); */ /** * The parseCommand method takes our command string and parses out * which command to invoke and with what arguments. It will attempt * to dynamically compile the class if it can not find it. * @param input The string representing our unprocessed input * @param ioObj The BasicTerminalIO object associated with the current user * @return A integer representing the command success or failure */ public static int parseCommand(String input, Player player) throws IOException { String command; String[] args, subsetArray, inputArray; InputStream in = player.getSocket().getInputStream(); OutputStream out = player.getSocket().getOutputStream(); if(input.trim().equals("")) return 0; //Input is blank, drop right away inputArray = input.split(" "); if(inputArray == null || inputArray.length == 0) { /* m_log.error("Null input provided to CommandD..."); */ return 0; } command = inputArray[0]; if(command.equals("'")) command = "say"; command = Command.COMMAND_PACKAGE + command; /* m_log.debug("Command received: "+ command); */ System.arraycopy(inputArray, 1, subsetArray = new String[inputArray.length - 1], 0, inputArray.length - 1); args = subsetArray; System.out.println(Thread.currentThread().getName() + " executed command ["+command+"]"); return doCommand(command, args, player); } /** * The method that actually performs the reflection calls * doCommand does the real work of the CommandD after having the * information required parsed out by the parseCommand method. * @param command The parsed command (i.e class name) we are going to instantiate and invoke * @param args The string arguments we will pass to the command * @param ioObj The BasicTerminalIO associated with the current user * @return An integer representing the command success or failure */ private static int doCommand(String command, String[] args, Player player) throws IOException { InputStream in = player.getSocket().getInputStream(); OutputStream out = player.getSocket().getOutputStream(); PrintWriter output = new PrintWriter(out, true); try { Class<?> commandToExecute = Class.forName(command); Command t = (Command) commandToExecute.newInstance(); Class<?>[] argTypes = new Class[] { String[].class, Player.class }; Method main = commandToExecute.getDeclaredMethod( Command.ENTRY_POINT, argTypes); main.invoke(t, new Object[] { args, player }); return 1; } catch(ClassNotFoundException e) { /* for now disable dynamic compiling int errorCode = CommandLoader.compile(command); m_log.debug("Compile Error Code: "+ errorCode); if(errorCode == 0) return doCommand(command, args, in, out, who); else { m_log.error("ClassNotFound: "+ command, e); */ output.println("What?\r"); return 0; /* } */ } catch(InstantiationException e) { /* m_log.error("Error loading "+ command +".", e); */ output.println("InstantiationException"); output.println("Your sensitive mind notices a wrongness in the fabric of space...\r"); } catch(IllegalAccessException e) { /* m_log.error("Unable to accesss constructer for "+ command +".", e); */ output.println("IllegalAccessException"); output.println("Your sensitive mind notices a wrongness in the fabric of space...\r"); } catch(NoSuchMethodException e) { /* m_log.error("Command "+ command +" does not contain "+ Command.ENTRY_POINT, e); */ output.println("NoSuchMethodException"); output.println("Your sensitive mind notices a wrongness in the fabric of space...\r"); } catch(InvocationTargetException e) { /* m_log.error("Error invoking "+ Command.ENTRY_POINT +"() on class "+ command, e); */ output.println("Entry point: "+ Command.ENTRY_POINT); output.println(e.getCause()); output.println("InvocationTargetException"); output.println("Your sensitive mind notices a wrongness in the fabric of space...\r"); } return 0; } }