Fix for too early disconnects on slow client connections. (#563)
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
34 public class CommandSelector
37 private static Map<Thread, CommandSelector> instances
38 = new ConcurrentHashMap<Thread, CommandSelector>();
39 private static Map<String, Class<?>> commandClassesMapping
40 = new ConcurrentHashMap<String, Class<?>>();
44 String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
45 for(String className : classes)
47 if(className.charAt(0) == '#')
55 addCommandHandler(className);
57 catch(ClassNotFoundException ex)
59 Log.get().warning("Could not load command class: " + ex);
61 catch(InstantiationException ex)
63 Log.get().severe("Could not instantiate command class: " + ex);
65 catch(IllegalAccessException ex)
67 Log.get().severe("Could not access command class: " + ex);
72 public static void addCommandHandler(String className)
73 throws ClassNotFoundException, InstantiationException, IllegalAccessException
75 Class<?> clazz = Class.forName(className);
76 Command cmd = (Command)clazz.newInstance();
77 String[] cmdStrs = cmd.getSupportedCommandStrings();
78 for (String cmdStr : cmdStrs)
80 commandClassesMapping.put(cmdStr, clazz);
84 public static CommandSelector getInstance()
86 CommandSelector csel = instances.get(Thread.currentThread());
89 csel = new CommandSelector();
90 instances.put(Thread.currentThread(), csel);
95 private Map<String, Command> commandMapping = new HashMap<String, Command>();
96 private Command unsupportedCmd = new UnsupportedCommand();
98 private CommandSelector()
101 public Command get(String commandName)
105 commandName = commandName.toUpperCase();
106 Command cmd = this.commandMapping.get(commandName);
110 Class<?> clazz = commandClassesMapping.get(commandName);
113 cmd = this.unsupportedCmd;
117 cmd = (Command)clazz.newInstance();
118 this.commandMapping.put(commandName, cmd);
121 else if(cmd.isStateful())
123 cmd = cmd.getClass().newInstance();
130 ex.printStackTrace();
131 return this.unsupportedCmd;