Issue #538 fixed.
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)
58 if(className.charAt(0) == '#')
66 Class<?> clazz = Class.forName(className);
67 Command cmd = (Command)clazz.newInstance();
68 String[] cmdStrs = cmd.getSupportedCommandStrings();
69 for(String cmdStr : cmdStrs)
71 this.commandMapping.put(cmdStr, cmd);
74 catch(ClassNotFoundException ex)
76 Log.get().warning("Could not load command class: " + ex);
78 catch(InstantiationException ex)
80 Log.get().severe("Could not instantiate command class: " + ex);
82 catch(IllegalAccessException ex)
84 Log.get().severe("Could not access command class: " + ex);
89 public Command get(String commandName)
93 commandName = commandName.toUpperCase();
94 Command cmd = this.commandMapping.get(commandName);
98 return this.unsupportedCmd;
100 else if(cmd.isStateful())
102 return cmd.getClass().newInstance();
111 ex.printStackTrace();
112 return this.unsupportedCmd;