diff -r c404a87db5b7 -r 74139325d305 src/org/sonews/daemon/CommandSelector.java --- a/src/org/sonews/daemon/CommandSelector.java Sun Aug 29 17:43:58 2010 +0200 +++ b/src/org/sonews/daemon/CommandSelector.java Sun Aug 29 18:17:37 2010 +0200 @@ -35,107 +35,84 @@ public class CommandSelector { - private static Map instances - = new ConcurrentHashMap(); - private static Map> commandClassesMapping - = new ConcurrentHashMap>(); + private static Map instances = new ConcurrentHashMap(); + private static Map> commandClassesMapping = new ConcurrentHashMap>(); - static - { - String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n"); - for(String className : classes) - { - if(className.charAt(0) == '#') - { - // Skip comments - continue; - } + static { + String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n"); + for (String className : classes) { + if (className.charAt(0) == '#') { + // Skip comments + continue; + } - try - { - addCommandHandler(className); - } - catch(ClassNotFoundException ex) - { - Log.get().warning("Could not load command class: " + ex); - } - catch(InstantiationException ex) - { - Log.get().severe("Could not instantiate command class: " + ex); - } - catch(IllegalAccessException ex) - { - Log.get().severe("Could not access command class: " + ex); - } - } - } + try { + addCommandHandler(className); + } catch (ClassNotFoundException ex) { + Log.get().warning("Could not load command class: " + ex); + } catch (InstantiationException ex) { + Log.get().severe("Could not instantiate command class: " + ex); + } catch (IllegalAccessException ex) { + Log.get().severe("Could not access command class: " + ex); + } + } + } - public static void addCommandHandler(String className) - throws ClassNotFoundException, InstantiationException, IllegalAccessException - { - Class clazz = Class.forName(className); - Command cmd = (Command)clazz.newInstance(); - String[] cmdStrs = cmd.getSupportedCommandStrings(); - for (String cmdStr : cmdStrs) - { - commandClassesMapping.put(cmdStr, clazz); - } - } + public static void addCommandHandler(String className) + throws ClassNotFoundException, InstantiationException, + IllegalAccessException + { + Class clazz = Class.forName(className); + Command cmd = (Command) clazz.newInstance(); + String[] cmdStrs = cmd.getSupportedCommandStrings(); + for (String cmdStr : cmdStrs) { + commandClassesMapping.put(cmdStr, clazz); + } + } - public static Set getCommandNames() - { - return commandClassesMapping.keySet(); - } + public static Set getCommandNames() + { + return commandClassesMapping.keySet(); + } - public static CommandSelector getInstance() - { - CommandSelector csel = instances.get(Thread.currentThread()); - if(csel == null) - { - csel = new CommandSelector(); - instances.put(Thread.currentThread(), csel); - } - return csel; - } + public static CommandSelector getInstance() + { + CommandSelector csel = instances.get(Thread.currentThread()); + if (csel == null) { + csel = new CommandSelector(); + instances.put(Thread.currentThread(), csel); + } + return csel; + } + private Map commandMapping = new HashMap(); + private Command unsupportedCmd = new UnsupportedCommand(); - private Map commandMapping = new HashMap(); - private Command unsupportedCmd = new UnsupportedCommand(); + private CommandSelector() + { + } - private CommandSelector() - {} + public Command get(String commandName) + { + try { + commandName = commandName.toUpperCase(); + Command cmd = this.commandMapping.get(commandName); - public Command get(String commandName) - { - try - { - commandName = commandName.toUpperCase(); - Command cmd = this.commandMapping.get(commandName); + if (cmd == null) { + Class clazz = commandClassesMapping.get(commandName); + if (clazz == null) { + cmd = this.unsupportedCmd; + } else { + cmd = (Command) clazz.newInstance(); + this.commandMapping.put(commandName, cmd); + } + } else if (cmd.isStateful()) { + cmd = cmd.getClass().newInstance(); + } - if(cmd == null) - { - Class clazz = commandClassesMapping.get(commandName); - if(clazz == null) - { - cmd = this.unsupportedCmd; - } - else - { - cmd = (Command)clazz.newInstance(); - this.commandMapping.put(commandName, cmd); - } - } - else if(cmd.isStateful()) - { - cmd = cmd.getClass().newInstance(); - } - - return cmd; - } - catch(Exception ex) - { - ex.printStackTrace(); - return this.unsupportedCmd; - } - } - + return cmd; + } catch (Exception ex) { + ex.printStackTrace(); + return this.unsupportedCmd; + } + } }