View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Rice module configurer for KRAD
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
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          // TODO really the constant value should be "krad" but there's some work to do in order to make
54          // that really work, see KULRICE-6532
55          super(KRADConstants.KR_MODULE_NAME);
56          setValidRunModes(Arrays.asList(RunMode.LOCAL));
57          setIncludeKnsSpringBeans(true);
58      }
59  
60      @Override
61      public void addAdditonalToConfig() {
62          // export the state of KNS enablement to the global config
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          // return a lower value which will give the data dictionary indexing higher precedence since DD indexing should
100         // be started as soon as it can be
101         return -1000;
102     }
103 
104     /**
105      * Used to "poke" the Data Dictionary again after the Spring Context is initialized.  This is to
106      * allow for modules loaded with KualiModule after the KNS has already been initialized to work.
107      *
108      * Also initializes the DateTimeService
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             // KULRICE-4513 After the Data Dictionary is loaded and validated, perform Data Dictionary bean overrides.
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      * Returns true - KNS UI should always be included.
165      *
166      * @see org.kuali.rice.core.framework.config.module.ModuleConfigurer#shouldRenderWebInterface()
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      * Used to "poke" the Data Dictionary again after the Spring Context is initialized.  This is to
199      * allow for modules loaded with KualiModule after the KNS has already been initialized to work.
200      *
201      * Also initializes the DateTimeService
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      * Indicates whether the legacy KNS module should be included which will include
220      * the KNS spring beans file
221      *
222      * @return boolean true if kns should be supported, false if not
223      */
224     public boolean isIncludeKnsSpringBeans() {
225         return includeKnsSpringBeans;
226     }
227 
228     /**
229      * Setter for the include kns support indicator
230      *
231      * @param includeKnsSpringBeans
232      */
233     public void setIncludeKnsSpringBeans(boolean includeKnsSpringBeans) {
234         this.includeKnsSpringBeans = includeKnsSpringBeans;
235     }
236 }