/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/DataLoader.java
ViewVC logotype

Contents of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/DataLoader.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2608 - (show annotations) (download)
Wed Jul 15 06:52:05 2015 UTC (8 years, 10 months ago) by torben
File size: 1672 byte(s)
Separate database interface and implementation (preparation for mocking and more unit tests)
1 package dk.daoas.daoadresseservice;
2
3 import java.sql.SQLException;
4
5 import javax.servlet.ServletContext;
6
7 import dk.daoas.daoadresseservice.admin.ServiceConfig;
8 import dk.daoas.daoadresseservice.db.DatabaseLayerImplementation;
9
10 public class DataLoader {
11
12 boolean isRunning = false;
13
14 ServiceConfig config;
15
16 ServletContext ctxt;
17 public DataLoader(ServletContext ctxt,ServiceConfig conf) {
18 this.ctxt = ctxt;
19
20 config = conf;
21 }
22
23 public void doLoad() {
24 synchronized(this) {
25 if (isRunning == false) {
26 isRunning = true;
27
28 Thread t = new Thread( new DataLoadWorker(this), "SearchDataLoader");
29 t.start();
30 }
31 }
32 }
33
34 public boolean isRunning() {
35 synchronized(this) {
36 return isRunning;
37 }
38 }
39
40 protected void setRunningFalse() {
41 synchronized(this) {
42 isRunning = false;
43 }
44 }
45
46
47
48
49 public static class DataLoadWorker implements Runnable {
50 DataLoader loader;
51 public DataLoadWorker(DataLoader ld) {
52 this.loader = ld;
53 }
54
55
56 @Override
57 public void run() {
58
59
60 try {
61 AddressSearch search = new AddressSearch(new DatabaseLayerImplementation(), loader.config);
62 search.buildSearchStructures();
63
64 AddressSearch oldSearch = (AddressSearch) loader.ctxt.getAttribute("search");
65 loader.ctxt.setAttribute("search", search);
66
67 if (oldSearch != null) {
68 oldSearch.clear();
69 }
70
71 } catch (SQLException e) {
72 System.out.println("Error loading data: " + e.getMessage());
73 } finally {
74 loader.setRunningFalse();
75 }
76
77 }
78
79
80 }
81
82 }

  ViewVC Help
Powered by ViewVC 1.1.20