1.1 --- a/org/sonews/daemon/CommandSelector.java Mon Aug 24 13:00:05 2009 +0200
1.2 +++ b/org/sonews/daemon/CommandSelector.java Mon Aug 24 14:40:37 2009 +0200
1.3 @@ -29,28 +29,17 @@
1.4 /**
1.5 * Selects the correct command processing class.
1.6 * @author Christian Lins
1.7 + * @since sonews/1.0
1.8 */
1.9 -class CommandSelector
1.10 +public class CommandSelector
1.11 {
1.12
1.13 private static Map<Thread, CommandSelector> instances
1.14 = new ConcurrentHashMap<Thread, CommandSelector>();
1.15 -
1.16 - public static CommandSelector getInstance()
1.17 - {
1.18 - CommandSelector csel = instances.get(Thread.currentThread());
1.19 - if(csel == null)
1.20 - {
1.21 - csel = new CommandSelector();
1.22 - instances.put(Thread.currentThread(), csel);
1.23 - }
1.24 - return csel;
1.25 - }
1.26 + private static Map<String, Class<?>> commandClassesMapping
1.27 + = new ConcurrentHashMap<String, Class<?>>();
1.28
1.29 - private Map<String, Command> commandMapping = new HashMap<String, Command>();
1.30 - private Command unsupportedCmd = new UnsupportedCommand();
1.31 -
1.32 - private CommandSelector()
1.33 + static
1.34 {
1.35 String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
1.36 for(String className : classes)
1.37 @@ -63,13 +52,7 @@
1.38
1.39 try
1.40 {
1.41 - Class<?> clazz = Class.forName(className);
1.42 - Command cmd = (Command)clazz.newInstance();
1.43 - String[] cmdStrs = cmd.getSupportedCommandStrings();
1.44 - for(String cmdStr : cmdStrs)
1.45 - {
1.46 - this.commandMapping.put(cmdStr, cmd);
1.47 - }
1.48 + addCommandHandler(className);
1.49 }
1.50 catch(ClassNotFoundException ex)
1.51 {
1.52 @@ -86,6 +69,35 @@
1.53 }
1.54 }
1.55
1.56 + public static void addCommandHandler(String className)
1.57 + throws ClassNotFoundException, InstantiationException, IllegalAccessException
1.58 + {
1.59 + Class<?> clazz = Class.forName(className);
1.60 + Command cmd = (Command)clazz.newInstance();
1.61 + String[] cmdStrs = cmd.getSupportedCommandStrings();
1.62 + for (String cmdStr : cmdStrs)
1.63 + {
1.64 + commandClassesMapping.put(cmdStr, clazz);
1.65 + }
1.66 + }
1.67 +
1.68 + public static CommandSelector getInstance()
1.69 + {
1.70 + CommandSelector csel = instances.get(Thread.currentThread());
1.71 + if(csel == null)
1.72 + {
1.73 + csel = new CommandSelector();
1.74 + instances.put(Thread.currentThread(), csel);
1.75 + }
1.76 + return csel;
1.77 + }
1.78 +
1.79 + private Map<String, Command> commandMapping = new HashMap<String, Command>();
1.80 + private Command unsupportedCmd = new UnsupportedCommand();
1.81 +
1.82 + private CommandSelector()
1.83 + {}
1.84 +
1.85 public Command get(String commandName)
1.86 {
1.87 try
1.88 @@ -95,16 +107,23 @@
1.89
1.90 if(cmd == null)
1.91 {
1.92 - return this.unsupportedCmd;
1.93 + Class<?> clazz = commandClassesMapping.get(commandName);
1.94 + if(clazz == null)
1.95 + {
1.96 + cmd = this.unsupportedCmd;
1.97 + }
1.98 + else
1.99 + {
1.100 + cmd = (Command)clazz.newInstance();
1.101 + this.commandMapping.put(commandName, cmd);
1.102 + }
1.103 }
1.104 else if(cmd.isStateful())
1.105 {
1.106 - return cmd.getClass().newInstance();
1.107 + cmd = cmd.getClass().newInstance();
1.108 }
1.109 - else
1.110 - {
1.111 - return cmd;
1.112 - }
1.113 +
1.114 + return cmd;
1.115 }
1.116 catch(Exception ex)
1.117 {