1
|
package eu.dnetlib.data.actionmanager.is;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import java.util.NoSuchElementException;
|
6
|
import java.util.Set;
|
7
|
import javax.annotation.Resource;
|
8
|
import javax.xml.ws.Endpoint;
|
9
|
|
10
|
import com.google.common.collect.Iterables;
|
11
|
import com.google.common.collect.Lists;
|
12
|
import com.google.common.collect.Sets;
|
13
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
14
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
15
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.ActionManagerException;
|
16
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.ActionManagerSet;
|
17
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.ActionManagerSet.ImpactTypes;
|
18
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.RawSet;
|
19
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
20
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
21
|
import eu.dnetlib.rmi.enabling.ISRegistryException;
|
22
|
import eu.dnetlib.rmi.enabling.ISRegistryService;
|
23
|
import org.antlr.stringtemplate.StringTemplate;
|
24
|
import org.apache.commons.logging.Log;
|
25
|
import org.apache.commons.logging.LogFactory;
|
26
|
import org.dom4j.Attribute;
|
27
|
import org.dom4j.Document;
|
28
|
import org.dom4j.Element;
|
29
|
import org.dom4j.io.SAXReader;
|
30
|
import org.springframework.beans.factory.annotation.Required;
|
31
|
|
32
|
public class ISClient {
|
33
|
|
34
|
private static final Log log = LogFactory.getLog(ISClient.class); // NOPMD by marko on 11/24/08 5:02 PM
|
35
|
/**
|
36
|
* endpoint builder.
|
37
|
*/
|
38
|
private static final String ENDPOINT_TEMPLATE = "http://%s:%s/%s/services/actionManager";
|
39
|
@Resource
|
40
|
private UniqueServiceLocator serviceLocator;
|
41
|
/**
|
42
|
* Template for ActionManagerSet profiles
|
43
|
*/
|
44
|
private StringTemplate actionManagerSetDsTemplate;
|
45
|
/**
|
46
|
* service endpoint.
|
47
|
*/
|
48
|
private Endpoint endpoint;
|
49
|
private String hostname;
|
50
|
private String port;
|
51
|
private String context;
|
52
|
|
53
|
public String registerSetProfile(final ActionManagerSet set) throws ActionManagerException {
|
54
|
if (existsSet(set.getId())) { throw new ActionManagerException("Set " + set.getId() + " already registered"); }
|
55
|
try {
|
56
|
StringTemplate template = new StringTemplate(actionManagerSetDsTemplate.getTemplate());
|
57
|
template.setAttribute("serviceUri", String.format(ENDPOINT_TEMPLATE, hostname, port, context));
|
58
|
template.setAttribute("set", set);
|
59
|
template.setAttribute("latest", RawSet.newInstance());
|
60
|
return serviceLocator.getService(ISRegistryService.class).registerProfile(template.toString());
|
61
|
} catch (ISRegistryException e) {
|
62
|
throw new ActionManagerException("Error registering set " + set, e);
|
63
|
}
|
64
|
}
|
65
|
|
66
|
public List<ActionManagerSet> listSets() throws ActionManagerException {
|
67
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') return $x";
|
68
|
return getActionManagerSets(q);
|
69
|
}
|
70
|
|
71
|
public List<ActionManagerSet> listValidSets() throws ActionManagerException {
|
72
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') \n"
|
73
|
+ "where string-length($x//RAW_SETS/LATEST/@id/string()) > 0\n"
|
74
|
+ "return $x";
|
75
|
|
76
|
return getActionManagerSets(q);
|
77
|
}
|
78
|
|
79
|
private List<ActionManagerSet> getActionManagerSets(final String q) throws ActionManagerException {
|
80
|
final List<ActionManagerSet> list = Lists.newArrayList();
|
81
|
|
82
|
try {
|
83
|
final String basePath = getBasePathHDFS();
|
84
|
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(q)) {
|
85
|
list.add(getActionManagerSet(basePath, s));
|
86
|
}
|
87
|
} catch (ISLookUpException e) {
|
88
|
log.error("Error accessing Sets, using query: " + q);
|
89
|
}
|
90
|
return list;
|
91
|
}
|
92
|
|
93
|
public ActionManagerSet getSet(final String setId) throws ActionManagerException, ISLookUpException {
|
94
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') "
|
95
|
+ "where $x//SET/@id = '" + setId + "' return $x";
|
96
|
|
97
|
try {
|
98
|
final String basePath = getBasePathHDFS();
|
99
|
final String setProfile = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(q);
|
100
|
return getActionManagerSet(basePath, setProfile);
|
101
|
} catch (ISLookUpException e) {
|
102
|
throw new ActionManagerException("Error accessing Sets, using query: " + q);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
private ActionManagerSet getActionManagerSet(final String basePath, final String profile) throws ActionManagerException {
|
107
|
final SAXReader reader = new SAXReader();
|
108
|
final ActionManagerSet set = new ActionManagerSet();
|
109
|
|
110
|
try {
|
111
|
final Document doc = reader.read(new StringReader(profile));
|
112
|
|
113
|
set.setId(doc.valueOf("//SET/@id").trim());
|
114
|
set.setName(doc.valueOf("//SET").trim());
|
115
|
set.setImpact(ImpactTypes.valueOf(doc.valueOf("//IMPACT").trim()));
|
116
|
set.setLatest(doc.valueOf("//RAW_SETS/LATEST/@id"), doc.valueOf("//RAW_SETS/LATEST/@creationDate"), doc.valueOf("//RAW_SETS/LATEST/@lastUpdate"));
|
117
|
set.setDirectory(doc.valueOf("//SET/@directory"));
|
118
|
final List expiredNodes = doc.selectNodes("//RAW_SETS/EXPIRED");
|
119
|
if (expiredNodes != null) {
|
120
|
for (int i = 0; i < expiredNodes.size(); i++) {
|
121
|
Element ex = (Element) expiredNodes.get(i);
|
122
|
set.addExpired(ex.attributeValue("id"), ex.attributeValue("creationDate"), ex.attributeValue("lastUpdate"));
|
123
|
}
|
124
|
}
|
125
|
|
126
|
final StringBuilder sb = new StringBuilder();
|
127
|
sb.append(basePath);
|
128
|
sb.append("/");
|
129
|
sb.append(doc.valueOf("//SET/@directory"));
|
130
|
sb.append("/");
|
131
|
sb.append(doc.valueOf("//RAW_SETS/LATEST/@id"));
|
132
|
set.setPathToLatest(sb.toString());
|
133
|
|
134
|
return set;
|
135
|
} catch (Exception e) {
|
136
|
throw new ActionManagerException("Error creating set from profile: " + profile, e);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
public boolean deleteSetProfile(final String setId) throws ActionManagerException {
|
141
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') where $x//SET/@id = '" + setId
|
142
|
+ "' return $x//RESOURCE_IDENTIFIER/@value/string()";
|
143
|
|
144
|
try {
|
145
|
final String profId = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(q);
|
146
|
return serviceLocator.getService(ISRegistryService.class).deleteProfile(profId);
|
147
|
} catch (Exception e) {
|
148
|
log.error("Error deleting set " + setId, e);
|
149
|
throw new ActionManagerException("Error deleting set " + setId, e);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
public boolean existsSet(final String id) throws ActionManagerException {
|
154
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') where $x//SET/@id = '" + id
|
155
|
+ "' return $x";
|
156
|
try {
|
157
|
return !serviceLocator.getService(ISLookUpService.class).quickSearchProfile(q).isEmpty();
|
158
|
} catch (ISLookUpException e) {
|
159
|
log.error("Error accessing Sets, using query: " + q, e);
|
160
|
throw new ActionManagerException("Error running xquery: " + q, e);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
public boolean existsRawSet(final String id) throws ActionManagerException {
|
165
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') where $x//RAW_SETS/*/@id = '" + id
|
166
|
+ "' return $x";
|
167
|
try {
|
168
|
return !serviceLocator.getService(ISLookUpService.class).quickSearchProfile(q).isEmpty();
|
169
|
} catch (ISLookUpException e) {
|
170
|
log.error("Error accessing RawSets, using query: " + q, e);
|
171
|
throw new ActionManagerException("Error running xquery: " + q, e);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
public Set<String> listValidRawSets() throws ActionManagerException {
|
176
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') return $x//RAW_SETS/LATEST/@id/string()";
|
177
|
try {
|
178
|
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(q);
|
179
|
return Sets.newHashSet(list);
|
180
|
} catch (Exception e) {
|
181
|
log.error("Error running xquery: " + q, e);
|
182
|
throw new ActionManagerException("Error running xquery: " + q, e);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
public RawSet geLatestRawSet(final String set) throws ActionManagerException {
|
187
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') where $x//SET/@id = '" + set
|
188
|
+ "' return concat(' ' , $x//RAW_SETS/LATEST/@id, ' @@@ ', $x//RAW_SETS/LATEST/@creationDate, ' @@@ ', $x//RAW_SETS/LATEST/@lastUpdate, ' ')";
|
189
|
try {
|
190
|
String[] arr = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(q).split("@@@");
|
191
|
return RawSet.newInstance(arr[0].trim(), arr[1].trim(), arr[2].trim());
|
192
|
} catch (ISLookUpException e) {
|
193
|
log.error("Error accessing Sets, using query: " + q, e);
|
194
|
throw new ActionManagerException("Error running xquery: " + q, e);
|
195
|
}
|
196
|
}
|
197
|
|
198
|
public String getGarbageTimeMargin() throws ActionManagerException {
|
199
|
return queryServiceProperty("garbageTimeMargin");
|
200
|
}
|
201
|
|
202
|
public String getBasePathHDFS() throws ActionManagerException {
|
203
|
return queryServiceProperty("basePath");
|
204
|
}
|
205
|
|
206
|
public String getGarbageRetainThreshold() throws ActionManagerException {
|
207
|
return queryServiceProperty("garbageRetainThreshold");
|
208
|
}
|
209
|
|
210
|
private String queryServiceProperty(final String propertyName) throws ActionManagerException {
|
211
|
final String q = "for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='ActionManagerServiceResourceType'] return $x//SERVICE_PROPERTIES/PROPERTY[./@ key='"
|
212
|
+ propertyName + "']/@value/string()";
|
213
|
log.debug("quering for service property: " + q);
|
214
|
try {
|
215
|
final List<String> value = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(q);
|
216
|
return Iterables.getOnlyElement(value);
|
217
|
} catch (ISLookUpException e) {
|
218
|
String msg = "Error accessing service profile, using query: " + q;
|
219
|
log.error(msg, e);
|
220
|
throw new ActionManagerException(msg, e);
|
221
|
} catch (NoSuchElementException e) {
|
222
|
String msg = "missing service property: " + propertyName;
|
223
|
log.error(msg, e);
|
224
|
throw new ActionManagerException(msg, e);
|
225
|
} catch (IllegalArgumentException e) {
|
226
|
String msg = "found more than one service property: " + propertyName;
|
227
|
log.error(msg, e);
|
228
|
throw new ActionManagerException(msg, e);
|
229
|
}
|
230
|
}
|
231
|
|
232
|
public void addLatestRawSet(final String set, final RawSet rawSet) throws ActionManagerException {
|
233
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType') where $x//SET/@id = '" + set
|
234
|
+ "' return $x";
|
235
|
try {
|
236
|
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(q);
|
237
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
238
|
final String profId = doc.valueOf("//RESOURCE_IDENTIFIER/@value");
|
239
|
final Element latest = (Element) doc.selectSingleNode("//RAW_SETS/LATEST");
|
240
|
final Element expired = ((Element) doc.selectSingleNode("//RAW_SETS")).addElement("EXPIRED");
|
241
|
|
242
|
for (Object o : latest.attributes()) {
|
243
|
final Attribute a = (Attribute) o;
|
244
|
expired.addAttribute(a.getName(), a.getValue());
|
245
|
}
|
246
|
|
247
|
latest.addAttribute("id", rawSet.getId());
|
248
|
latest.addAttribute("creationDate", rawSet.getCreationDate());
|
249
|
latest.addAttribute("lastUpdate", rawSet.getLastUpdate());
|
250
|
|
251
|
serviceLocator.getService(ISRegistryService.class).updateProfile(profId, doc.asXML(), "ActionManagerSetDSResourceType");
|
252
|
} catch (Exception e) {
|
253
|
log.error("Error updating profile of set: " + set);
|
254
|
throw new ActionManagerException("Error running xquery: " + q, e);
|
255
|
}
|
256
|
}
|
257
|
|
258
|
public void updateLastUpdate(final String rawSet) throws ActionManagerException {
|
259
|
final String q = "for $x in collection('/db/DRIVER/ActionManagerSetDSResources/ActionManagerSetDSResourceType')//RAW_SETS/*[@id='" + rawSet
|
260
|
+ "']/@lastUpdate return update replace $x with '" + DateUtils.now_ISO8601() + "'";
|
261
|
|
262
|
try {
|
263
|
serviceLocator.getService(ISRegistryService.class).executeXUpdate(q);
|
264
|
} catch (Exception e) {
|
265
|
log.error("Error updating lastUpdate using query: " + q, e);
|
266
|
throw new ActionManagerException("Error updating lastUpdate using query: " + q, e);
|
267
|
}
|
268
|
}
|
269
|
|
270
|
public StringTemplate getActionManagerSetDsTemplate() {
|
271
|
return actionManagerSetDsTemplate;
|
272
|
}
|
273
|
|
274
|
@Required
|
275
|
public void setActionManagerSetDsTemplate(final StringTemplate actionManagerSetDsTemplate) {
|
276
|
this.actionManagerSetDsTemplate = actionManagerSetDsTemplate;
|
277
|
}
|
278
|
|
279
|
public Endpoint getEndpoint() {
|
280
|
return endpoint;
|
281
|
}
|
282
|
|
283
|
@Required
|
284
|
public void setEndpoint(final Endpoint endpoint) {
|
285
|
this.endpoint = endpoint;
|
286
|
}
|
287
|
|
288
|
public String getHostname() {
|
289
|
return hostname;
|
290
|
}
|
291
|
|
292
|
@Required
|
293
|
public void setHostname(final String hostname) {
|
294
|
this.hostname = hostname;
|
295
|
}
|
296
|
|
297
|
public String getPort() {
|
298
|
return port;
|
299
|
}
|
300
|
|
301
|
@Required
|
302
|
public void setPort(final String port) {
|
303
|
this.port = port;
|
304
|
}
|
305
|
|
306
|
public String getContext() {
|
307
|
return context;
|
308
|
}
|
309
|
|
310
|
@Required
|
311
|
public void setContext(final String context) {
|
312
|
this.context = context;
|
313
|
}
|
314
|
}
|