Refs #5109: Provide REST interface for Smart Executor
Task-Url: https://support.d4science.org/issues/5109 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor-client@162067 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
74a8de32ce
commit
07c7f77045
8
pom.xml
8
pom.xml
|
@ -46,6 +46,14 @@
|
|||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-jaxws-calls</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.resources.discovery</groupId>
|
||||
<artifactId>ic-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.vremanagement</groupId>
|
||||
<artifactId>smart-executor-api</artifactId>
|
||||
|
|
|
@ -10,12 +10,32 @@ import org.gcube.vremanagement.executor.api.SmartExecutor;
|
|||
*/
|
||||
public class Constants {
|
||||
|
||||
/* Must be the same of generated WEB-INF/gcube-app.xml */
|
||||
public static final String GCUBE_SERVICE_CLASS = "VREManagement";
|
||||
public static final String GCUBE_SERVICE_NAME = "SmartExecutor";
|
||||
/* Used for REST*/
|
||||
public static final String SERVICE_CLASS = "VREManagement";
|
||||
public static final String SERVICE_NAME = "SmartExecutor";
|
||||
public static final String SERVICE_ENTRY_NAME = "org.gcube.vremanagement.executor.ResourceInitializer";
|
||||
|
||||
|
||||
/* Used for SOAP */
|
||||
|
||||
/* Must be the same of generated WEB-INF/gcube-app.xml */
|
||||
/**
|
||||
* Use SERVICE_CLASS instead
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String GCUBE_SERVICE_CLASS = SERVICE_CLASS;
|
||||
/**
|
||||
* Use SERVICE_NAME instead
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String GCUBE_SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
@Deprecated
|
||||
public static final QName SMART_EXECUTOR_QNAME = new QName(SmartExecutor.TARGET_NAMESPACE, SmartExecutor.WEB_SERVICE_SERVICE_NAME);
|
||||
@Deprecated
|
||||
public static final GcubeService<SmartExecutor> smartExecutor = GcubeService.service().withName(SMART_EXECUTOR_QNAME).andInterface(SmartExecutor.class);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package org.gcube.vremanagement.executor.client;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.gcube.vremanagement.executor.api.rest.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.query.Discover;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.ServiceEndpointQueryFilter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class SmartExecutorClientFactory {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SmartExecutorClientFactory.class);
|
||||
|
||||
private static String FORCED_URL = null;
|
||||
|
||||
protected static void forceToURL(String url) {
|
||||
FORCED_URL = url;
|
||||
}
|
||||
|
||||
public static SmartExecutor create(String pluginName, Map<String,String> capabilites,
|
||||
ServiceEndpointQueryFilter serviceEndpointQueryFilter, GCoreEndpointQueryFilter gCoreEndpointQueryFilter) {
|
||||
|
||||
if(FORCED_URL != null) {
|
||||
return new SmartExecutorClientImpl(pluginName, FORCED_URL);
|
||||
}
|
||||
|
||||
Discover discover = new Discover(Constants.SERVICE_ENTRY_NAME);
|
||||
discover.filterByPluginName(pluginName);
|
||||
discover.filterByCapabilities(capabilites);
|
||||
discover.setServiceEndpointQueryFilter(serviceEndpointQueryFilter);
|
||||
discover.setGCoreEndpointQueryFilter(gCoreEndpointQueryFilter);
|
||||
List<String> addresses = discover.getAddresses();
|
||||
|
||||
if(addresses == null || addresses.isEmpty()) {
|
||||
logger.debug("No REST smart-executor found. Looking for old SOAP Version");
|
||||
|
||||
ExecutorPlugin executorPlugin = new ExecutorPlugin();
|
||||
discover = new SmartExecutorPluginQuery(executorPlugin);
|
||||
discover.filterByPluginName(pluginName);
|
||||
discover.filterByCapabilities(capabilites);
|
||||
discover.setServiceEndpointQueryFilter(serviceEndpointQueryFilter);
|
||||
discover.setGCoreEndpointQueryFilter(gCoreEndpointQueryFilter);
|
||||
|
||||
addresses = discover.getAddresses();
|
||||
if(addresses == null || addresses.isEmpty()) {
|
||||
String error = String.format("No %s:%s found in the current context", Constants.SERVICE_CLASS,
|
||||
Constants.SERVICE_NAME);
|
||||
throw new RuntimeException(error);
|
||||
}
|
||||
return new SmartExecutorClientSOAPWrapper((SmartExecutorPluginQuery) discover, executorPlugin);
|
||||
}
|
||||
|
||||
Random random = new Random();
|
||||
int index = random.nextInt(addresses.size());
|
||||
return new SmartExecutorClientImpl(pluginName, addresses.get(index));
|
||||
}
|
||||
|
||||
public static SmartExecutor create(String pluginName) {
|
||||
return create(pluginName, null, null, null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
package org.gcube.vremanagement.executor.client;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.vremanagement.executor.api.rest.RestConstants;
|
||||
import org.gcube.vremanagement.executor.api.rest.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
||||
import org.gcube.vremanagement.executor.client.util.HTTPCall;
|
||||
import org.gcube.vremanagement.executor.client.util.HTTPCall.HTTPMETHOD;
|
||||
import org.gcube.vremanagement.executor.exception.ExecutorException;
|
||||
import org.gcube.vremanagement.executor.exception.InputsNullException;
|
||||
import org.gcube.vremanagement.executor.exception.LaunchException;
|
||||
import org.gcube.vremanagement.executor.exception.PluginInstanceNotFoundException;
|
||||
import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
|
||||
import org.gcube.vremanagement.executor.json.SEMapper;
|
||||
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class SmartExecutorClientImpl implements SmartExecutor {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SmartExecutorClientImpl.class);
|
||||
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
|
||||
protected final String pluginName;
|
||||
protected final String address;
|
||||
protected HTTPCall httpCall;
|
||||
|
||||
public SmartExecutorClientImpl(String pluginName, String address) {
|
||||
this.pluginName = pluginName;
|
||||
this.address = address;
|
||||
|
||||
}
|
||||
|
||||
private HTTPCall getHTTPCall() throws MalformedURLException {
|
||||
if(httpCall == null) {
|
||||
httpCall = new HTTPCall(address, SmartExecutorClientImpl.class.getSimpleName());
|
||||
}
|
||||
return httpCall;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String launch(String launchParameter)
|
||||
throws InputsNullException, PluginNotFoundException, LaunchException, ExecutorException {
|
||||
try {
|
||||
|
||||
logger.info("Going to launch {} ", launchParameter);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(RestConstants.PLUGINS_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(pluginName);
|
||||
|
||||
HTTPCall httpCall = getHTTPCall();
|
||||
String uuid = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.POST, launchParameter);
|
||||
|
||||
logger.debug("{} launched with UUID {} ", pluginName, uuid);
|
||||
return uuid;
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public UUID launch(LaunchParameter launchParameter)
|
||||
throws InputsNullException, PluginNotFoundException, LaunchException, ExecutorException {
|
||||
try {
|
||||
String uuid = launch(SEMapper.marshal(launchParameter));
|
||||
return UUID.fromString(uuid);
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getPluginStateEvolution(String executionIdentifier, Integer iterationNumber) throws PluginInstanceNotFoundException, ExecutorException {
|
||||
try {
|
||||
logger.info("Going to get {} of {} with UUID {} ", PluginStateEvolution.class.getSimpleName(), pluginName, executionIdentifier);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(RestConstants.PLUGINS_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(pluginName);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(executionIdentifier);
|
||||
|
||||
HTTPCall httpCall = getHTTPCall();
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
if(iterationNumber!=null) {
|
||||
parameters.put(RestConstants.ITERATION_NUMBER_PARAM, iterationNumber.intValue());
|
||||
}
|
||||
|
||||
String pluginStateEvolution = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
|
||||
return pluginStateEvolution;
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginStateEvolution getPluginStateEvolution(UUID executionIdentifier, Integer iterationNumber) throws PluginInstanceNotFoundException, ExecutorException {
|
||||
try {
|
||||
String pluginStateEvolution = getPluginStateEvolution(executionIdentifier.toString(), iterationNumber);
|
||||
return SEMapper.unmarshal(PluginStateEvolution.class, pluginStateEvolution);
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String executionIdentifier, boolean globally) throws ExecutorException {
|
||||
try {
|
||||
logger.info("Going to stop plugin with UUID {} ", executionIdentifier);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(RestConstants.PLUGINS_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(pluginName);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(executionIdentifier);
|
||||
|
||||
HTTPCall httpCall = getHTTPCall();
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
parameters.put(RestConstants.GLOBALLY_PARAM, globally);
|
||||
Boolean stopped = httpCall.call(Boolean.class, stringWriter.toString(), HTTPMETHOD.DELETE, parameters);
|
||||
|
||||
logger.debug("Plugin with UUID {} {} stopped", executionIdentifier, stopped ? "successfully" : "was not");
|
||||
return stopped;
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(UUID executionIdentifier, boolean globally) throws ExecutorException {
|
||||
return delete(executionIdentifier.toString(), globally);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package org.gcube.vremanagement.executor.client;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.vremanagement.executor.api.rest.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
||||
import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
|
||||
import org.gcube.vremanagement.executor.exception.ExecutorException;
|
||||
import org.gcube.vremanagement.executor.exception.InputsNullException;
|
||||
import org.gcube.vremanagement.executor.exception.LaunchException;
|
||||
import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
|
||||
import org.gcube.vremanagement.executor.json.SEMapper;
|
||||
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
* Added to launch plugins running on SOAP only old smart-executor
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
class SmartExecutorClientSOAPWrapper implements SmartExecutor {
|
||||
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
|
||||
protected SmartExecutorProxy smartExecutorProxy;
|
||||
|
||||
public SmartExecutorClientSOAPWrapper(SmartExecutorProxy smartExecutorProxy) {
|
||||
this.smartExecutorProxy = smartExecutorProxy;
|
||||
}
|
||||
|
||||
public SmartExecutorClientSOAPWrapper(SmartExecutorPluginQuery query, ExecutorPlugin executorPlugin) {
|
||||
this.smartExecutorProxy = ExecutorPlugin.getExecutorProxy(query, executorPlugin).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String launch(String launchParameter)
|
||||
throws InputsNullException, PluginNotFoundException, LaunchException, ExecutorException {
|
||||
try {
|
||||
LaunchParameter lp = SEMapper.unmarshal(LaunchParameter.class, launchParameter);
|
||||
try {
|
||||
return smartExecutorProxy.launch(lp);
|
||||
}catch (Exception e) {
|
||||
throw (Exception) e.getCause();
|
||||
}
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new ExecutorException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID launch(LaunchParameter launchParameter)
|
||||
throws InputsNullException, PluginNotFoundException, LaunchException, ExecutorException {
|
||||
try {
|
||||
try {
|
||||
String uuid = smartExecutorProxy.launch(launchParameter);
|
||||
return UUID.fromString(uuid);
|
||||
}catch (Exception e) {
|
||||
throw (Exception) e.getCause();
|
||||
}
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPluginStateEvolution(String executionIdentifier, Integer iterationNumber) throws ExecutorException {
|
||||
try {
|
||||
try {
|
||||
PluginStateEvolution pluginStateEvolution = null;
|
||||
if(iterationNumber!=null) {
|
||||
pluginStateEvolution = smartExecutorProxy.getIterationStateEvolution(executionIdentifier, iterationNumber);
|
||||
}else {
|
||||
pluginStateEvolution = smartExecutorProxy.getStateEvolution(executionIdentifier);
|
||||
}
|
||||
return SEMapper.marshal(pluginStateEvolution);
|
||||
}catch (Exception e) {
|
||||
throw (Exception) e.getCause();
|
||||
}
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new ExecutorException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginStateEvolution getPluginStateEvolution(UUID executionIdentifier, Integer iterationNumber) throws ExecutorException {
|
||||
try {
|
||||
try {
|
||||
PluginStateEvolution pluginStateEvolution = null;
|
||||
if(iterationNumber!=null) {
|
||||
pluginStateEvolution = smartExecutorProxy.getIterationStateEvolution(executionIdentifier.toString(), iterationNumber);
|
||||
}else {
|
||||
pluginStateEvolution = smartExecutorProxy.getStateEvolution(executionIdentifier.toString());
|
||||
}
|
||||
return pluginStateEvolution;
|
||||
}catch (Exception e) {
|
||||
throw (Exception) e.getCause();
|
||||
}
|
||||
} catch(ExecutorException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String executionIdentifier, boolean globally) throws ExecutorException {
|
||||
return smartExecutorProxy.unSchedule(executionIdentifier, globally);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(UUID executionIdentifier, boolean globally) throws ExecutorException {
|
||||
return delete(executionIdentifier.toString(), globally);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import org.gcube.vremanagement.executor.client.Constants;
|
|||
* @param <S>
|
||||
* @param <P>
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class AbstractPlugin<S,P> implements Plugin<S,P> {
|
||||
|
||||
public final String name;
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.gcube.common.clients.config.ProxyConfig;
|
|||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||
import org.gcube.vremanagement.executor.api.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.client.Constants;
|
||||
import org.gcube.vremanagement.executor.client.SmartExecutorClientFactory;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter;
|
||||
|
@ -21,54 +22,99 @@ import org.gcube.vremanagement.executor.client.util.Tuple;
|
|||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* Use {@link SmartExecutorClientFactory} instead
|
||||
*/
|
||||
public class ExecutorPlugin extends AbstractPlugin<SmartExecutor, SmartExecutorProxy> {
|
||||
@Deprecated
|
||||
public class ExecutorPlugin extends AbstractPlugin<SmartExecutor,SmartExecutorProxy> {
|
||||
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(
|
||||
String pluginName,
|
||||
Tuple<String, String>[] tuples,
|
||||
ServiceEndpointQueryFilter serviceEndpointQueryFilter,
|
||||
EndpointDiscoveryFilter endpointDiscoveryFilter) {
|
||||
/**
|
||||
* Use {@link SmartExecutorClientFactory#create(String, java.util.Map, org.gcube.vremanagement.executor.client.query.filter.ServiceEndpointQueryFilter, org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter)}
|
||||
* instead
|
||||
* @param pluginName
|
||||
* @param tuples
|
||||
* @param serviceEndpointQueryFilter
|
||||
* @param endpointDiscoveryFilter
|
||||
* @return
|
||||
*/
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(String pluginName, Tuple<String,String>[] tuples,
|
||||
ServiceEndpointQueryFilter serviceEndpointQueryFilter, EndpointDiscoveryFilter endpointDiscoveryFilter) {
|
||||
ExecutorPlugin executorPlugin = new ExecutorPlugin();
|
||||
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
|
||||
query.addConditions(pluginName, tuples);
|
||||
query.setServiceEndpointQueryFilter(serviceEndpointQueryFilter);
|
||||
query.setEndpointDiscoveryFilter(endpointDiscoveryFilter);
|
||||
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query);
|
||||
return new ProxyBuilderImpl<SmartExecutor,SmartExecutorProxy>(executorPlugin, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use {@link SmartExecutorClientFactory#create(String, java.util.Map, org.gcube.vremanagement.executor.client.query.filter.ServiceEndpointQueryFilter, org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter)}
|
||||
* instead
|
||||
* @param pluginName
|
||||
* @param tuples
|
||||
* @return
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(String pluginName, Tuple<String, String> ... tuples) {
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(String pluginName, Tuple<String,String>... tuples) {
|
||||
ExecutorPlugin executorPlugin = new ExecutorPlugin();
|
||||
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
|
||||
query.addConditions(pluginName, tuples);
|
||||
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query);
|
||||
return new ProxyBuilderImpl<SmartExecutor,SmartExecutorProxy>(executorPlugin, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use {@link SmartExecutorClientFactory#create()} instead
|
||||
* @return
|
||||
*/
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy() {
|
||||
ExecutorPlugin executorPlugin = new ExecutorPlugin();
|
||||
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin);
|
||||
return new ProxyBuilderImpl<SmartExecutor,SmartExecutorProxy>(executorPlugin);
|
||||
}
|
||||
|
||||
public ExecutorPlugin(){
|
||||
/**
|
||||
* For internal use only to support backward compatibility
|
||||
* @param query
|
||||
* @param executorPlugin
|
||||
* @return
|
||||
*/
|
||||
@Deprecated
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(SmartExecutorPluginQuery query,
|
||||
ExecutorPlugin executorPlugin) {
|
||||
return new ProxyBuilderImpl<SmartExecutor,SmartExecutorProxy>(executorPlugin, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* For internal use only to support backward compatibility
|
||||
*
|
||||
*/
|
||||
public ExecutorPlugin() {
|
||||
super(SmartExecutor.WEB_SERVICE_SERVICE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* For internal use only to support backward compatibility
|
||||
* Use {@link SmartExecutorClientFactory} instead
|
||||
*/
|
||||
@Override
|
||||
public Exception convert(Exception fault, ProxyConfig<?, ?> proxyConfig) {
|
||||
public Exception convert(Exception fault, ProxyConfig<?,?> proxyConfig) {
|
||||
return fault;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For internal use only to support backward compatibility
|
||||
* Use {@link SmartExecutorClientFactory#create(String)} instead
|
||||
*/
|
||||
@Override
|
||||
public SmartExecutorProxy newProxy(ProxyDelegate<SmartExecutor> proxyDelegate) {
|
||||
return new DefaultSmartExecutorProxy(proxyDelegate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For internal use only to support backward compatibility
|
||||
* Use {@link SmartExecutorClientFactory} instead
|
||||
*/
|
||||
@Override
|
||||
public SmartExecutor resolve(EndpointReference endpoint, ProxyConfig<?, ?> proxyConfig)
|
||||
throws Exception {
|
||||
public SmartExecutor resolve(EndpointReference endpoint, ProxyConfig<?,?> proxyConfig) throws Exception {
|
||||
return StubFactory.stubFor(Constants.smartExecutor).at(endpoint);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -12,118 +12,26 @@ 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.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.RandomEndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter;
|
||||
import org.gcube.vremanagement.executor.client.util.Tuple;
|
||||
import org.gcube.vremanagement.executor.client.query.Discover;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.impl.RandomGCoreEndpointQueryFilter;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* For internal use only to maintain backward compatibility
|
||||
*/
|
||||
public class SmartExecutorPluginQuery implements Query<EndpointReference> {
|
||||
|
||||
public static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
||||
public static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
|
||||
public static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
||||
public static String containsFormat = "contains($entry/string(),'%1s')";
|
||||
|
||||
private final Plugin<?,?> plugin;
|
||||
|
||||
private final SimpleQuery smartExecutorDiscoveryQuery;
|
||||
|
||||
private ServiceEndpointQueryFilter serviceEndpointQueryFilter;
|
||||
private EndpointDiscoveryFilter endpointDiscoveryFilter = new RandomEndpointDiscoveryFilter();;
|
||||
|
||||
private static SimpleQuery makeEndpointDiscoveryQuery(Plugin<?,?> plugin){
|
||||
return 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()");
|
||||
}
|
||||
public class SmartExecutorPluginQuery extends Discover implements Query<EndpointReference> {
|
||||
|
||||
public SmartExecutorPluginQuery(Plugin<?,?> plugin){
|
||||
this.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");
|
||||
|
||||
super(plugin.name());
|
||||
this.endpointDiscoveryFilter = new RandomGCoreEndpointQueryFilter();
|
||||
this.containsFormat = "contains($entry/string(),'%1s')";
|
||||
}
|
||||
|
||||
@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));
|
||||
if(tuples==null){
|
||||
return;
|
||||
}
|
||||
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() eq '%s'", propertyVariableName, tuple.getName()))
|
||||
.addCondition(String.format("%s/Value/text() eq '%s'", propertyVariableName, tuple.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public void setServiceEndpointQueryFilter(ServiceEndpointQueryFilter serviceEndpointQueryFilter){
|
||||
this.serviceEndpointQueryFilter = serviceEndpointQueryFilter;
|
||||
}
|
||||
|
||||
public void setEndpointDiscoveryFilter(EndpointDiscoveryFilter endpointDiscoveryFilter){
|
||||
if(endpointDiscoveryFilter!=null){
|
||||
this.endpointDiscoveryFilter = endpointDiscoveryFilter;
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> discoverEndpoints(EndpointDiscoveryFilter endpointDiscoveryFilter) throws DiscoveryException {
|
||||
if(serviceEndpointQueryFilter!=null){
|
||||
serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery);
|
||||
}
|
||||
|
||||
List<ServiceEndpoint> serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class).submit(smartExecutorDiscoveryQuery);
|
||||
|
||||
if(serviceEndpoints.size() == 0){
|
||||
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
|
||||
}
|
||||
|
||||
SimpleQuery preRunQuery = makeEndpointDiscoveryQuery(plugin);
|
||||
endpointDiscoveryFilter.filter(preRunQuery, serviceEndpoints);
|
||||
|
||||
return ICFactory.client().submit(preRunQuery);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<EndpointReference> fire() throws DiscoveryException {
|
||||
if(serviceEndpointQueryFilter!=null){
|
||||
serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery);
|
||||
}
|
||||
|
||||
List<ServiceEndpoint> serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class).submit(smartExecutorDiscoveryQuery);
|
||||
|
||||
if(serviceEndpoints.size() == 0){
|
||||
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
|
||||
}
|
||||
|
||||
|
||||
SimpleQuery gCoreEndpointDiscoveryQuery = makeEndpointDiscoveryQuery(plugin);
|
||||
endpointDiscoveryFilter.filter(gCoreEndpointDiscoveryQuery, serviceEndpoints);
|
||||
|
||||
List<EndpointReference> refs = new ArrayList<EndpointReference>();
|
||||
try {
|
||||
List<String> addresses = ICFactory.client().submit(gCoreEndpointDiscoveryQuery);
|
||||
List<String> addresses = getAddresses();
|
||||
for(String address : addresses)
|
||||
refs.add(new W3CEndpointReferenceBuilder().address(address).build());
|
||||
} catch(org.gcube.resources.discovery.client.api.DiscoveryException ex) {
|
||||
|
@ -133,5 +41,4 @@ public class SmartExecutorPluginQuery implements Query<EndpointReference> {
|
|||
return refs;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -7,17 +7,20 @@ import java.util.List;
|
|||
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* Use {@link GCoreEndpointQueryFilter} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public interface EndpointDiscoveryFilter {
|
||||
|
||||
/**
|
||||
* @param simpleQuery
|
||||
* @param serviceEndpoints
|
||||
*/
|
||||
@Deprecated
|
||||
void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints);
|
||||
|
||||
}
|
||||
|
|
|
@ -7,14 +7,15 @@ import java.util.List;
|
|||
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
public class ListEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
|
||||
|
||||
protected static final String containsFormat = "contains($entry/string(),'%1s')";
|
||||
|
||||
@Override
|
||||
public void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints) {
|
||||
|
||||
|
@ -22,7 +23,7 @@ public class ListEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
|
|||
int size = serviceEndpoints.size();
|
||||
for(int i=0; i<size; i++){
|
||||
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
|
||||
String condition = String.format(SmartExecutorPluginQuery.containsFormat, hostedOn);
|
||||
String condition = String.format(containsFormat, hostedOn);
|
||||
expression = String.format("%s %s", expression, condition);
|
||||
if(i<(size-1)){
|
||||
expression = String.format("%s or ", expression);
|
||||
|
|
|
@ -10,14 +10,17 @@ import java.util.Random;
|
|||
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.impl.RandomGCoreEndpointQueryFilter;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* Use {@link RandomGCoreEndpointQueryFilter} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public class RandomEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
|
||||
|
||||
protected static final String containsFormat = "contains($entry/string(),'%1s')";
|
||||
|
||||
@Override
|
||||
public void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints) {
|
||||
/*
|
||||
|
@ -36,7 +39,7 @@ public class RandomEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
|
|||
|
||||
/* Getting random hosts using the generated random number*/
|
||||
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
|
||||
simpleQuery.addCondition(format(SmartExecutorPluginQuery.containsFormat, hostedOn));
|
||||
simpleQuery.addCondition(format(containsFormat, hostedOn));
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -7,8 +7,9 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* Use {@link org.gcube.vremanagement.executor.client.query.filter.ServiceEndpointQueryFilter} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public interface ServiceEndpointQueryFilter {
|
||||
|
||||
public void filter(SimpleQuery simpleQuery);
|
||||
|
|
|
@ -9,14 +9,17 @@ import java.util.List;
|
|||
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.impl.SpecificGCoreEndpointQueryFilter;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* Use {@link SpecificGCoreEndpointQueryFilter} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public class SpecificEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
|
||||
|
||||
protected static final String containsFormat = "contains($entry/string(),'%1s')";
|
||||
|
||||
private String endpointURI;
|
||||
|
||||
public SpecificEndpointDiscoveryFilter(String endpointURI){
|
||||
|
@ -25,7 +28,7 @@ public class SpecificEndpointDiscoveryFilter implements EndpointDiscoveryFilter
|
|||
|
||||
@Override
|
||||
public void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints) {
|
||||
simpleQuery.addCondition(format(SmartExecutorPluginQuery.containsFormat, endpointURI));
|
||||
simpleQuery.addCondition(format(containsFormat, endpointURI));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.gcube.common.clients.Call;
|
|||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||
import org.gcube.vremanagement.executor.api.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
||||
import org.gcube.vremanagement.executor.client.SmartExecutorClientFactory;
|
||||
import org.gcube.vremanagement.executor.exception.ExecutorException;
|
||||
import org.gcube.vremanagement.executor.exception.InputsNullException;
|
||||
import org.gcube.vremanagement.executor.exception.LaunchException;
|
||||
|
@ -21,7 +22,10 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
* Use {@link SmartExecutorClientFactory} instead to obtain {@link org.gcube.vremanagement.executor.api.rest.SmartExecutor}
|
||||
* instead
|
||||
*/
|
||||
@Deprecated
|
||||
public class DefaultSmartExecutorProxy implements SmartExecutorProxy {
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,12 +4,15 @@
|
|||
package org.gcube.vremanagement.executor.client.proxies;
|
||||
|
||||
import org.gcube.vremanagement.executor.api.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.client.SmartExecutorClientFactory;
|
||||
import org.gcube.vremanagement.executor.exception.ExecutorException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
* Use {@link SmartExecutorClientFactory} instead to obtain {@link org.gcube.vremanagement.executor.api.rest.SmartExecutor}
|
||||
* instead
|
||||
*/
|
||||
@Deprecated
|
||||
public interface SmartExecutorProxy extends SmartExecutor {
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
package org.gcube.vremanagement.executor.client.query;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.common.clients.exceptions.DiscoveryException;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
import org.gcube.vremanagement.executor.client.Constants;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter;
|
||||
import org.gcube.vremanagement.executor.client.query.filter.ServiceEndpointQueryFilter;
|
||||
import org.gcube.vremanagement.executor.client.util.Tuple;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Discover {
|
||||
|
||||
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'";
|
||||
|
||||
protected String containsFormat;
|
||||
|
||||
protected final String entryName;
|
||||
|
||||
private final SimpleQuery serviceEndpointQuery;
|
||||
|
||||
protected org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter serviceEndpointQueryFilter;
|
||||
protected EndpointDiscoveryFilter endpointDiscoveryFilter;
|
||||
|
||||
protected SimpleQuery getGCoreEndpointQuery() {
|
||||
return ICFactory.queryFor(GCoreEndpoint.class)
|
||||
.addCondition(String.format(classFormat, Constants.SERVICE_CLASS))
|
||||
.addCondition(String.format(nameFormat, Constants.SERVICE_NAME))
|
||||
.addCondition(String.format(statusFormat))
|
||||
.addVariable("$entry", "$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
|
||||
.addCondition(String.format(containsFormat, entryName))
|
||||
.setResult("$entry/text()");
|
||||
}
|
||||
|
||||
public Discover(String entryName) {
|
||||
this.entryName = entryName;
|
||||
this.serviceEndpointQuery = ICFactory.queryFor(ServiceEndpoint.class)
|
||||
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", Constants.SERVICE_CLASS))
|
||||
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", Constants.SERVICE_NAME))
|
||||
.setResult("$resource");
|
||||
this.containsFormat = "$entry/@EntryName eq '%1s'";
|
||||
|
||||
}
|
||||
|
||||
public void filterByPluginName(String pluginName) {
|
||||
serviceEndpointQuery.addVariable("$accessPoint", "$resource/Profile/AccessPoint")
|
||||
.addCondition(String.format("$accessPoint/Interface/Endpoint/@EntryName eq '%s'", pluginName));
|
||||
}
|
||||
|
||||
public void filterByCapabilities(Map<String, String> capabilities) {
|
||||
if(capabilities!=null && !capabilities.isEmpty()) {
|
||||
int i=0;
|
||||
for(String key : capabilities.keySet()) {
|
||||
String propertyVariableName = String.format("$property%d", i);
|
||||
serviceEndpointQuery.addVariable(propertyVariableName, "$accessPoint/Properties/Property")
|
||||
.addCondition(String.format("%s/Name/text() eq '%s'", propertyVariableName, key))
|
||||
.addCondition(String.format("%s/Value/text() eq '%s'", propertyVariableName, capabilities.get(key)));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Deprecated
|
||||
public void addConditions(String pluginName, Tuple<String,String>... tuples) {
|
||||
|
||||
filterByPluginName(pluginName);
|
||||
|
||||
if(tuples == null) {
|
||||
return;
|
||||
}
|
||||
Map<String, String> capabilities = new HashMap<>();
|
||||
for(int i = 0; i < tuples.length; i++) {
|
||||
capabilities.put(tuples[i].getName(), tuples[i].getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void setServiceEndpointQueryFilter(ServiceEndpointQueryFilter serviceEndpointQueryFilter) {
|
||||
this.serviceEndpointQueryFilter = serviceEndpointQueryFilter;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setServiceEndpointQueryFilter(org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter serviceEndpointQueryFilter) {
|
||||
this.serviceEndpointQueryFilter = serviceEndpointQueryFilter;
|
||||
}
|
||||
|
||||
public void setGCoreEndpointQueryFilter(GCoreEndpointQueryFilter gCoreEndpointQueryFilter) {
|
||||
if(gCoreEndpointQueryFilter != null) {
|
||||
this.endpointDiscoveryFilter = gCoreEndpointQueryFilter;
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setEndpointDiscoveryFilter(EndpointDiscoveryFilter endpointDiscoveryFilter) {
|
||||
if(endpointDiscoveryFilter != null) {
|
||||
this.endpointDiscoveryFilter = endpointDiscoveryFilter;
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public List<String> discoverEndpoints(EndpointDiscoveryFilter endpointDiscoveryFilter) throws DiscoveryException {
|
||||
if(serviceEndpointQueryFilter != null) {
|
||||
serviceEndpointQueryFilter.filter(serviceEndpointQuery);
|
||||
}
|
||||
|
||||
List<ServiceEndpoint> serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class)
|
||||
.submit(serviceEndpointQuery);
|
||||
|
||||
if(serviceEndpoints.size() == 0) {
|
||||
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
|
||||
}
|
||||
|
||||
SimpleQuery gCoreEndpointQuery = getGCoreEndpointQuery();
|
||||
endpointDiscoveryFilter.filter(gCoreEndpointQuery, serviceEndpoints);
|
||||
|
||||
return ICFactory.client().submit(gCoreEndpointQuery);
|
||||
|
||||
}
|
||||
|
||||
public List<String> getAddresses() throws DiscoveryException {
|
||||
if(serviceEndpointQueryFilter != null) {
|
||||
serviceEndpointQueryFilter.filter(serviceEndpointQuery);
|
||||
}
|
||||
|
||||
List<ServiceEndpoint> serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class)
|
||||
.submit(serviceEndpointQuery);
|
||||
|
||||
if(serviceEndpoints.size() == 0) {
|
||||
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
|
||||
}
|
||||
|
||||
SimpleQuery gCoreEndpointDiscoveryQuery = getGCoreEndpointQuery();
|
||||
if(endpointDiscoveryFilter!=null) {
|
||||
endpointDiscoveryFilter.filter(gCoreEndpointDiscoveryQuery, serviceEndpoints);
|
||||
}
|
||||
|
||||
List<String> addresses = ICFactory.client().submit(gCoreEndpointDiscoveryQuery);
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package org.gcube.vremanagement.executor.client.query.filter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public interface GCoreEndpointQueryFilter extends EndpointDiscoveryFilter {
|
||||
|
||||
@Override
|
||||
void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints);
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.vremanagement.executor.client.query.filter;
|
||||
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public interface ServiceEndpointQueryFilter
|
||||
extends org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter {
|
||||
|
||||
@Override
|
||||
public void filter(SimpleQuery simpleQuery);
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.gcube.vremanagement.executor.client.query.filter.impl;
|
||||
|
||||
import org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class RandomGCoreEndpointQueryFilter
|
||||
extends org.gcube.vremanagement.executor.client.plugins.query.filter.RandomEndpointDiscoveryFilter
|
||||
implements GCoreEndpointQueryFilter {
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.gcube.vremanagement.executor.client.query.filter.impl;
|
||||
|
||||
import org.gcube.vremanagement.executor.client.query.filter.GCoreEndpointQueryFilter;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class SpecificGCoreEndpointQueryFilter
|
||||
extends org.gcube.vremanagement.executor.client.plugins.query.filter.SpecificEndpointDiscoveryFilter
|
||||
implements GCoreEndpointQueryFilter {
|
||||
|
||||
public SpecificGCoreEndpointQueryFilter(String endpointURI) {
|
||||
super(endpointURI);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,244 @@
|
|||
package org.gcube.vremanagement.executor.client.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.vremanagement.executor.exception.ExecutorException;
|
||||
import org.gcube.vremanagement.executor.exception.SmartExecutorExceptionMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class HTTPCall {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HTTPCall.class);
|
||||
|
||||
public static final String APPLICATION_JSON_CHARSET_UTF_8 = "application/json;charset=UTF-8";
|
||||
|
||||
public enum HTTPMETHOD {
|
||||
HEAD, GET, POST, PUT, DELETE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
public static final String PARAM_STARTER = "?";
|
||||
public static final String PARAM_EQUALS = "=";
|
||||
public static final String PARAM_SEPARATOR = "&";
|
||||
public static final String UTF8 = "UTF-8";
|
||||
|
||||
protected final String address;
|
||||
protected final String userAgent;
|
||||
|
||||
public HTTPCall(String address, String userAgent) {
|
||||
this.address = address;
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
protected String getParametersDataString(Map<String, ? extends Object> parameters)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
if (parameters == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String key : parameters.keySet()) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append(PARAM_SEPARATOR);
|
||||
}
|
||||
result.append(URLEncoder.encode(key, UTF8));
|
||||
result.append(PARAM_EQUALS);
|
||||
result.append(URLEncoder.encode(String.valueOf(parameters.get(key)), UTF8));
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
protected URL getURL(String address, String path, String urlParameters) throws MalformedURLException {
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(address);
|
||||
|
||||
if (address.endsWith(PATH_SEPARATOR)) {
|
||||
if (path.startsWith(PATH_SEPARATOR)) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
} else {
|
||||
if (!path.startsWith(PATH_SEPARATOR)) {
|
||||
stringWriter.append(PARAM_SEPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
stringWriter.append(path);
|
||||
|
||||
if (urlParameters != null) {
|
||||
stringWriter.append(PARAM_STARTER);
|
||||
stringWriter.append(urlParameters);
|
||||
}
|
||||
|
||||
return getURL(stringWriter.toString());
|
||||
}
|
||||
|
||||
protected URL getURL(String urlString) throws MalformedURLException {
|
||||
URL url = new URL(urlString);
|
||||
if (url.getProtocol().compareTo("https") == 0) {
|
||||
url = new URL(url.getProtocol(), url.getHost(), url.getDefaultPort(), url.getFile());
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
protected HttpURLConnection getConnection(String path, String urlParameters, HTTPMETHOD method, String body)
|
||||
throws Exception {
|
||||
URL url = getURL(address, path, urlParameters);
|
||||
return getConnection(url, method, body);
|
||||
}
|
||||
|
||||
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method, String body) throws Exception {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
if (SecurityTokenProvider.instance.get() == null) {
|
||||
if (ScopeProvider.instance.get() == null) {
|
||||
throw new RuntimeException("Null Token and Scope. Please set your token first.");
|
||||
}
|
||||
connection.setRequestProperty("gcube-scope", ScopeProvider.instance.get());
|
||||
} else {
|
||||
connection.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
|
||||
SecurityTokenProvider.instance.get());
|
||||
}
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
connection.setRequestProperty("Content-type", APPLICATION_JSON_CHARSET_UTF_8);
|
||||
connection.setRequestProperty("User-Agent", userAgent);
|
||||
|
||||
connection.setRequestMethod(method.toString());
|
||||
|
||||
if (body != null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
||||
wr.write(body.getBytes("UTF-8"));
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.trace("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
|
||||
URL redirectURL = getURL(connection.getHeaderField("Location"));
|
||||
|
||||
logger.trace("{} is going to be redirect to {}", url.toString(), redirectURL.toString());
|
||||
|
||||
connection = getConnection(redirectURL, method, body);
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method) throws Exception {
|
||||
return call(clz, path, method, null, null);
|
||||
}
|
||||
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method, Map<String, ? extends Object> parameters)
|
||||
throws Exception {
|
||||
return call(clz, path, method, parameters, null);
|
||||
}
|
||||
|
||||
public <C> C call(Class<C> clz, String path, HTTPMETHOD method, String body) throws Exception {
|
||||
return call(clz, path, method, null, body);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <C> C call(Class<C> clz, String path, HTTPMETHOD method, Map<String, ? extends Object> parameters,
|
||||
String body) throws Exception {
|
||||
|
||||
String urlParameters = getParametersDataString(parameters);
|
||||
|
||||
HttpURLConnection connection = getConnection(path, urlParameters, method, body);
|
||||
|
||||
try {
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.info("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (method == HTTPMETHOD.HEAD) {
|
||||
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
|
||||
return null;
|
||||
}
|
||||
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND || responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
|
||||
throw new ExecutorException("Not Found");
|
||||
}
|
||||
if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
|
||||
throw new ExecutorException("Forbidden");
|
||||
}
|
||||
}
|
||||
|
||||
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
|
||||
|
||||
InputStream inputStream = connection.getErrorStream();
|
||||
StringBuilder result = getStringBuilder(inputStream);
|
||||
|
||||
String res = result.toString();
|
||||
|
||||
ExecutorException rre = null;
|
||||
try {
|
||||
rre = SmartExecutorExceptionMapper.unmarshal(ExecutorException.class, res);
|
||||
} catch (Exception e) {
|
||||
rre = new ExecutorException(responseMessage);
|
||||
}
|
||||
|
||||
throw rre;
|
||||
|
||||
}
|
||||
|
||||
StringBuilder result = getStringBuilder(connection.getInputStream());
|
||||
|
||||
String res = result.toString();
|
||||
logger.trace("Server returned content : {}", res);
|
||||
|
||||
if (Boolean.class.isAssignableFrom(clz)) {
|
||||
return (C) ((Boolean) Boolean.valueOf(res));
|
||||
}
|
||||
|
||||
return (C) res;
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -5,8 +5,8 @@ package org.gcube.vremanagement.executor.client.util;
|
|||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
public class Tuple<Name,Value> {
|
||||
|
||||
protected Name name;
|
||||
|
|
Loading…
Reference in New Issue