Fix for #567 "mailinglist gateway does not recover after database outage".
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;
24 import java.util.concurrent.ConcurrentHashMap;
25 import org.sonews.daemon.command.Command;
26 import org.sonews.daemon.command.UnsupportedCommand;
27 import org.sonews.util.Log;
28 import org.sonews.util.io.Resource;
31 * Selects the correct command processing class.
32 * @author Christian Lins
35 public class CommandSelector
38 private static Map<Thread, CommandSelector> instances
39 = new ConcurrentHashMap<Thread, CommandSelector>();
40 private static Map<String, Class<?>> commandClassesMapping
41 = new ConcurrentHashMap<String, Class<?>>();
45 String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
46 for(String className : classes)
48 if(className.charAt(0) == '#')
56 addCommandHandler(className);
58 catch(ClassNotFoundException ex)
60 Log.get().warning("Could not load command class: " + ex);
62 catch(InstantiationException ex)
64 Log.get().severe("Could not instantiate command class: " + ex);
66 catch(IllegalAccessException ex)
68 Log.get().severe("Could not access command class: " + ex);
73 public static void addCommandHandler(String className)
74 throws ClassNotFoundException, InstantiationException, IllegalAccessException
76 Class<?> clazz = Class.forName(className);
77 Command cmd = (Command)clazz.newInstance();
78 String[] cmdStrs = cmd.getSupportedCommandStrings();
79 for (String cmdStr : cmdStrs)
81 commandClassesMapping.put(cmdStr, clazz);
85 public static Set<String> getCommandNames()
87 return commandClassesMapping.keySet();
90 public static CommandSelector getInstance()
92 CommandSelector csel = instances.get(Thread.currentThread());
95 csel = new CommandSelector();
96 instances.put(Thread.currentThread(), csel);
101 private Map<String, Command> commandMapping = new HashMap<String, Command>();
102 private Command unsupportedCmd = new UnsupportedCommand();
104 private CommandSelector()
107 public Command get(String commandName)
111 commandName = commandName.toUpperCase();
112 Command cmd = this.commandMapping.get(commandName);
116 Class<?> clazz = commandClassesMapping.get(commandName);
119 cmd = this.unsupportedCmd;
123 cmd = (Command)clazz.newInstance();
124 this.commandMapping.put(commandName, cmd);
127 else if(cmd.isStateful())
129 cmd = cmd.getClass().newInstance();
136 ex.printStackTrace();
137 return this.unsupportedCmd;