View Javadoc

1   /**
2    * Copyright 2005-2011 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.ConfigurationException;
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.impl.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.kuali.rice.ksb.api.KsbApiServiceLocator;
27  
28  import javax.sql.DataSource;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.concurrent.Executors;
32  import java.util.concurrent.ScheduledExecutorService;
33  import java.util.concurrent.TimeUnit;
34  
35  public class KRADConfigurer extends ModuleConfigurer {
36  
37      private DataSource applicationDataSource;
38      private DataSource serverDataSource;
39  
40      private boolean includeKnsSpringBeans;
41  
42      private static final String KRAD_SPRING_BEANS_PATH = "classpath:org/kuali/rice/krad/config/KRADSpringBeans.xml";
43      private static final String KNS_SPRING_BEANS_PATH = "classpath:org/kuali/rice/kns/config/KNSSpringBeans.xml";
44  
45      @Override
46      public void addAdditonalToConfig() {
47          configureDataSource();
48      }
49  
50      @Override
51      public List<String> getPrimarySpringFiles() {
52          final List<String> springFileLocations = new ArrayList<String>();
53          springFileLocations.add(KRAD_SPRING_BEANS_PATH);
54  
55          if (isExposeServicesOnBus()) {
56              //TODO FIXME hack!  KRAD should not be loading core!  (needed for now to publish core services)
57              springFileLocations.add("classpath:org/kuali/rice/core/config/COREServiceBusSpringBeans.xml");
58          }
59  
60          if (isIncludeKnsSpringBeans()) {
61              springFileLocations.add(KNS_SPRING_BEANS_PATH);
62          }
63  
64          return springFileLocations;
65      }
66  
67      @Override
68      public void doAdditionalContextStartedLogic() {
69          loadDataDictionary();
70          publishDataDictionaryComponents();
71      }
72  
73      /**
74       * Used to "poke" the Data Dictionary again after the Spring Context is initialized.  This is to
75       * allow for modules loaded with KualiModule after the KNS has already been initialized to work.
76       *
77       * Also initializes the DateTimeService
78       */
79      protected void loadDataDictionary() {
80          if (isLoadDataDictionary()) {
81              LOG.info("KRAD Configurer - Loading DD");
82              DataDictionaryService dds = KRADServiceLocatorWeb.getDataDictionaryService();
83              if (dds == null) {
84                  dds = (DataDictionaryService) GlobalResourceLoader
85                          .getService(KRADServiceLocatorWeb.DATA_DICTIONARY_SERVICE);
86              }
87              dds.getDataDictionary().parseDataDictionaryConfigurationFiles(false);
88  
89              if (isValidateDataDictionary()) {
90                  LOG.info("KRAD Configurer - Validating DD");
91                  dds.getDataDictionary().validateDD(isValidateDataDictionaryEboReferences());
92              }
93  
94              // KULRICE-4513 After the Data Dictionary is loaded and validated, perform Data Dictionary bean overrides.
95              dds.getDataDictionary().performBeanOverrides();
96          }
97      }
98  
99      protected void publishDataDictionaryComponents() {
100         if (isComponentPublishingEnabled()) {
101             long delay = getComponentPublishingDelay();
102             LOG.info("Publishing of Data Dictionary components is enabled, scheduling publish after " + delay + " millisecond delay");
103             ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
104             try {
105                 scheduler.schedule(new Runnable() {
106                     @Override
107                     public void run() {
108                         long s = System.currentTimeMillis();
109                         LOG.info("Executing scheduled Data Dictionary component publishing...");
110                         try {
111                             KRADServiceLocatorInternal.getDataDictionaryComponentPublisherService().publishAllComponents();
112                         } catch (RuntimeException e) {
113                             LOG.error("Failed to publish data dictionary components.", e);
114                             throw e;
115                         } finally {
116                             long e = System.currentTimeMillis();
117                             LOG.info("... finished scheduled execution of Data Dictionary component publishing.  Took " + (e-s) + " milliseconds");
118                         }
119                     }
120                 }, delay, TimeUnit.MILLISECONDS);
121             } finally {
122                 scheduler.shutdown();
123             }
124         }
125     }
126 
127     /**
128      * Returns true - KNS UI should always be included.
129      *
130      * @see org.kuali.rice.core.impl.config.module.ModuleConfigurer#shouldRenderWebInterface()
131      */
132     @Override
133     public boolean shouldRenderWebInterface() {
134         return true;
135     }
136 
137     public boolean isLoadDataDictionary() {
138         return ConfigContext.getCurrentContextConfig().getBooleanProperty("load.data.dictionary", true);
139     }
140 
141     public boolean isValidateDataDictionary() {
142         return ConfigContext.getCurrentContextConfig().getBooleanProperty("validate.data.dictionary", false);
143     }
144 
145     public boolean isValidateDataDictionaryEboReferences() {
146         return ConfigContext.getCurrentContextConfig().getBooleanProperty("validate.data.dictionary.ebo.references",
147                 false);
148     }
149 
150     public boolean isComponentPublishingEnabled() {
151         return ConfigContext.getCurrentContextConfig().getBooleanProperty(KRADConstants.Config.COMPONENT_PUBLISHING_ENABLED, false);
152     }
153 
154     public long getComponentPublishingDelay() {
155         return ConfigContext.getCurrentContextConfig().getNumericProperty(KRADConstants.Config.COMPONENT_PUBLISHING_DELAY, 0);
156     }
157 
158     /**
159      * Used to "poke" the Data Dictionary again after the Spring Context is initialized.  This is to
160      * allow for modules loaded with KualiModule after the KNS has already been initialized to work.
161      *
162      * Also initializes the DateTimeService
163      */
164     protected void configureDataSource() {
165         if (getApplicationDataSource() != null && getServerDataSource() == null) {
166             throw new ConfigurationException(
167                     "An application data source was defined but a server data source was not defined.  Both must be specified.");
168         }
169         if (getApplicationDataSource() == null && getServerDataSource() != null) {
170             throw new ConfigurationException(
171                     "A server data source was defined but an application data source was not defined.  Both must be specified.");
172         }
173 
174         if (getApplicationDataSource() != null) {
175             ConfigContext.getCurrentContextConfig()
176                     .putObject(KRADConstants.KRAD_APPLICATION_DATASOURCE, getApplicationDataSource());
177         }
178         if (getServerDataSource() != null) {
179             ConfigContext.getCurrentContextConfig()
180                     .putObject(KRADConstants.KRAD_SERVER_DATASOURCE, getServerDataSource());
181         }
182     }
183 
184     public DataSource getApplicationDataSource() {
185         return this.applicationDataSource;
186     }
187 
188     public DataSource getServerDataSource() {
189         return this.serverDataSource;
190     }
191 
192     public void setApplicationDataSource(DataSource applicationDataSource) {
193         this.applicationDataSource = applicationDataSource;
194     }
195 
196     public void setServerDataSource(DataSource serverDataSource) {
197         this.serverDataSource = serverDataSource;
198     }
199 
200     /**
201      * Indicates whether the legacy KNS module should be included which will include
202      * the KNS spring beans file
203      *
204      * @return boolean true if kns should be supported, false if not
205      */
206     public boolean isIncludeKnsSpringBeans() {
207         return includeKnsSpringBeans;
208     }
209 
210     /**
211      * Setter for the include kns support indicator
212      *
213      * @param includeKnsSpringBeans
214      */
215     public void setIncludeKnsSpringBeans(boolean includeKnsSpringBeans) {
216         this.includeKnsSpringBeans = includeKnsSpringBeans;
217     }
218 }