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;
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
chris@3: */
chris@3: class CommandSelector
chris@3: {
chris@3:
chris@3: private static Map instances
chris@3: = new ConcurrentHashMap();
chris@3:
chris@3: public static CommandSelector getInstance()
chris@3: {
chris@3: CommandSelector csel = instances.get(Thread.currentThread());
chris@3: if(csel == null)
chris@3: {
chris@3: csel = new CommandSelector();
chris@3: instances.put(Thread.currentThread(), csel);
chris@3: }
chris@3: return csel;
chris@3: }
chris@3:
chris@3: private Map commandMapping = new HashMap();
chris@3: private Command unsupportedCmd = new UnsupportedCommand();
chris@3:
chris@3: private CommandSelector()
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: {
chris@3: Class> clazz = Class.forName(className);
chris@3: Command cmd = (Command)clazz.newInstance();
chris@3: String[] cmdStrs = cmd.getSupportedCommandStrings();
chris@3: for(String cmdStr : cmdStrs)
chris@3: {
chris@3: this.commandMapping.put(cmdStr, cmd);
chris@3: }
chris@3: }
chris@3: catch(ClassNotFoundException ex)
chris@3: {
chris@3: Log.msg("Could not load command class: " + ex, false);
chris@3: }
chris@3: catch(InstantiationException ex)
chris@3: {
chris@3: Log.msg("Could not instantiate command class: " + ex, false);
chris@3: }
chris@3: catch(IllegalAccessException ex)
chris@3: {
chris@3: Log.msg("Could not access command class: " + ex, false);
chris@3: }
chris@3: }
chris@3: }
chris@3:
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: {
chris@3: return this.unsupportedCmd;
chris@3: }
chris@3: else if(cmd.isStateful())
chris@3: {
chris@3: return cmd.getClass().newInstance();
chris@3: }
chris@3: else
chris@3: {
chris@3: return cmd;
chris@3: }
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: }