Project

General

Profile

1
package eu.dnetlib.data.download;
2

    
3
import java.util.Map;
4

    
5
import org.springframework.beans.BeansException;
6
import org.springframework.beans.factory.BeanFactory;
7
import org.springframework.beans.factory.ListableBeanFactory;
8

    
9
import com.google.common.collect.Maps;
10

    
11
import eu.dnetlib.data.download.rmi.DownloadPlugin;
12
import eu.dnetlib.data.download.rmi.DownloadPluginEnumerator;
13
import eu.dnetlib.data.download.rmi.DownloadServiceException;
14

    
15
/**
16
 * The Class DownloadPluginEnumeratorImpl.
17
 */
18
public class DownloadPluginEnumeratorImpl implements DownloadPluginEnumerator {
19

    
20
	/**
21
	 * bean factory.
22
	 */
23
	private ListableBeanFactory beanFactory;
24

    
25
	/*
26
	 * (non-Javadoc)
27
	 *
28
	 * @see eu.dnetlib.data.download.rmi.DownloadPluginEnumerator#getAll()
29
	 */
30
	@Override
31
	public Map<String, DownloadPlugin> getAll() {
32
		return beanFactory.getBeansOfType(DownloadPlugin.class);
33
	}
34

    
35
	/*
36
	 * (non-Javadoc)
37
	 *
38
	 * @see eu.dnetlib.data.download.rmi.DownloadPluginEnumerator#getByProtocols()
39
	 */
40
	@Override
41
	public Map<String, DownloadPlugin> getByProtocols() {
42
		final Map<String, DownloadPlugin> res = Maps.newHashMap();
43
		for (DownloadPlugin cp : getAll().values()) {
44
			res.put(cp.getPluginName().toLowerCase(), cp);
45
		}
46
		return res;
47
	}
48

    
49
	/*
50
	 * (non-Javadoc)
51
	 *
52
	 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
53
	 */
54
	@Override
55
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
56
		this.beanFactory = (ListableBeanFactory) beanFactory;
57
	}
58

    
59
	/**
60
	 * Gets the bean factory.
61
	 *
62
	 * @return the bean factory
63
	 */
64
	public ListableBeanFactory getBeanFactory() {
65
		return beanFactory;
66
	}
67

    
68
	/**
69
	 * Get given DownloadPlugin or throws exception.
70
	 *
71
	 * @param protocol
72
	 *            the protocol
73
	 * @return the download plugin
74
	 * @throws eu.dnetlib.data.download.rmi.DownloadServiceException
75
	 *             the download service exception
76
	 */
77
	public DownloadPlugin get(final String protocol) throws DownloadServiceException {
78
		final DownloadPlugin plugin = getByProtocols().get(protocol.toLowerCase());
79
		if (plugin == null) throw new DownloadServiceException("plugin not found for name: " + protocol);
80
		return plugin;
81
	}
82

    
83
}
(2-2/5)