3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.daemon;
21 import java.util.HashMap;
23 import java.util.concurrent.ConcurrentHashMap;
24 import org.sonews.daemon.command.Command;
25 import org.sonews.daemon.command.UnsupportedCommand;
26 import org.sonews.util.Log;
27 import org.sonews.util.io.Resource;
30 * Selects the correct command processing class.
31 * @author Christian Lins
36 private static Map<Thread, CommandSelector> instances
37 = new ConcurrentHashMap<Thread, CommandSelector>();
39 public static CommandSelector getInstance()
41 CommandSelector csel = instances.get(Thread.currentThread());
44 csel = new CommandSelector();
45 instances.put(Thread.currentThread(), csel);
50 private Map<String, Command> commandMapping = new HashMap<String, Command>();
51 private Command unsupportedCmd = new UnsupportedCommand();
53 private CommandSelector()
55 String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
56 for(String className : classes)
60 Class<?> clazz = Class.forName(className);
61 Command cmd = (Command)clazz.newInstance();
62 String[] cmdStrs = cmd.getSupportedCommandStrings();
63 for(String cmdStr : cmdStrs)
65 this.commandMapping.put(cmdStr, cmd);
68 catch(ClassNotFoundException ex)
70 Log.msg("Could not load command class: " + ex, false);
72 catch(InstantiationException ex)
74 Log.msg("Could not instantiate command class: " + ex, false);
76 catch(IllegalAccessException ex)
78 Log.msg("Could not access command class: " + ex, false);
83 public Command get(String commandName)
87 commandName = commandName.toUpperCase();
88 Command cmd = this.commandMapping.get(commandName);
92 return this.unsupportedCmd;
94 else if(cmd.isStateful())
96 return cmd.getClass().newInstance();
105 ex.printStackTrace();
106 return this.unsupportedCmd;