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.  
  63. /*--------------------------------------------------------------------------*/
  64.  
  65. public CommandHandler()
  66. {
  67. commands = new HashMap<String, Object[]>();
  68. }
  69.  
  70. /*--------------------------------------------------------------------------*/
  71.  
  72. public void register(String cmd, Object obj, String method)
  73. throws NoSuchMethodException
  74. {
  75. Class[] params = { String.class, String[].class };
  76. Method meth = obj.getClass().getMethod(method, params);
  77. Object[] tmp = { obj, meth };
  78. commands.put(cmd, tmp);
  79. }
  80.  
  81. /*--------------------------------------------------------------------------*/
  82.  
  83. public void call(String cmd, String[] args)
  84. throws Exception
  85. {
  86. try
  87. {
  88. Object[] meth = commands.get(cmd);
  89. if (meth == null)
  90. meth = commands.get("unknown");
  91. if (meth == null)
  92. {
  93. unknown(cmd, args);
  94. return;
  95. }
  96.  
  97. Object[] params = { cmd, args };
  98. ((Method) meth[1]).invoke(meth[0], (Object[]) params);
  99. }
  100. catch (IllegalAccessException e)
  101. {
  102. throw new Exception(e.getMessage());
  103. }
  104. catch (InvocationTargetException e)
  105. {
  106. throw new Exception(e.getCause().getMessage());
  107. }
  108. catch (IllegalArgumentException e)
  109. {
  110. throw new Exception(e.getMessage());
  111. }
  112. }
  113.  
  114. /*--------------------------------------------------------------------------*/
  115.  
  116. public void unknown(String cmd, String[] args)
  117. {
  118. System.out.println("Error: Unknown command: " + cmd);
  119. return;
  120. }
  121. }
  122.