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