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 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
37
38
39
40
41
42
43
44
45 public class Log extends AbstractCommand implements LogLevelTags {
46
47
48
49 public Log() {
50 super();
51 }
52
53
54
55
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
63
64
65 ListIterator argumentsIter = arguments.getArgumentList().listIterator();
66 boolean stop = false;
67
68
69 while (!stop && argumentsIter.hasNext()) {
70 Object argument = argumentsIter.next();
71 if (argument instanceof TagArgument) {
72 String tag = ((TagArgument) argument).getTag();
73
74
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
86 argumentsIter.previous();
87 stop = true;
88 }
89 }
90
91
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
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
115
116
117
118
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
141
142
143
144
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
154
155
156
157
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
167
168
169
170
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
180
181
182
183
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
193
194
195
196
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
206
207
208
209
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
219
220
221 protected void validateArguments(Arguments arguments, SieveContext context)
222 throws SieveException {
223
224 }
225
226 }