Run the Sample Java SE Gateway Application Using Apache Felix

Run the sample Java SE gateway application using Apache Felix to learn how to use the client software library APIs. The sample Java SE gateway application simulates a gateway that polls humidity and temperature sensors, and sends sensor data to the Oracle Fusion Cloud IoT Intelligent Applications instance.

  1. Set up your Java SE development environment. See Set Up Your Development Environment to Use the Java SE Client Software Libraries.
  2. Upload the humidity and temperature sensor device models to Oracle Fusion Cloud IoT Intelligent Applications. See Upload the Sample Device Models.
  3. Download and extract the content of the latest version of the Apache Felix framework.
  4. Copy the following definition of the gateway device application and create a file named SampleActivator.java in the $CL_HOME/samples/src/main/java/com/oracle/iot/sample directory.
    /*
    * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
    *
    * This software is dual-licensed to you under the MIT License (MIT) and
    * the Universal Permissive License (UPL). See the LICENSE file in the root
    * directory for license terms. You may choose either license, or both.
    */
    
    package com.oracle.iot.sample;
     
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.BundleException;
    
    /**
     * Activator class for OSGi platform. Refer to the on-line, Oracle IoT Cloud Service
     * Java Client Library documentation for details on how to run a client library
     * sample from an OSGi bundle.
     */
    public class SampleActivator implements BundleActivator
    {
        /**
         * Implements BundleActivator.start().
         * 
         * Note: expects following system properties to be set: 'sample.name', 'sample.args'
         * 
         * For example: {@code -Dsample.name=GatewayDeviceSample -Dsample.args="activationId.jks password"}
         * @param context the framework context for the bundle.
         **/
        public void start(BundleContext context) throws Exception
        {
            Thread executeSample = new Thread(new ExecuteSample(context));
            executeSample.setDaemon(true);
            executeSample.start();
        }
     
       /**
         * Implements BundleActivator.stop().
         * @param context the framework context for the bundle.
         **/
        public void stop(BundleContext context) throws Exception
        {
            System.err.println("SampleActivator.stop called");
            String sampleName = System.getProperty("sample.name", "GatewayDeviceSample");
            String classname = "com.oracle.iot.sample."+sampleName;
            Class<?> cls = Class.forName(classname);
            java.lang.reflect.Field f = cls.getDeclaredField("exiting");
            f.set(null, true);
        }
    private class ExecuteSample implements Runnable {
            private BundleContext context;
            public ExecuteSample(BundleContext context) { this.context = context; }
            @Override
            public void run() {
                String sampleName = System.getProperty("sample.name", "GatewayDeviceSample");
                String classname = "com.oracle.iot.sample."+sampleName;
                try {
                    Class<?> cls = Class.forName(classname);
                    java.lang.reflect.Field f = cls.getDeclaredField("isUnderFramework");
                    f.set(null, true);
     
                    f = cls.getDeclaredField("exiting");
                    f.set(null, false);
     
                    String[] arguments = System.getProperty("sample.args","").split("\\s+");
     
                    @SuppressWarnings("unchecked")
                    final java.lang.reflect.Method meth = cls.getMethod("main", String[].class);
                    meth.invoke(null, (Object) arguments); // static method doesn't have an instance
     
                } catch(Exception e) {
                    try {
                        context.getBundle().stop();
                    } catch(BundleException be) {
                    }
                }           
            }
        }
    }
  5. Run the following command to create the build directory:
    cd $CL_HOME/samples
    mkdir -p build/classes
  6. Run the following command to build the classes:
    javac -d ./build/classes -cp Apache_Felix_directory/bin/felix.jar:../lib/device-library.jar 
    src/main/java/com/oracle/iot/sample/GatewayDeviceSample.java 
    src/main/java/com/oracle/iot/sample/SampleActivator.java 
    src/main/java/com/oracle/iot/sample/TemperatureSensor.java 
    src/main/java/com/oracle/iot/sample/HumiditySensor.java
    The output classes appear in the $CL_HOME/samples/build/classes/ directory.
  7. Create the MANIFEST.MF file in the$CL_HOME/samples directory using the values in the following table:
    Sample Name MANIFEST.MF
    GatewayDeviceSample and DirectlyConnectedDeviceSample
    Bundle-ManifestVersion: 2
    Bundle-Name: GatewayDevice Sample
    Bundle-SymbolicName: com.oracle.iot.sample
    Bundle-Version: 1.0.0
    Bundle-Activator: com.oracle.iot.sample.SampleActivator
    Import-Package: org.osgi.framework,oracle.iot.client.device,oracle.iot.client
    
    EnterpriseClientSample
    Bundle-ManifestVersion: 2
    Bundle-Name: GatewayDevice Sample
    Bundle-SymbolicName: com.oracle.iot.sample
    Bundle-Version: 1.0.0
    Bundle-Activator: com.oracle.iot.sample.SampleActivator
    Import-Package: org.osgi.framework,oracle.iot.client.enterprise,oracle.iot.client
    
    ext.DirectlyConnectedDeviceSample and ext.GatewayDeviceSample
    Bundle-ManifestVersion: 2
    Bundle-Name: GatewayDevice Sample
    Bundle-SymbolicName: com.oracle.iot.sample
    Bundle-Version: 1.0.0
    Bundle-Activator: com.oracle.iot.sample.SampleActivator
    Import-Package: org.osgi.framework,org.json,com.oracle.iot.client.device,com.oracle.iot.client.message,com.oracle.iot.client.device.util
    

    Note:

    Make sure there is a line break after the last line of the MANIFEST.MF file.
  8. Run this command to create the bundle jar file:
    jar -cmf MANIFEST.MF osgi-sample.jar -C ./build/classes com
  9. Register the gateway device and download the provisioning file.
  10. Change directories to Apache_Felix_directory.
  11. Run the following command to start Apache Felix, and replace the sample_name and the sample_args using the values in the table:
    java -Dsample.name=sample_name -Dsample.args="sample_args" -jar FELIX_PATH/bin/felix.jar
    
    To run the application with device policies, add this parameter to the command:
    -Dcom.oracle.iot.sample.use_policy=true
    Run this command to start the application with device policies:
    java -Dcom.oracle.iot.sample.use_policy=true -Dsample.name=sample_name -Dsample.args="sample_args" -jar FELIX_PATH/bin/felix.jar
    
    sample_name sample_args
    DirectlyConnectedDeviceSample provisioning_file_name provisioning_file_password
    GatewayDeviceSample provisioning_file_name provisioning_file_passwordprovisioning_file_name provisioning_file_password temperature_sensor_endpointID humidity_sensor_endpointID
    EnterpriseClientSample provisioning_file_name provisioning_file_passwordprovisioning_file_name provisioning_file_password deviceID [,deviceID]provisioning_file_name provisioning_file_password deviceID ][reset | on | off]

    java com.oracle.iot.sample.EnterpriseClientSample

    java com.oracle.iot.sample.EnterpriseClientSample provisioning_file_name provisioning_file_password deviceID maxThreshold minThreshold

    ext.DirectlyConnectedDeviceSample provisioning_file_name provisioning_file_password
    ext.GatewayDeviceSample provisioning_file_name provisioning_file_passwordprovisioning_file_name provisioning_file_password temperature_sensor_endpointID humidity_sensor_endpointID
  12. Install the device-library.jar, json–20160212.jar, osgi-sample.jar files, and then start your bundle using the assigned ID.
    The output should be similar to the following example:
    Welcome to Apache Felix Gogo
    g! install CL_HOME/lib/device-library.jar
    Bundle ID: 5
    g! install CL_HOME/lib/json-20200518.jar
    Bundle ID: 6
    g! install CL_HOME/samples/osgi-sample.jar 
    Bundle ID: 7
    g! start 7
    Starting to listen for service events.
    Creating the gateway instance...
    Created virtural temperature sensor 0-2UJQ
    Created virtural humidity sensor 0-2YJQ
        Press enter to exit.
    Tue Aug 02 17:18:18 EDT 2016 : 0-2YJQ : Set : "humidity"=81,"maxThreshold"=90
    Tue Aug 02 17:18:17 EDT 2016 : 0-2UJQ : Set : "power"=true,"temp"=58.5,"unit"=°C,"minTemp"=58.5,"maxTemp"=58.5,"minThreshold"=0,"maxThreshold"=65

    Note:

    The Bundle ID values may vary.