1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.imap.cmd;
14
15 import org.abstracthorizon.danube.connection.ConnectionHandler;
16 import org.abstracthorizon.mercury.common.command.CommandException;
17
18 import java.lang.reflect.Constructor;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25
26
27
28
29
30
31
32 public class IMAPCommandFactory {
33
34
35 protected static final Logger logger = LoggerFactory.getLogger(IMAPCommandFactory.class);
36
37
38 protected static Map<String, Class<? extends IMAPCommand>> commands;
39
40 {
41 commands = new HashMap<String, Class<? extends IMAPCommand>>();
42 add("APPEND", Append.class);
43 add("AUTHENTICATE", Authenticate.class);
44 add("CAPABILITY", Capability.class);
45 add("CHECK", Check.class);
46 add("CLOSE", Close.class);
47 add("COPY", Copy.class);
48 add("CREATE", Create.class);
49 add("DELETE", Delete.class);
50 add("EXAMINE", Examine.class);
51 add("EXPUNGE", Expunge.class);
52 add("FETCH", Fetch.class);
53 add("IDLE", Idle.class);
54 add("LIST", List.class);
55 add("LOGIN", Login.class);
56 add("LOGOUT", Logout.class);
57 add("LSUB", LSub.class);
58 add("NOOP", NOOP.class);
59 add("PARTIAL", Partial.class);
60 add("RENAME", Rename.class);
61 add("SEARCH", Search.class);
62 add("STATUS", Status.class);
63 add("SELECT", Select.class);
64 add("STARTTLS", StartTLS.class);
65 add("STORE", Store.class);
66 add("SUBSCRIBE", Subscribe.class);
67 add("UID", UID.class);
68 add("UNSUBSCRIBE", Unsubscribe.class);
69 }
70
71
72
73
74 public IMAPCommandFactory() {
75 super();
76 }
77
78
79
80
81
82
83 protected static void add(String mnemonic, Class<? extends IMAPCommand> c) {
84 commands.put(mnemonic, c);
85 }
86
87
88
89
90
91
92
93 public ConnectionHandler getCommand(String mnemonic) throws CommandException {
94
95 Class<?> cls = (Class<?>)commands.get(mnemonic.toUpperCase());
96 if (cls == null) {
97 return new Bad();
98 }
99
100 try {
101 Constructor<?> c = cls.getConstructor(new Class[]{String.class});
102 if (c == null) {
103 return new Bad();
104 }
105
106 ConnectionHandler command = (ConnectionHandler)c.newInstance(new Object[]{mnemonic});
107 return command;
108 } catch (Exception e) {
109 logger.error("BAD: ", e);
110 return new Bad();
111 }
112
113 }
114
115 }