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.concurrent.ConcurrentHashMap; 23 import java.util.concurrent.ConcurrentMap; 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 * <h4>Thread Safety</h4> 37 * <p>An instance maybe safe accessed concurrently by multiple threads.</p> 38 */ 39 public class ActionDispatcher 40 { 41 /** 42 * A Map keyed by the type of Action. The values are the methods to invoke to 43 * handle the Action. 44 * <Action, MailAction> 45 */ 46 private ConcurrentMap<Class, MailAction> fieldMailActionMap; 47 48 /** 49 * Constructor for ActionDispatcher. 50 * @throws NoSuchMethodException 51 */ 52 public ActionDispatcher() throws MessagingException 53 { 54 super(); 55 setMethodMap(defaultMethodMap()); 56 } 57 58 /** 59 * Method execute executes the passed Action by invoking the method mapped by the 60 * receiver with a parameter of the EXACT type of Action. 61 * @param anAction not null 62 * @param aMail not null 63 * @param context not null 64 * @throws MessagingException 65 */ 66 public void execute(final Action anAction, final Mail aMail, final ActionContext context) throws MessagingException 67 { 68 final MailAction mailAction = getMethodMap().get(anAction.getClass()); 69 mailAction.execute(anAction, aMail, context); 70 } 71 72 /** 73 * Returns the methodMap. 74 * @return Map 75 */ 76 public ConcurrentMap<Class, MailAction> getMethodMap() 77 { 78 return fieldMailActionMap; 79 } 80 81 /** 82 * Returns a new methodMap. 83 * @return Map 84 */ 85 private ConcurrentMap<Class, MailAction> defaultMethodMap() 86 { 87 final ConcurrentMap<Class, MailAction> actionMap = new ConcurrentHashMap<Class, MailAction>(4); 88 actionMap.put(ActionFileInto.class, new FileIntoAction()); 89 actionMap.put(ActionKeep.class, new KeepAction()); 90 actionMap.put(ActionRedirect.class, new RedirectAction()); 91 actionMap.put(ActionReject.class, new RejectAction()); 92 return actionMap; 93 } 94 95 /** 96 * Sets the mail action mail. 97 * @param mailActionMap <Action, MailAction> not null 98 */ 99 protected void setMethodMap(ConcurrentMap<Class, MailAction> mailActionMap) 100 { 101 fieldMailActionMap = mailActionMap; 102 } 103 }