View Javadoc

1   /**
2    * Copyright 2005-2013 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.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          // TODO really the constant value should be "krad" but there's some work to do in order to make
50          // that really work, see KULRICE-6532
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          // return a lower value which will give the data dictionary indexing higher precedence since DD indexing should
94          // be started as soon as it can be
95          return -1000;
96      }
97  
98      /**
99       * Used to "poke" the Data Dictionary again after the Spring Context is initialized.  This is to
100      * allow for modules loaded with KualiModule after the KNS has already been initialized to work.
101      *
102      * Also initializes the DateTimeService
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             // KULRICE-4513 After the Data Dictionary is loaded and validated, perform Data Dictionary bean overrides.
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      * Returns true - KNS UI should always be included.
159      *
160      * @see org.kuali.rice.core.framework.config.module.ModuleConfigurer#shouldRenderWebInterface()
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      * Used to "poke" the Data Dictionary again after the Spring Context is initialized.  This is to
193      * allow for modules loaded with KualiModule after the KNS has already been initialized to work.
194      *
195      * Also initializes the DateTimeService
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      * Indicates whether the legacy KNS module should be included which will include
214      * the KNS spring beans file
215      *
216      * @return boolean true if kns should be supported, false if not
217      */
218     public boolean isIncludeKnsSpringBeans() {
219         return includeKnsSpringBeans;
220     }
221 
222     /**
223      * Setter for the include kns support indicator
224      *
225      * @param includeKnsSpringBeans
226      */
227     public void setIncludeKnsSpringBeans(boolean includeKnsSpringBeans) {
228         this.includeKnsSpringBeans = includeKnsSpringBeans;
229     }
230 }