Project

General

Profile

« Previous | Next » 

Revision 45121

codebase used to migrate to java8 the production system

View differences:

modules/cnr-notifications-common/trunk/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-notifications-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-notifications-common"}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/actions/SubscriptionAction.java
1
package eu.dnetlib.enabling.actions;
2

  
3
import eu.dnetlib.enabling.tools.blackboard.NotificationHandler;
4

  
5
/**
6
 * Manager subscription action.
7
 *
8
 * @author marko
9
 *
10
 */
11
public interface SubscriptionAction extends NotificationHandler {
12
	/**
13
	 * topic expression associated with this subscription action.
14
	 *
15
	 * @return SN topic expression
16
	 */
17
	String getTopicExpression();
18

  
19
	/**
20
	 * react on this topic prefix. Might be shorter than the topic expression.
21
	 *
22
	 * @return SN topic prefix
23
	 */
24
	String getTopicPrefix();
25
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/actions/AbstractSubscriptionAction.java
1
package eu.dnetlib.enabling.actions;
2

  
3
import javax.annotation.PostConstruct;
4

  
5
import org.springframework.beans.factory.annotation.Required;
6

  
7
import eu.dnetlib.enabling.actions.SubscriptionAction;
8
import eu.dnetlib.enabling.tools.Enableable;
9

  
10
/**
11
 * Common subscription action.
12
 *
13
 * @author marko
14
 *
15
 */
16
public abstract class AbstractSubscriptionAction implements SubscriptionAction, Enableable {
17

  
18
	/**
19
	 * topic expression.
20
	 */
21
	private String topicExpression;
22
	
23
	/**
24
	 * topic prefix.
25
	 */
26
	private String topicPrefix;
27

  
28
	/**
29
	 * true if enabled.
30
	 */
31
	private boolean enabled = true;
32

  
33
	/**
34
	 * init the default topic prefix.
35
	 */
36
	@PostConstruct
37
	public void init() {
38
		if (topicPrefix == null)
39
			topicPrefix = getTopicExpression().replace("/", ".");
40
	}
41

  
42
	@Override
43
	public String getTopicExpression() {
44
		return topicExpression;
45
	}
46
	
47
	@Required
48
	public void setTopicExpression(final String topicExpression) {
49
		this.topicExpression = topicExpression;
50
	}
51

  
52
	@Override
53
	public String getTopicPrefix() {
54
		return topicPrefix;
55
	}
56

  
57
	public void setTopicPrefix(final String topicPrefix) {
58
		this.topicPrefix = topicPrefix;
59
	}
60

  
61
	@Override
62
	public boolean isEnabled() {
63
		return enabled;
64
	}
65

  
66
	@Override
67
	public void setEnabled(boolean enabled) {
68
		this.enabled = enabled;
69
	}
70

  
71
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/AbstractSchedulable.java
1
package eu.dnetlib.enabling.tools;
2

  
3
import java.text.ParseException;
4

  
5
import javax.annotation.PostConstruct;
6
import javax.annotation.Resource;
7

  
8
import org.quartz.CronExpression;
9
import org.quartz.CronTrigger;
10
import org.quartz.Job;
11
import org.quartz.JobDataMap;
12
import org.quartz.JobDetail;
13
import org.quartz.JobExecutionContext;
14
import org.quartz.JobExecutionException;
15
import org.quartz.Scheduler;
16
import org.quartz.SchedulerException;
17
import org.quartz.Trigger;
18
import org.springframework.beans.factory.BeanNameAware;
19
import org.springframework.beans.factory.annotation.Required;
20

  
21
/**
22
 * Common implementation for schedulable beans.
23
 * 
24
 * @author claudio
25
 * 
26
 */
27
public abstract class AbstractSchedulable implements Schedulable, Job, BeanNameAware {
28

  
29
	private static final String THIS = "this";
30

  
31
	private final static String GROUP = "schedulableJobs";
32

  
33
	private boolean enabled;
34

  
35
	private CronExpression cronExpression;
36

  
37
	private String beanName;
38

  
39
	@Resource(name = "dnetJobScheduler")
40
	private Scheduler jobScheduler;
41

  
42
	@PostConstruct
43
	protected void init() {
44
		try {
45

  
46
			JobDataMap jobDataMap = new JobDataMap();
47
			jobDataMap.put(THIS, this);
48

  
49
			JobDetail jd = new JobDetail();
50
			jd.setName(getBeanName());
51
			jd.setGroup(GROUP);
52
			jd.setJobDataMap(jobDataMap);
53
			jd.setJobClass(this.getClass());
54

  
55
			jobScheduler.scheduleJob(jd, createTrigger());
56

  
57
		} catch (SchedulerException e) {
58
			throw new RuntimeException(e);
59
		}
60
	}
61

  
62
	private Trigger createTrigger() {
63
		try {
64
			CronTrigger trigger = new CronTrigger(getBeanName(), GROUP, getCronExpression());
65
			trigger.setMisfireInstruction(Trigger.INSTRUCTION_NOOP);
66
			trigger.setJobGroup(GROUP);
67
			trigger.setJobName(getBeanName());
68
			return trigger;
69
		} catch (ParseException e) {
70
			throw new IllegalArgumentException("invalid cron expression: " + cronExpression, e);
71
		}
72
	}
73

  
74
	protected abstract void doExecute();
75

  
76
	@Override
77
	public void execute() {
78
		// bean represents the quartz instance of this object
79
		if (isEnabled()) {
80
			doExecute();
81
		}
82
	}
83

  
84
	@Override
85
	public void execute(JobExecutionContext context) throws JobExecutionException {
86
		AbstractSchedulable bean = (AbstractSchedulable) context.getJobDetail().getJobDataMap().get(THIS);
87

  
88
		// bean represents the quartz instance of this object
89
		if (bean.isEnabled()) {
90
			bean.doExecute();
91
		}
92
	}
93

  
94
	@Override
95
	public boolean isEnabled() {
96
		return enabled;
97
	}
98

  
99
	@Override
100
	public void setEnabled(boolean enabled) {
101
		this.enabled = enabled;
102
	}
103

  
104
	@Override
105
	public String getCronExpression() {
106
		return cronExpression.getCronExpression();
107
	}
108

  
109
	@Required
110
	public void setCronExpression(String cronExpression) {
111
		try {
112
			this.cronExpression = new CronExpression(cronExpression);
113
		} catch (ParseException e) {
114
			throw new IllegalArgumentException("invalid cron expression: " + cronExpression, e);
115
		}
116
	}
117

  
118
	@Override
119
	public void updateCronExpression(String cronExpression) {
120

  
121
		if (!cronExpression.equals(getCronExpression())) {
122
			setCronExpression(cronExpression);
123
			try {
124
				jobScheduler.rescheduleJob(getBeanName(), GROUP, createTrigger());
125
			} catch (SchedulerException e) {
126
				throw new RuntimeException("unable to reschedule trigger", e);
127
			}
128
		}
129
	}
130

  
131
	@Override
132
	public String getNextFireTime() {
133
		try {
134
			if (isPaused()) {
135
				return "";
136
			}
137
			if (!isEnabled()) {
138
				return "";
139
			}
140
			Trigger t = jobScheduler.getTrigger(getBeanName(), GROUP);
141
			return t != null ? t.getNextFireTime().toString() : "";
142
		} catch (SchedulerException e) {
143
			throw new RuntimeException("unable to get trigger", e);
144
		}
145
	}
146

  
147
	@Override
148
	public boolean isPaused() {
149
		try {
150
			int state = jobScheduler.getTriggerState(getBeanName(), GROUP);
151
			switch (state) {
152
			case Trigger.STATE_PAUSED:
153
			case Trigger.STATE_NONE:
154
			case Trigger.STATE_ERROR:
155
				return true;
156
			default:
157
				return false;
158
			}
159
		} catch (SchedulerException e) {
160
			throw new RuntimeException("unable to get trigger", e);
161
		}
162
	}
163

  
164
	@Override
165
	public void pause() {
166
		try {
167
			jobScheduler.pauseTrigger(getBeanName(), GROUP);
168
		} catch (SchedulerException e) {
169
			throw new RuntimeException("unable to pause trigger", e);
170
		}
171
	}
172

  
173
	@Override
174
	public void resume() {
175
		try {
176
			jobScheduler.resumeTrigger(getBeanName(), GROUP);
177
		} catch (SchedulerException e) {
178
			throw new RuntimeException("unable to resume trigger", e);
179
		}
180
	}
181

  
182
	public String getBeanName() {
183
		return beanName;
184
	}
185

  
186
	@Override
187
	@Required
188
	public void setBeanName(String beanName) {
189
		this.beanName = beanName;
190
	}
191

  
192
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/SchedulableProfileUpdater.java
1
package eu.dnetlib.enabling.tools;
2

  
3
import javax.annotation.Resource;
4

  
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
import org.springframework.beans.factory.annotation.Required;
8

  
9
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
10
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
11
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
12

  
13
/**
14
 * Schedulable implementation of an xUpdate.
15
 * 
16
 * @author claudio
17
 * 
18
 */
19
public class SchedulableProfileUpdater extends AbstractSchedulable {
20

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

  
23
	private String xUpdate;
24

  
25
	@Resource
26
	private UniqueServiceLocator serviceLocator;
27

  
28
	@Override
29
	protected void doExecute() {
30
		try {
31
			log.info("triggering scheduled reindex: " + getxUpdate());
32
			serviceLocator.getService(ISRegistryService.class).executeXUpdate(getxUpdate());
33
		} catch (ISRegistryException e) {
34

  
35
			throw new RuntimeException(e);
36
		}
37
	}
38

  
39
	public String getxUpdate() {
40
		return xUpdate;
41
	}
42

  
43
	@Required
44
	public void setxUpdate(String xUpdate) {
45
		this.xUpdate = xUpdate;
46
	}
47

  
48
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationInfo.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import eu.dnetlib.miscutils.datetime.DateUtils;
4

  
5
public class NotificationInfo {
6
	private String name;
7
	private String subscrId;
8
	private String topic;
9
	private String rsId;
10
	private String profile;
11
	private String date;
12
	
13
	public NotificationInfo() {	
14
		this.date = DateUtils.now_ISO8601();
15
	}
16
	
17
	public NotificationInfo(String name, String subscrId, String topic, String rsId, String profile) {
18
		super();
19
		this.name = name;
20
		this.subscrId = subscrId;
21
		this.topic = topic;
22
		this.rsId = rsId;
23
		this.profile = profile;
24
		this.date = DateUtils.now_ISO8601();
25
	}
26

  
27
	public String getName() {
28
		return name;
29
	}
30

  
31
	public void setName(String name) {
32
		this.name = name;
33
	}
34

  
35
	public String getSubscrId() {
36
		return subscrId;
37
	}
38

  
39
	public void setSubscrId(String subscrId) {
40
		this.subscrId = subscrId;
41
	}
42

  
43
	public String getTopic() {
44
		return topic;
45
	}
46

  
47
	public void setTopic(String topic) {
48
		this.topic = topic;
49
	}
50

  
51
	public String getRsId() {
52
		return rsId;
53
	}
54

  
55
	public void setRsId(String rsId) {
56
		this.rsId = rsId;
57
	}
58

  
59
	public String getProfile() {
60
		return profile;
61
	}
62

  
63
	public void setProfile(String profile) {
64
		this.profile = profile;
65
	}
66

  
67
	public String getDate() {
68
		return date;
69
	}
70

  
71
	public void setDate(String date) {
72
		this.date = date;
73
	}
74
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3

  
4
/**
5
 * Each service may have a chain of NotificationHandlers, which process incoming notifications.
6
 * 
7
 * @author marko
8
 *
9
 */
10
public interface NotificationHandler {
11
	/**
12
	 * Incoming notification received.
13
	 * 
14
	 * @param subscrId subscriptionId
15
	 * @param topic topic
16
	 * @param rsId resource id
17
	 * @param profile resource profile
18
	 */
19
	void notified(String subscrId, final String topic, final String rsId, final String profile);
20
	
21
	
22
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHandlerChain.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Invokes a chain of notification handlers for each incoming notification. Normally an instance of this class is
5
 * registered as the notification handler for a given service.
6
 * 
7
 * @author marko
8
 * 
9
 */
10
public interface NotificationHandlerChain extends NotificationHandler {
11

  
12
	public void delegateNotification(final String subscrId, final String topic, final String rsId, final String profile, final NotificationHandler handler);
13

  
14
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHistory.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.util.Map;
4

  
5
public interface NotificationHistory {
6

  
7
	/**
8
	 * Return a saved notification associated to a bean and to a rsId.
9
	 * 
10
	 * @param name
11
	 * @return
12
	 */
13
	public NotificationInfo obtainNotification(String bean, String rsId);
14
	
15
	/**
16
	 * Save a notification.
17
	 * 
18
	 * @param info
19
	 */
20
	void saveNotification(NotificationInfo info);
21

  
22
	
23
	/**
24
	 * Return all saved notifications.
25
	 * 
26
	 * @return
27
	 */
28
	Map<String, Map<String, NotificationInfo>> obtainAllNotifications();
29
	
30
	/**
31
	 * Remove a saved notifications.
32
	 * 
33
	 * @return
34
	 */
35
	public void clearNotification(String bean, String rsId);
36
	
37
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHandlerChainImpl.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.util.Collection;
4
import java.util.Map;
5

  
6
import javax.annotation.Resource;
7

  
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.springframework.beans.factory.annotation.Required;
11
import org.springframework.core.task.TaskExecutor;
12

  
13
import eu.dnetlib.enabling.actions.AbstractSubscriptionAction;
14
import eu.dnetlib.enabling.tools.Enableable;
15
import eu.dnetlib.enabling.tools.EnableableEnumerator;
16

  
17
public class NotificationHandlerChainImpl implements NotificationHandlerChain {
18
	
19
	/**
20
	 * logger.
21
	 */
22
	private static final Log log = LogFactory.getLog(NotificationHandlerChain.class); // NOPMD by marko on 11/24/08 5:02 PM
23

  
24
	/**
25
	 * notification handler chain.
26
	 */
27
	private Collection<NotificationHandler> handlers;
28

  
29
	/**
30
	 * task executor used for invoking the handlers.
31
	 */
32
	private TaskExecutor handlerExecutor;
33

  
34
	@Resource
35
	private NotificationHistory notificationHistory;
36

  
37
	@Resource
38
	private EnableableEnumerator enableableEnumerator;
39

  
40
	/**
41
	 * {@inheritDoc}
42
	 * 
43
	 * @see eu.dnetlib.enabling.tools.blackboard.NotificationHandler#notified(java.lang.String, java.lang.String,
44
	 *      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

  
49
		for (final NotificationHandler handler : handlers) {
50

  
51
			try {
52
				if (handler instanceof AbstractSubscriptionAction)
53
					if (topic.startsWith(((AbstractSubscriptionAction) handler).getTopicPrefix()))
54
						if (!((Enableable) handler).isEnabled()) {
55
							for (Map.Entry<String, Enableable> entry : enableableEnumerator.getAllEnableables().entrySet()) {
56
								if (entry.getValue() == handler) {
57
									NotificationInfo info = new NotificationInfo();
58
									info.setName(entry.getKey());
59
									info.setProfile(profile);
60
									info.setRsId(rsId);
61
									info.setSubscrId(subscrId);
62
									info.setTopic(topic);
63
									notificationHistory.saveNotification(info);
64
								}
65
							}
66
							continue;
67
						}
68
					
69
				delegateNotification(subscrId, topic, rsId, profile, handler);
70
			} catch (final RuntimeException e) {
71
				log.fatal("error processing notification handler " + handler, e);
72
			}
73
		}
74
	}
75

  
76
	@Override
77
	public void delegateNotification(final String subscrId, final String topic, final String rsId, final String profile, final NotificationHandler handler) {
78

  
79
		handlerExecutor.execute(new Runnable() {
80
			@Override
81
			public void run() {
82
				handler.notified(subscrId, topic, rsId, profile);
83
			}
84
		});
85
	}
86

  
87
	public Collection<NotificationHandler> getHandlers() {
88
		return handlers;
89
	}
90

  
91
	@Required
92
	public void setHandlers(final Collection<NotificationHandler> handlers) {
93
		this.handlers = handlers;
94
	}
95

  
96
	public TaskExecutor getHandlerExecutor() {
97
		return handlerExecutor;
98
	}
99

  
100
	@Required
101
	public void setHandlerExecutor(TaskExecutor handlerExecutor) {
102
		this.handlerExecutor = handlerExecutor;
103
	}
104
}
0 105

  
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHistoryImpl.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

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

  
6

  
7
public class NotificationHistoryImpl implements NotificationHistory {
8
	
9
	private Map<String, Map<String, NotificationInfo>> notifications = new HashMap<String, Map<String, NotificationInfo>>();
10

  
11
	@Override
12
	public NotificationInfo obtainNotification(String bean, String rsId) {
13
		if (!notifications.containsKey(bean))
14
			return null;
15
		return notifications.get(bean).get(rsId);
16
	}
17
	
18
	@Override
19
	public void saveNotification(NotificationInfo info) {
20
		String bean = info.getName();
21
		String rsId = info.getRsId();
22
		
23
		if (!notifications.containsKey(bean))
24
			notifications.put(bean, new HashMap<String, NotificationInfo>());
25
		
26
		notifications.get(bean).put(rsId, info);
27
	}
28

  
29
	@Override
30
	public Map<String, Map<String, NotificationInfo>> obtainAllNotifications() {
31
		return notifications;
32
	}
33

  
34
	@Override
35
	public void clearNotification(String bean, String rsId) {
36
		if (notifications.containsKey(bean))
37
			notifications.get(bean).remove(rsId);
38
	}
39
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/Enableable.java
1
package eu.dnetlib.enabling.tools;
2

  
3
/**
4
 * Simple interface for classes, which's objects can be enabled or disabled.
5
 * 
6
 * 
7
 * @author marko
8
 * 
9
 */
10
public interface Enableable {
11
	/**
12
	 * True if this object enabled.
13
	 * 
14
	 * @return if this object is currently enabled.
15
	 */
16
	boolean isEnabled();
17

  
18
	/**
19
	 * Enables of disables this object.
20
	 * 
21
	 * @param enabled
22
	 *            enabled
23
	 */
24
	void setEnabled(boolean enabled);
25
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/Schedulable.java
1
package eu.dnetlib.enabling.tools;
2

  
3
/**
4
 * Simple interface for classes, which's objects actions can be scheduled.
5
 * 
6
 * @author claudio
7
 */
8
public interface Schedulable extends Enableable {
9
	/**
10
	 * Updates the cron expression.
11
	 * 
12
	 * @param cronExpression
13
	 */
14
	void updateCronExpression(String cronExpression);
15
	
16
	/**
17
	 * Getter for the cron expression.
18
	 * 
19
	 * @return the String representing the cron expression.
20
	 */
21
	String getCronExpression();
22
	
23
	/**
24
	 * Computes the next time the doExecute method will be invoked.
25
	 * 
26
	 * @return the String representing the next fire time.
27
	 */
28
	String getNextFireTime();
29

  
30
	/**
31
	 * True if the object has been paused.
32
	 * 
33
	 * @return if the bean has been paused.
34
	 */
35
	boolean isPaused();
36
	
37
	/**
38
	 * Prevents the bean for being scheduled for future invocations.
39
	 */
40
	void pause();
41
	
42
	/**
43
	 * Resumes the bean for being scheduled for future invocations.
44
	 */
45
	void resume();
46
	
47
	/**
48
	 * Must be implemented to perform the real action.
49
	 */
50
	void execute();	
51
	
52
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/EnableableEnumerator.java
1
package eu.dnetlib.enabling.tools;
2

  
3
import java.util.Map;
4

  
5
import org.springframework.beans.BeansException;
6
import org.springframework.beans.factory.BeanFactory;
7
import org.springframework.beans.factory.BeanFactoryAware;
8
import org.springframework.beans.factory.ListableBeanFactory;
9
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
10

  
11
/**
12
 * This bean has to live in the same bean factory where all enableables live. Thus it cannot be put inside the EnableablController, which
13
 * lives in the webContext.
14
 * 
15
 * @author marko
16
 * 
17
 */
18
public class EnableableEnumerator implements BeanFactoryAware {
19

  
20
	/**
21
	 * bean factory.
22
	 */
23
	private ListableBeanFactory beanFactory;
24

  
25
	/**
26
	 * Get all beans implementing the Enableable interface.
27
	 * 
28
	 * @return
29
	 */
30
	public Map<String, Enableable> getAllEnableables() {
31
		return beanFactory.getBeansOfType(Enableable.class);
32
	}
33

  
34
	@Override
35
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
36
		this.beanFactory = (ListableBeanFactory) beanFactory;
37
	}
38

  
39
	public ListableBeanFactory getBeanFactory() {
40
		return beanFactory;
41
	}
42

  
43
	/**
44
	 * Get given enableable or null.
45
	 * 
46
	 * @param name
47
	 * @return
48
	 */
49
	public Enableable getEnableable(final String name) {
50
		try {
51
			return beanFactory.getBean(name, Enableable.class);
52
		} catch (final NoSuchBeanDefinitionException e) {
53
			return null;
54
		}
55
	}
56

  
57
}
modules/cnr-notifications-common/trunk/src/main/java/eu/dnetlib/enabling/tools/SchedulableEnumerator.java
1
package eu.dnetlib.enabling.tools;
2

  
3
import java.util.Map;
4

  
5
import org.springframework.beans.BeansException;
6
import org.springframework.beans.factory.BeanFactory;
7
import org.springframework.beans.factory.BeanFactoryAware;
8
import org.springframework.beans.factory.ListableBeanFactory;
9
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
10

  
11
/**
12
 * This bean has to live in the same bean factory where all schedulables live. Thus it cannot be put inside the EnableablController, which
13
 * lives in the webContext.
14
 * 
15
 * @author claudio
16
 * 
17
 */
18
public class SchedulableEnumerator implements BeanFactoryAware {
19

  
20
	/**
21
	 * bean factory.
22
	 */
23
	private ListableBeanFactory beanFactory;
24

  
25
	/**
26
	 * Get all beans implementing the Schedulable interface.
27
	 * 
28
	 * @return
29
	 */
30
	public Map<String, Schedulable> getAllSchedulables() {
31
		return beanFactory.getBeansOfType(Schedulable.class);
32
	}
33

  
34
	@Override
35
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
36
		this.beanFactory = (ListableBeanFactory) beanFactory;
37
	}
38

  
39
	public ListableBeanFactory getBeanFactory() {
40
		return beanFactory;
41
	}
42

  
43
	/**
44
	 * Get given schedulable or null.
45
	 * 
46
	 * @param name
47
	 * @return
48
	 */
49
	public Schedulable getSchedulable(final String name) {
50
		try {
51
			return beanFactory.getBean(name, Schedulable.class);
52
		} catch (final NoSuchBeanDefinitionException e) {
53
			return null;
54
		}
55
	}
56

  
57
}
modules/cnr-notifications-common/trunk/src/main/resources/eu/dnetlib/enabling/tools/applicationContext-enableable.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
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
5

  
6
	<bean id="enableableEnumerator"
7
		class="eu.dnetlib.enabling.tools.EnableableEnumerator" />
8
		
9
	<bean id="notificationHistory"
10
		class="eu.dnetlib.enabling.tools.blackboard.NotificationHistoryImpl" />
11

  
12
</beans>
modules/cnr-notifications-common/trunk/src/main/resources/eu/dnetlib/enabling/tools/applicationContext-schedulable.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" xmlns:jaxws="http://cxf.apache.org/jaxws"
4
	xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:wsa="http://cxf.apache.org/ws/addressing"
5
	xmlns:p="http://www.springframework.org/schema/p" xmlns:http="http://cxf.apache.org/transports/http/configuration"
6
	xmlns:t="http://dnetlib.eu/springbeans/t" xmlns:template="http://dnetlib.eu/springbeans/template"
7
	xmlns:util="http://www.springframework.org/schema/util"
8
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
9
						http://cxf.apache.org/ws/addressing http://cxf.apache.org/schemas/ws-addr-conf.xsd
10
						http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd
11
						http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
12
						http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
13
						http://dnetlib.eu/springbeans/template http://dnetlib.eu/springbeans/template.xsd
14
						http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
15

  
16
	<bean id="dnetJobScheduler"
17
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
18
		destroy-method="destroy">
19
		<property name="jobFactory">
20
			<bean id="jobSchedulerFactory"
21
				class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
22
		</property>
23
	</bean>
24

  
25
	<bean id="schedulableEnumerator"
26
		class="eu.dnetlib.enabling.tools.SchedulableEnumerator" />
27

  
28
</beans>
modules/cnr-notifications-common/trunk/pom.xml
1
<?xml version="1.0" ?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<parent>
4
		<groupId>eu.dnetlib</groupId>
5
		<artifactId>dnet-parent</artifactId>
6
		<version>1.0.0</version>
7
		<relativePath />
8
	</parent>
9
	<modelVersion>4.0.0</modelVersion>
10
	<groupId>eu.dnetlib</groupId>
11
	<artifactId>cnr-notifications-common</artifactId>
12
	<packaging>jar</packaging>
13
	<version>2.1.1-SNAPSHOT</version>
14
	<scm>
15
	  <developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/cnr-notifications-common/trunk</developerConnection>
16
	</scm>
17
	<dependencies>
18
		<dependency>
19
			<groupId>eu.dnetlib</groupId>
20
			<artifactId>cnr-service-common</artifactId>
21
			<version>[2.1.0,3.0.0)</version>
22
		</dependency>
23
		<dependency>
24
			<groupId>org.springframework</groupId>
25
			<artifactId>spring-context-support</artifactId>
26
			<version>${spring.version}</version>
27
		</dependency>
28
		<dependency>
29
			<groupId>opensymphony</groupId>
30
			<artifactId>quartz</artifactId>
31
			<version>1.6.6</version>
32
		</dependency>
33
	</dependencies>
34
</project>
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.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-notifications-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-notifications-common"}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/actions/SubscriptionAction.java
1
package eu.dnetlib.enabling.actions;
2

  
3
import eu.dnetlib.enabling.tools.blackboard.NotificationHandler;
4

  
5
/**
6
 * Manager subscription action.
7
 *
8
 * @author marko
9
 *
10
 */
11
public interface SubscriptionAction extends NotificationHandler {
12
	/**
13
	 * topic expression associated with this subscription action.
14
	 *
15
	 * @return SN topic expression
16
	 */
17
	String getTopicExpression();
18

  
19
	/**
20
	 * react on this topic prefix. Might be shorter than the topic expression.
21
	 *
22
	 * @return SN topic prefix
23
	 */
24
	String getTopicPrefix();
25
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/actions/AbstractSubscriptionAction.java
1
package eu.dnetlib.enabling.actions;
2

  
3
import javax.annotation.PostConstruct;
4

  
5
import org.springframework.beans.factory.annotation.Required;
6

  
7
import eu.dnetlib.enabling.actions.SubscriptionAction;
8
import eu.dnetlib.enabling.tools.Enableable;
9

  
10
/**
11
 * Common subscription action.
12
 *
13
 * @author marko
14
 *
15
 */
16
public abstract class AbstractSubscriptionAction implements SubscriptionAction, Enableable {
17

  
18
	/**
19
	 * topic expression.
20
	 */
21
	private String topicExpression;
22
	
23
	/**
24
	 * topic prefix.
25
	 */
26
	private String topicPrefix;
27

  
28
	/**
29
	 * true if enabled.
30
	 */
31
	private boolean enabled = true;
32

  
33
	/**
34
	 * init the default topic prefix.
35
	 */
36
	@PostConstruct
37
	public void init() {
38
		if (topicPrefix == null)
39
			topicPrefix = getTopicExpression().replace("/", ".");
40
	}
41

  
42
	@Override
43
	public String getTopicExpression() {
44
		return topicExpression;
45
	}
46
	
47
	@Required
48
	public void setTopicExpression(final String topicExpression) {
49
		this.topicExpression = topicExpression;
50
	}
51

  
52
	@Override
53
	public String getTopicPrefix() {
54
		return topicPrefix;
55
	}
56

  
57
	public void setTopicPrefix(final String topicPrefix) {
58
		this.topicPrefix = topicPrefix;
59
	}
60

  
61
	@Override
62
	public boolean isEnabled() {
63
		return enabled;
64
	}
65

  
66
	@Override
67
	public void setEnabled(boolean enabled) {
68
		this.enabled = enabled;
69
	}
70

  
71
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/tools/AbstractSchedulable.java
1
package eu.dnetlib.enabling.tools;
2

  
3
import java.text.ParseException;
4

  
5
import javax.annotation.PostConstruct;
6
import javax.annotation.Resource;
7

  
8
import org.quartz.CronExpression;
9
import org.quartz.CronTrigger;
10
import org.quartz.Job;
11
import org.quartz.JobDataMap;
12
import org.quartz.JobDetail;
13
import org.quartz.JobExecutionContext;
14
import org.quartz.JobExecutionException;
15
import org.quartz.Scheduler;
16
import org.quartz.SchedulerException;
17
import org.quartz.Trigger;
18
import org.springframework.beans.factory.BeanNameAware;
19
import org.springframework.beans.factory.annotation.Required;
20

  
21
/**
22
 * Common implementation for schedulable beans.
23
 * 
24
 * @author claudio
25
 * 
26
 */
27
public abstract class AbstractSchedulable implements Schedulable, Job, BeanNameAware {
28

  
29
	private static final String THIS = "this";
30

  
31
	private final static String GROUP = "schedulableJobs";
32

  
33
	private boolean enabled;
34

  
35
	private CronExpression cronExpression;
36

  
37
	private String beanName;
38

  
39
	@Resource(name = "dnetJobScheduler")
40
	private Scheduler jobScheduler;
41

  
42
	@PostConstruct
43
	protected void init() {
44
		try {
45

  
46
			JobDataMap jobDataMap = new JobDataMap();
47
			jobDataMap.put(THIS, this);
48

  
49
			JobDetail jd = new JobDetail();
50
			jd.setName(getBeanName());
51
			jd.setGroup(GROUP);
52
			jd.setJobDataMap(jobDataMap);
53
			jd.setJobClass(this.getClass());
54

  
55
			jobScheduler.scheduleJob(jd, createTrigger());
56

  
57
		} catch (SchedulerException e) {
58
			throw new RuntimeException(e);
59
		}
60
	}
61

  
62
	private Trigger createTrigger() {
63
		try {
64
			CronTrigger trigger = new CronTrigger(getBeanName(), GROUP, getCronExpression());
65
			trigger.setMisfireInstruction(Trigger.INSTRUCTION_NOOP);
66
			trigger.setJobGroup(GROUP);
67
			trigger.setJobName(getBeanName());
68
			return trigger;
69
		} catch (ParseException e) {
70
			throw new IllegalArgumentException("invalid cron expression: " + cronExpression, e);
71
		}
72
	}
73

  
74
	protected abstract void doExecute();
75

  
76
	@Override
77
	public void execute() {
78
		// bean represents the quartz instance of this object
79
		if (isEnabled()) {
80
			doExecute();
81
		}
82
	}
83

  
84
	@Override
85
	public void execute(JobExecutionContext context) throws JobExecutionException {
86
		AbstractSchedulable bean = (AbstractSchedulable) context.getJobDetail().getJobDataMap().get(THIS);
87

  
88
		// bean represents the quartz instance of this object
89
		if (bean.isEnabled()) {
90
			bean.doExecute();
91
		}
92
	}
93

  
94
	@Override
95
	public boolean isEnabled() {
96
		return enabled;
97
	}
98

  
99
	@Override
100
	public void setEnabled(boolean enabled) {
101
		this.enabled = enabled;
102
	}
103

  
104
	@Override
105
	public String getCronExpression() {
106
		return cronExpression.getCronExpression();
107
	}
108

  
109
	@Required
110
	public void setCronExpression(String cronExpression) {
111
		try {
112
			this.cronExpression = new CronExpression(cronExpression);
113
		} catch (ParseException e) {
114
			throw new IllegalArgumentException("invalid cron expression: " + cronExpression, e);
115
		}
116
	}
117

  
118
	@Override
119
	public void updateCronExpression(String cronExpression) {
120

  
121
		if (!cronExpression.equals(getCronExpression())) {
122
			setCronExpression(cronExpression);
123
			try {
124
				jobScheduler.rescheduleJob(getBeanName(), GROUP, createTrigger());
125
			} catch (SchedulerException e) {
126
				throw new RuntimeException("unable to reschedule trigger", e);
127
			}
128
		}
129
	}
130

  
131
	@Override
132
	public String getNextFireTime() {
133
		try {
134
			if (isPaused()) {
135
				return "";
136
			}
137
			if (!isEnabled()) {
138
				return "";
139
			}
140
			Trigger t = jobScheduler.getTrigger(getBeanName(), GROUP);
141
			return t != null ? t.getNextFireTime().toString() : "";
142
		} catch (SchedulerException e) {
143
			throw new RuntimeException("unable to get trigger", e);
144
		}
145
	}
146

  
147
	@Override
148
	public boolean isPaused() {
149
		try {
150
			int state = jobScheduler.getTriggerState(getBeanName(), GROUP);
151
			switch (state) {
152
			case Trigger.STATE_PAUSED:
153
			case Trigger.STATE_NONE:
154
			case Trigger.STATE_ERROR:
155
				return true;
156
			default:
157
				return false;
158
			}
159
		} catch (SchedulerException e) {
160
			throw new RuntimeException("unable to get trigger", e);
161
		}
162
	}
163

  
164
	@Override
165
	public void pause() {
166
		try {
167
			jobScheduler.pauseTrigger(getBeanName(), GROUP);
168
		} catch (SchedulerException e) {
169
			throw new RuntimeException("unable to pause trigger", e);
170
		}
171
	}
172

  
173
	@Override
174
	public void resume() {
175
		try {
176
			jobScheduler.resumeTrigger(getBeanName(), GROUP);
177
		} catch (SchedulerException e) {
178
			throw new RuntimeException("unable to resume trigger", e);
179
		}
180
	}
181

  
182
	public String getBeanName() {
183
		return beanName;
184
	}
185

  
186
	@Override
187
	@Required
188
	public void setBeanName(String beanName) {
189
		this.beanName = beanName;
190
	}
191

  
192
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/tools/SchedulableProfileUpdater.java
1
package eu.dnetlib.enabling.tools;
2

  
3
import javax.annotation.Resource;
4

  
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
import org.springframework.beans.factory.annotation.Required;
8

  
9
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
10
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
11

  
12
/**
13
 * Schedulable implementation of an xUpdate.
14
 * 
15
 * @author claudio
16
 * 
17
 */
18
public class SchedulableProfileUpdater extends AbstractSchedulable {
19

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

  
22
	private String xUpdate;
23

  
24
	@Resource
25
	private ServiceLocator<ISRegistryService> registryLocator;
26

  
27
	@Override
28
	protected void doExecute() {
29
		try {
30
			log.info("triggering scheduled reindex: " + getxUpdate());
31
			registryLocator.getService().executeXUpdate(getxUpdate());
32
		} catch (ISRegistryException e) {
33

  
34
			throw new RuntimeException(e);
35
		}
36
	}
37

  
38
	public String getxUpdate() {
39
		return xUpdate;
40
	}
41

  
42
	@Required
43
	public void setxUpdate(String xUpdate) {
44
		this.xUpdate = xUpdate;
45
	}
46

  
47
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationInfo.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import eu.dnetlib.miscutils.datetime.DateUtils;
4

  
5
public class NotificationInfo {
6
	private String name;
7
	private String subscrId;
8
	private String topic;
9
	private String rsId;
10
	private String profile;
11
	private String date;
12
	
13
	public NotificationInfo() {	
14
		this.date = DateUtils.now_ISO8601();
15
	}
16
	
17
	public NotificationInfo(String name, String subscrId, String topic, String rsId, String profile) {
18
		super();
19
		this.name = name;
20
		this.subscrId = subscrId;
21
		this.topic = topic;
22
		this.rsId = rsId;
23
		this.profile = profile;
24
		this.date = DateUtils.now_ISO8601();
25
	}
26

  
27
	public String getName() {
28
		return name;
29
	}
30

  
31
	public void setName(String name) {
32
		this.name = name;
33
	}
34

  
35
	public String getSubscrId() {
36
		return subscrId;
37
	}
38

  
39
	public void setSubscrId(String subscrId) {
40
		this.subscrId = subscrId;
41
	}
42

  
43
	public String getTopic() {
44
		return topic;
45
	}
46

  
47
	public void setTopic(String topic) {
48
		this.topic = topic;
49
	}
50

  
51
	public String getRsId() {
52
		return rsId;
53
	}
54

  
55
	public void setRsId(String rsId) {
56
		this.rsId = rsId;
57
	}
58

  
59
	public String getProfile() {
60
		return profile;
61
	}
62

  
63
	public void setProfile(String profile) {
64
		this.profile = profile;
65
	}
66

  
67
	public String getDate() {
68
		return date;
69
	}
70

  
71
	public void setDate(String date) {
72
		this.date = date;
73
	}
74
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHandler.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3

  
4
/**
5
 * Each service may have a chain of NotificationHandlers, which process incoming notifications.
6
 * 
7
 * @author marko
8
 *
9
 */
10
public interface NotificationHandler {
11
	/**
12
	 * Incoming notification received.
13
	 * 
14
	 * @param subscrId subscriptionId
15
	 * @param topic topic
16
	 * @param rsId resource id
17
	 * @param profile resource profile
18
	 */
19
	void notified(String subscrId, final String topic, final String rsId, final String profile);
20
	
21
	
22
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHandlerChain.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
/**
4
 * Invokes a chain of notification handlers for each incoming notification. Normally an instance of this class is
5
 * registered as the notification handler for a given service.
6
 * 
7
 * @author marko
8
 * 
9
 */
10
public interface NotificationHandlerChain extends NotificationHandler {
11

  
12
	public void delegateNotification(final String subscrId, final String topic, final String rsId, final String profile, final NotificationHandler handler);
13

  
14
}
modules/cnr-notifications-common/tags/cnr-notifications-common-1.1.0/src/main/java/eu/dnetlib/enabling/tools/blackboard/NotificationHistory.java
1
package eu.dnetlib.enabling.tools.blackboard;
2

  
3
import java.util.Map;
4

  
5
public interface NotificationHistory {
6

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff