Project

General

Profile

« Previous | Next » 

Revision 30515

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

View differences:

modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/deploy.info
1
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/cnr-blackboard-common/trunk/", "deploy_repository": "dnet4-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/dnet4-snapshots", "name": "cnr-blackboard-common"}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/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-1.0.0/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-1.0.0/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-1.0.0/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
	@Override
48
	public String getId() {
49
		return message.getId();
50
	}
51

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

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

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

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

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

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

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

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

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

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

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

  
142
	public void setMessage(final BlackboardMessage message) {
143
		this.message = message;
144
	}
145

  
146
	public String getServiceId() {
147
		return serviceId;
148
	}
149

  
150
	public void setServiceId(final String serviceId) {
151
		this.serviceId = serviceId;
152
	}
153

  
154
	@Override
155
	public boolean isCompleted() {
156
		return getActionStatus() == ActionStatus.DONE || getActionStatus() == ActionStatus.FAILED;
157
	}
158

  
159
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/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.w3c.dom.Element;
13

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

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

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

  
35
	/**
36
	 * registry locator.
37
	 */
38
	private ServiceLocator<ISRegistryService> registryLocator;
39

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

  
45
	/**
46
	 * {@inheritDoc}
47
	 *
48
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardClientHandler#assign(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
49
	 */
50
	@Override
51
	public void assign(final BlackboardJob job) {
52
		checkJob(job);
53
		
54
		try {
55
			final BlackboardJobImpl impl = (BlackboardJobImpl) job;
56
			registryLocator.getService().addBlackBoardMessage(impl.getServiceId(), job.getId(), messageFactory.serialize(impl.getMessage()));
57
		} catch (final ISRegistryException e) {
58
			throw new IllegalStateException("cannot register blackboard message", e);
59
		} catch (final JAXBException e) {
60
			throw new IllegalArgumentException("cannot serialize blackboard message", e);
61
		}
62
	}
63

  
64

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

  
77

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

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

  
104
		job.setId(uuidGenerator.generateIdentifier());
105
		return job;
106
	}
107

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

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

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

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

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

  
132

  
133
	public JaxbFactory<BlackboardMessage> getMessageFactory() {
134
		return messageFactory;
135
	}
136

  
137

  
138
	public void setMessageFactory(final JaxbFactory<BlackboardMessage> messageFactory) {
139
		this.messageFactory = messageFactory;
140
	}
141

  
142

  
143
	public ServiceLocator<ISRegistryService> getRegistryLocator() {
144
		return registryLocator;
145
	}
146

  
147

  
148
	public void setRegistryLocator(final ServiceLocator<ISRegistryService> registryLocator) {
149
		this.registryLocator = registryLocator;
150
	}
151

  
152

  
153
	public UniqueIdentifierGenerator getUuidGenerator() {
154
		return uuidGenerator;
155
	}
156

  
157

  
158
	public void setUuidGenerator(final UniqueIdentifierGenerator uuidGenerator) {
159
		this.uuidGenerator = uuidGenerator;
160
	}
161

  
162
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/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.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.tools.OpaqueResource;
18
import eu.dnetlib.enabling.tools.ServiceLocator;
19
import eu.dnetlib.miscutils.jaxb.JaxbFactory;
20

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

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

  
34
	/**
35
	 * registry locator.
36
	 */
37
	private ServiceLocator<ISRegistryService> registryLocator;
38

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

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

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

  
53
			if (source == null)
54
				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
			final BlackboardJobImpl impl = (BlackboardJobImpl) job;
111
			registryLocator.getService().replyBlackBoardMessage(impl.getServiceId(), messageFactory.serialize(impl.getMessage()));
112
		} catch (final ISRegistryException e) {
113
			throw new IllegalStateException("cannot reply the blackboard message", e);
114
		} catch (final JAXBException e) {
115
			throw new IllegalArgumentException("cannot serialize blackboard message", e);
116
		}
117
	}
118

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

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

  
127
	public ServiceLocator<ISRegistryService> getRegistryLocator() {
128
		return registryLocator;
129
	}
130

  
131
	public void setRegistryLocator(final ServiceLocator<ISRegistryService> registryLocator) {
132
		this.registryLocator = registryLocator;
133
	}
134

  
135
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/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.
13
 * Job should be registered to only 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
	 * @see eu.dnetlib.enabling.tools.blackboard.BlackboardNotificationHandler#processJob(eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
34
	 */
35
	@Override
36
	protected void processJob(final BlackboardJob job) {
37

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

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

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

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

  
69
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/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.lang.builder.EqualsBuilder;
7
import org.apache.commons.lang.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-1.0.0/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-1.0.0/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-1.0.0/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-1.0.0/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-1.0.0/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.lang.builder.EqualsBuilder;
13
import org.apache.commons.lang.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-1.0.0/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-1.0.0/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
	 * Get the message id.
14
	 *
15
	 * @return id
16
	 */
17
	String getId();
18

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

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

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

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

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

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

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

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

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

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

  
95

  
96
	/**
97
	 * True if the action status is one of the termination statuses (e.g. done and failed).
98
	 *
99
	 * @return true if completed
100
	 */
101
	boolean isCompleted();
102

  
103
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/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-1.0.0/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-1.0.0/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-1.0.0/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
}
modules/cnr-blackboard-common/tags/cnr-blackboard-common-1.0.0/src/main/java/eu/dnetlib/enabling/tools/blackboard/BlackboardJobListener.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Implement this interface in order to receive notifications for specific blackboard messages.
5
 *
6
 * @author marko
7
 *
8
 */
9
public interface BlackboardJobListener {
10

  
11
	/**
12
	 * process the given job.
13
	 *
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff