You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@edgent.apache.org by dj...@apache.org on 2016/03/17 18:13:24 UTC

[09/10] incubator-quarks git commit: Make IotProvider concrete and use a function to supply the IotDevice

Make IotProvider concrete and use a function to supply the IotDevice


Project: http://git-wip-us.apache.org/repos/asf/incubator-quarks/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-quarks/commit/8563b51c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quarks/tree/8563b51c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quarks/diff/8563b51c

Branch: refs/heads/master
Commit: 8563b51c0d24ed13fcb622b777697103d74cee46
Parents: b97b0a4
Author: Dan Debrunner <dj...@debrunners.com>
Authored: Thu Mar 17 08:57:29 2016 -0700
Committer: Dan Debrunner <dj...@debrunners.com>
Committed: Thu Mar 17 08:57:29 2016 -0700

----------------------------------------------------------------------
 .../java/quarks/providers/iot/IotProvider.java  | 80 +++++++++++++++++---
 .../quarks/test/fvt/iot/IotProviderTest.java    |  8 +-
 2 files changed, 72 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quarks/blob/8563b51c/providers/iot/src/main/java/quarks/providers/iot/IotProvider.java
----------------------------------------------------------------------
diff --git a/providers/iot/src/main/java/quarks/providers/iot/IotProvider.java b/providers/iot/src/main/java/quarks/providers/iot/IotProvider.java
index 3440f90..d2a05f1 100644
--- a/providers/iot/src/main/java/quarks/providers/iot/IotProvider.java
+++ b/providers/iot/src/main/java/quarks/providers/iot/IotProvider.java
@@ -35,6 +35,7 @@ import quarks.execution.DirectSubmitter;
 import quarks.execution.Job;
 import quarks.execution.services.ControlService;
 import quarks.execution.services.ServiceContainer;
+import quarks.function.Function;
 import quarks.providers.direct.DirectProvider;
 import quarks.runtime.appservice.AppService;
 import quarks.runtime.jsoncontrol.JsonControlService;
@@ -44,7 +45,7 @@ import quarks.topology.TopologyProvider;
 import quarks.topology.services.ApplicationService;
 
 /**
- * Abstract IoT provider supporting multiple topologies with a single connection to a
+ * IoT provider supporting multiple topologies with a single connection to a
  * message hub. A provider that uses a single {@link IotDevice} to communicate
  * with an IoT scale message hub.
  * {@link quarks.connectors.pubsub.PublishSubscribe Publish-subscribe} is
@@ -72,31 +73,60 @@ import quarks.topology.services.ApplicationService;
  * identifier {@link Commands#CONTROL_SERVICE quarksControl}.
  * </UL>
  * </P>
+ * <P>
+ * An {@code IotProvider} is created with a provider and submitter that it delegates
+ * the creation and submission of topologies to.
+ * </P>
  * 
  * @see IotDevice
  * @see IotDevicePubSub
  */
