forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailer.java
More file actions
118 lines (100 loc) · 3.45 KB
/
Mailer.java
File metadata and controls
118 lines (100 loc) · 3.45 KB
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
package act.mail;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.E;
import org.osgl.util.S;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
/**
* A Tag annotation indicate a class or method is used to preparing and send out email
* <p>Note if the annotation is tagged on a class, then framework will treat all public
* void method with name start with "send" as sender method</p>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Mailer {
/**
* Defines the {@link MailerConfig mailer configure ID}.
* <p>Default value: {@code "default"}</p>
*/
String value() default "default";
public static class Util {
protected static final Logger logger = L.get(Mailer.class);
public static Future<Boolean> send(Object... args) {
throw E.unsupport("to be enhanced");
}
public static MailerContext mailer() {
throw E.unsupport("to be enhanced");
}
public static void from(String from) {
ctx().from = from;
}
public static void to(String ... to) {
ctx().to = S.join(",", to);
}
public static void cc(String ... cc) {
ctx().cc = S.join(",", cc);
}
public static void bcc(String ... bcc) {
ctx().bcc = S.join(",", bcc);
}
public static void subject(String subject) {
ctx().subject = subject;
}
private static SimpleContext ctx() {
return _ctx.get();
}
public static Future<Boolean> doSendWithoutLoadThreadLocal(final MailerContext context) {
return context.app().jobManager().now(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return context.send();
}
});
}
public static Future<Boolean> doSend(final MailerContext context) {
SimpleContext ctx = _ctx.get();
if (null != ctx) {
if (S.notBlank(ctx.from)) {
context.from(ctx.from);
}
if (S.notBlank(ctx.to)) {
context.to(ctx.to);
}
if (S.notBlank(ctx.cc)) {
context.cc(ctx.cc);
}
if (S.notBlank(ctx.bcc)) {
context.bcc(ctx.bcc);
}
if (S.notBlank(ctx.subject)) {
context.subject(ctx.subject);
}
_ctx.remove();
}
return context.app().jobManager().now(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return context.send();
}
});
}
private static final ThreadLocal<SimpleContext> _ctx = new ThreadLocal<SimpleContext>() {
@Override
protected SimpleContext initialValue() {
return new SimpleContext();
}
};
private static class SimpleContext {
String from;
String to;
String cc;
String bcc;
String subject;
}
}
}