001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.data;
017
018import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
019import org.kuali.rice.core.framework.resourceloader.BeanFactoryResourceLoader;
020import org.kuali.rice.krad.data.DataObjectService;
021import org.kuali.rice.krad.data.KradDataServiceLocator;
022import org.springframework.beans.factory.FactoryBean;
023import org.springframework.context.support.ClassPathXmlApplicationContext;
024
025import javax.xml.namespace.QName;
026
027/**
028 * A factory bean which load and/or acquires a reference to the data framework.
029 *
030 * <p>Will lazy-initialize the framework if it's not already been initialized. The factory bean will return a reference
031 * the {@link DataObjectService} which is the main API entry point into the data framework.</p>
032 *
033 * @see DataObjectService
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class KradDataFactoryBean implements FactoryBean<DataObjectService> {
038
039    private static final String SPRING_FILE = "classpath:org/kuali/rice/krad/data/config/KRADDataSpringBeans.xml";
040
041    @Override
042    public DataObjectService getObject() throws Exception {
043        // first, let's see if it's already been initialized
044        DataObjectService dataObjectService = KradDataServiceLocator.getDataObjectService();
045        if (dataObjectService == null) {
046            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(SPRING_FILE);
047            BeanFactoryResourceLoader rl = new BeanFactoryResourceLoader(new QName("krad-data"), context);
048            rl.start();
049            GlobalResourceLoader.addResourceLoader(rl);
050            dataObjectService = KradDataServiceLocator.getDataObjectService();
051        }
052        if (dataObjectService == null) {
053            throw new IllegalStateException("Failed to locate or initialize krad data framework.");
054        }
055        return dataObjectService;
056    }
057
058    @Override
059    public Class<?> getObjectType() {
060        return DataObjectService.class;
061    }
062
063    @Override
064    public boolean isSingleton() {
065        return true;
066    }
067
068}