Drupal: hlavičky, poměrně funkční čtení příspěvků (vlánka).
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 = new ConcurrentHashMap<Thread, CommandSelector>();
39 private static Map<String, Class<?>> commandClassesMapping = new ConcurrentHashMap<String, Class<?>>();
42 String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
43 for (String className : classes) {
44 if (className.charAt(0) == '#') {
50 addCommandHandler(className);
51 } catch (ClassNotFoundException ex) {
52 Log.get().warning("Could not load command class: " + ex);
53 } catch (InstantiationException ex) {
54 Log.get().severe("Could not instantiate command class: " + ex);
55 } catch (IllegalAccessException ex) {
56 Log.get().severe("Could not access command class: " + ex);
61 public static void addCommandHandler(String className)
62 throws ClassNotFoundException, InstantiationException,
63 IllegalAccessException
65 Class<?> clazz = Class.forName(className);
66 Command cmd = (Command) clazz.newInstance();
67 String[] cmdStrs = cmd.getSupportedCommandStrings();
68 for (String cmdStr : cmdStrs) {
69 commandClassesMapping.put(cmdStr, clazz);
73 public static Set<String> getCommandNames()
75 return commandClassesMapping.keySet();
78 public static CommandSelector getInstance()
80 CommandSelector csel = instances.get(Thread.currentThread());
82 csel = new CommandSelector();
83 instances.put(Thread.currentThread(), csel);
87 private Map<String, Command> commandMapping = new HashMap<String, Command>();
88 private Command unsupportedCmd = new UnsupportedCommand();
90 private CommandSelector()
94 public Command get(String commandName)
97 commandName = commandName.toUpperCase();
98 Command cmd = this.commandMapping.get(commandName);
101 Class<?> clazz = commandClassesMapping.get(commandName);
103 cmd = this.unsupportedCmd;
105 cmd = (Command) clazz.newInstance();
106 this.commandMapping.put(commandName, cmd);
108 } else if (cmd.isStateful()) {
109 cmd = cmd.getClass().newInstance();
113 } catch (Exception ex) {
114 ex.printStackTrace();
115 return this.unsupportedCmd;