1
|
package eu.dnetlib.data.mdstore.modular.action;
|
2
|
|
3
|
import java.util.concurrent.Executor;
|
4
|
import java.util.concurrent.Executors;
|
5
|
|
6
|
import javax.annotation.Resource;
|
7
|
|
8
|
import eu.dnetlib.msro.workflows.sarasvati.registry.GraphProcessRegistry;
|
9
|
import org.antlr.stringtemplate.StringTemplate;
|
10
|
import org.apache.commons.io.IOUtils;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.springframework.core.io.ClassPathResource;
|
14
|
|
15
|
import eu.dnetlib.data.mdstore.MDStoreServiceException;
|
16
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
|
17
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
18
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
19
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
|
20
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerAction;
|
21
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
|
22
|
|
23
|
/**
|
24
|
* The Class AbstractMDStoreAction.
|
25
|
*/
|
26
|
public abstract class AbstractMDStoreAction implements BlackboardServerAction<MDStoreActions> {
|
27
|
|
28
|
@Resource
|
29
|
private UniqueServiceLocator serviceLocator;
|
30
|
|
31
|
@Resource
|
32
|
private GraphProcessRegistry graphProcessRegistry;
|
33
|
|
34
|
/** The dao. */
|
35
|
private MDStoreDao dao;
|
36
|
|
37
|
/** The executor. */
|
38
|
private final Executor executor = Executors.newCachedThreadPool();
|
39
|
|
40
|
/** Logger */
|
41
|
private static final Log log = LogFactory.getLog(AbstractMDStoreAction.class);
|
42
|
|
43
|
private static final ClassPathResource mdstoreServiceStatusTemplate = new ClassPathResource(
|
44
|
"/eu/dnetlib/data/mdstore/modular/mdstoreServiceStatusTemplate.xml.st");
|
45
|
|
46
|
/**
|
47
|
* Execute async.
|
48
|
*
|
49
|
* @param handler
|
50
|
* the handler
|
51
|
* @param job
|
52
|
* the job
|
53
|
* @throws MDStoreServiceException
|
54
|
* the MD store service exception
|
55
|
*/
|
56
|
protected abstract void executeAsync(final BlackboardServerHandler handler, final BlackboardJob job) throws MDStoreServiceException;
|
57
|
|
58
|
/*
|
59
|
* (non-Javadoc)
|
60
|
*
|
61
|
* @see
|
62
|
* eu.dnetlib.enabling.tools.blackboard.BlackboardServerAction#execute(eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler,
|
63
|
* eu.dnetlib.enabling.tools.blackboard.BlackboardJob)
|
64
|
*/
|
65
|
@Override
|
66
|
public void execute(final BlackboardServerHandler handler, final BlackboardJob job) {
|
67
|
executor.execute(new Runnable() {
|
68
|
|
69
|
@Override
|
70
|
public void run() {
|
71
|
try {
|
72
|
handler.ongoing(job);
|
73
|
executeAsync(handler, job);
|
74
|
} catch (MDStoreServiceException e) {
|
75
|
handler.failed(job, e);
|
76
|
}
|
77
|
}
|
78
|
});
|
79
|
}
|
80
|
|
81
|
protected void completeWithSuccess(final BlackboardServerHandler handler, final BlackboardJob job) {
|
82
|
// Don't change this synchronization rule
|
83
|
synchronized (this) {
|
84
|
updateMDStoreServiceProfile(job);
|
85
|
handler.done(job);
|
86
|
}
|
87
|
}
|
88
|
|
89
|
protected void completeWithFail(final BlackboardServerHandler handler, final BlackboardJob job, final Throwable e) {
|
90
|
// Don't change this synchronization rule
|
91
|
synchronized (this) {
|
92
|
updateMDStoreServiceProfile(job);
|
93
|
handler.failed(job, e);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
private void updateMDStoreServiceProfile(final BlackboardJob job) {
|
98
|
final String id = job.getServiceId();
|
99
|
|
100
|
log.info("Updating mdstore service profile status, id: " + id);
|
101
|
try {
|
102
|
final StringTemplate st = new StringTemplate(IOUtils.toString(mdstoreServiceStatusTemplate.getInputStream()));
|
103
|
st.setAttribute("status", dao.getDBStatus());
|
104
|
serviceLocator.getService(ISRegistryService.class).updateProfileNode(id, "//STATUS", st.toString());
|
105
|
} catch (Exception e) {
|
106
|
log.error("Error upadating profile " + id, e);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Gets the dao.
|
112
|
*
|
113
|
* @return the dao
|
114
|
*/
|
115
|
public MDStoreDao getDao() {
|
116
|
return dao;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Sets the dao.
|
121
|
*
|
122
|
* @param dao
|
123
|
* the new dao
|
124
|
*/
|
125
|
public void setDao(final MDStoreDao dao) {
|
126
|
this.dao = dao;
|
127
|
}
|
128
|
|
129
|
public GraphProcessRegistry getGraphProcessRegistry() {
|
130
|
return graphProcessRegistry;
|
131
|
}
|
132
|
|
133
|
}
|