1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 package org.apache.jsieve.mailet; 21 22 import java.util.HashMap; 23 import java.util.Map; 24 25 import javax.mail.MessagingException; 26 27 import org.apache.jsieve.mail.Action; 28 import org.apache.jsieve.mail.ActionFileInto; 29 import org.apache.jsieve.mail.ActionKeep; 30 import org.apache.jsieve.mail.ActionRedirect; 31 import org.apache.jsieve.mail.ActionReject; 32 import org.apache.mailet.Mail; 33 34 /** 35 * Dynamically dispatches an Action depending on the type of Action received at runtime. 36 */ 37 public class ActionDispatcher 38 { 39 /** 40 * A Map keyed by the type of Action. The values are the methods to invoke to 41 * handle the Action. 42 * <Action, MailAction> 43 */ 44 private Map/*<Action, MailAction>*/ fieldMailActionMap; 45 46 /** 47 * Constructor for ActionDispatcher. 48 * @throws NoSuchMethodException 49 */ 50 public ActionDispatcher() throws MessagingException 51 { 52 super(); 53 setMethodMap(defaultMethodMap()); 54 } 55 56 /** 57 * Method execute executes the passed Action by invoking the method mapped by the 58 * receiver with a parameter of the EXACT type of Action. 59 * @param anAction not null 60 * @param aMail not null 61 * @param context not null 62 * @throws MessagingException 63 */ 64 public void execute(final Action anAction, final Mail aMail, final ActionContext context) throws MessagingException 65 { 66 MailAction mailAction = (MailAction) getMethodMap().get(anAction.getClass()); 67 mailAction.execute(anAction, aMail, context); 68 } 69 70 /** 71 * Returns the methodMap. 72 * @return Map 73 */ 74 public Map getMethodMap() 75 { 76 return fieldMailActionMap; 77 } 78 79 /** 80 * Returns a new methodMap. 81 * @return Map 82 */ 83 private Map defaultMethodMap() 84 { 85 Map/*<Action, MailAction>*/ actionMap = new HashMap/*<Action, MailAction>*/(4); 86 actionMap.put(ActionFileInto.class, new FileIntoAction()); 87 actionMap.put(ActionKeep.class, new KeepAction()); 88 actionMap.put(ActionRedirect.class, new RedirectAction()); 89 actionMap.put(ActionReject.class, new RejectAction()); 90 return actionMap; 91 } 92 93 /** 94 * Sets the mail action mail. 95 * @param mailActionMap <Action, MailAction> not null 96 */ 97 protected void setMethodMap(Map/*<Action, MailAction>*/ mailActionMap) 98 { 99 fieldMailActionMap = mailActionMap; 100 } 101 }