View Javadoc
1   /**
2    * Copyright 2005-2016 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.data;
17  
18  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19  import org.kuali.rice.core.framework.resourceloader.BeanFactoryResourceLoader;
20  import org.kuali.rice.krad.data.DataObjectService;
21  import org.kuali.rice.krad.data.KradDataServiceLocator;
22  import org.springframework.beans.factory.FactoryBean;
23  import org.springframework.context.support.ClassPathXmlApplicationContext;
24  
25  import javax.xml.namespace.QName;
26  
27  /**
28   * A factory bean which load and/or acquires a reference to the data framework.
29   *
30   * <p>Will lazy-initialize the framework if it's not already been initialized. The factory bean will return a reference
31   * the {@link DataObjectService} which is the main API entry point into the data framework.</p>
32   *
33   * @see DataObjectService
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class KradDataFactoryBean implements FactoryBean<DataObjectService> {
38  
39      private static final String SPRING_FILE = "classpath:org/kuali/rice/krad/data/config/KRADDataSpringBeans.xml";
40  
41      @Override
42      public DataObjectService getObject() throws Exception {
43          // first, let's see if it's already been initialized
44          DataObjectService dataObjectService = KradDataServiceLocator.getDataObjectService();
45          if (dataObjectService == null) {
46              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(SPRING_FILE);
47              BeanFactoryResourceLoader rl = new BeanFactoryResourceLoader(new QName("krad-data"), context);
48              rl.start();
49              GlobalResourceLoader.addResourceLoader(rl);
50              dataObjectService = KradDataServiceLocator.getDataObjectService();
51          }
52          if (dataObjectService == null) {
53              throw new IllegalStateException("Failed to locate or initialize krad data framework.");
54          }
55          return dataObjectService;
56      }
57  
58      @Override
59      public Class<?> getObjectType() {
60          return DataObjectService.class;
61      }
62  
63      @Override
64      public boolean isSingleton() {
65          return true;
66      }
67  
68  }