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.lang.reflect.InvocationTargetException;
  32. import java.lang.reflect.Method;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. import java.util.Set;
  36.  
  37. /*
  38.  * CommandHandler
  39.  * Allows you to register and invoke methods based on keywords/commands
  40.  * Method registed with keyword "unknown" will be invoked if there's no
  41.  * matching keyword
  42.  *
  43.  * @author Manuel Mausz (0728348)
  44.  */
  45. public class CommandHandler
  46. {
  47. class Exception
  48. extends java.lang.RuntimeException
  49. {
  50. public Exception()
  51. {
  52. super();
  53. }
  54.  
  55. public Exception(String message)
  56. {
  57. super(message);
  58. }
  59. }
  60.  
  61. /*==========================================================================*/
  62.  
  63. private HashMap<String, Object[]> commands;
  64.  
  65. /*--------------------------------------------------------------------------*/
  66.  
  67. public CommandHandler()
  68. {
  69. commands = new HashMap<String, Object[]>();
  70. }
  71.  
  72. /*--------------------------------------------------------------------------*/
  73.  
  74. public Set<Map.Entry<String, Object[]>> entrySet()
  75. {
  76. return commands.entrySet();
  77. }
  78.  
  79. /*--------------------------------------------------------------------------*/
  80.  
  81. public void register(String cmd, Object obj, String method)
  82. throws NoSuchMethodException
  83. {
  84. Class[] params = { String.class, String[].class };
  85. Method meth = obj.getClass().getMethod(method, params);
  86. Object[] tmp = { obj, meth };
  87. commands.put(cmd, tmp);
  88. }
  89.  
  90. /*--------------------------------------------------------------------------*/
  91.  
  92. public void call(String cmd, String[] args)
  93. throws Exception
  94. {
  95. try
  96. {
  97. Object[] meth = commands.get(cmd);
  98. if (meth == null)
  99. meth = commands.get("unknown");
  100. if (meth == null)
  101. {
  102. unknown(cmd, args);
  103. return;
  104. }
  105.  
  106. Object[] params = { cmd, args };
  107. ((Method) meth[1]).invoke(meth[0], (Object[]) params);
  108. }
  109. catch (IllegalAccessException e)
  110. {
  111. throw new Exception(e.getMessage());
  112. }
  113. catch (InvocationTargetException e)
  114. {
  115. throw new Exception(e.getCause().getMessage());
  116. }
  117. catch (IllegalArgumentException e)
  118. {
  119. throw new Exception(e.getMessage());
  120. }
  121. }
  122.  
  123. /*--------------------------------------------------------------------------*/
  124.  
  125. public void unknown(String cmd, String[] args)
  126. {
  127. System.out.println("Error: Unknown command: " + cmd);
  128. return;
  129. }
  130. }
  131.