Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2010, Manuel Mausz. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - The names of the authors may not be used to endorse or promote products
  16.  * derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. import java.util.HashMap;
  32. import java.lang.reflect.Method;
  33. import java.lang.reflect.InvocationTargetException;
  34.  
  35. /*
  36.  * CommandHandler
  37.  * Allows you to register and invoke methods based on keywords/commands
  38.  * Method registed with keyword "unknown" will be invoked if there's no
  39.  * matching keyword
  40.  *
  41.  * @author Manuel Mausz (0728348)
  42.  */
  43. public class CommandHandler
  44. {
  45. class Exception
  46. extends java.lang.RuntimeException
  47. {
  48. public Exception()
  49. {
  50. super();
  51. }
  52.  
  53. public Exception(String message)
  54. {
  55. super(message);
  56. }
  57. }
  58.  
  59. /*==========================================================================*/
  60.  
  61. private HashMap<String, Object[]> commands;
  62. private String cmd = null;
  63. private String[] args = null;
  64.  
  65. /*--------------------------------------------------------------------------*/
  66.  
  67. public CommandHandler()
  68. {
  69. commands = new HashMap<String, Object[]>();
  70. }
  71.  
  72. /*--------------------------------------------------------------------------*/
  73.  
  74. public void register(String cmd, Object obj, String method)
  75. throws NoSuchMethodException
  76. {
  77. Class[] params = { String.class, String[].class };
  78. Method meth = obj.getClass().getMethod(method, params);
  79. Object[] tmp = { obj, meth };
  80. commands.put(cmd, tmp);
  81. }
  82.  
  83. /*--------------------------------------------------------------------------*/
  84.  
  85. public void call(String cmd, String[] args)
  86. throws Exception
  87. {
  88. this.cmd = cmd;
  89. this.args = args;
  90.  
  91. try
  92. {
  93. Object[] meth = commands.get(cmd);
  94. if (meth == null)
  95. meth = commands.get("unknown");
  96. if (meth == null)
  97. {
  98. unknown(cmd, args);
  99. return;
  100. }
  101.  
  102. Object[] params = { cmd, args };
  103. ((Method) meth[1]).invoke(meth[0], (Object[]) params);
  104. }
  105. catch (IllegalAccessException e)
  106. {
  107. throw new Exception(e.getMessage());
  108. }
  109. catch (InvocationTargetException e)
  110. {
  111. throw new Exception(e.getCause().getMessage());
  112. }
  113. catch (IllegalArgumentException e)
  114. {
  115. throw new Exception(e.getMessage());
  116. }
  117. }
  118.  
  119. /*--------------------------------------------------------------------------*/
  120.  
  121. public void call2()
  122. throws Exception
  123. {
  124. if (cmd == null || args == null)
  125. return;
  126. call(cmd, args);
  127. }
  128.  
  129. /*--------------------------------------------------------------------------*/
  130.  
  131. public void unknown(String cmd, String[] args)
  132. {
  133. System.out.println("Error: Unknown command: " + cmd);
  134. return;
  135. }
  136. }
  137.