chris@3
|
1 |
/*
|
chris@3
|
2 |
* SONEWS News Server
|
chris@3
|
3 |
* see AUTHORS for the list of contributors
|
chris@3
|
4 |
*
|
chris@3
|
5 |
* This program is free software: you can redistribute it and/or modify
|
chris@3
|
6 |
* it under the terms of the GNU General Public License as published by
|
chris@3
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
chris@3
|
8 |
* (at your option) any later version.
|
chris@3
|
9 |
*
|
chris@3
|
10 |
* This program is distributed in the hope that it will be useful,
|
chris@3
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
chris@3
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
chris@3
|
13 |
* GNU General Public License for more details.
|
chris@3
|
14 |
*
|
chris@3
|
15 |
* You should have received a copy of the GNU General Public License
|
chris@3
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
chris@3
|
17 |
*/
|
chris@3
|
18 |
|
chris@3
|
19 |
package org.sonews.daemon;
|
chris@3
|
20 |
|
chris@3
|
21 |
import java.util.HashMap;
|
chris@3
|
22 |
import java.util.Map;
|
cli@26
|
23 |
import java.util.Set;
|
chris@3
|
24 |
import java.util.concurrent.ConcurrentHashMap;
|
chris@3
|
25 |
import org.sonews.daemon.command.Command;
|
chris@3
|
26 |
import org.sonews.daemon.command.UnsupportedCommand;
|
chris@3
|
27 |
import org.sonews.util.Log;
|
chris@3
|
28 |
import org.sonews.util.io.Resource;
|
chris@3
|
29 |
|
chris@3
|
30 |
/**
|
chris@3
|
31 |
* Selects the correct command processing class.
|
chris@3
|
32 |
* @author Christian Lins
|
cli@21
|
33 |
* @since sonews/1.0
|
chris@3
|
34 |
*/
|
cli@21
|
35 |
public class CommandSelector
|
chris@3
|
36 |
{
|
chris@3
|
37 |
|
cli@37
|
38 |
private static Map<Thread, CommandSelector> instances = new ConcurrentHashMap<Thread, CommandSelector>();
|
cli@37
|
39 |
private static Map<String, Class<?>> commandClassesMapping = new ConcurrentHashMap<String, Class<?>>();
|
chris@3
|
40 |
|
cli@37
|
41 |
static {
|
cli@37
|
42 |
String[] classes = Resource.getAsString("helpers/commands.list", true).split("\n");
|
cli@37
|
43 |
for (String className : classes) {
|
cli@37
|
44 |
if (className.charAt(0) == '#') {
|
cli@37
|
45 |
// Skip comments
|
cli@37
|
46 |
continue;
|
cli@37
|
47 |
}
|
cli@10
|
48 |
|
cli@37
|
49 |
try {
|
cli@37
|
50 |
addCommandHandler(className);
|
cli@37
|
51 |
} catch (ClassNotFoundException ex) {
|
cli@37
|
52 |
Log.get().warning("Could not load command class: " + ex);
|
cli@37
|
53 |
} catch (InstantiationException ex) {
|
cli@37
|
54 |
Log.get().severe("Could not instantiate command class: " + ex);
|
cli@37
|
55 |
} catch (IllegalAccessException ex) {
|
cli@37
|
56 |
Log.get().severe("Could not access command class: " + ex);
|
cli@37
|
57 |
}
|
cli@37
|
58 |
}
|
cli@37
|
59 |
}
|
chris@3
|
60 |
|
cli@37
|
61 |
public static void addCommandHandler(String className)
|
cli@37
|
62 |
throws ClassNotFoundException, InstantiationException,
|
cli@37
|
63 |
IllegalAccessException
|
cli@37
|
64 |
{
|
cli@37
|
65 |
Class<?> clazz = Class.forName(className);
|
cli@37
|
66 |
Command cmd = (Command) clazz.newInstance();
|
cli@37
|
67 |
String[] cmdStrs = cmd.getSupportedCommandStrings();
|
cli@37
|
68 |
for (String cmdStr : cmdStrs) {
|
cli@37
|
69 |
commandClassesMapping.put(cmdStr, clazz);
|
cli@37
|
70 |
}
|
cli@37
|
71 |
}
|
cli@21
|
72 |
|
cli@37
|
73 |
public static Set<String> getCommandNames()
|
cli@37
|
74 |
{
|
cli@37
|
75 |
return commandClassesMapping.keySet();
|
cli@37
|
76 |
}
|
cli@26
|
77 |
|
cli@37
|
78 |
public static CommandSelector getInstance()
|
cli@37
|
79 |
{
|
cli@37
|
80 |
CommandSelector csel = instances.get(Thread.currentThread());
|
cli@37
|
81 |
if (csel == null) {
|
cli@37
|
82 |
csel = new CommandSelector();
|
cli@37
|
83 |
instances.put(Thread.currentThread(), csel);
|
cli@37
|
84 |
}
|
cli@37
|
85 |
return csel;
|
cli@37
|
86 |
}
|
cli@37
|
87 |
private Map<String, Command> commandMapping = new HashMap<String, Command>();
|
cli@37
|
88 |
private Command unsupportedCmd = new UnsupportedCommand();
|
cli@21
|
89 |
|
cli@37
|
90 |
private CommandSelector()
|
cli@37
|
91 |
{
|
cli@37
|
92 |
}
|
cli@21
|
93 |
|
cli@37
|
94 |
public Command get(String commandName)
|
cli@37
|
95 |
{
|
cli@37
|
96 |
try {
|
cli@37
|
97 |
commandName = commandName.toUpperCase();
|
cli@37
|
98 |
Command cmd = this.commandMapping.get(commandName);
|
cli@21
|
99 |
|
cli@37
|
100 |
if (cmd == null) {
|
cli@37
|
101 |
Class<?> clazz = commandClassesMapping.get(commandName);
|
cli@37
|
102 |
if (clazz == null) {
|
cli@37
|
103 |
cmd = this.unsupportedCmd;
|
cli@37
|
104 |
} else {
|
cli@37
|
105 |
cmd = (Command) clazz.newInstance();
|
cli@37
|
106 |
this.commandMapping.put(commandName, cmd);
|
cli@37
|
107 |
}
|
cli@37
|
108 |
} else if (cmd.isStateful()) {
|
cli@37
|
109 |
cmd = cmd.getClass().newInstance();
|
cli@37
|
110 |
}
|
chris@3
|
111 |
|
cli@37
|
112 |
return cmd;
|
cli@37
|
113 |
} catch (Exception ex) {
|
cli@37
|
114 |
ex.printStackTrace();
|
cli@37
|
115 |
return this.unsupportedCmd;
|
cli@37
|
116 |
}
|
cli@37
|
117 |
}
|
chris@3
|
118 |
}
|