Project

General

Profile

1
package eu.dnetlib.data.mdstore.modular.action;
2

    
3
import java.util.concurrent.Executor;
4
import java.util.concurrent.Executors;
5

    
6
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
7
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
8
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
9
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerAction;
10
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
11
import eu.dnetlib.rmi.data.MDStoreServiceException;
12
import eu.dnetlib.rmi.enabling.ISRegistryService;
13
import org.antlr.stringtemplate.StringTemplate;
14
import org.apache.commons.io.IOUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.core.io.ClassPathResource;
19

    
20
/**
21
 * The Class AbstractMDStoreAction.
22
 */
23
public abstract class AbstractMDStoreAction implements BlackboardServerAction<MDStoreActions> {
24

    
25
	/**
26
	 * Logger
27
	 */
28
	private static final Log log = LogFactory.getLog(AbstractMDStoreAction.class);
29
	private static final ClassPathResource mdstoreServiceStatusTemplate = new ClassPathResource(
30
			"/eu/dnetlib/data/mdstore/modular/mdstoreServiceStatusTemplate.xml.st");
31
	/**
32
	 * The executor.
33
	 */
34
	private final Executor executor = Executors.newCachedThreadPool();
35
	@Autowired
36
	private UniqueServiceLocator serviceLocator;
37
	/**
38
	 * The dao.
39
	 */
40
	private MDStoreDao dao;
41

    
42
	/**
43
	 * Execute async.
44
	 *
45
	 * @param handler the handler
46
	 * @param job     the job
47
	 * @throws MDStoreServiceException the MD store service exception
48
	 */
49
	protected abstract void executeAsync(final BlackboardServerHandler handler, final BlackboardJob job) throws MDStoreServiceException;
50

    
51
	/*
52
	 * (non-Javadoc)
53
	 * 
54
	 * @see
55
	 * eu.dnetlib.enabling.tools.blackboard.BlackboardServerAction#execute(eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler,
56
	 * eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
57
	 */
58
	@Override
59
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) {
60
		executor.execute(new Runnable() {
61

    
62
			@Override
63
			public void run() {
64
				try {
65
					handler.ongoing(job);
66
					executeAsync(handler, job);
67
				} catch (MDStoreServiceException e) {
68
					handler.failed(job, e);
69
				}
70
			}
71
		});
72
	}
73

    
74
	protected void completeWithSuccess(final BlackboardServerHandler handler, final BlackboardJob job) {
75
		// Don't change this synchronization rule
76
		synchronized (this) {
77
			updateMDStoreServiceProfile(job);
78
			handler.done(job);
79
		}
80
	}
81

    
82
	protected void completeWithFail(final BlackboardServerHandler handler, final BlackboardJob job, final Throwable e) {
83
		// Don't change this synchronization rule
84
		synchronized (this) {
85
			updateMDStoreServiceProfile(job);
86
			handler.failed(job, e);
87
		}
88
	}
89

    
90
	private void updateMDStoreServiceProfile(final BlackboardJob job) {
91
		final String id = job.getServiceId();
92

    
93
		log.info("Updating mdstore service profile status, id: " + id);
94
		try {
95
			final StringTemplate st = new StringTemplate(IOUtils.toString(mdstoreServiceStatusTemplate.getInputStream()));
96
			st.setAttribute("status", dao.getDBStatus());
97
			serviceLocator.getService(ISRegistryService.class).updateProfileNode(id, "//STATUS", st.toString());
98
		} catch (Exception e) {
99
			log.error("Error upadating profile " + id, e);
100
		}
101
	}
102

    
103
	/**
104
	 * Gets the dao.
105
	 *
106
	 * @return the dao
107
	 */
108
	public MDStoreDao getDao() {
109
		return dao;
110
	}
111

    
112
	/**
113
	 * Sets the dao.
114
	 *
115
	 * @param dao the new dao
116
	 */
117
	public void setDao(final MDStoreDao dao) {
118
		this.dao = dao;
119
	}
120

    
121
}
(1-1/11)