Project

General

Profile

1
package eu.dnetlib.data.hadoop;
2

    
3
import java.util.List;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9

    
10
import com.google.common.collect.Iterables;
11

    
12
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
13
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
14
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
15
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
16
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
17
import eu.dnetlib.miscutils.datetime.DateUtils;
18

    
19
public class ISClient {
20

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

    
23
	@Resource
24
	private UniqueServiceLocator serviceLocator;
25

    
26
	public String getJobProfile(final String jobName) throws ISLookUpException {
27
		return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
28
				"/RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'HadoopJobConfigurationDSResourceType' and .//HADOOP_JOB/@name='" + jobName + "']");
29
	}
30

    
31
	public String queryForServiceProperty(final String key) throws ISLookUpException {
32
		return getServiceConfigValue(
33
				String.format(
34
						"distinct-values("
35
								+ "for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='HadoopServiceResourceType'] "
36
								+ "return $x//SERVICE_PROPERTIES/PROPERTY[./@ key='%s']/@value/string())",
37
						key));
38
	}
39

    
40
	public void updateCountElement(final String jobName, final String element, final String delta) {
41
		final String xquery =
42
				"let $x := //RESOURCE_PROFILE[" + ".//RESOURCE_TYPE/@value='HadoopJobConfigurationDSResourceType' and .//HADOOP_JOB/@name='" + jobName
43
						+ "'], $tot := $x//STATUS/" + element + "/@value/number() " + delta + " return update replace $x//STATUS/" + element + " with <"
44
						+ element + " value='{$tot}' />";
45

    
46
		executeXUpdate(xquery);
47
	}
48

    
49
	public void updateDate(final String jobName) {
50
		log.info("increment last submission date for job: " + jobName);
51
		executeXUpdate("for $x in collection('')/RESOURCE_PROFILE["
52
				+ ".//RESOURCE_TYPE/@value='HadoopJobConfigurationDSResourceType' and .//HADOOP_JOB/@name='"
53
				+ jobName + "'] " + " return update value $x//LAST_SUBMISSION_DATE/@value with '" + DateUtils.now_ISO8601() + "' ");
54
	}
55

    
56
	private String getServiceConfigValue(final String xquery) throws ISLookUpException {
57
		log.debug("quering for service property: " + xquery);
58
		final List<String> urls = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
59
		if ((urls == null) || (urls.size() != 1))
60
			throw new IllegalStateException("unable to find unique service property, xquery: " + xquery);
61
		return Iterables.getOnlyElement(urls);
62
	}
63

    
64
	private boolean executeXUpdate(final String xupdate) {
65
		try {
66
			log.debug("running xupdate: " + xupdate);
67
			return serviceLocator.getService(ISRegistryService.class).executeXUpdate(xupdate);
68
		} catch (final ISRegistryException e) {
69
			log.error("unable to run xupdate: " + xupdate, e);
70
			return false;
71
		}
72
	}
73
}
(6-6/7)