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