diff options
author | slewis | 2015-09-08 02:19:55 +0000 |
---|---|---|
committer | slewis | 2015-09-08 02:19:55 +0000 |
commit | 4348cd04c2f1db24c973cb4ad4f6bdd549622163 (patch) | |
tree | 139bb5c9dc8a6d6dfdaaa21447f7b6f8b9a4482e /examples | |
parent | 60093fcef0aafffed29bfeb31ed2b78e23d1aaee (diff) | |
download | org.eclipse.ecf-4348cd04c2f1db24c973cb4ad4f6bdd549622163.tar.gz org.eclipse.ecf-4348cd04c2f1db24c973cb4ad4f6bdd549622163.tar.xz org.eclipse.ecf-4348cd04c2f1db24c973cb4ad4f6bdd549622163.zip |
Simplification and formatting changes.
Change-Id: Ib6d584dd86f9efef9c26313ca10f68765cec8306
Diffstat (limited to 'examples')
2 files changed, 33 insertions, 43 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 72d3ec15b..f43ea0a38 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 @@ -39,6 +39,7 @@ import com.mycorp.examples.timeservice.ITimeService; public class TimeServiceRestClientContainer extends RestClientContainer { public static final String TIMESERVICE_CONSUMER_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.consumer"; + private static final String TIMESERVICE_HOST_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.host"; private IRemoteServiceRegistration reg; @@ -46,7 +47,17 @@ public class TimeServiceRestClientContainer extends RestClientContainer { super(id); // This sets up the JSON deserialization of the server's response. // See below for implementation of TimeServiceRestResponseDeserializer - setResponseDeserializer(new TimeServiceRestResponseDeserializer()); + setResponseDeserializer(new IRemoteResponseDeserializer() { + public Object deserializeResponse(String endpoint, IRemoteCall call, IRemoteCallable callable, + @SuppressWarnings("rawtypes") Map responseHeaders, byte[] responseBody) throws NotSerializableException { + try { + return new JSONObject(new String(responseBody)).get("time"); + } catch (JSONException e1) { + NotSerializableException t = new NotSerializableException("Exception serializing response from endpoing="+endpoint); + t.setStackTrace(e1.getStackTrace()); + throw t; + } + }}); } @Override @@ -71,21 +82,6 @@ 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 { - // 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()); - } - } - - } - @Override public Namespace getConnectNamespace() { return RestNamespace.INSTANCE; @@ -93,23 +89,18 @@ public class TimeServiceRestClientContainer extends RestClientContainer { 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 + // Create new container instance with random uuid 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; } - } - } diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java index caf9e5e8d..67437fb0a 100644 --- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java +++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java @@ -28,6 +28,7 @@ import org.eclipse.ecf.remoteservice.servlet.RemoteServiceHttpServlet; import org.eclipse.ecf.remoteservice.servlet.ServletServerContainer; import org.json.JSONException; import org.json.JSONObject; +import org.osgi.service.http.NamespaceException; import com.mycorp.examples.timeservice.ITimeService; @@ -36,30 +37,13 @@ public class TimeServiceServerContainer extends ServletServerContainer { public static final String TIMESERVICE_HOST_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.host"; public static final String TIMESERVICE_SERVLET_NAME = "/" + ITimeService.class.getName(); - public static class Instantiator extends RemoteServiceContainerInstantiator { - @Override - public IContainer createInstance(ContainerTypeDescription description, Map<String, ?> parameters) - throws ContainerCreateException { - return new TimeServiceServerContainer( - RestNamespace.INSTANCE.createInstance(new Object[] { (String) parameters.get("id") })); - } - - public String[] getSupportedConfigs(ContainerTypeDescription description) { - return new String[] { TIMESERVICE_HOST_CONFIG_NAME }; - } - } - - TimeServiceServerContainer(ID id) throws ContainerCreateException { + TimeServiceServerContainer(ID id) throws ServletException, NamespaceException { super(id); // Register our servlet with the given httpService with the // TIMESERVICE_SERVLET_NAME // which is "/com.mycorp.examples.timeservice.ITimeService" - try { - TimeServiceHttpServiceComponent.getDefault().registerServlet(TIMESERVICE_SERVLET_NAME, + TimeServiceHttpServiceComponent.getDefault().registerServlet(TIMESERVICE_SERVLET_NAME, new TimeRemoteServiceHttpServlet(), null, null); - } catch (Exception e) { - throw new ContainerCreateException("Could not create Time Service Server Container", e); - } } @Override @@ -76,8 +60,7 @@ public class TimeServiceServerContainer extends ServletServerContainer { class TimeRemoteServiceHttpServlet extends RemoteServiceHttpServlet { private static final long serialVersionUID = 3906126401901826462L; - - // Handle get call right here. + // Handle remote time service get call here. @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get local OSGi ITimeService @@ -92,5 +75,21 @@ public class TimeServiceServerContainer extends ServletServerContainer { } } } + + public static class Instantiator extends RemoteServiceContainerInstantiator { + @Override + public IContainer createInstance(ContainerTypeDescription description, Map<String, ?> parameters) + throws ContainerCreateException { + try { + return new TimeServiceServerContainer( + RestNamespace.INSTANCE.createInstance(new Object[] { (String) parameters.get("id") })); + } catch (Exception e) { + throw new ContainerCreateException("Could not create time service server", e); + } + } + public String[] getSupportedConfigs(ContainerTypeDescription description) { + return new String[] { TIMESERVICE_HOST_CONFIG_NAME }; + } + } } |