-public abstract class IotProvider implements TopologyProvider,
+public class IotProvider implements TopologyProvider,
  DirectSubmitter<Topology, Job> {
     
     private final TopologyProvider provider;
+    private final Function<Topology, IotDevice> iotDeviceCreator;
     private final DirectSubmitter<Topology, Job> submitter;
     
     private final List<Topology> systemApps = new ArrayList<>();
 
     private JsonControlService controlService = new JsonControlService();
     
-    protected IotProvider() {   
-        this(new DirectProvider());
+    /**
+     * Create an {@code IotProvider} that uses its own {@code DirectProvider}.
+     * @param iotDeviceCreator How the {@code IotDevice} is created.
+     * 
+     * @see DirectProvider
+     */
+    public IotProvider(Function<Topology, IotDevice> iotDeviceCreator) {   
+        this(new DirectProvider(), iotDeviceCreator);
     }
     
-    protected IotProvider(DirectProvider provider) {
-        this(provider, provider);
+    /**
+     * Create an {@code IotProvider} that uses the passed in {@code DirectProvider}.
+     * 
+     * @param provider {@code DirectProvider} to use for topology creation and submission.
+     * @param iotDeviceCreator How the {@code IotDevice} is created.
+     * 
+     * @see DirectProvider
+     *
+     */
+    public IotProvider(DirectProvider provider, Function<Topology, IotDevice> iotDeviceCreator) {
+        this(provider, provider, iotDeviceCreator);
     }
 
-    protected IotProvider(TopologyProvider provider, DirectSubmitter<Topology, Job> submitter) {
+    /**
+     * Create an {@code IotProvider}.
+     * @param provider How topologies are created.
+     * @param submitter How topologies will be submitted.
+     * @param iotDeviceCreator How the {@code IotDevice} is created.
+     * 
+     */
+    public IotProvider(TopologyProvider provider, DirectSubmitter<Topology, Job> submitter,
+            Function<Topology, IotDevice> iotDeviceCreator) {
         this.provider = provider;
         this.submitter = submitter;
+        this.iotDeviceCreator = iotDeviceCreator;
         
         registerControlService();
         registerApplicationService();
@@ -106,27 +136,48 @@ public abstract class IotProvider implements TopologyProvider,
         createIotCommandToControlApp();
     }
     
+    /**
+     * Get the application service.
+     * Callers may use this to register applications to
+     * be executed by this provider.
+     * @return application service.
+     */
     public ApplicationService getApplicationService() {
         return getServices().getService(ApplicationService.class);
     }
     
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public ServiceContainer getServices() {
         return submitter.getServices();
     }
     
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public final Topology newTopology() {
         return provider.newTopology();
     }
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public final Topology newTopology(String name) {
         return provider.newTopology(name);
     }
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public final Future<Job> submit(Topology topology) {
         return submitter.submit(topology);
     }
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public final Future<Job> submit(Topology topology, JsonObject config) {
         return submitter.submit(topology, config);
@@ -189,6 +240,12 @@ public abstract class IotProvider implements TopologyProvider,
         systemApps.add(topology);
     }
     
+    /**
+     * Start this provider by starting its system applications.
+     * 
+     * @throws InterruptedException Interrupted exception starting applications.
+     * @throws ExecutionException Exception starting applications.
+     */
     public void start() throws InterruptedException, ExecutionException {
         for (Topology topology : systemApps) {
             JsonObject config = new JsonObject();
@@ -200,7 +257,7 @@ public abstract class IotProvider implements TopologyProvider,
     /**
      * Create the connection to the message hub.
      * 
-     * A sub-class creates an instance of {@link IotDevice}
+     * Creates an instance of {@link IotDevice}
      * used to communicate with the message hub. This
      * provider creates and submits an application
      * that subscribes to published events to send
@@ -208,6 +265,9 @@ public abstract class IotProvider implements TopologyProvider,
      * <BR>
      * The application is created using
      * {@link IotDevicePubSub#createApplication(IotDevice)}.
+     * <BR>
+     * The {@code IotDevice} is created using the function
+     * passed into the constructor.
      * 
      * @param topology Topology the {@code IotDevice} will be contained in.
      * @return IotDevice device used to communicate with the message hub.
@@ -215,5 +275,7 @@ public abstract class IotProvider implements TopologyProvider,
      * @see IotDevice
      * @see IotDevicePubSub
      */
-    protected abstract IotDevice createMessageHubDevice(Topology topology);
+    protected IotDevice createMessageHubDevice(Topology topology) {
+        return iotDeviceCreator.apply(topology);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-quarks/blob/8563b51c/test/fvtiot/src/test/java/quarks/test/fvt/iot/IotProviderTest.java
----------------------------------------------------------------------
diff --git a/test/fvtiot/src/test/java/quarks/test/fvt/iot/IotProviderTest.java b/test/fvtiot/src/test/java/quarks/test/fvt/iot/IotProviderTest.java
index 17d8cab..d6e5297 100644
--- a/test/fvtiot/src/test/java/quarks/test/fvt/iot/IotProviderTest.java
+++ b/test/fvtiot/src/test/java/quarks/test/fvt/iot/IotProviderTest.java
@@ -54,13 +54,7 @@ public class IotProviderTest {
     @Test
     public void testIotProviderStartApplications() throws Exception {
 
-        IotProvider provider = new IotProvider() {
-
-            @Override
-            protected IotDevice createMessageHubDevice(Topology topology) {
-                return new EchoIotDevice(topology);
-            }
-        };
+        IotProvider provider = new IotProvider(EchoIotDevice::new);
         
         assertSame(provider.getApplicationService(),
                 provider.getServices().getService(ApplicationService.class));