KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > commands > ImapCommandFactory


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver.commands;
19
20 import org.apache.avalon.framework.CascadingRuntimeException;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.logger.LogEnabled;
23 import org.apache.james.imapserver.CommandFetch;
24 import org.apache.james.imapserver.CommandStore;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * A factory for ImapCommand instances, provided based on the command name.
31  *
32  * @version 0.2 on 04 Aug 2002
33  */

34 public final class ImapCommandFactory
35         extends AbstractLogEnabled
36 {
37     private Map JavaDoc _imapCommands;
38     private ImapCommand _invalidCommand;
39
40     public ImapCommandFactory()
41     {
42         _invalidCommand = new InvalidCommand();
43
44         _imapCommands = new HashMap JavaDoc();
45         // Commands valid in any state
46
// CAPABILITY, NOOP, and LOGOUT
47
_imapCommands.put( "CAPABILITY", CapabilityCommand.class );
48         _imapCommands.put( "NOOP", NoopCommand.class );
49         _imapCommands.put( "LOGOUT", LogoutCommand.class );
50
51         // Commands valid in NON_AUTHENTICATED state.
52
// AUTHENTICATE and LOGIN
53
_imapCommands.put( "AUTHENTICATE", AuthenticateCommand.class );
54         _imapCommands.put( "LOGIN", LoginCommand.class );
55
56         // Commands valid in AUTHENTICATED or SELECTED state.
57
// RFC2060: SELECT, EXAMINE, CREATE, DELETE, RENAME, SUBSCRIBE, UNSUBSCRIBE, LIST, LSUB, STATUS, and APPEND
58
_imapCommands.put( "SELECT", SelectCommand.class );
59         _imapCommands.put( "EXAMINE", ExamineCommand.class );
60         _imapCommands.put( "CREATE", CreateCommand.class );
61         _imapCommands.put( "DELETE", DeleteCommand.class );
62         _imapCommands.put( "RENAME", RenameCommand.class );
63         _imapCommands.put( "SUBSCRIBE", SubscribeCommand.class );
64         _imapCommands.put( "UNSUBSCRIBE", UnsubscribeCommand.class );
65         _imapCommands.put( "LIST", ListCommand.class );
66         _imapCommands.put( "LSUB", LsubCommand.class );
67         _imapCommands.put( "STATUS", StatusCommand.class );
68         _imapCommands.put( "APPEND", AppendCommand.class );
69         // RFC2342 NAMESPACE
70
_imapCommands.put( "NAMESPACE", NamespaceCommand.class );
71         // RFC2086 GETACL, SETACL, DELETEACL, LISTRIGHTS, MYRIGHTS
72
_imapCommands.put( "GETACL", GetAclCommand.class );
73         _imapCommands.put( "SETACL", SetAclCommand.class );
74         _imapCommands.put( "DELETEACL", DeleteAclCommand.class );
75         _imapCommands.put( "LISTRIGHTS", ListRightsCommand.class );
76         _imapCommands.put( "MYRIGHTS", MyRightsCommand.class );
77
78
79         // Commands only valid in SELECTED state.
80
// CHECK, CLOSE, EXPUNGE, SEARCH, FETCH, STORE, COPY, and UID
81
_imapCommands.put( "CHECK", CheckCommand.class );
82         _imapCommands.put( "CLOSE", CloseCommand.class );
83         _imapCommands.put( "COPY", CopyCommand.class );
84         _imapCommands.put( "EXPUNGE", ExpungeCommand.class );
85         _imapCommands.put( "SEARCH", SearchCommand.class );
86         _imapCommands.put( "FETCH", CommandFetch.class );
87         _imapCommands.put( "STORE", CommandStore.class );
88         _imapCommands.put( "UID", UidCommand.class );
89     }
90
91     public ImapCommand getCommand( String JavaDoc commandName )
92     {
93         Class JavaDoc cmdClass = (Class JavaDoc)_imapCommands.get( commandName.toUpperCase() );
94
95         if ( cmdClass == null ) {
96             return _invalidCommand;
97         }
98         else {
99             return createCommand( cmdClass );
100         }
101     }
102
103     private ImapCommand createCommand( Class JavaDoc commandClass )
104     {
105         try {
106             ImapCommand cmd = (ImapCommand) commandClass.newInstance();
107             if ( cmd instanceof LogEnabled ) {
108                 ((LogEnabled) cmd).enableLogging( getLogger() );
109             }
110             return cmd;
111         }
112         catch ( Exception JavaDoc e ) {
113             throw new CascadingRuntimeException( "Could not create command instance: " + commandClass.getName(), e );
114         }
115     }
116
117
118 }
119
Popular Tags