Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. public class ApplicationDispatchHandler extends CommandHandlerBase {
  2. //CommandHandlerBase is the base class of the commands we want to execute
  3.  
  4. //default handlers are those provided by the NAC framework
  5. private final static Map<AppCommandType, Class<? extends CommandHandlerBase>> defaultHandlers =
  6. new HashMap<AppCommandType, Class<? extends CommandHandlerBase>>();
  7.  
  8. //override handlers store custom behavior to override the behavior supplied by the NAC framework
  9. //this allows customers to specify their own behavior for any particular app command type, while
  10. //maintaining core behavior for all other app command types
  11. private final static Map<AppCommandType, Class<? extends CommandHandlerBase>> overrideHandlers =
  12. new HashMap<AppCommandType, Class<? extends CommandHandlerBase>>();
  13.  
  14. static {
  15. registerDefaultHandler(AppCommandType.ALARM, AlarmAppHandler.class);
  16. registerDefaultHandler(AppCommandType.CALENDAR, CalendarAppHandler.class);
  17. registerDefaultHandler(AppCommandType.CALLING, VoiceDialHandler.class);
  18. //...
  19. }
  20.  
  21. private static void registerDefaultHandler(AppCommandType key, Class<? extends CommandHandlerBase> handler) {
  22. defaultHandlers.put(key, handler);
  23. }
  24.  
  25. public static void registerHandler(AppCommandType key, Class<? extends CommandHandlerBase> handler) {
  26. overrideHandlers.put(key, handler);
  27. }
  28.  
  29. public static void unregisterHandler(AppCommandType key) {
  30. overrideHandlers.remove(key);
  31. }
  32.  
  33. public static void unregisterAll() {
  34. overrideHandlers.clear();
  35. }
  36.  
  37. @Override
  38. public boolean execute(String appTypeString) {
  39. //...
  40. AppCommandType appType = getAppType(appTypeString);
  41. if (appType == null) {
  42. log.error("No AppType found for action: " + appTypeString);
  43. return false;
  44. }
  45.  
  46. Class<? extends CommandHandlerBase> handlerClass = getHandler(appType);
  47. CommandHandlerBase handler = null;
  48. if (handlerClass != null) {
  49.  
  50. try {
  51. handler = handlerClass.getDeclaredConstructor(ICommandHandlerListener.class).newInstance(getListener());
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. return false;
  55. }
  56. }
  57. // operate on the handler
  58. //...
  59. }
  60.  
  61. private AppCommandType getAppType(String appTypeString) {
  62. AppCommandType appType = null;
  63. try {
  64. appType = new AppCommandType(appTypeString);
  65. } catch (IllegalArgumentException e) {
  66. log.error("App name '" + action.getApp() + "' is not supported");
  67. return null;
  68. }
  69. return appType;
  70. }
  71.  
  72.  
  73. private static Class<? extends CommandHandlerBase> getHandler(AppCommandType appType) {
  74. //CommandHandlerBase not shown here
  75. Class<? extends CommandHandlerBase> handlerClass = null;
  76. handlerClass = overrideHandlers.get(appType);
  77. if (handlerClass == null) {
  78. handlerClass = defaultHandlers.get(appType);
  79. }
  80. return handlerClass;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement