com.zergiu.tvman.shows.ShowsProviderFactoryBean.java Source code

Java tutorial

Introduction

Here is the source code for com.zergiu.tvman.shows.ShowsProviderFactoryBean.java

Source

/*
 * Copyright (c) Sergiu Giurgiu 2011.
 *
 * This file is part of TVMan.
 *
 * TVMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TVMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TVMan.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.zergiu.tvman.shows;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;

/**
 * 
 * @author sergiu
 */
public class ShowsProviderFactoryBean implements Serializable {

    private static final long serialVersionUID = 672313633272816684L;
    private Map<String, ShowProvider> providers = new HashMap<String, ShowProvider>();
    private static Logger log = LoggerFactory.getLogger(ShowsProviderFactoryBean.class);

    public ShowsProviderFactoryBean() {
        findProviders("com.zergiu.tvman.shows");
    }

    /**
     * 
     */
    private void findProviders(String packageName) {
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
                false);
        scanner.addIncludeFilter(new AssignableTypeFilter(ShowProvider.class));
        for (BeanDefinition bd : scanner.findCandidateComponents(packageName)) {
            ShowProvider provider = getProvider(bd);
            if (provider != null) {
                providers.put(provider.getProviderId(), provider);
            }
        }
    }

    private ShowProvider getProvider(BeanDefinition beanDefinition) {
        try {
            Class<?> providerClass = Class.forName(beanDefinition.getBeanClassName());
            log.debug("found show provider " + providerClass);
            return (ShowProvider) providerClass.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Map<String, ShowProvider> getProviders() {
        return providers;
    }

    public void addProviderPackage(String packageName) {
        findProviders(packageName);
    }
}