Project

General

Profile

1
package eu.dnetlib.enabling.tools.registration;
2

    
3
import java.io.InputStream;
4

    
5
import org.antlr.stringtemplate.StringTemplate;
6
import org.apache.commons.io.IOUtils;
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10

    
11
import eu.dnetlib.clients.ISLookUpClient;
12
import eu.dnetlib.clients.ISRegistryClient;
13
import eu.dnetlib.enabling.annotations.DnetServiceType;
14
import eu.dnetlib.exceptions.ISLookUpException;
15
import eu.dnetlib.services.BaseService;
16

    
17
public class ServiceRegistrator {
18

    
19
	private static final Log log = LogFactory.getLog(ServiceRegistrator.class);
20

    
21
	@Autowired
22
	private ISLookUpClient isLookup;
23
	@Autowired
24
	private ISRegistryClient isRegistry;
25

    
26
	/**
27
	 * component which finds an hnm profile.
28
	 *
29
	 * @param url
30
	 */
31

    
32
	public String registerService(final BaseService service, final String url) throws Exception {
33

    
34
		final DnetServiceType type = service.getServiceType();
35

    
36
		ensureSchemaExists(type);
37

    
38
		final String st = IOUtils.toString(getClass().getResourceAsStream("ServiceProfileTemplate.st"));
39

    
40
		final StringTemplate templ = new StringTemplate(st);
41

    
42
		final String resourceType = type + "ResourceType";
43
		templ.setAttribute("resourceType", resourceType);
44
		templ.setAttribute("serviceName", type);
45
		templ.setAttribute("address", url);
46
		templ.setAttribute("protocols", null);
47
		templ.setAttribute("properties", null);
48

    
49
		log.debug("template: " + templ.toString());
50

    
51
		final String profId = isRegistry.register(templ.toString());
52

    
53
		log.info("registered profile for " + resourceType + ": " + profId);
54

    
55
		return profId;
56
	}
57

    
58
	/**
59
	 * Check that the service schema for this service already exists, and create it if it doesn't.
60
	 *
61
	 * @param type
62
	 *            service name
63
	 */
64
	protected void ensureSchemaExists(final DnetServiceType type) throws Exception {
65

    
66
		final String xq = "//*[local-name() = 'complexType' and @name = 'RESOURCE_TYPEType']//*[local-name() = 'enumeration' and @value = '" + type
67
				+ "ResourceType']";
68
		try {
69
			isLookup.findOne(xq);
70
			log.info("schema for " + type + " appears to exist");
71
		} catch (final ISLookUpException e) {
72
			registerServiceSchema(type);
73
		}
74
	}
75

    
76
	private void registerServiceSchema(final DnetServiceType type) throws Exception {
77

    
78
		final InputStream schemaStream = getClass().getResourceAsStream("ServiceProfileSchemaTemplate.st");
79
		if (schemaStream == null) { throw new IllegalStateException("cannot find service profile schema template"); }
80

    
81
		final StringTemplate schema = new StringTemplate(IOUtils.toString(schemaStream));
82
		final String resourceType = type + "ResourceType";
83
		schema.setAttribute("resourceType", resourceType);
84

    
85
		isRegistry.registerSchema(resourceType, schema.toString());
86

    
87
		log.info("registered schema for " + type);
88

    
89
	}
90

    
91
}
(2-2/2)