1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.computerlabs.dao;
16
17 import java.io.IOException;
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.apache.log4j.Logger;
25 import org.kuali.mobility.computerlabs.entity.Lab;
26 import org.kuali.mobility.computerlabs.entity.LabGroup;
27 import org.kuali.mobility.computerlabs.entity.Location;
28 import org.kuali.mobility.util.mapper.DataMapper;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.context.ApplicationContextAware;
31
32
33
34
35
36 public class ComputerLabsInitBean implements ApplicationContextAware {
37
38 private static Logger LOG = Logger.getLogger(ComputerLabsInitBean.class);
39 private ApplicationContext applicationContext;
40 private ComputerLabsDao dao;
41 private int waitInterval = 300;
42 private static Thread backgroundThread = null;
43 private Map<String, List<String>> labUrls;
44 private DataMapper dataMapper;
45 private String dataMappingUrl;
46 private boolean shouldRun = true;
47
48 public void init() {
49 backgroundThread = new Thread(new ComputerLabsInitBean.BackgroundThread());
50 backgroundThread.setDaemon(true);
51 backgroundThread.start();
52 }
53
54 public void cleanup() {
55 LOG.info("Cleaning up computer lab data.");
56 }
57
58
59
60
61 public ComputerLabsDao getDao() {
62 return dao;
63 }
64
65
66
67
68 public void setDao(ComputerLabsDao dao) {
69 this.dao = dao;
70 }
71
72
73
74
75 public ApplicationContext getApplicationContext() {
76 return applicationContext;
77 }
78
79
80
81
82 public void setApplicationContext(ApplicationContext applicationContext) {
83 this.applicationContext = applicationContext;
84 }
85
86
87
88
89 public int getWaitInterval() {
90 return waitInterval;
91 }
92
93
94
95
96 public void setWaitInterval(int waitInterval) {
97 this.waitInterval = waitInterval;
98 }
99
100
101
102
103 public Map<String, List<String>> getLabUrls() {
104 return labUrls;
105 }
106
107
108
109
110 public void setLabUrls(Map<String, List<String>> labUrls) {
111 this.labUrls = labUrls;
112 }
113
114
115
116
117 public DataMapper getDataMapper() {
118 return dataMapper;
119 }
120
121
122
123
124 public void setDataMapper(DataMapper dataMapper) {
125 this.dataMapper = dataMapper;
126 }
127
128
129
130
131 public String getDataMappingUrl() {
132 return dataMappingUrl;
133 }
134
135
136
137
138 public void setDataMappingUrl(String dataMappingUrl) {
139 this.dataMappingUrl = dataMappingUrl;
140 }
141
142 public void loadData() {
143 List<LabGroup> groups = new ArrayList<LabGroup>();
144 for (String key : getLabUrls().keySet()) {
145 LabGroup group = (LabGroup) getApplicationContext().getBean("computerLabGroupBean");
146 group.setName(key);
147 LOG.debug("Processing lab group " + key);
148 for (String groupUrl : getLabUrls().get(key)) {
149 try {
150 LOG.debug("SourceUrl: " + groupUrl);
151 List<Lab> groupLabs = new ArrayList<Lab>();
152 try {
153 URL url = new URL(groupUrl);
154 if (getDataMappingUrl() != null && !"".equals(getDataMappingUrl().trim())) {
155 groupLabs = getDataMapper().mapData(groupLabs, url, new URL(getDataMappingUrl()));
156 } else {
157 groupLabs = getDataMapper().mapData(groupLabs, url, "labMapping.xml");
158 }
159 } catch (MalformedURLException mue) {
160 LOG.error(mue.getLocalizedMessage(), mue);
161 LOG.debug( "Bad URL, attempting to load data as file in classpath: "+groupUrl);
162 groupLabs = getDataMapper().mapData(groupLabs, groupUrl, "labMapping.xml");
163 }
164 LOG.debug("Loaded " + groupLabs.size() + " labs for group " + group.getName());
165 Map<String, Location> locations = new LinkedHashMap<String, Location>();
166 for (Lab lab : groupLabs) {
167 Location location = null;
168 if (locations.containsKey(lab.getBuildingCode())) {
169 location = locations.get(lab.getBuildingCode());
170 } else {
171 location = (Location) getApplicationContext().getBean("computerLabLocationBean");
172 location.setName(lab.getBuildingCode());
173 locations.put(lab.getBuildingCode(), location);
174 }
175 location.addLab(lab);
176 }
177 LOG.debug("Sorted into " + locations.size() + " locations.");
178 group.setLocations(new ArrayList(locations.values()));
179 } catch (MalformedURLException mue) {
180 LOG.error(mue.getLocalizedMessage(), mue);
181 } catch (ClassNotFoundException cnfe) {
182 LOG.error(cnfe.getLocalizedMessage(), cnfe);
183 } catch (IOException io) {
184 LOG.error(io.getLocalizedMessage(), io);
185 } catch (Exception e) {
186 LOG.error(e.getLocalizedMessage(), e);
187 }
188 }
189 groups.add(group);
190 }
191 LOG.debug("Preparing to set " + groups.size() + " lab groups in dao.");
192 getDao().setLabGroups(groups);
193 }
194
195
196
197
198 public boolean shouldRun() {
199 return shouldRun;
200 }
201
202
203
204
205 public void setShouldRun(boolean shouldRun) {
206 this.shouldRun = shouldRun;
207 }
208
209 private class BackgroundThread implements Runnable {
210
211 @Override
212 public void run() {
213 while (shouldRun()) {
214 try {
215
216
217
218
219
220
221 Thread.sleep(1000 * getWaitInterval());
222 loadData();
223
224 } catch (InterruptedException ie) {
225 LOG.error(ie.getLocalizedMessage(), ie);
226 } catch (NullPointerException npe) {
227
228 LOG.error(npe.getLocalizedMessage(), npe);
229 } catch (Exception e) {
230 LOG.error(e.getLocalizedMessage(), e);
231 }
232 }
233 }
234 }
235
236
237
238
239 public static Thread getBackgroundThread() {
240 return backgroundThread;
241 }
242
243
244
245
246 public static void setBackgroundThread(Thread aBackgroundThread) {
247 backgroundThread = aBackgroundThread;
248 }
249 }