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