1 |
package dk.daoas.daoadresseservice;
|
2 |
|
3 |
import java.sql.SQLException;
|
4 |
import java.util.Properties;
|
5 |
import java.util.logging.Logger;
|
6 |
|
7 |
import javax.mail.Message;
|
8 |
import javax.mail.Session;
|
9 |
import javax.mail.Transport;
|
10 |
import javax.mail.internet.InternetAddress;
|
11 |
import javax.mail.internet.MimeMessage;
|
12 |
import javax.servlet.ServletContext;
|
13 |
|
14 |
import dk.daoas.daoadresseservice.admin.ServiceConfig;
|
15 |
import dk.daoas.daoadresseservice.db.DatabaseLayerImplementation;
|
16 |
|
17 |
public class DataLoader {
|
18 |
|
19 |
boolean isRunning = false;
|
20 |
|
21 |
ServiceConfig config;
|
22 |
|
23 |
ServletContext ctxt;
|
24 |
public DataLoader(ServletContext ctxt,ServiceConfig conf) {
|
25 |
this.ctxt = ctxt;
|
26 |
|
27 |
config = conf;
|
28 |
}
|
29 |
|
30 |
public void doLoad() {
|
31 |
synchronized(this) {
|
32 |
if (isRunning == false) {
|
33 |
isRunning = true;
|
34 |
|
35 |
Thread t = new Thread( new DataLoadWorker(this), "SearchDataLoader");
|
36 |
t.start();
|
37 |
}
|
38 |
}
|
39 |
}
|
40 |
|
41 |
public boolean isRunning() {
|
42 |
synchronized(this) {
|
43 |
return isRunning;
|
44 |
}
|
45 |
}
|
46 |
|
47 |
protected void setRunningFalse() {
|
48 |
synchronized(this) {
|
49 |
isRunning = false;
|
50 |
}
|
51 |
}
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
public static class DataLoadWorker implements Runnable {
|
57 |
|
58 |
static final Logger logger = Logger.getLogger(DataLoadWorker.class.getName());
|
59 |
|
60 |
DataLoader loader;
|
61 |
public DataLoadWorker(DataLoader ld) {
|
62 |
this.loader = ld;
|
63 |
}
|
64 |
|
65 |
|
66 |
@Override
|
67 |
public void run() {
|
68 |
|
69 |
|
70 |
try {
|
71 |
AddressSearch search = new AddressSearch(new DatabaseLayerImplementation(), loader.config);
|
72 |
search.buildSearchStructures();
|
73 |
|
74 |
AddressSearch oldSearch = (AddressSearch) loader.ctxt.getAttribute("search");
|
75 |
loader.ctxt.setAttribute("search", search);
|
76 |
|
77 |
if (loader.config.notifyEmail != null && loader.config.notifyEmail.length() > 0) {
|
78 |
sendStatusEmail(search, oldSearch);
|
79 |
}
|
80 |
|
81 |
if (oldSearch != null) {
|
82 |
oldSearch.clear();
|
83 |
}
|
84 |
|
85 |
} catch (SQLException e) {
|
86 |
System.out.println("Error loading data: " + e.getMessage());
|
87 |
} finally {
|
88 |
loader.setRunningFalse();
|
89 |
}
|
90 |
|
91 |
}
|
92 |
|
93 |
protected void sendStatusEmail(AddressSearch newSearch, AddressSearch oldSearch) {
|
94 |
Properties props = new Properties();
|
95 |
props.put("mail.smtp.host", "10.30.1.28");
|
96 |
|
97 |
try {
|
98 |
// create some properties and get the default Session
|
99 |
Session session = Session.getDefaultInstance(props, null);
|
100 |
session.setDebug(false);
|
101 |
|
102 |
|
103 |
// create a message
|
104 |
|
105 |
MimeMessage msg = new MimeMessage(session);
|
106 |
msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
|
107 |
|
108 |
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(loader.config.notifyEmail) );
|
109 |
msg.setSubject("DaoAdresseService status ");
|
110 |
msg.setText("--");
|
111 |
|
112 |
Transport.send(msg);
|
113 |
} catch (Exception e) {
|
114 |
logger.warning("Unable to send CircuitBreaker notification mail " + e );
|
115 |
}
|
116 |
}
|
117 |
|
118 |
|
119 |
}
|
120 |
|
121 |
}
|