1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.config;
17
18 import org.kuali.rice.core.api.config.module.RunMode;
19 import org.kuali.rice.core.api.config.property.ConfigContext;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.kuali.rice.core.framework.config.module.ModuleConfigurer;
22 import org.kuali.rice.krad.service.DataDictionaryService;
23 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24 import org.kuali.rice.krad.util.KRADConstants;
25 import org.springframework.context.ApplicationEvent;
26 import org.springframework.context.ApplicationListener;
27 import org.springframework.context.event.ContextRefreshedEvent;
28 import org.springframework.context.event.SmartApplicationListener;
29
30 import javax.sql.DataSource;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.TimeUnit;
37
38
39
40
41
42
43 public class KRADConfigurer extends ModuleConfigurer implements SmartApplicationListener {
44
45 private DataSource applicationDataSource;
46
47 private boolean includeKnsSpringBeans;
48
49 private static final String KRAD_SPRING_BEANS_PATH = "classpath:org/kuali/rice/krad/config/KRADSpringBeans.xml";
50 private static final String KNS_SPRING_BEANS_PATH = "classpath:org/kuali/rice/kns/config/KNSSpringBeans.xml";
51
52 public KRADConfigurer() {
53
54
55 super(KRADConstants.KR_MODULE_NAME);
56 setValidRunModes(Arrays.asList(RunMode.LOCAL));
57 setIncludeKnsSpringBeans(true);
58 }
59
60 @Override
61 public void addAdditonalToConfig() {
62 configureDataSource();
63 }
64
65 @Override
66 public List<String> getPrimarySpringFiles() {
67 final List<String> springFileLocations = new ArrayList<String>();
68 springFileLocations.add(KRAD_SPRING_BEANS_PATH);
69
70 if (isIncludeKnsSpringBeans()) {
71 springFileLocations.add(KNS_SPRING_BEANS_PATH);
72 }
73
74 return springFileLocations;
75 }
76
77 @Override
78 public void onApplicationEvent(ApplicationEvent applicationEvent) {
79 if (applicationEvent instanceof ContextRefreshedEvent) {
80 loadDataDictionary();
81 publishDataDictionaryComponents();
82 }
83 }
84
85 @Override
86 public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
87 return true;
88 }
89
90 @Override
91 public boolean supportsSourceType(Class<?> aClass) {
92 return true;
93 }
94
95 @Override
96 public int getOrder() {
97
98
99 return -1000;
100 }
101
102
103
104
105
106
107
108 protected void loadDataDictionary() {
109 if (isLoadDataDictionary()) {
110 LOG.info("KRAD Configurer - Loading DD");
111 DataDictionaryService dds = KRADServiceLocatorWeb.getDataDictionaryService();
112 if (dds == null) {
113 dds = (DataDictionaryService) GlobalResourceLoader
114 .getService(KRADServiceLocatorWeb.DATA_DICTIONARY_SERVICE);
115 }
116 dds.getDataDictionary().parseDataDictionaryConfigurationFiles(false);
117
118 if (isValidateDataDictionary()) {
119 LOG.info("KRAD Configurer - Validating DD");
120 dds.getDataDictionary().validateDD(isValidateDataDictionaryEboReferences());
121 }
122
123
124 dds.getDataDictionary().performBeanOverrides();
125 }
126 }
127
128 protected void publishDataDictionaryComponents() {
129 if (isComponentPublishingEnabled()) {
130 long delay = getComponentPublishingDelay();
131 LOG.info("Publishing of Data Dictionary components is enabled, scheduling publish after " + delay + " millisecond delay");
132 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
133 try {
134 scheduler.schedule(new Runnable() {
135 @Override
136 public void run() {
137 long start = System.currentTimeMillis();
138 LOG.info("Executing scheduled Data Dictionary component publishing...");
139 try {
140 KRADServiceLocatorWeb.getDataDictionaryComponentPublisherService().publishAllComponents();
141 } catch (RuntimeException e) {
142 LOG.error("Failed to publish data dictionary components.", e);
143 throw e;
144 } finally {
145 long end = System.currentTimeMillis();
146 LOG.info("... finished scheduled execution of Data Dictionary component publishing. Took " + (end-start) + " milliseconds");
147 }
148 }
149 }, delay, TimeUnit.MILLISECONDS);
150 } finally {
151 scheduler.shutdown();
152 }
153 }
154 }
155
156 @Override
157 public boolean hasWebInterface() {
158 return true;
159 }
160
161
162
163
164
165
166 @Override
167 public boolean shouldRenderWebInterface() {
168 return true;
169 }
170
171
172
173 public boolean isLoadDataDictionary() {
174 return ConfigContext.getCurrentContextConfig().getBooleanProperty("load.data.dictionary", true);
175 }
176
177 public boolean isValidateDataDictionary() {
178 return ConfigContext.getCurrentContextConfig().getBooleanProperty("validate.data.dictionary", false);
179 }
180
181 public boolean isValidateDataDictionaryEboReferences() {
182 return ConfigContext.getCurrentContextConfig().getBooleanProperty("validate.data.dictionary.ebo.references",
183 false);
184 }
185
186 public boolean isComponentPublishingEnabled() {
187 return ConfigContext.getCurrentContextConfig().getBooleanProperty(
188 KRADConstants.Config.COMPONENT_PUBLISHING_ENABLED, false);
189 }
190
191 public long getComponentPublishingDelay() {
192 return ConfigContext.getCurrentContextConfig().getNumericProperty(KRADConstants.Config.COMPONENT_PUBLISHING_DELAY, 0);
193 }
194
195
196
197
198
199
200
201 protected void configureDataSource() {
202 if (getApplicationDataSource() != null) {
203 ConfigContext.getCurrentContextConfig()
204 .putObject(KRADConstants.KRAD_APPLICATION_DATASOURCE, getApplicationDataSource());
205 }
206 }
207
208 public DataSource getApplicationDataSource() {
209 return this.applicationDataSource;
210 }
211
212 public void setApplicationDataSource(DataSource applicationDataSource) {
213 this.applicationDataSource = applicationDataSource;
214 }
215
216
217
218
219
220
221
222 public boolean isIncludeKnsSpringBeans() {
223 return includeKnsSpringBeans;
224 }
225
226
227
228
229
230
231 public void setIncludeKnsSpringBeans(boolean includeKnsSpringBeans) {
232 this.includeKnsSpringBeans = includeKnsSpringBeans;
233 }
234 }