Project

General

Profile

« Previous | Next » 

Revision 28353

Added descriptor for automatic discovery of the index service instance

View differences:

modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/IndexBackendEnumerator.java
1
package eu.dnetlib.functionality.index;
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.BeanFactoryAware;
8
import org.springframework.beans.factory.ListableBeanFactory;
9

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

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

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

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

  
29
	/**
30
	 * {@inheritDoc}
31
	 * 
32
	 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
33
	 */
34
	@Override
35
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
36
		this.beanFactory = (ListableBeanFactory) beanFactory;
37

  
38
	}
39

  
40
	/**
41
	 * Gets the bean factory.
42
	 * 
43
	 * @return the beanFactory
44
	 */
45
	public ListableBeanFactory getBeanFactory() {
46
		return beanFactory;
47
	}
48

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

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

  
81
}
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/IndexBackendDescriptor.java
1
package eu.dnetlib.functionality.index;
2

  
3
import java.util.Map;
4

  
5
/**
6
 * The Interface IndexBackendDescriptor.
7
 */
8
public interface IndexBackendDescriptor {
9

  
10
	/**
11
	 * Gets the endpoint.
12
	 * 
13
	 * @return the endpoint
14
	 */
15
	public Map<String, String> getEndpoint();
16

  
17
	/**
18
	 * Gets the service properties.
19
	 * 
20
	 * @return the service properties
21
	 */
22
	public Map<String, String> getServiceProperties();
23

  
24
}

Also available in: Unified diff