/
AbstractLogging.java
395 lines (360 loc) · 14.6 KB
/
AbstractLogging.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018 Jason Mehrens. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.mail.util.logging;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Common super class for the logging test suite.
*
* @author Jason Mehrens
* @since JavaMail 1.5.6
*/
abstract class AbstractLogging {
/**
* Used to print debug information about the given throwable.
*
* @param t the throwable.
* @throws NullPointerException if given throwable is null.
*/
@SuppressWarnings({"CallToThreadDumpStack", "CallToPrintStackTrace"})
static void dump(final Throwable t) {
t.printStackTrace();
}
/**
* Gets all of the predefined Levels.
*
* @return an array of log levels.
*/
static Level[] getAllLevels() {
final Field[] fields = Level.class.getFields();
List<Level> a = new ArrayList<>(fields.length);
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())
&& Level.class.isAssignableFrom(field.getType())) {
try {
a.add((Level) field.get((Object) null));
} catch (IllegalArgumentException | IllegalAccessException ex) {
fail(ex.toString());
}
}
}
return a.toArray(new Level[a.size()]);
}
/**
* Determines if the given class is from the Jakarta Mail API Reference
* Implementation {@code com.sun.mail} package.
*
* @param k the type to test.
* @return true if this is part of reference implementation but not part of
* the official API spec.
* @throws Exception if there is a problem.
*/
final boolean isPrivateSpec(final Class<?> k) throws Exception {
return isFromJakartaMail(k, false);
}
/**
* Reinitialize the logging properties using the given properties.
*
* @param manager the log manager.
* @param props the properties.
* @throws IOException if there is a problem.
* @throws NullPointerException if either argument is null.
*/
final void read(LogManager manager, Properties props) throws IOException {
//JDK-4810637
ByteArrayOutputStream out = new ByteArrayOutputStream(512);
props.store(out, getClass().getName());
manager.readConfiguration(new ByteArrayInputStream(out.toByteArray()));
}
/**
* Sets the log record time using milliseconds from the epoch of
* 1970-01-01T00:00:00Z. Any nanosecond information is set to zero. This
* method is used to support JDK8 when running on JDK9 or newer.
*
* @param record the log record to adjust.
* @param epochMilli the time in milliseconds from epoch.
* @throws NullPointerException if the given record is null.
*/
@SuppressWarnings("deprecation") //See JDK-8144262 and K7091.
static void setEpochMilli(final LogRecord record, final long epochMilli) {
record.setMillis(epochMilli);
}
/**
* Sets the log record time using the seconds and nanoseconds of the epoch
* from 1970-01-01T00:00:00Z.
*
* @param record the log record.
* @param epochSecond the seconds.
* @param nanoAdjustment the nano seconds.
* @throws ClassNotFoundException if running on pre JDK 8.
* @throws NoSuchMethodException if running on JDK 8.
* @throws Exception if there is a problem.
*/
static void setEpochSecond(final LogRecord record, final long epochSecond,
final long nanoAdjustment) throws Exception {
final Class<?> k = Class.forName("java.time.Instant");
Method instant = k.getMethod("ofEpochSecond", long.class, long.class);
Method set = LogRecord.class.getMethod("setInstant", k);
set.invoke(record, instant.invoke(null, epochSecond, nanoAdjustment));
}
/**
* Determines if the {@code java.time} APIs are available for this JVM.
*
* @return true if the time classes can be loaded.
*/
static boolean hasJavaTimeModule() {
try {
Class.forName("java.time.Duration");
Class.forName("java.time.Instant");
Class.forName("java.time.ZonedDateTime");
Class.forName("java.time.ZoneId");
return true;
} catch (final ClassNotFoundException | LinkageError notSupported) {
}
return false;
}
/**
* Fails if any declared types are outside of the logging-mailhandler.jar.
* This includes classes from the Jakarta Mail spec.
*
* @param k the type to check for dependencies.
* @throws Exception if there is a problem.
*/
final void testLinkage(final Class<?> k) throws Exception {
testLinkage(k, true);
}
/**
* Fails if any declared types are outside of the logging-mailhandler.jar.
*
* @param k the type to check for dependencies.
* @param includeSpec if true this includes official JakartaMail spec classes.
* @throws Exception if there is a problem.
*/
final void testLinkage(final Class<?> k, final boolean includeSpec)
throws Exception {
assertFalse(k.getName(), isFromJakartaMail(k, includeSpec));
for (Annotation an : k.getDeclaredAnnotations()) {
assertFalse(an.toString(),
isFromJakartaMail(an.annotationType(), includeSpec));
}
for (Method m : k.getDeclaredMethods()) {
assertFalse(m.getReturnType().getName(),
isFromJakartaMail(m.getReturnType(), includeSpec));
for (Class<?> p : m.getParameterTypes()) {
assertFalse(p.getName(), isFromJakartaMail(p, includeSpec));
}
for (Class<?> e : m.getExceptionTypes()) {
assertFalse(e.getName(), isFromJakartaMail(e, includeSpec));
}
for (Annotation an : m.getDeclaredAnnotations()) {
assertFalse(an.toString(),
isFromJakartaMail(an.annotationType(), includeSpec));
}
}
for (Constructor<?> c : k.getDeclaredConstructors()) {
for (Class<?> p : c.getParameterTypes()) {
assertFalse(p.getName(), isFromJakartaMail(p, includeSpec));
}
for (Class<?> e : c.getExceptionTypes()) {
assertFalse(e.getName(), isFromJakartaMail(e, includeSpec));
}
for (Annotation an : c.getDeclaredAnnotations()) {
assertFalse(an.toString(),
isFromJakartaMail(an.annotationType(), includeSpec));
}
}
for (Field f : k.getDeclaredFields()) {
for (Annotation an : k.getDeclaredAnnotations()) {
assertFalse(an.toString(),
isFromJakartaMail(an.annotationType(), includeSpec));
}
assertFalse(f.getName(), isFromJakartaMail(f.getType(), includeSpec));
}
}
/**
* Tests that the private static loadDeclaredClasses method of the given
* type. Objects used by the MailHandler during a push might require
* declaring classes to be loaded on create since a push may happen after a
* class loader is shutdown.
*
* @param k the type to check never null.
* @throws Exception if there is a problem.
*/
final void testLoadDeclaredClasses(Class<?> k) throws Exception {
Method m = k.getDeclaredMethod("loadDeclaredClasses");
assertTrue(Modifier.isStatic(m.getModifiers()));
assertTrue(Modifier.isPrivate(m.getModifiers()));
assertEquals(Class[].class, m.getReturnType());
m.setAccessible(true);
Class<?>[] named = (Class<?>[]) m.invoke((Object) null);
assertTrue(named.length != 0);
HashSet<Class<?>> declared = new HashSet<>(
Arrays.<Class<?>>asList(k.getDeclaredClasses()));
for (Class<?> c : named) {
assertEquals(c.toString(), k, c.getEnclosingClass());
assertTrue(c.getDeclaredClasses().length == 0);
declared.remove(c);
}
assertTrue(declared.toString(), declared.isEmpty());
}
/**
* Checks that the given class is visible to the LogManager.
*
* @param c the class to check.
* @throws Exception if there is a problem.
*/
final void testLogManagerModifiers(final Class<?> c) throws Exception {
assertTrue(Modifier.isPublic(c.getModifiers()));
assertTrue(Modifier.isPublic(c.getConstructor().getModifiers()));
}
/**
* Checks that the given class is not dependent on the
* {@code javax.annotation} classes as they are not present in all
* environments.
*
* @param k the class to inspect.
* @throws Exception if there is a problem.
*/
final void testNoDependencyOnJavaxAnnotations(Class<?> k) throws Exception {
for (Method m : k.getDeclaredMethods()) {
testNoJavaxAnnotation(m);
}
for (Field f : k.getDeclaredFields()) {
testNoJavaxAnnotation(f);
}
for (Constructor<?> c : k.getDeclaredConstructors()) {
testNoJavaxAnnotation(c);
}
for (Class<?> i : k.getInterfaces()) {
testNoJavaxAnnotation(i);
}
for (Class<?> d : k.getDeclaredClasses()) {
testNoDependencyOnJavaxAnnotations(d);
}
}
/**
* WebappClassLoader.clearReferencesStaticFinal() method will ignore fields
* that have type names that start with 'java.' or 'javax.'. This test
* checks that the given class conforms to this rule so it doesn't become a
* target that will be nullified by the WebappClassLoader.
*
* @param c the class to check.
* @throws Exception if there is a problem.
*/
final void testWebappClassLoaderFieldNames(Class<?> c) throws Exception {
for (Field f : c.getDeclaredFields()) {
Class<?> k = f.getType();
while (k.isArray()) {
k = k.getComponentType();
}
/**
* The WebappClassLoader ignores primitives, non-static, and
* synthetic fields. For the logging API, this test is stricter than
* what the WebappClassLoader actually clears. This restricts the
* logging API to standard field types for both static and
* non-static fields. The idea is to try to stay forward compatible
* with WebappClassLoader.
*/
if (f.getName().indexOf('$') < 0 && !k.isPrimitive()
&& !k.getName().startsWith("java.")
&& !k.getName().startsWith("javax.")
&& !k.getName().startsWith("jakarta.")) {
fail(f.toString());
}
}
for (Class<?> ic : c.getDeclaredClasses()) {
testWebappClassLoaderFieldNames(ic);
}
}
/**
* Blocks the current thread until current time has elapsed by one
* millisecond.
*
* @throws InterruptedException if the current thread is interrupted.
*/
static void tickMilli() throws InterruptedException {
tickMilli(1L);
}
/**
* Blocks the current thread until current time has elapsed by the given
* delay in milliseconds.
*
* @param delay the number of milliseconds that have to elapse.
* @throws IllegalArgumentException if the given delay is zero or less.
* @throws InterruptedException if the current thread is interrupted.
*/
@SuppressWarnings("SleepWhileInLoop")
static void tickMilli(long delay) throws InterruptedException {
if (delay <= 0L) {
throw new IllegalArgumentException(Long.toString(delay));
}
long then = System.currentTimeMillis();
for (int i = 0; i < Short.MAX_VALUE; i++) {
long now = System.currentTimeMillis();
long delta = (now - then);
if (delta >= delay) {
return;
}
Thread.sleep(delay - delta);
}
throw new AssertionError(then + " " + System.currentTimeMillis());
}
private boolean isFromJakartaMail(Class<?> k, boolean include) throws Exception {
for (Class<?> t = k; t != null; t = t.getSuperclass()) {
final String n = t.getName();
if (n.startsWith("jakarta.mail.")) {
return include;
}
//Not included with logging-mailhandler.jar.
if (n.startsWith("com.sun.mail.")
&& !n.startsWith("com.sun.mail.util.logging.")) {
return true;
}
}
return false;
}
private void testNoJavaxAnnotation(AccessibleObject fm) throws Exception {
for (Annotation a : fm.getAnnotations()) {
testNoJavaxAnnotation(a.annotationType());
}
}
private void testNoJavaxAnnotation(Class<?> k) throws Exception {
for (; k != null; k = k.getSuperclass()) {
assertFalse(k.toString(),
k.getName().startsWith("javax.annotation"));
}
}
}