/* * 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="SAMPLE-PROVISING-FILE changeit"} * @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) { } } } } }