chris@3: /* chris@3: * SONEWS News Server chris@3: * see AUTHORS for the list of contributors chris@3: * chris@3: * This program is free software: you can redistribute it and/or modify chris@3: * it under the terms of the GNU General Public License as published by chris@3: * the Free Software Foundation, either version 3 of the License, or chris@3: * (at your option) any later version. chris@3: * chris@3: * This program is distributed in the hope that it will be useful, chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@3: * GNU General Public License for more details. chris@3: * chris@3: * You should have received a copy of the GNU General Public License chris@3: * along with this program. If not, see . chris@3: */ chris@3: chris@3: package org.sonews.daemon; chris@3: chris@3: import java.util.HashMap; chris@3: import java.util.Map; cli@26: import java.util.Set; chris@3: import java.util.concurrent.ConcurrentHashMap; chris@3: import org.sonews.daemon.command.Command; chris@3: import org.sonews.daemon.command.UnsupportedCommand; chris@3: import org.sonews.util.Log; chris@3: import org.sonews.util.io.Resource; chris@3: chris@3: /** chris@3: * Selects the correct command processing class. chris@3: * @author Christian Lins cli@21: * @since sonews/1.0 chris@3: */ cli@21: public class CommandSelector chris@3: { chris@3: chris@3: private static Map instances chris@3: = new ConcurrentHashMap(); cli@21: private static Map> commandClassesMapping cli@21: = new ConcurrentHashMap>(); chris@3: cli@21: static chris@3: { chris@3: String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n"); chris@3: for(String className : classes) chris@3: { cli@10: if(className.charAt(0) == '#') cli@10: { cli@10: // Skip comments cli@10: continue; cli@10: } cli@10: chris@3: try chris@3: { cli@21: addCommandHandler(className); chris@3: } chris@3: catch(ClassNotFoundException ex) chris@3: { cli@15: Log.get().warning("Could not load command class: " + ex); chris@3: } chris@3: catch(InstantiationException ex) chris@3: { cli@15: Log.get().severe("Could not instantiate command class: " + ex); chris@3: } chris@3: catch(IllegalAccessException ex) chris@3: { cli@15: Log.get().severe("Could not access command class: " + ex); chris@3: } chris@3: } chris@3: } chris@3: cli@21: public static void addCommandHandler(String className) cli@21: throws ClassNotFoundException, InstantiationException, IllegalAccessException cli@21: { cli@21: Class clazz = Class.forName(className); cli@21: Command cmd = (Command)clazz.newInstance(); cli@21: String[] cmdStrs = cmd.getSupportedCommandStrings(); cli@21: for (String cmdStr : cmdStrs) cli@21: { cli@21: commandClassesMapping.put(cmdStr, clazz); cli@21: } cli@21: } cli@21: cli@26: public static Set getCommandNames() cli@26: { cli@26: return commandClassesMapping.keySet(); cli@26: } cli@26: cli@21: public static CommandSelector getInstance() cli@21: { cli@21: CommandSelector csel = instances.get(Thread.currentThread()); cli@21: if(csel == null) cli@21: { cli@21: csel = new CommandSelector(); cli@21: instances.put(Thread.currentThread(), csel); cli@21: } cli@21: return csel; cli@21: } cli@21: cli@21: private Map commandMapping = new HashMap(); cli@21: private Command unsupportedCmd = new UnsupportedCommand(); cli@21: cli@21: private CommandSelector() cli@21: {} cli@21: chris@3: public Command get(String commandName) chris@3: { chris@3: try chris@3: { chris@3: commandName = commandName.toUpperCase(); chris@3: Command cmd = this.commandMapping.get(commandName); chris@3: chris@3: if(cmd == null) chris@3: { cli@21: Class clazz = commandClassesMapping.get(commandName); cli@21: if(clazz == null) cli@21: { cli@21: cmd = this.unsupportedCmd; cli@21: } cli@21: else cli@21: { cli@21: cmd = (Command)clazz.newInstance(); cli@21: this.commandMapping.put(commandName, cmd); cli@21: } chris@3: } chris@3: else if(cmd.isStateful()) chris@3: { cli@21: cmd = cmd.getClass().newInstance(); chris@3: } cli@21: cli@21: return cmd; chris@3: } chris@3: catch(Exception ex) chris@3: { chris@3: ex.printStackTrace(); chris@3: return this.unsupportedCmd; chris@3: } chris@3: } chris@3: chris@3: }