View Javadoc

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.commands;
21  
22  import org.apache.jsieve.Block;
23  import org.apache.jsieve.ConditionManager;
24  import org.apache.jsieve.SieveContext;
25  import org.apache.jsieve.exception.SieveException;
26  import org.apache.jsieve.mail.MailAdapter;
27  
28  /**
29   * Abstract class AbstractConditionalCommand defines a framework of common
30   * behavior for conditional Commands (if, elsif, else). Conditional commands use
31   * a ConditionManager to relate and validate Commands within their Blocks.
32   */
33  public abstract class AbstractConditionalCommand extends AbstractControlCommand {
34  
35      /**
36       * Constructor for AbstractConditionalCommand.
37       */
38      public AbstractConditionalCommand() {
39          super();
40      }
41  
42      /**
43       * Method execute executes a Block within the context of a new
44       * ConditionManager.
45       * 
46       * @param mail
47       * @param block
48       * @param context
49       *            TODO
50       * @return Object
51       * @throws SieveException
52       */
53      protected Object execute(MailAdapter mail, Block block, SieveContext context)
54              throws SieveException {
55          // Switch to a new ConditionManager
56          ConditionManager oldManager = context.getConditionManager();
57          context.setConditionManager(new ConditionManager());
58  
59          try {
60              // Execute the Block
61              Object result = block.execute(mail, context);
62              return result;
63          } finally {
64              // Always restore the old ConditionManager
65              context.setConditionManager(oldManager);
66          }
67      }
68  
69      /**
70       * @see org.apache.jsieve.commands.AbstractCommand#validateBlock(Block,
71       *      SieveContext)
72       */
73      protected void validateBlock(Block block, SieveContext context)
74              throws SieveException {
75          if (null == block)
76              throw context.getCoordinate().syntaxException("Expecting a Block.");
77      }
78  
79  }