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("for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='HadoopServiceResourceType'] return $x//SERVICE_PROPERTIES/PROPERTY[./@ key='"
33
				+ key + "']/@value/string()");
34
	}
35

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

    
42
		executeXUpdate(xquery);
43
	}
44

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

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

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