diff options
Diffstat (limited to 'examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java')
-rw-r--r-- | examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java index df0f1efc5..72d3ec15b 100644 --- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java +++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java @@ -10,26 +10,31 @@ package com.mycorp.examples.timeservice.internal.provider.rest.consumer; import java.io.NotSerializableException; +import java.util.Arrays; import java.util.Map; +import java.util.UUID; import org.eclipse.ecf.core.ContainerConnectException; +import org.eclipse.ecf.core.ContainerCreateException; +import org.eclipse.ecf.core.ContainerTypeDescription; +import org.eclipse.ecf.core.IContainer; import org.eclipse.ecf.core.identity.ID; -import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.identity.Namespace; import org.eclipse.ecf.core.security.IConnectContext; import org.eclipse.ecf.remoteservice.IRemoteCall; import org.eclipse.ecf.remoteservice.IRemoteServiceRegistration; import org.eclipse.ecf.remoteservice.client.IRemoteCallable; import org.eclipse.ecf.remoteservice.client.IRemoteResponseDeserializer; +import org.eclipse.ecf.remoteservice.provider.RemoteServiceContainerInstantiator; import org.eclipse.ecf.remoteservice.rest.RestCallableFactory; import org.eclipse.ecf.remoteservice.rest.client.HttpGetRequestType; import org.eclipse.ecf.remoteservice.rest.client.RestClientContainer; import org.eclipse.ecf.remoteservice.rest.identity.RestID; +import org.eclipse.ecf.remoteservice.rest.identity.RestNamespace; import org.json.JSONException; import org.json.JSONObject; import com.mycorp.examples.timeservice.ITimeService; -import com.mycorp.examples.timeservice.provider.rest.common.TimeServiceRestNamespace; public class TimeServiceRestClientContainer extends RestClientContainer { @@ -37,29 +42,24 @@ public class TimeServiceRestClientContainer extends RestClientContainer { private IRemoteServiceRegistration reg; - TimeServiceRestClientContainer() { - // Create a random ID for the client container - super((RestID) IDFactory.getDefault().createID( - TimeServiceRestNamespace.NAME, "uuid:" - + java.util.UUID.randomUUID().toString())); + TimeServiceRestClientContainer(RestID id) { + super(id); // This sets up the JSON deserialization of the server's response. // See below for implementation of TimeServiceRestResponseDeserializer setResponseDeserializer(new TimeServiceRestResponseDeserializer()); } @Override - public void connect(ID targetID, IConnectContext connectContext1) - throws ContainerConnectException { + public void connect(ID targetID, IConnectContext connectContext1) throws ContainerConnectException { super.connect(targetID, connectContext1); // Create the IRemoteCallable to represent - // access to the ITimeService method. - IRemoteCallable callable = RestCallableFactory.createCallable( - "getCurrentTime", ITimeService.class.getName(), null, - new HttpGetRequestType(), 30000); + // access to the ITimeService method. + IRemoteCallable callable = RestCallableFactory.createCallable("getCurrentTime", ITimeService.class.getName(), + null, new HttpGetRequestType(), 30000); // Register the callable and associate it with the ITimeService class // name - reg = registerCallables(new String[] { ITimeService.class.getName() }, - new IRemoteCallable[][] { { callable } }, null); + reg = registerCallables(new String[] { ITimeService.class.getName() }, new IRemoteCallable[][] { { callable } }, + null); } @Override @@ -71,19 +71,16 @@ public class TimeServiceRestClientContainer extends RestClientContainer { } } - class TimeServiceRestResponseDeserializer implements - IRemoteResponseDeserializer { - public Object deserializeResponse(String endpoint, IRemoteCall call, - IRemoteCallable callable, - @SuppressWarnings("rawtypes") Map responseHeaders, - byte[] responseBody) throws NotSerializableException { + class TimeServiceRestResponseDeserializer implements IRemoteResponseDeserializer { + public Object deserializeResponse(String endpoint, IRemoteCall call, IRemoteCallable callable, + @SuppressWarnings("rawtypes") Map responseHeaders, byte[] responseBody) + throws NotSerializableException { // We simply need to read the response body (json String), // And return the value of the "time" field try { return new JSONObject(new String(responseBody)).get("time"); } catch (JSONException e1) { - throw new NotSerializableException( - TimeServiceRestResponseDeserializer.class.getName()); + throw new NotSerializableException(TimeServiceRestResponseDeserializer.class.getName()); } } @@ -91,7 +88,28 @@ public class TimeServiceRestClientContainer extends RestClientContainer { @Override public Namespace getConnectNamespace() { - return IDFactory.getDefault().getNamespaceByName( - TimeServiceRestNamespace.NAME); + return RestNamespace.INSTANCE; } + + public static class Instantiator extends RemoteServiceContainerInstantiator { + + private static final String TIMESERVICE_HOST_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.host"; + + @Override + public IContainer createInstance(ContainerTypeDescription description, Map<String, ?> parameters) + throws ContainerCreateException { + // Create new container instance + return new TimeServiceRestClientContainer((RestID) RestNamespace.INSTANCE + .createInstance(new Object[] { "uuid:" + UUID.randomUUID().toString() })); + } + + public String[] getImportedConfigs(ContainerTypeDescription description, String[] exporterSupportedConfigs) { + if (Arrays.asList(exporterSupportedConfigs).contains(TIMESERVICE_HOST_CONFIG_NAME)) + return new String[] { TimeServiceRestClientContainer.TIMESERVICE_CONSUMER_CONFIG_NAME }; + else + return null; + } + + } + } |