1 package eu.sydisnet.blog.samples.deltaspike.cdi12.boundary;
2
3 import eu.sydisnet.blog.samples.deltaspike.cdi12.control.MessageFormatter;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import javax.annotation.PostConstruct;
8 import javax.annotation.PreDestroy;
9 import javax.inject.Inject;
10 import javax.inject.Singleton;
11 import java.lang.invoke.MethodHandles;
12
13
14
15
16
17
18
19
20
21 @Singleton
22 public class SingletonHelloService {
23
24
25
26
27 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
28
29
30
31
32 private static final String UNKNOWN_USERNAME = "M. John Doe";
33
34
35
36
37 private String defaultMessage;
38
39
40
41
42 @Inject
43 private MessageFormatter messageFormatter;
44
45
46
47
48
49
50 @PostConstruct
51 void initBean() {
52 defaultMessage = "Welcome %s !";
53
54 LOG.info(String.format("##### %s stopped !", MethodHandles.lookup().lookupClass().getSimpleName()));
55 }
56
57
58
59
60
61
62 @PreDestroy
63 void tearDown() {
64 LOG.info(String.format("##### %s stopped !", MethodHandles.lookup().lookupClass().getSimpleName()));
65 }
66
67
68
69
70
71
72
73 public String sayHello(final String userName) {
74 if (userName == null || userName.isEmpty()) {
75 return messageFormatter.format(defaultMessage, UNKNOWN_USERNAME);
76 }
77
78 return messageFormatter.format(defaultMessage, userName);
79 }
80 }