1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.datadictionary; |
17 | |
|
18 | |
import no.geosoft.cc.io.FileListener; |
19 | |
import no.geosoft.cc.io.FileMonitor; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
24 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
25 | |
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
26 | |
import org.springframework.core.io.FileSystemResource; |
27 | |
import org.springframework.core.io.InputStreamResource; |
28 | |
import org.springframework.core.io.Resource; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.io.InputStream; |
32 | |
import java.net.URL; |
33 | |
import java.util.ArrayList; |
34 | |
import java.util.List; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class ReloadingDataDictionary extends DataDictionary implements FileListener, URLMonitor.URLContentChangedListener { |
55 | 0 | private static final Log LOG = LogFactory.getLog(DataDictionary.class); |
56 | |
|
57 | |
private static final String CLASS_DIR_CONFIG_PARM = "reload.data.dictionary.classes.dir"; |
58 | |
private static final String SOURCE_DIR_CONFIG_PARM = "reload.data.dictionary.source.dir"; |
59 | |
private static final String INTERVAL_CONFIG_PARM = "reload.data.dictionary.interval"; |
60 | |
|
61 | |
private URLMonitor dictionaryUrlMonitor; |
62 | |
|
63 | |
|
64 | |
public ReloadingDataDictionary() { |
65 | 0 | super(); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Override |
75 | |
public void parseDataDictionaryConfigurationFiles(boolean allowConcurrentValidation) { |
76 | 0 | ConfigurationService configurationService = KRADServiceLocator.getKualiConfigurationService(); |
77 | |
|
78 | |
|
79 | 0 | String classesDir = configurationService.getPropertyValueAsString(CLASS_DIR_CONFIG_PARM); |
80 | |
|
81 | |
|
82 | 0 | String sourceDir = configurationService.getPropertyValueAsString(SOURCE_DIR_CONFIG_PARM); |
83 | |
|
84 | |
|
85 | 0 | int reloadInterval = Integer.parseInt(configurationService.getPropertyValueAsString(INTERVAL_CONFIG_PARM)); |
86 | |
|
87 | 0 | FileMonitor dictionaryFileMonitor = new FileMonitor(reloadInterval); |
88 | |
|
89 | 0 | dictionaryUrlMonitor = new URLMonitor(reloadInterval); |
90 | 0 | dictionaryUrlMonitor.addListener(this); |
91 | |
|
92 | |
|
93 | |
|
94 | 0 | List<String> configLocations = new ArrayList<String>(configFileLocations); |
95 | |
|
96 | 0 | super.parseDataDictionaryConfigurationFiles(allowConcurrentValidation); |
97 | 0 | for (String configLocation : configLocations) { |
98 | 0 | Resource classFileResource = getFileResource(configLocation); |
99 | |
try { |
100 | 0 | if (classFileResource.getURI().toString().startsWith("jar:")) { |
101 | 0 | LOG.debug("Monitoring dictionary file at URI: " + classFileResource.getURI().toString()); |
102 | 0 | dictionaryUrlMonitor.addURI(classFileResource.getURL()); |
103 | |
} else { |
104 | 0 | String filePathClassesDir = classFileResource.getFile().getAbsolutePath(); |
105 | 0 | String sourceFilePath = StringUtils.replace(filePathClassesDir, classesDir, sourceDir); |
106 | 0 | File dictionaryFile = new File(filePathClassesDir); |
107 | 0 | if (dictionaryFile.exists()) { |
108 | 0 | LOG.debug("Monitoring dictionary file: " + dictionaryFile.getName()); |
109 | 0 | dictionaryFileMonitor.addFile(dictionaryFile); |
110 | |
} |
111 | |
} |
112 | |
} |
113 | 0 | catch (Exception e) { |
114 | 0 | LOG.info("Exception in picking up dictionary file for monitoring: " + e.getMessage(), e); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
|
119 | 0 | dictionaryFileMonitor.addListener(this); |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
@Override |
130 | |
public void fileChanged(File file) { |
131 | 0 | LOG.info("reloading dictionary configuration for " + file.getName()); |
132 | |
try { |
133 | 0 | Resource resource = new FileSystemResource(file); |
134 | 0 | xmlReader.loadBeanDefinitions(resource); |
135 | |
|
136 | |
|
137 | 0 | ddIndex.run(); |
138 | |
} |
139 | 0 | catch (Exception e) { |
140 | 0 | LOG.info("Exception in dictionary hot deploy: " + e.getMessage(), e); |
141 | 0 | } |
142 | 0 | } |
143 | |
|
144 | |
public void urlContentChanged(final URL url) { |
145 | 0 | LOG.info("reloading dictionary configuration for " + url.toString()); |
146 | |
try { |
147 | 0 | InputStream urlStream = url.openStream(); |
148 | 0 | InputStreamResource resource = new InputStreamResource(urlStream); |
149 | |
|
150 | 0 | int originalValidationMode = xmlReader.getValidationMode(); |
151 | 0 | xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD); |
152 | 0 | xmlReader.loadBeanDefinitions(resource); |
153 | 0 | xmlReader.setValidationMode(originalValidationMode); |
154 | |
|
155 | |
|
156 | 0 | ddIndex.run(); |
157 | |
} |
158 | 0 | catch (Exception e) { |
159 | 0 | LOG.info("Exception in dictionary hot deploy: " + e.getMessage(), e); |
160 | 0 | } |
161 | 0 | } |
162 | |
} |