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.extensions;
21  
22  import static org.apache.jsieve.commands.extensions.LogLevelTags.DEBUG_TAG;
23  import static org.apache.jsieve.commands.extensions.LogLevelTags.ERROR_TAG;
24  import static org.apache.jsieve.commands.extensions.LogLevelTags.FATAL_TAG;
25  import static org.apache.jsieve.commands.extensions.LogLevelTags.INFO_TAG;
26  import static org.apache.jsieve.commands.extensions.LogLevelTags.TRACE_TAG;
27  import static org.apache.jsieve.commands.extensions.LogLevelTags.WARN_TAG;
28  
29  import java.util.List;
30  import java.util.ListIterator;
31  
32  import org.apache.jsieve.Argument;
33  import org.apache.jsieve.Arguments;
34  import org.apache.jsieve.Block;
35  import org.apache.jsieve.SieveContext;
36  import org.apache.jsieve.StringListArgument;
37  import org.apache.jsieve.TagArgument;
38  import org.apache.jsieve.commands.AbstractCommand;
39  import org.apache.jsieve.exception.SieveException;
40  import org.apache.jsieve.exception.SyntaxException;
41  import org.apache.jsieve.mail.MailAdapter;
42  
43  /**
44   * <p>
45   * Class Log is an extension that implements a Command to write messages to the
46   * Sieve Log. The BNF syntax is...
47   * </p>
48   * <code>log [(:fatal / :error / :warn / :info / :debug / :trace)] string</code>
49   * <p>
50   * The default log level is :info.
51   * </p>
52   */
53  public class Log extends AbstractCommand {
54      /**
55       * Constructor for Log.
56       */
57      public Log() {
58          super();
59      }
60  
61      /**
62       * @see org.apache.jsieve.commands.AbstractCommand#executeBasic(MailAdapter,
63       *      Arguments, Block, SieveContext)
64       */
65      protected Object executeBasic(MailAdapter mail, Arguments arguments,
66              Block block, SieveContext context) throws SieveException {
67          String logLevel = null;
68          String message = null;
69  
70          // First MAY be a tag argument of fatal, error, warn, info, debug or
71          // trace.
72          // default is info.
73          final ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
74          boolean stop = false;
75  
76          // Tag processing
77          while (!stop && argumentsIter.hasNext()) {
78              final Argument argument = argumentsIter.next();
79              if (argument instanceof TagArgument) {
80                  final String tag = ((TagArgument) argument).getTag();
81  
82                  // LogLevel?
83                  if (null == logLevel
84                          && (tag.equals(FATAL_TAG) || tag.equals(ERROR_TAG)
85                                  || tag.equals(WARN_TAG) || tag.equals(INFO_TAG)
86                                  || tag.equals(DEBUG_TAG) || tag
87                                  .equals(TRACE_TAG)))
88                      logLevel = tag;
89                  else
90                      throw context.getCoordinate().syntaxException(
91                              "Found unexpected TagArgument");
92              } else {
93                  // Stop when a non-tag argument is encountered
94                  argumentsIter.previous();
95                  stop = true;
96              }
97          }
98  
99          // Next MUST be a String
100         if (argumentsIter.hasNext()) {
101             final Argument argument = argumentsIter.next();
102             if (argument instanceof StringListArgument) {
103                 List<String> strings = ((StringListArgument) argument).getList();
104                 if (1 == strings.size())
105                     message = strings.get(0);
106             }
107         }
108         if (null == message)
109             throw context.getCoordinate().syntaxException("Expecting a String");
110 
111         // Everthing else is an error
112         if (argumentsIter.hasNext())
113             throw context.getCoordinate().syntaxException(
114                     "Found unexpected arguments");
115 
116         log(null == logLevel ? ":info" : logLevel, message, context);
117 
118         return null;
119     }
120 
121     /**
122      * Method log.
123      * 
124      * @param logLevel
125      * @param message
126      * @throws SyntaxException
127      */
128     protected void log(String logLevel, String message, SieveContext context)
129             throws SyntaxException {
130         if (logLevel.equals(INFO_TAG))
131             logInfo(message, context);
132         else if (logLevel.equals(ERROR_TAG))
133             logError(message, context);
134         else if (logLevel.equals(WARN_TAG))
135             logWarn(message, context);
136         else if (logLevel.equals(DEBUG_TAG))
137             logDebug(message, context);
138         else if (logLevel.equals(FATAL_TAG))
139             logFatal(message, context);
140         else if (logLevel.equals(TRACE_TAG))
141             logTrace(message, context);
142         else
143             throw context.getCoordinate().syntaxException(
144                     new StringBuilder("Unsupported logging level: ").append (logLevel));
145     }
146 
147     /**
148      * Method logFatal.
149      * 
150      * @param message not null
151      * @param sieveContext not null
152      */
153     protected void logFatal(String message, SieveContext sieveContext) {
154         org.apache.commons.logging.Log log = sieveContext.getLog();
155         if (log.isFatalEnabled())
156             log.fatal(message);
157     }
158 
159     /**
160      * Method logWarn.
161      * 
162      * @param message not null
163      * @param context not null
164      */
165     protected void logWarn(String message, SieveContext context) {
166         org.apache.commons.logging.Log log = context.getLog();
167         if (log.isWarnEnabled())
168             log.warn(message);
169     }
170 
171     /**
172      * Method logInfo.
173      * 
174      * @param message not null
175      * @param context not null
176      */
177     protected void logInfo(String message, SieveContext context) {
178         org.apache.commons.logging.Log log = context.getLog();
179         if (log.isInfoEnabled())
180             log.info(message);
181     }
182 
183     /**
184      * Method logDebug.
185      * 
186      * @param message not null
187      * @param context not null
188      */
189     protected void logDebug(String message, SieveContext context) {
190         org.apache.commons.logging.Log log = context.getLog();
191         if (log.isDebugEnabled())
192             log.debug(message);
193     }
194 
195     /**
196      * Method logTrace.
197      * 
198      * @param message not null
199      * @param context not null
200      */
201     protected void logTrace(String message, SieveContext context) {
202         org.apache.commons.logging.Log log = context.getLog();
203         if (log.isTraceEnabled())
204             log.trace(message);
205     }
206 
207     /**
208      * Method logError.
209      * 
210      * @param message not null
211      * @param context not null
212      */
213     protected void logError(String message, SieveContext context) {
214         org.apache.commons.logging.Log log = context.getLog();
215         if (log.isErrorEnabled())
216             log.error(message);
217     }
218 
219     /**
220      * @see org.apache.jsieve.commands.AbstractCommand#validateArguments(Arguments,
221      *      SieveContext)
222      */
223     protected void validateArguments(Arguments arguments, SieveContext context)
224             throws SieveException {
225         // Validation is performed in executeBasic()
226     }
227 
228 }