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
63 ConfigContext.getCurrentContextConfig().putProperty(KRADConstants.Config.KNS_ENABLED, Boolean.valueOf(isIncludeKnsSpringBeans()).toString());
64 configureDataSource();
65 }
66
67 @Override
68 public List<String> getPrimarySpringFiles() {
69 final List<String> springFileLocations = new ArrayList<String>();
70 springFileLocations.add(KRAD_SPRING_BEANS_PATH);
71
72 if (isIncludeKnsSpringBeans()) {
73 springFileLocations.add(KNS_SPRING_BEANS_PATH);
74 }
75
76 return springFileLocations;
77 }
78
79 @Override
80 public void onApplicationEvent(ApplicationEvent applicationEvent) {
81 if (applicationEvent instanceof ContextRefreshedEvent) {
82 loadDataDictionary();
83 publishDataDictionaryComponents();
84 }
85 }
86
87 @Override
88 public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
89 return true;
90 }
91
92 @Override
93 public boolean supportsSourceType(Class<?> aClass) {
94 return true;
95 }
96
97 @Override
98 public int getOrder() {
99
100
101 return -1000;
102 }
103
104
105
106
107
108
109
110 protected void loadDataDictionary() {
111 if (isLoadDataDictionary()) {
112 LOG.info("KRAD Configurer - Loading DD");
113 DataDictionaryService dds = KRADServiceLocatorWeb.getDataDictionaryService();
114 if (dds == null) {
115 dds = (DataDictionaryService) GlobalResourceLoader
116 .getService(KRADServiceLocatorWeb.DATA_DICTIONARY_SERVICE);
117 }
118 dds.getDataDictionary().parseDataDictionaryConfigurationFiles(false);
119
120 if (isValidateDataDictionary()) {
121 LOG.info("KRAD Configurer - Validating DD");
122 dds.getDataDictionary().validateDD(isValidateDataDictionaryEboReferences());
123 }
124
125
126 dds.getDataDictionary().performBeanOverrides();
127 }
128 }
129
130 protected void publishDataDictionaryComponents() {
131 if (isComponentPublishingEnabled()) {
132 long delay = getComponentPublishingDelay();
133 LOG.info("Publishing of Data Dictionary components is enabled, scheduling publish after " + delay + " millisecond delay");
134 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
135 try {
136 scheduler.schedule(new Runnable() {
137 @Override
138 public void run() {
139 long start = System.currentTimeMillis();
140 LOG.info("Executing scheduled Data Dictionary component publishing...");
141 try {
142 KRADServiceLocatorWeb.getDataDictionaryComponentPublisherService().publishAllComponents();
143 } catch (RuntimeException e) {
144 LOG.error("Failed to publish data dictionary components.", e);
145 throw e;
146 } finally {
147 long end = System.currentTimeMillis();
148 LOG.info("... finished scheduled execution of Data Dictionary component publishing. Took " + (end-start) + " milliseconds");
149 }
150 }
151 }, delay, TimeUnit.MILLISECONDS);
152 } finally {
153 scheduler.shutdown();
154 }
155 }
156 }
157
158 @Override
159 public boolean hasWebInterface() {
160 return true;
161 }
162
163
164
165
166
167
168 @Override
169 public boolean shouldRenderWebInterface() {
170 return true;
171 }
172
173
174
175 public boolean isLoadDataDictionary() {
176 return ConfigContext.getCurrentContextConfig().getBooleanProperty("load.data.dictionary", true);
177 }
178
179 public boolean isValidateDataDictionary() {
180 return ConfigContext.getCurrentContextConfig().getBooleanProperty("validate.data.dictionary", false);
181 }
182
183 public boolean isValidateDataDictionaryEboReferences() {
184 return ConfigContext.getCurrentContextConfig().getBooleanProperty("validate.data.dictionary.ebo.references",
185 false);
186 }
187
188 public boolean isComponentPublishingEnabled() {
189 return ConfigContext.getCurrentContextConfig().getBooleanProperty(
190 KRADConstants.Config.COMPONENT_PUBLISHING_ENABLED, false);
191 }
192
193 public long getComponentPublishingDelay() {
194 return ConfigContext.getCurrentContextConfig().getNumericProperty(KRADConstants.Config.COMPONENT_PUBLISHING_DELAY, 0);
195 }
196
197
198
199
200
201
202
203 protected void configureDataSource() {
204 if (getApplicationDataSource() != null) {
205 ConfigContext.getCurrentContextConfig()
206 .putObject(KRADConstants.KRAD_APPLICATION_DATASOURCE, getApplicationDataSource());
207 }
208 }
209
210 public DataSource getApplicationDataSource() {
211 return this.applicationDataSource;
212 }
213
214 public void setApplicationDataSource(DataSource applicationDataSource) {
215 this.applicationDataSource = applicationDataSource;
216 }
217
218
219
220
221
222
223
224 public boolean isIncludeKnsSpringBeans() {
225 return includeKnsSpringBeans;
226 }
227
228
229
230
231
232
233 public void setIncludeKnsSpringBeans(boolean includeKnsSpringBeans) {
234 this.includeKnsSpringBeans = includeKnsSpringBeans;
235 }
236 }