01: package com.coldcore.coloradoftp.command.impl;
02:
03: import com.coldcore.coloradoftp.command.Command;
04: import com.coldcore.coloradoftp.command.CommandFactory;
05: import com.coldcore.coloradoftp.factory.ObjectFactory;
06: import com.coldcore.coloradoftp.factory.ObjectName;
07: import org.apache.log4j.Logger;
08:
09: import java.util.HashMap;
10: import java.util.HashSet;
11: import java.util.Map;
12: import java.util.Set;
13:
14: /**
15: * @see com.coldcore.coloradoftp.command.CommandFactory
16: */
17: public class GenericCommandFactory implements CommandFactory {
18:
19: private static Logger log = Logger
20: .getLogger(GenericCommandFactory.class);
21: protected Map<String, String> map;
22:
23: public GenericCommandFactory(Map<String, String> commandsRef) {
24: map = new HashMap<String, String>(commandsRef);
25: }
26:
27: public Set<String> listNames() {
28: return new HashSet<String>(map.keySet());
29: }
30:
31: public Command create(String input) {
32: String name;
33: String parameter;
34: try {
35: //First comes name of the command and then a single parameter (optional).
36: input = input.trim();
37: int i = input.indexOf(" ");
38: if (i == -1)
39: i = input.length();
40: name = input.substring(0, i).trim().toUpperCase();
41: parameter = input.substring(i).trim();
42: } catch (Throwable e) {
43: log.warn("Failed to process input: " + input);
44: return (Command) ObjectFactory
45: .getObject(ObjectName.COMMAND_SYNTAX_ERROR);
46: }
47:
48: Command command;
49: String ref = map.get(name);
50: if (ref != null) {
51: command = (Command) ObjectFactory.getObject(ref);
52: command.setName(name);
53: command.setParameter(parameter);
54: } else {
55: log.warn("Command " + name + " not implemented");
56: command = (Command) ObjectFactory
57: .getObject(ObjectName.COMMAND_NOT_IMPLEMENTED);
58: }
59: return command;
60: }
61: }
|