Project

General

Profile

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

    
3
import java.util.Map;
4

    
5
import javax.xml.bind.JAXBException;
6
import javax.xml.transform.dom.DOMSource;
7
import javax.xml.xpath.XPath;
8
import javax.xml.xpath.XPathConstants;
9
import javax.xml.xpath.XPathExpressionException;
10
import javax.xml.xpath.XPathFactory;
11

    
12
import org.springframework.beans.factory.annotation.Required;
13
import org.w3c.dom.Element;
14

    
15
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
16
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
17
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
18
import eu.dnetlib.enabling.tools.OpaqueResource;
19
import eu.dnetlib.enabling.tools.UniqueIdentifierGenerator;
20
import eu.dnetlib.enabling.tools.UniqueIdentifierGeneratorImpl;
21
import eu.dnetlib.miscutils.jaxb.JaxbFactory;
22

    
23
/**
24
 * Blackboard client.
25
 * 
26
 * @author marko
27
 * 
28
 */
29
public class BlackboardClientHandlerImpl implements BlackboardClientHandler {
30

    
31
	/**
32
	 * blackboard message factory.
33
	 */
34
	private JaxbFactory<BlackboardMessage> messageFactory;
35

    
36
	/**
37
	 * service locator.
38
	 */
39
	private UniqueServiceLocator serviceLocator;
40

    
41
	/**
42
	 * generates blackboard message identifiers for new jobs.
43
	 */
44
	private UniqueIdentifierGenerator uuidGenerator = new UniqueIdentifierGeneratorImpl("bb-");
45

    
46
	/**
47
	 * {@inheritDoc}
48
	 * 
49
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardClientHandler#assign(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
50
	 */
51
	@Override
52
	public void assign(final BlackboardJob job) {
53
		checkJob(job);
54

    
55
		try {
56
			serviceLocator.getService(ISRegistryService.class).addBlackBoardMessage(job.getServiceId(), job.getId(),
57
					messageFactory.serialize(job.getMessage()));
58
		} catch (final ISRegistryException e) {
59
			throw new IllegalStateException("cannot register blackboard message", e);
60
		} catch (final JAXBException e) {
61
			throw new IllegalArgumentException("cannot serialize blackboard message", e);
62
		}
63
	}
64

    
65
	/**
66
	 * Check that the job has sane values.
67
	 * 
68
	 * @param job
69
	 */
70
	protected void checkJob(final BlackboardJob job) {
71
		for (Map.Entry<String, String> param : job.getParameters().entrySet()) {
72
			if (param.getValue() == null) { throw new IllegalStateException("job parameter value cannot be null: " + param.getKey()); }
73
		}
74
	}
75

    
76
	/**
77
	 * {@inheritDoc}
78
	 * 
79
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardClientHandler#delete(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
80
	 */
81
	@Override
82
	public void delete(final BlackboardJob job) {
83
		try {
84
			serviceLocator.getService(ISRegistryService.class).deleteBlackBoardMessage(job.getServiceId(), job.getId());
85
		} catch (final ISRegistryException e) {
86
			throw new IllegalStateException("cannot delete blackboard message", e);
87
		}
88
	}
89

    
90
	/**
91
	 * {@inheritDoc}
92
	 * 
93
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardClientHandler#newJob()
94
	 */
95
	@Override
96
	public BlackboardJob newJob(final String serviceId) {
97
		final BlackboardJob job = new BlackboardJobImpl(serviceId, messageFactory.newInstance());
98
		job.setActionStatus(ActionStatus.ASSIGNED);
99
		job.getParameters().put("id", "");
100
		job.getParameters().put("error", "");
101

    
102
		job.setId(uuidGenerator.generateIdentifier());
103
		return job;
104
	}
105

    
106
	/**
107
	 * {@inheritDoc}
108
	 * 
109
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardHandler#getJob(eu.dnetlib.enabling.tools.OpaqueResource)
110
	 */
111
	@Override
112
	public BlackboardJob getJob(final OpaqueResource profile) {
113

    
114
		final XPath xpa = XPathFactory.newInstance().newXPath();
115

    
116
		try {
117
			final Element source = (Element) xpa.evaluate("/RESOURCE_PROFILE/BODY/BLACKBOARD/MESSAGE[@id = /RESOURCE_PROFILE/BODY/BLACKBOARD/LAST_RESPONSE]",
118
					profile.asDom(), XPathConstants.NODE);
119

    
120
			if (source == null) { throw new IllegalStateException("cannot find last blackboard message in the service profile"); }
121

    
122
			return new BlackboardJobImpl(profile.getResourceId(), messageFactory.parse(new DOMSource(source)));
123
		} catch (final JAXBException e) {
124
			throw new IllegalStateException("cannot parse blackboard message", e);
125
		} catch (final XPathExpressionException e) {
126
			throw new IllegalStateException("cannot find last blackboard message in the service profile", e);
127
		}
128
	}
129

    
130
	public JaxbFactory<BlackboardMessage> getMessageFactory() {
131
		return messageFactory;
132
	}
133

    
134
	public void setMessageFactory(final JaxbFactory<BlackboardMessage> messageFactory) {
135
		this.messageFactory = messageFactory;
136
	}
137

    
138
	public UniqueIdentifierGenerator getUuidGenerator() {
139
		return uuidGenerator;
140
	}
141

    
142
	public void setUuidGenerator(final UniqueIdentifierGenerator uuidGenerator) {
143
		this.uuidGenerator = uuidGenerator;
144
	}
145

    
146
	public UniqueServiceLocator getServiceLocator() {
147
		return serviceLocator;
148
	}
149

    
150
	@Required
151
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
152
		this.serviceLocator = serviceLocator;
153
	}
154

    
155
}
(5-5/21)