1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
48
49
50
51
52
53 public class Log extends AbstractCommand {
54
55
56
57 public Log() {
58 super();
59 }
60
61
62
63
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
71
72
73 final ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
74 boolean stop = false;
75
76
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
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
94 argumentsIter.previous();
95 stop = true;
96 }
97 }
98
99
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
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
123
124
125
126
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
149
150
151
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
161
162
163
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
173
174
175
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
185
186
187
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
197
198
199
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
209
210
211
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
221
222
223 protected void validateArguments(Arguments arguments, SieveContext context)
224 throws SieveException {
225
226 }
227
228 }