KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > MailCommandBuilder


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

16 package org.apache.cocoon.mail;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.context.Contextualizable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.cocoon.mail.command.AbstractMailCommand;
24
25 /**
26  * Build an AbstractMailCommand from MailContext.
27  * <p>
28  * As a user requests a command, the command is mapped to an MailCommand instance.
29  * The registration of MailCommand, and the resolution of a command string to
30  * a command instance are the tasks of this class.
31  * </p>
32  *
33  * @author Bernhard Huber
34  * @since 28. Dezember 2002
35  * @version CVS $Id: MailCommandBuilder.java 30981 2004-07-30 09:46:00Z cziegeler $
36  */

37 public class MailCommandBuilder extends AbstractLogEnabled {
38
39     // global factory settings
40
private Map JavaDoc cmdMap;
41
42     /**
43      *Constructor for the MailCommandBuilder object
44      */

45     public MailCommandBuilder() {
46         configure();
47     }
48
49
50     /**
51      * Build a mail command.
52      *
53      *@param mailContext Description of the Parameter
54      *@return Description of the Return Value
55      */

56     public AbstractMailCommand buildAbstractMailCommand(MailContext mailContext) {
57         AbstractMailCommand ama = null;
58
59         try {
60             // request parameter say "what"
61
String JavaDoc cmd = mailContext.getParameter("cmd");
62             if (cmd == null) {
63                 cmd = (String JavaDoc)mailContext.get( MailContext.MAIL_CURRENT_WORKING_COMMAND_ENTRY );
64             }
65             Class JavaDoc clazz = getClassForCommand(cmd);
66             if (clazz != null) {
67                 ama = (AbstractMailCommand) clazz.newInstance();
68                 // enable logging of the mail command
69
ama.enableLogging(getLogger());
70                 // contextualize the mail command
71
if (ama instanceof Contextualizable) {
72                     ((Contextualizable) ama).contextualize(mailContext);
73                 }
74                 return ama;
75             } else {
76                 getLogger().error("Cmd " + String.valueOf(cmd) + " is invalid");
77             }
78         } catch (Exception JavaDoc e) {
79             String JavaDoc message = "Cannto build AbstractMailCommand";
80             getLogger().error(message, e);
81         }
82         return ama;
83     }
84
85
86     /**
87      * get Class for a command
88      *
89      * @param cmd the command
90      * @return Class associated with cmd, or null iff cmd is not mapped to any class
91      */

92     protected Class JavaDoc getClassForCommand( String JavaDoc cmd ) {
93         Class JavaDoc clazz = (Class JavaDoc)cmdMap.get( cmd );
94         return clazz;
95     }
96     
97     /**
98      * test if command is mapped to a Command class
99      *
100      * @param cmd the command
101      * @return true iff command is mapped to a Class, otherwise return false
102      */

103     public boolean isCommandMapped( String JavaDoc cmd ) {
104         return cmdMap.containsKey( cmd );
105     }
106     
107     /**
108      * configure the cmd to mail command class mapping.
109      * <p>
110      * New commands are registered here. A command name is associated with
111      * each command class.
112      * </p>
113      */

114     public void configure() {
115         cmdMap = new HashMap JavaDoc();
116         cmdMap.put("cat-folder", MailCommandManager.MailFolderCatCommand.class);
117         cmdMap.put("refresh-folder", MailCommandManager.MailRefreshFolderCommand.class);
118         cmdMap.put("list-folder", MailCommandManager.MailListFolderCommand.class);
119         cmdMap.put("list-folder-messages", MailCommandManager.MailListMessagesCommand.class);
120         cmdMap.put("search-folder-messages", MailCommandManager.MailSearchMessagesCommand.class);
121         cmdMap.put("cat-message-by-uid", MailCommandManager.MailCatMessageByUIDCommand.class);
122         cmdMap.put("cat-message-by-id", MailCommandManager.MailCatMessageByIdCommand.class);
123         cmdMap.put("cat-attachment-of-message-by-id", MailCommandManager.MailCatAttachmentMessageByIdCommand.class);
124     }
125
126 }
127
128
Popular Tags