Project

General

Profile

1
package eu.dnetlib.index;
2

    
3
import java.util.Map;
4

    
5
import com.google.common.collect.Maps;
6
import org.springframework.beans.BeansException;
7
import org.springframework.beans.factory.BeanFactory;
8
import org.springframework.beans.factory.BeanFactoryAware;
9
import org.springframework.beans.factory.ListableBeanFactory;
10

    
11
/**
12
 * The Class IndexBackendEnumerator.
13
 */
14
public class IndexBackendEnumerator implements BeanFactoryAware {
15

    
16
	/**
17
	 * The bean factory.
18
	 */
19
	private ListableBeanFactory beanFactory;
20

    
21
	/**
22
	 * Gets the all.
23
	 *
24
	 * @return the all
25
	 */
26
	public Map<String, IndexBackendDescriptor> getAll() {
27
		return beanFactory.getBeansOfType(IndexBackendDescriptor.class);
28
	}
29

    
30
	/**
31
	 * Gets the bean factory.
32
	 *
33
	 * @return the beanFactory
34
	 */
35
	public ListableBeanFactory getBeanFactory() {
36
		return beanFactory;
37
	}
38

    
39
	/**
40
	 * {@inheritDoc}
41
	 *
42
	 * @see BeanFactoryAware#setBeanFactory(BeanFactory)
43
	 */
44
	@Override
45
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
46
		this.beanFactory = (ListableBeanFactory) beanFactory;
47

    
48
	}
49

    
50
	/**
51
	 * Gets the all protocols.
52
	 *
53
	 * @return the all protocols
54
	 */
55
	public Map<String, String> getAllProtocols() {
56
		final Map<String, String> res = Maps.newHashMap();
57
		for (IndexBackendDescriptor desc : getAll().values()) {
58
			String id = desc.getEndpoint().get(IndexBackendDescriptor.ID);
59
			String address = desc.getEndpoint().get(IndexBackendDescriptor.ADDRESS);
60
			res.put(id, address);
61
		}
62
		return res;
63
	}
64

    
65
	/**
66
	 * Gets the all service properties.
67
	 *
68
	 * @return the all service properties
69
	 */
70
	public Map<String, String> getAllServiceProperties() {
71
		final Map<String, String> res = Maps.newHashMap();
72
		for (IndexBackendDescriptor desc : getAll().values()) {
73
			String name = desc.getEndpoint().get(IndexBackendDescriptor.ID);
74
			if (name == null) throw new IllegalStateException("Missing field name");
75
			Map<String, String> currentProp = desc.getServiceProperties();
76
			for (String key : currentProp.keySet()) {
77
				// Append Prefix Name to each property key
78
				res.put(name + ":" + key, currentProp.get(key));
79
			}
80
		}
81
		return res;
82
	}
83

    
84
	@SuppressWarnings("unchecked")
85
	public <T extends IndexBackendDescriptor> T getDescriptor(final String identifier) {
86
		// TODO implement a faster access to the IndexBackendDescriptor
87
		for (IndexBackendDescriptor desc : getAll().values()) {
88
			String name = desc.getEndpoint().get(IndexBackendDescriptor.ID);
89
			if (name.equals(identifier)) return (T) desc;
90
		}
91
		return null;
92
	}
93

    
94
}
(3-3/9)