public void stop(BundleContext context) throws Exception { oneWireProducerRegister.stop(); eventProducer.stop(); // kill the monitor dm.killMonitor(); // free port used by adapter adapter.freePort(); }
/** Load the configuration properties and register e new service */ private void initialize() throws Exception { if (context == null) return; // Get the Config-Location value from the manifest String configLocation = null; Dictionary dict = context.getBundle().getHeaders(); Enumeration enum1 = dict.keys(); while (enum1.hasMoreElements()) { Object nextKey = enum1.nextElement(); Object nextElem = dict.get(nextKey); if (nextKey.equals("Config-Location")) { configLocation = nextElem.toString(); break; } } if (configLocation == null) { configLocation = CONFIGFILE; } // Load properties from configLocation file InputStream is = getClass().getResourceAsStream(configLocation); Properties props = Configuration.loadProperties(is); String ptraceout = props.getProperty("traceout"); PrintStream traceout = null; if (ptraceout != null) { if (ptraceout.equals("System.out")) { traceout = System.out; } else if (ptraceout.equals("System.err")) { traceout = System.err; } else { // TODO instanciante the class with the name System.err.println("custom trace is not implemented !"); } } else { traceout = null; } // Get the properties String adapter_name = props.getProperty("adapterName"); String port_name = props.getProperty("portName"); traceln("Configured Adapter:" + adapter_name + " on port:" + port_name); String poll_interval = props.getProperty("pollInterval"); int pollInterval = 1000; // default 1000 millisec if (poll_interval != null) { pollInterval = Integer.parseInt(poll_interval); } try { if (adapter_name == null || port_name == null) { // get the default adapter adapter = OneWireAccessProvider.getDefaultAdapter(); } else { adapter = OneWireAccessProvider.getAdapter(adapter_name, port_name); } traceln("Found adapter: " + adapter.getAdapterName() + " on port: " + adapter.getPortName()); } catch (Exception e) { traceln("That is not a valid adapter/port combination."); Enumeration en = OneWireAccessProvider.enumerateAllAdapters(); while (en.hasMoreElements()) { DSPortAdapter temp = (DSPortAdapter) en.nextElement(); traceln("Adapter: " + temp.getAdapterName()); Enumeration f = temp.getPortNames(); while (f.hasMoreElements()) { traceln(" Port name : " + ((String) f.nextElement())); } } return; } traceln("Adapter: " + adapter.getAdapterName() + " Port: " + adapter.getPortName()); // clear any previous search restrictions adapter.setSearchAllDevices(); adapter.targetAllFamilies(); adapter.setSpeed(DSPortAdapter.SPEED_REGULAR); // SPEED_OVERDRIVE, SPEED_HYPERDRIVE, SPEED_FLEX // create a network monitor dm = new DeviceMonitor(adapter); eventProducer = new EventProducer(context, traceout); oneWireProducerRegister = new OneWireProducerRegister(context, pollInterval, traceout); // add this to the event listers dm.addDeviceMonitorEventListener(eventProducer); dm.addDeviceMonitorEventListener(oneWireProducerRegister); // start the monitor // start the monitor Thread thread = new Thread(dm); thread.setName(DeviceMonitor.class.getName()); thread.start(); }