View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * IMAP Commands factory
29   *
30   * @author Daniel Sendula
31   */
32  public class IMAPCommandFactory {
33  
34      /** Logger */
35      protected static final Logger logger = LoggerFactory.getLogger(IMAPCommandFactory.class);
36  
37      /** Map of commands */
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       * Constructor
73       */
74      public IMAPCommandFactory() {
75          super();
76      }
77  
78      /**
79       * Adds new command
80       * @param mnemonic mnemonic
81       * @param c command
82       */
83      protected static void add(String mnemonic, Class<? extends IMAPCommand> c) {
84          commands.put(mnemonic, c);
85      }
86  
87      /**
88       * Returns requested command
89       * @param mnemonic mnemonic
90       * @return command or {@link Bad} if command can't be found
91       * @throws CommandException
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 }