001/**
002 * Copyright 2005-2014 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.provider;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.krad.data.KradDataServiceLocator;
020import org.springframework.beans.factory.InitializingBean;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026/**
027 * Provides a mechanism for registering Providers in the {@link ProviderRegistry} using Spring.
028 *
029 * @see Provider
030 * @see ProviderRegistry
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class ProviderRegistrar implements InitializingBean {
035
036    private static final Logger LOG = Logger.getLogger(ProviderRegistrar.class);
037
038    /**
039     * The provider registry.
040     */
041    protected ProviderRegistry providerRegistry;
042
043    /**
044     * The providers currently assigned to the registry.
045     */
046    protected List<Provider> providers = Collections.unmodifiableList(Collections.<Provider>emptyList());
047
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public void afterPropertiesSet() throws Exception {
053        if ( getProviders() != null ) {
054            if ( getProviderRegistry() != null ) {
055                for ( Provider provider : getProviders() ) {
056                    LOG.info( "Registering data module provider for "+ provider);
057                    getProviderRegistry().registerProvider(provider);
058                }
059            } else {
060                LOG.error( "Provider registry not initialized.");
061            }
062        }
063    }
064
065    /**
066     * Sets the list of providers for this module.
067     *
068     * @param providers list of providers
069     */
070    public void setProviders(List<Provider> providers) {
071        this.providers = Collections.unmodifiableList(new ArrayList<Provider>(providers));
072    }
073
074    /**
075     * Gets the list of providers for this module.
076     *
077     * @return the list of providers for this module.
078     */
079    public List<Provider> getProviders() {
080        return providers;
081    }
082
083    /**
084     * Gets the provider registry.
085     *
086     * @return the provider registry.
087     */
088    public ProviderRegistry getProviderRegistry() {
089        if(this.providerRegistry == null){
090            return KradDataServiceLocator.getProviderRegistry();
091        }
092        return this.providerRegistry;
093    }
094
095    /**
096     * Setter for the provider registry.
097     *
098     * @param providerRegistry the provider registry to set.
099     */
100    public void setProviderRegistry(ProviderRegistry providerRegistry) {
101        this.providerRegistry = providerRegistry;
102    }
103
104}