Project

General

Profile

« Previous | Next » 

Revision 50655

[maven-release-plugin] copy for tag cnr-blackboard-common-2.2.2

View differences:

modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/deploy.info
1
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/cnr-blackboard-common/trunk/", "deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "cnr-blackboard-common"}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/test/java/eu/dnetlib/enabling/tools/blackboard/NotificationHandlerChainTest.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import javax.annotation.Resource;
4

  
5
import org.junit.Test;
6
import org.junit.runner.RunWith;
7
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
8
import org.springframework.test.context.ContextConfiguration;
9
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10

  
11
@RunWith(value = SpringJUnit4ClassRunner.class)
12
@ContextConfiguration
13
public class NotificationHandlerChainTest {
14

  
15
	public class RecJob implements Runnable {
16

  
17
		private int times;
18
		private int num;
19

  
20
		public RecJob(int num, int times) {
21
			this.num = num;
22
			this.times = times;
23
		}
24

  
25
		@Override
26
		public void run() {
27
			System.out.println("starting " + num);
28
			if(times >= 0)
29
				executor.execute(new  RecJob(num + 1, times - 1));
30
			try {
31
				Thread.sleep(4000);
32
			} catch (InterruptedException e) {
33
				// TODO Auto-generated catch block
34
				e.printStackTrace();
35
			}
36
			
37
			System.out.println("thread finished - " + num);
38
		}
39

  
40
	}
41

  
42
	private static final class Job implements Runnable {
43
		private final int value;
44

  
45
		public Job(final int i) {
46
			value = i;
47
		}
48

  
49
		@Override
50
		public void run() {
51
			System.out.println("thread started - " + value);
52
			try {
53
				Thread.sleep(4000);
54
			} catch (final InterruptedException e) {
55
				// 
56
			}
57
			System.out.println("thread finished - " + value);
58
		}
59
	}
60

  
61
	@Resource
62
	private transient ThreadPoolTaskExecutor executor;
63

  
64
	@Test
65
	public void testDelegateNotification() throws InterruptedException {
66
		System.out.println(executor);
67

  
68
		System.out.println("executing");
69

  
70
		for (int i = 0; i < 100; i++)
71
			executor.execute(new Job(i));
72

  
73
		System.out.println("executed - waiting");
74
		Thread.sleep(2000);
75
		
76
		System.out.println("active count: " + executor.getActiveCount());
77
		System.out.println("current pool size: " + executor.getCorePoolSize());
78
		System.out.println("pool size " + executor.getPoolSize());
79
		
80
		Thread.sleep(3000);
81
		
82
		System.out.println("ok");
83
	}
84

  
85
	@Test
86
	public void testRecursive() throws InterruptedException {
87
		
88
		for (int i = 0; i < 4; i++)
89
			executor.execute(new RecJob(i * 10, 4));
90

  
91
		
92
		System.out.println("executed - waiting");
93
		Thread.sleep(2000);
94
		
95
		System.out.println("active count: " + executor.getActiveCount());
96
		System.out.println("current pool size: " + executor.getCorePoolSize());
97
		System.out.println("pool size " + executor.getPoolSize());
98
		
99
		Thread.sleep(3000);
100
		
101
		System.out.println("ok");
102
	}
103

  
104
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/test/resources/eu/dnetlib/enabling/tools/blackboard/NotificationHandlerChainTest-context.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
	xmlns:lang="http://www.springframework.org/schema/lang"
5
	xmlns:p="http://www.springframework.org/schema/p"
6
	xmlns:util="http://www.springframework.org/schema/util"
7
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
8
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
9
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
10

  
11

  
12
	<bean id="msroNotificationExecutor"
13
		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
14
		p:corePoolSize="32" p:keepAliveSeconds="3600" p:queueCapacity="0" />
15

  
16
</beans>
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardServerExecutorNotificationHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import javax.annotation.PostConstruct;
4

  
5
/**
6
 * In most cases a service just dispatches every incoming blackboard execution request to the blackboard server
7
 * executor, so that the right action call back will be called.
8
 * 
9
 * <p>
10
 * This bean will wrap a BlackboardServerExecutor as a BlackboardNotificationHandler so that it can be installed as a
11
 * notification handler of a server.</p>
12
 * 
13
 * @author marko
14
 * 
15
 * @param <T>
16
 */
17
public class BlackboardServerExecutorNotificationHandler<T extends Enum<T>> extends AbstractBlackboardNotificationHandler<BlackboardServerHandler> {
18

  
19
	private BlackboardServerActionExecutor<T> blackboardExecutor;
20

  
21
	@PostConstruct
22
	public void init() {
23
		setBlackboardHandler(blackboardExecutor.getBlackboardHandler());
24
	}
25

  
26
	@Override
27
	protected void processJob(final BlackboardJob job) {
28
		blackboardExecutor.execute(job);
29
	}
30

  
31
	public BlackboardServerActionExecutor<T> getBlackboardExecutor() {
32
		return blackboardExecutor;
33
	}
34

  
35
	public void setBlackboardExecutor(BlackboardServerActionExecutor<T> blackboardExecutor) {
36
		this.blackboardExecutor = blackboardExecutor;
37
	}
38

  
39
	/**
40
	 * {@inheritDoc}
41
	 * 
42
	 * @see eu.dnetlib.enabling.tools.blackboard.AbstractBlackboardNotificationHandler#setBlackboardHandler(eu.dnetlib.enabling.tools.blackboard.BlackboardHandler)
43
	 * 
44
	 *      Redefined here to avoid inheriting the @Required property. It simplifies the spring config for an
45
	 *      unnecessarry property, since we can get it from the blackboardExecutor.
46
	 */
47
	@Override
48
	public void setBlackboardHandler(BlackboardServerHandler handler) {
49
		super.setBlackboardHandler(handler);
50
	}
51

  
52
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardJobImpl.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5
import java.util.Map.Entry;
6

  
7
/**
8
 * High level representation of a blackboard job.
9
 * 
10
 * @author marko
11
 * 
12
 */
13
public class BlackboardJobImpl implements BlackboardJob {
14

  
15
	/**
16
	 * underlying low level blackboard message.
17
	 */
18
	private BlackboardMessage message;
19

  
20
	/**
21
	 * service identifier.
22
	 */
23
	private String serviceId;
24

  
25
	/**
26
	 * parameters.
27
	 */
28
	private final transient Map<String, String> parameters = new HashMap<String, String>();
29

  
30
	/**
31
	 * Construct a new blackboard job from a blackboard message.
32
	 * 
33
	 * @param serviceId
34
	 *            service identifier
35
	 * @param message
36
	 *            underlying low-level blackboard message
37
	 */
38
	public BlackboardJobImpl(final String serviceId, final BlackboardMessage message) {
39
		super();
40
		this.message = message;
41
		this.serviceId = serviceId;
42

  
43
		for (final BlackboardParameter param : message.getParameters()) {
44
			parameters.put(param.getName(), param.getValue());
45
		}
46
	}
47

  
48
	@Override
49
	public String getId() {
50
		return message.getId();
51
	}
52

  
53
	/**
54
	 * {@inheritDoc}
55
	 * 
56
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardJob#setId(java.lang.String)
57
	 */
58
	@Override
59
	public void setId(final String identifier) {
60
		message.setId(identifier);
61
	}
62

  
63
	@Override
64
	public String getAction() {
65
		return message.getAction();
66
	}
67

  
68
	@Override
69
	public ActionStatus getActionStatus() {
70
		return message.getActionStatus();
71
	}
72

  
73
	@Override
74
	public String getDate() {
75
		return message.getDate();
76
	}
77

  
78
	@Override
79
	public String getError() {
80
		return getParameters().get("error");
81
	}
82

  
83
	@Override
84
	public Map<String, String> getParameters() {
85
		return parameters;
86
	}
87

  
88
	/**
89
	 * {@inheritDoc}
90
	 * 
91
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardJob#setAction(java.lang.String)
92
	 */
93
	@Override
94
	public void setAction(final String action) {
95
		message.setAction(action);
96
	}
97

  
98
	/**
99
	 * {@inheritDoc}
100
	 * 
101
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardJob#setActionStatus(eu.dnetlib.enabling.tools.blackboard.ActionStatus)
102
	 */
103
	@Override
104
	public void setActionStatus(final ActionStatus actionStatus) {
105
		message.setActionStatus(actionStatus);
106
	}
107

  
108
	/**
109
	 * {@inheritDoc}
110
	 * 
111
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardJob#setDate(java.lang.String)
112
	 */
113
	@Override
114
	public void setDate(final String date) {
115
		message.setDate(date);
116
	}
117

  
118
	/**
119
	 * {@inheritDoc}
120
	 * 
121
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardJob#setError(java.lang.String)
122
	 */
123
	@Override
124
	public void setError(final String error) {
125
		getParameters().put("error", error);
126
	}
127

  
128
	/**
129
	 * returns the blackboard message, potentially modified.
130
	 * 
131
	 * @return underlying blackboard message
132
	 */
133
	@Override
134
	public BlackboardMessage getMessage() {
135
		message.getParameters().clear();
136
		for (final Entry<String, String> entry : getParameters().entrySet()) {
137
			final BlackboardParameterImpl param = new BlackboardParameterImpl(); // NOPMD
138
			param.setName(entry.getKey());
139
			param.setValue(entry.getValue());
140
			message.getParameters().add(param);
141
		}
142
		return message;
143
	}
144

  
145
	public void setMessage(final BlackboardMessage message) {
146
		this.message = message;
147
	}
148

  
149
	@Override
150
	public String getServiceId() {
151
		return serviceId;
152
	}
153

  
154
	public void setServiceId(final String serviceId) {
155
		this.serviceId = serviceId;
156
	}
157

  
158
	@Override
159
	public boolean isCompleted() {
160
		return getActionStatus() == ActionStatus.DONE || getActionStatus() == ActionStatus.FAILED;
161
	}
162

  
163
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardClientHandlerImpl.java
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
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardHandlerImpl.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.io.PrintWriter;
4
import java.io.StringWriter;
5

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

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

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

  
22
/**
23
 * Blackboard handler implementation.
24
 * 
25
 * @author marko
26
 * 
27
 */
28
public class BlackboardHandlerImpl implements BlackboardServerHandler {
29

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

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

  
40
	/**
41
	 * {@inheritDoc}
42
	 * 
43
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardHandler#getJob(eu.dnetlib.enabling.tools.OpaqueResource)
44
	 */
45
	@Override
46
	public BlackboardJob getJob(final OpaqueResource profile) {
47

  
48
		final XPath xpa = XPathFactory.newInstance().newXPath();
49

  
50
		try {
51
			final Element source = (Element) xpa.evaluate("/RESOURCE_PROFILE/BODY/BLACKBOARD/MESSAGE[@id = /RESOURCE_PROFILE/BODY/BLACKBOARD/LAST_REQUEST]",
52
					profile.asDom(), XPathConstants.NODE);
53

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

  
56
			return new BlackboardJobImpl(profile.getResourceId(), messageFactory.parse(new DOMSource(source)));
57
		} catch (final JAXBException e) {
58
			throw new IllegalStateException("cannot parse blackboard message", e);
59
		} catch (final XPathExpressionException e) {
60
			throw new IllegalStateException("cannot find last blackboard message in the service profile", e);
61
		}
62
	}
63

  
64
	/**
65
	 * {@inheritDoc}
66
	 * 
67
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardHandler#done(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
68
	 */
69
	@Override
70
	public void done(final BlackboardJob job) {
71
		job.setActionStatus(ActionStatus.DONE);
72
		replyJob(job);
73
	}
74

  
75
	/**
76
	 * {@inheritDoc}
77
	 * 
78
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardHandler#failed(eu.dnetlib.enabling.tools.blackboard.BlackboardJob,
79
	 *      java.lang.Exception)
80
	 */
81
	@Override
82
	public void failed(final BlackboardJob job, final Throwable exception) {
83
		job.setActionStatus(ActionStatus.FAILED);
84
		final StringWriter stackTrace = new StringWriter();
85
		exception.printStackTrace(new PrintWriter(stackTrace));
86
		job.getParameters().put("error", exception.toString());
87
		job.getParameters().put("errorDetails", stackTrace.toString());
88
		replyJob(job);
89
	}
90

  
91
	/**
92
	 * {@inheritDoc}
93
	 * 
94
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardHandler#ongoing(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
95
	 */
96
	@Override
97
	public void ongoing(final BlackboardJob job) {
98
		job.setActionStatus(ActionStatus.ONGOING);
99
		replyJob(job);
100
	}
101

  
102
	/**
103
	 * Internal helper method which replies a blackboard job.
104
	 * 
105
	 * @param job
106
	 *            blackboard job
107
	 */
108
	protected void replyJob(final BlackboardJob job) {
109
		try {
110
			serviceLocator.getService(ISRegistryService.class).replyBlackBoardMessage(job.getServiceId(), messageFactory.serialize(job.getMessage()));
111
		} catch (final ISRegistryException e) {
112
			throw new IllegalStateException("cannot reply the blackboard message", e);
113
		} catch (final JAXBException e) {
114
			throw new IllegalArgumentException("cannot serialize blackboard message", e);
115
		}
116
	}
117

  
118
	public JaxbFactory<BlackboardMessage> getMessageFactory() {
119
		return messageFactory;
120
	}
121

  
122
	public void setMessageFactory(final JaxbFactory<BlackboardMessage> messageFactory) {
123
		this.messageFactory = messageFactory;
124
	}
125

  
126
	public UniqueServiceLocator getServiceLocator() {
127
		return serviceLocator;
128
	}
129

  
130
	@Required
131
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
132
		this.serviceLocator = serviceLocator;
133
	}
134

  
135
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/DeletingBlackboardNotificationHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import javax.annotation.Resource;
4
import javax.xml.bind.JAXBException;
5

  
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8

  
9
import eu.dnetlib.miscutils.jaxb.JaxbFactory;
10

  
11
/**
12
 * This BB handler deletes completed (successful or unsuccessful) blackboard messages after dispatching. Job should be registered to only
13
 * one "deleting BB handler".
14
 * 
15
 * @author marko
16
 * 
17
 */
18
public class DeletingBlackboardNotificationHandler extends BlackboardNotificationHandler<BlackboardClientHandler> {
19

  
20
	/**
21
	 * Logger.
22
	 */
23
	private static final Log log = LogFactory.getLog(DeletingBlackboardNotificationHandler.class); // NOPMD by marko on 11/24/08 5:02 PM
24

  
25
	/**
26
	 * blackboard message factory.
27
	 */
28
	@Resource(name = "blackboardMessageFactory")
29
	private JaxbFactory<BlackboardMessage> messageFactory;
30

  
31
	/**
32
	 * {@inheritDoc}
33
	 * 
34
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardNotificationHandler#processJob(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
35
	 */
36
	@Override
37
	protected void processJob(final BlackboardJob job) {
38

  
39
		if (getListeners().containsKey(job.getId()) && job.isCompleted()) {
40
			if (log.isDebugEnabled()) {
41
				log.debug(serializeBlackBoardMessage(job));
42
			}
43
			getBlackboardHandler().delete(job);
44
		}
45
		super.processJob(job);
46
	}
47

  
48
	/**
49
	 * Helper method, serializes a Blackboard message using a blackboardMessageFactory.
50
	 * 
51
	 * @param job
52
	 * @return
53
	 */
54
	private String serializeBlackBoardMessage(final BlackboardJob job) {
55
		try {
56
			return getMessageFactory().serialize(job.getMessage());
57
		} catch (JAXBException e) {
58
			return "cannot serialize blackboard message: " + e.getMessage();
59
		}
60
	}
61

  
62
	public void setMessageFactory(final JaxbFactory<BlackboardMessage> messageFactory) {
63
		this.messageFactory = messageFactory;
64
	}
65

  
66
	public JaxbFactory<BlackboardMessage> getMessageFactory() {
67
		return messageFactory;
68
	}
69

  
70
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardParameterImpl.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import javax.xml.bind.annotation.XmlAttribute;
4
import javax.xml.bind.annotation.XmlRootElement;
5

  
6
import org.apache.commons.lang3.builder.EqualsBuilder;
7
import org.apache.commons.lang3.builder.HashCodeBuilder;
8

  
9
/**
10
 * Blackboard parameter.
11
 * 
12
 * @author marko
13
 * 
14
 */
15
@XmlRootElement(name = "PARAMETER")
16
public class BlackboardParameterImpl implements BlackboardParameter {
17

  
18
	/**
19
	 * hash seed.
20
	 */
21
	private static final int HASH_SEED_2 = 59;
22

  
23
	/**
24
	 * hash seed.
25
	 */
26
	private static final int HASH_SEED = 35;
27

  
28
	/**
29
	 * parameter name.
30
	 */
31
	private String name;
32

  
33
	/**
34
	 * parameter value.
35
	 */
36
	private String value;
37

  
38
	/**
39
	 * {@inheritDoc}
40
	 * 
41
	 * @see java.lang.Object#equals(java.lang.Object)
42
	 */
43
	@Override
44
	public boolean equals(final Object obj) {
45
		if (!(obj instanceof BlackboardParameter))
46
			return false;
47

  
48
		if (this == obj)
49
			return true;
50

  
51
		final BlackboardParameter rhs = (BlackboardParameter) obj;
52
		return new EqualsBuilder().append(name, rhs.getName()).append(value, rhs.getValue()).isEquals();
53
	}
54

  
55
	/**
56
	 * {@inheritDoc}
57
	 * 
58
	 * @see java.lang.Object#hashCode()
59
	 */
60
	@Override
61
	public int hashCode() {
62
		return new HashCodeBuilder(HASH_SEED, HASH_SEED_2).append(name).append(value).toHashCode();
63
	}
64

  
65
	@Override
66
	@XmlAttribute
67
	public String getName() {
68
		return name;
69
	}
70

  
71
	@Override
72
	public void setName(final String name) {
73
		this.name = name;
74
	}
75

  
76
	@Override
77
	@XmlAttribute
78
	public String getValue() {
79
		return value;
80
	}
81

  
82
	@Override
83
	public void setValue(final String value) {
84
		this.value = value;
85
	}
86

  
87
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardJobRegistry.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Registers BlackboardJobListeners to be called when related incoming blackboard job notifications arrive.
5
 * @author marko
6
 *
7
 */
8
public interface BlackboardJobRegistry {
9
	/**
10
	 * Registers a new job listener. It will be notified when the job changes state. The listener will be unregistered
11
	 * when the job arrives to a final state (DONE, or FAILED).
12
	 *
13
	 * @param job
14
	 *            job
15
	 * @param listener
16
	 *            job listener
17
	 */
18
	void registerJobListener(BlackboardJob job, BlackboardJobListener listener);
19
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/AbstractBlackboardJobListener.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Utility abstract class which dispatches to commodity onSomething() methods, one for each interesting
5
 * blackboard job state.
6
 *
7
 * @author marko
8
 *
9
 */
10
public abstract class AbstractBlackboardJobListener implements BlackboardJobListener {
11

  
12
	/**
13
	 * {@inheritDoc}
14
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardJobListener#processJob(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
15
	 */
16
	@Override
17
	public void processJob(final BlackboardJob job) {
18
		if (job.getActionStatus() == ActionStatus.DONE)
19
			onDone(job);
20
		else if (job.getActionStatus() == ActionStatus.FAILED)
21
			onFailed(job);
22
		else if (job.getActionStatus() == ActionStatus.ASSIGNED)
23
			onAssigned(job);
24
		else if (job.getActionStatus() == ActionStatus.ONGOING)
25
			onOngoing(job);
26
	}
27

  
28
	/**
29
	 * Called when the job enters the ASSIGNED state.
30
	 *
31
	 * @param job job
32
	 */
33
	protected void onAssigned(final BlackboardJob job) { // NOPMD
34
		// default no operation
35
		// TODO: increase job expiry time
36
	}
37

  
38
	/**
39
	 * Called when the job enters the ONGOING state.
40
	 *
41
	 * @param job job
42
	 */
43
	protected void onOngoing(final BlackboardJob job) { // NOPMD
44
		// default no operation
45
		// TODO: increase job expiry time
46
	}
47

  
48
	/**
49
	 * Called when the job finishes in the FAILED state.
50
	 *
51
	 * @param job job
52
	 */
53
	protected abstract void onFailed(BlackboardJob job);
54

  
55
	/**
56
	 * Called when the job finishes in the DONE state.
57
	 *
58
	 * @param job job
59
	 */
60
	protected abstract void onDone(BlackboardJob job);
61

  
62
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardServerAction.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
4
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
5

  
6
/**
7
 * Callback interface for serverside blackboard actions.
8
 * 
9
 * <p>
10
 * Actions should use the 'handler' methods to set the 'ongoing' or 'done' status.
11
 * </p>
12
 * 
13
 * <p>
14
 * failed status is automatically set upon catching an exception thrown by this interface, so feel free to simply throw
15
 * whatever you want
16
 * </p>
17
 * 
18
 * @author marko
19
 * 
20
 * @param <X>
21
 */
22
public interface BlackboardServerAction<X extends Enum<?>> {
23
	void execute(BlackboardServerHandler handler, BlackboardJob job) throws Exception;
24
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardServerHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Helper component used to bridge the high level blackboard job handling from the low level blackboard protocol based
5
 * on notifications and blackboard messages.
6
 *
7
 * @author marko
8
 *
9
 */
10
public interface BlackboardServerHandler extends BlackboardHandler {
11
	/**
12
	 * Sets the ongoing action status to the given job, publishing this new state.
13
	 *
14
	 * @param job
15
	 *            blackboard job
16
	 */
17
	void ongoing(BlackboardJob job);
18

  
19
	/**
20
	 * Sets the "failed" action status to the given job, publishing this new state along with the error message obtained
21
	 * from the exception.
22
	 *
23
	 * @param job
24
	 *            blackboard job
25
	 * @param exception
26
	 *            exception which caused the failure
27
	 */
28
	void failed(BlackboardJob job, Throwable exception);
29

  
30
	/**
31
	 * Set the "done" action status to the given job, publishing the new state.
32
	 *
33
	 * @param job
34
	 *            blackboard job
35
	 */
36
	void done(BlackboardJob job);
37
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardMessageImpl.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.util.ArrayList;
4
import java.util.List;
5

  
6
import javax.xml.bind.annotation.XmlAccessType;
7
import javax.xml.bind.annotation.XmlAccessorType;
8
import javax.xml.bind.annotation.XmlAttribute;
9
import javax.xml.bind.annotation.XmlElement;
10
import javax.xml.bind.annotation.XmlRootElement;
11

  
12
import org.apache.commons.lang3.builder.EqualsBuilder;
13
import org.apache.commons.lang3.builder.HashCodeBuilder;
14

  
15
/**
16
 * standard serialization of the blackboard message.
17
 *
18
 * @author marko
19
 *
20
 */
21
@XmlRootElement(namespace = "", name = "MESSAGE")
22
@XmlAccessorType(XmlAccessType.NONE)
23
public class BlackboardMessageImpl implements BlackboardMessage {
24

  
25
	/**
26
	 * hash seed.
27
	 */
28
	private static final int HASH_SEED_2 = 63;
29

  
30
	/**
31
	 * hash seed.
32
	 */
33
	private static final int HASH_SEED = 13;
34

  
35
	/**
36
	 * blackboard message timestamp.
37
	 */
38
	@XmlAttribute
39
	private String date;
40

  
41
	/**
42
	 * blackboard message identifier.
43
	 */
44
	@XmlAttribute
45
	private String id; // NOPMD
46

  
47
	/**
48
	 * blackboard message action name.
49
	 */
50
	@XmlElement(name = "ACTION", required = true)
51
	private String action;
52

  
53
	/**
54
	 * blackboard message parameters (key/value).
55
	 */
56
	@XmlElement(name = "PARAMETER", type = BlackboardParameterImpl.class)
57
	private List<BlackboardParameter> parameters = new ArrayList<BlackboardParameter>();
58

  
59
	/**
60
	 * the status of the action described by this message.
61
	 */
62
	@XmlElement(name = "ACTION_STATUS", required = true)
63
	private ActionStatus actionStatus;
64

  
65
	/**
66
	 * {@inheritDoc}
67
	 *
68
	 * @see java.lang.Object#equals(java.lang.Object)
69
	 */
70
	@Override
71
	public boolean equals(final Object obj) {
72
		if (!(obj instanceof BlackboardMessage))
73
			return false;
74

  
75
		if (this == obj)
76
			return true;
77

  
78
		final BlackboardMessage rhs = (BlackboardMessage) obj;
79
		return new EqualsBuilder().append(id, rhs.getId()).append(date, rhs.getDate()).append(action, rhs.getAction()).append(actionStatus,
80
				rhs.getActionStatus()).append(parameters, rhs.getParameters()).isEquals();
81
	}
82

  
83
	/**
84
	 * {@inheritDoc}
85
	 *
86
	 * @see java.lang.Object#hashCode()
87
	 */
88
	@Override
89
	public int hashCode() {
90
		return new HashCodeBuilder(HASH_SEED, HASH_SEED_2).append(id).append(date).append(action).append(actionStatus).append(parameters).toHashCode();
91
	}
92

  
93
	@Override
94
	public String getDate() {
95
		return date;
96
	}
97

  
98
	@Override
99
	public void setDate(final String date) {
100
		this.date = date;
101
	}
102

  
103
	@Override
104
	public String getId() {
105
		return id;
106
	}
107

  
108
	@Override
109
	public void setId(final String id) { // NOPMD
110
		this.id = id;
111
	}
112

  
113
	@Override
114
	public String getAction() {
115
		return action;
116
	}
117

  
118
	@Override
119
	public void setAction(final String action) {
120
		this.action = action;
121
	}
122

  
123
	@Override
124
	public List<BlackboardParameter> getParameters() {
125
		return parameters;
126
	}
127

  
128
	public void setParameters(final List<BlackboardParameter> parameters) {
129
		this.parameters = parameters;
130
	}
131

  
132
	@Override
133
	public ActionStatus getActionStatus() {
134
		return actionStatus;
135
	}
136

  
137
	@Override
138
	public void setActionStatus(final ActionStatus actionStatus) {
139
		this.actionStatus = actionStatus;
140
	}
141

  
142
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/AbstractBlackboardNotificationHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.io.IOException;
4
import java.util.concurrent.Executor;
5
import java.util.concurrent.Executors;
6

  
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.xpath.XPathExpressionException;
9

  
10
import org.springframework.beans.factory.annotation.Required;
11
import org.xml.sax.SAXException;
12

  
13
import eu.dnetlib.enabling.tools.Enableable;
14
import eu.dnetlib.enabling.tools.StringOpaqueResource;
15

  
16
/**
17
 * Common blackboard notification handler. This notification handler processes only message with ...BODY.BLACKBOARD.LAST* as topic.
18
 * 
19
 * @param <T>
20
 *            type of blackboard handler used to extract the blackboard message (client or server)
21
 * @author marko
22
 * 
23
 */
24
public abstract class AbstractBlackboardNotificationHandler<T extends BlackboardHandler> implements NotificationHandler, Enableable {
25

  
26
	/**
27
	 * blackboard handler.
28
	 */
29
	private T blackboardHandler;
30

  
31
	/**
32
	 * true if enabled.
33
	 */
34
	private boolean enabled = true;
35

  
36
	/**
37
	 * Executor handles the notified request in a dedicated thread and allows to return immediately.
38
	 */
39
	private Executor executor = Executors.newCachedThreadPool();
40

  
41
	/**
42
	 * {@inheritDoc}
43
	 * 
44
	 * @see eu.dnetlib.data.mdstore.NotificationHandler#notified(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
45
	 */
46
	@Override
47
	public void notified(final String subscrId, final String topic, final String rsId, final String profile) {
48
		if (!topic.contains("BODY.BLACKBOARD.LAST")) return;
49

  
50
		executor.execute(new Runnable() {
51

  
52
			@Override
53
			public void run() {
54
				try {
55
					processJob(blackboardHandler.getJob(new StringOpaqueResource(profile)));
56
				} catch (final XPathExpressionException e) {
57
					throw new IllegalStateException(e);
58
				} catch (final SAXException e) {
59
					throw new IllegalStateException(e);
60
				} catch (final IOException e) {
61
					throw new IllegalStateException(e);
62
				} catch (final ParserConfigurationException e) {
63
					throw new IllegalStateException(e);
64
				}
65
			}
66
		});
67
	}
68

  
69
	/**
70
	 * Subclassess override this to process incoming blackboard jobs.
71
	 * 
72
	 * @param job
73
	 *            blackboard job
74
	 */
75
	protected abstract void processJob(BlackboardJob job);
76

  
77
	public T getBlackboardHandler() {
78
		return blackboardHandler;
79
	}
80

  
81
	@Required
82
	public void setBlackboardHandler(final T blackboardHandler) {
83
		this.blackboardHandler = blackboardHandler;
84
	}
85

  
86
	@Override
87
	public boolean isEnabled() {
88
		return enabled;
89
	}
90

  
91
	@Override
92
	public void setEnabled(final boolean enabled) {
93
		this.enabled = enabled;
94
	}
95

  
96
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardJob.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.util.Map;
4

  
5
/**
6
 * High level representation of a blackboard job.
7
 * 
8
 * @author marko
9
 * 
10
 */
11
public interface BlackboardJob {
12

  
13
	/**
14
	 * Get the message id.
15
	 * 
16
	 * @return id
17
	 */
18
	String getId();
19

  
20
	/**
21
	 * Set message id.
22
	 * 
23
	 * @param identifier
24
	 *            id
25
	 */
26
	void setId(String identifier);
27

  
28
	/**
29
	 * Get the action name.
30
	 * 
31
	 * @return action name
32
	 */
33
	String getAction();
34

  
35
	/**
36
	 * Set the action name.
37
	 * 
38
	 * @param action
39
	 *            action name
40
	 */
41
	void setAction(String action);
42

  
43
	/**
44
	 * Get the action status.
45
	 * 
46
	 * @return action status
47
	 */
48
	ActionStatus getActionStatus();
49

  
50
	/**
51
	 * Set the action status.
52
	 * 
53
	 * @param actionStatus
54
	 *            action status
55
	 */
56
	void setActionStatus(ActionStatus actionStatus);
57

  
58
	/**
59
	 * Get the message date.
60
	 * 
61
	 * @return date
62
	 */
63
	String getDate();
64

  
65
	/**
66
	 * Set the message date.
67
	 * 
68
	 * @param date
69
	 *            date
70
	 */
71
	void setDate(String date);
72

  
73
	/**
74
	 * obtains a mutable parameter map (key/value).
75
	 * 
76
	 * @return mutable parameter map
77
	 */
78
	Map<String, String> getParameters();
79

  
80
	/**
81
	 * Get the error message, if the actionStatus is FAILED. Usually the error message is also present in the parameter map but it's
82
	 * preferred to use this method.
83
	 * 
84
	 * @return error message
85
	 */
86
	String getError();
87

  
88
	/**
89
	 * Set the error message, if the actionStatus is FAILED. Usually the error message is also present in the parameter map but it's
90
	 * preferred to use this method.
91
	 * 
92
	 * @param error
93
	 *            error message
94
	 */
95
	void setError(String error);
96

  
97
	/**
98
	 * Get the setviceId.
99
	 * 
100
	 * @return the serviceId
101
	 */
102
	String getServiceId();
103

  
104
	/**
105
	 * True if the action status is one of the termination statuses (e.g. done and failed).
106
	 * 
107
	 * @return true if completed
108
	 */
109
	boolean isCompleted();
110

  
111
	/**
112
	 * Get the bb message.
113
	 * 
114
	 * @return the bb message
115
	 */
116
	BlackboardMessage getMessage();
117

  
118
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardClientHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Helpers for the blackboard protocol client.
5
 *
6
 * @author marko
7
 *
8
 */
9
public interface BlackboardClientHandler extends BlackboardHandler {
10
	/**
11
	 * Create a new job.
12
	 *
13
	 * @param serviceId service identifier
14
	 * @return newly created blackboard job
15
	 */
16
	BlackboardJob newJob(String serviceId);
17

  
18
	/**
19
	 * Assign a blackboard job to a service.
20
	 *
21
	 * @param job blackboard job to send
22
	 */
23
	void assign(BlackboardJob job);
24

  
25
	/**
26
	 * The client can delete the job after it has reached a final state
27
	 * or the job timeout has expired.
28
	 *
29
	 * @param job blackboard job to delete.
30
	 */
31
	void delete(BlackboardJob job);
32
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/ActionStatus.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Blackboard action status.
5
 * 
6
 * @author marko
7
 *
8
 */
9
public enum ActionStatus {
10
	/**
11
	 * The job/action is assigned but not jet taken into started execution.
12
	 */
13
	ASSIGNED,
14
	/**
15
	 * The job/action is ongoing.
16
	 */
17
	ONGOING,
18
	/**
19
	 * The job/action is completed successfully.
20
	 */
21
	DONE,
22
	/**
23
	 * The job/action is completed with failure.
24
	 */
25
	FAILED
26
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import eu.dnetlib.enabling.tools.OpaqueResource;
4

  
5
/**
6
 * Basic blackboard handler.
7
 *
8
 * @author marko
9
 *
10
 */
11
public interface BlackboardHandler {
12
	/**
13
	 * Get the current job from, as notified in the service profile.
14
	 *
15
	 * @param profile
16
	 *            service profile
17
	 * @return notified blackboard job
18
	 */
19
	BlackboardJob getJob(OpaqueResource profile);
20
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-2.2.2/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardParameter.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Blackboard parameter used in a BlackboardMessage.
5
 * 
6
 * @author marko
7
 *
8
 */
9
public interface BlackboardParameter {
10
	
11
	/**
12
	 * parameter name (key).
13
	 * @return name
14
	 */
15
	String getName();
16
	
17
	/**
18
	 * setter.
19
	 * 
20
	 * @param name name
21
	 */
22
	void setName(String name);
23
	
24
	/**
25
	 * parameter value.
26
	 * @return value
27
	 */
28
	String getValue();
29
	
30
	/**
31
	 * setter.
32
	 * 
33
	 * @param value value
34
	 */
35
	void setValue(String value);
36
}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff