Improving Client

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor-client@111993 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-02-12 17:57:57 +00:00
parent 4bb0c5417a
commit caa7994344
3 changed files with 198 additions and 1 deletions

View File

@ -14,6 +14,8 @@ import org.gcube.vremanagement.executor.api.SmartExecutor;
import org.gcube.vremanagement.executor.client.Constants;
import org.gcube.vremanagement.executor.client.proxies.DefaultSmartExecutorProxy;
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
import org.gcube.vremanagement.executor.client.util.SmartExecutorPluginQuery;
import org.gcube.vremanagement.executor.client.util.Tuple;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -23,10 +25,17 @@ public class ExecutorPlugin extends AbstractPlugin<SmartExecutor, SmartExecutorP
private static final ExecutorPlugin executorPlugin = new ExecutorPlugin();
@SafeVarargs
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(String pluginName, Tuple<String, String> ... tuples) {
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
query.addConditions(pluginName, tuples);
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query);
}
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy() {
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin);
}
public ExecutorPlugin(){
super(SmartExecutor.WEB_SERVICE_SERVICE_NAME);
}

View File

@ -0,0 +1,98 @@
/**
*
*/
package org.gcube.vremanagement.executor.client.util;
import static java.lang.String.format;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.xml.ws.EndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
import org.gcube.common.clients.Plugin;
import org.gcube.common.clients.exceptions.DiscoveryException;
import org.gcube.common.clients.queries.Query;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class SmartExecutorPluginQuery implements Query<EndpointReference> {
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
private static String containsFormat = "contains($entry/string(),'%1s')";
private static DiscoveryClient<ServiceEndpoint> smartExecutorDiscoveryClient = ICFactory.clientFor(ServiceEndpoint.class);
private static DiscoveryClient<String> gCoreEndpointDiscoveryClient = ICFactory.client();
private final SimpleQuery smartExecutorDiscoveryQuery;
private final SimpleQuery gCoreEndpointDiscoveryQuery;
public SmartExecutorPluginQuery(Plugin<?,?> plugin){
smartExecutorDiscoveryQuery = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", plugin.serviceClass()))
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", plugin.serviceName()))
.setResult("$resource");
gCoreEndpointDiscoveryQuery = ICFactory.queryFor(GCoreEndpoint.class)
.addCondition(String.format(classFormat, plugin.serviceClass()))
.addCondition(String.format(nameFormat, plugin.serviceName()))
.addCondition(String.format(statusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
.addCondition(String.format(containsFormat,plugin.name()))
.setResult("$entry/text()");
}
@SuppressWarnings("unchecked")
public void addConditions(String pluginName, Tuple<String, String> ... tuples){
smartExecutorDiscoveryQuery.addVariable("$accessPoint", "$resource/Profile/AccessPoint")
.addCondition(String.format("$accessPoint/Interface/Endpoint/@EntryName eq '%s'", pluginName));
for(int i=0; i<tuples.length; i++){
Tuple<String, String> tuple = tuples[i];
String propertyVariableName = String.format("$property%d", i);
smartExecutorDiscoveryQuery
.addVariable(propertyVariableName, "$accessPoint/Properties/Property")
.addCondition(String.format("%s/Name/text()", propertyVariableName, tuple.getName()))
.addCondition(String.format("%s/Value/text()", propertyVariableName, tuple.getValue()));
}
}
@Override
public List<EndpointReference> fire() throws DiscoveryException {
List<ServiceEndpoint> serviceEndpoints = smartExecutorDiscoveryClient.submit(smartExecutorDiscoveryQuery);
/* Getting randomly one the host */
Random random = new Random();
int i = random.nextInt() % serviceEndpoints.size();
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
gCoreEndpointDiscoveryQuery.addCondition(format(containsFormat, hostedOn));
List<EndpointReference> refs = new ArrayList<EndpointReference>();
try {
List<String> addresses = gCoreEndpointDiscoveryClient.submit(gCoreEndpointDiscoveryQuery);
for(String address : addresses)
refs.add(new W3CEndpointReferenceBuilder().address(address).build());
}
catch(org.gcube.resources.discovery.client.api.DiscoveryException ex) {
throw new DiscoveryException(ex);
}
return refs;
}
}

View File

@ -0,0 +1,90 @@
/**
*
*/
package org.gcube.vremanagement.executor.client.util;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class Tuple<Name,Value> {
protected Name name;
protected Value value;
public Tuple(){}
public Tuple(Name name, Value value){
this.name = name;
this.value = value;
}
/**
* @return the a
*/
public Name getName() {
return name;
}
/**
* @param a the a to set
*/
public void setName(Name name) {
this.name = name;
}
/**
* @return the b
*/
public Value getValue() {
return value;
}
/**
* @param b the b to set
*/
public void setValue(Value value) {
this.value = value;
}
/** {@inheritDoc} */
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
/** {@inheritDoc} */
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
@SuppressWarnings("rawtypes")
Tuple other = (Tuple) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
/** {@inheritDoc} */
@Override
public String toString() {
return String.format("<%s,%s>", name.toString(), value.toString());
}
}