Changed controllers to be singleton in the manager and moved isAvailable to AbstractController
This commit is contained in:
parent
a80a1c5cdc
commit
267054e4ba
5 changed files with 31 additions and 27 deletions
|
|
@ -27,6 +27,14 @@ package se.hal.intf;
|
||||||
|
|
||||||
public interface HalAbstractController {
|
public interface HalAbstractController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates if the controller has all the configuration
|
||||||
|
* data and resources needed to be able to initialize correctly.
|
||||||
|
*/
|
||||||
|
default boolean isAvailable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The framework might create dummy objects so any type of
|
* The framework might create dummy objects so any type of
|
||||||
* resource initialization should be handled in this method
|
* resource initialization should be handled in this method
|
||||||
|
|
@ -35,13 +43,13 @@ public interface HalAbstractController {
|
||||||
void initialize() throws Exception;
|
void initialize() throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will register an device type to be handled by this controller
|
* Will register an device type to be handled by this controller.
|
||||||
*/
|
*/
|
||||||
void register(HalDeviceConfig deviceConfig);
|
void register(HalDeviceConfig deviceConfig);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deregisters an device from this controller, the controller
|
* Deregisters an device from this controller, the controller
|
||||||
* will no longer handle that type of event
|
* will no longer handle that type of event.
|
||||||
*/
|
*/
|
||||||
void deregister(HalDeviceConfig deviceConfig);
|
void deregister(HalDeviceConfig deviceConfig);
|
||||||
|
|
||||||
|
|
@ -51,7 +59,7 @@ public interface HalAbstractController {
|
||||||
int size();
|
int size();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a listener that will receive all reports from the the registered devices
|
* Set a listener that will receive all reports from the the registered devices.
|
||||||
*/
|
*/
|
||||||
void setListener(HalDeviceReportListener listener);
|
void setListener(HalDeviceReportListener listener);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
/** A map of all instantiated controllers **/
|
/** A map of all instantiated controllers **/
|
||||||
protected static Map<Class, HalAbstractController> controllerMap = new ConcurrentHashMap<>();
|
protected static Map<Class, HalAbstractController> controllerMap;
|
||||||
/** All available sensor plugins **/
|
/** All available sensor plugins **/
|
||||||
protected List<Class<? extends C>> availableDeviceConfigs = new ArrayList<>();
|
protected List<Class<? extends C>> availableDeviceConfigs = new ArrayList<>();
|
||||||
|
|
||||||
|
|
@ -39,16 +39,16 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
|
||||||
logger.severe("Unable to retrieve Controller class from generics for class: " + this.getClass());
|
logger.severe("Unable to retrieve Controller class from generics for class: " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Class<? extends C> deviceConfig : getAvailableDeviceConfigs()){
|
// Instantiate autostart controllers, but only the first time
|
||||||
try {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<T> controllerClass = (Class<T>) deviceConfig.getDeclaredConstructor().newInstance().getDeviceControllerClass();
|
|
||||||
|
|
||||||
if (controllerClass.isAssignableFrom(HalAutostartController.class)) {
|
synchronized (this) {
|
||||||
getControllerInstance(controllerClass); // Instantiate controller
|
if (controllerMap == null) {
|
||||||
|
controllerMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
for (Iterator<Class<? extends HalAutostartController>> it = pluginManager.getClassIterator(HalAutostartController.class); it.hasNext(); ) {
|
||||||
|
Class controller = it.next();
|
||||||
|
getControllerInstance(controller); // Instantiate controller
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
logger.log(Level.WARNING, "Unable to instantiate Device Config for controller check: " + deviceConfig.getClass());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +118,7 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
|
||||||
/**
|
/**
|
||||||
* Will return a singleton controller instance of the given class.
|
* Will return a singleton controller instance of the given class.
|
||||||
* If a instance does not exist yet the a new instance will be allocated
|
* If a instance does not exist yet the a new instance will be allocated
|
||||||
* depending on if the controller is ready thorough the {@link HalAutostartController#isAvailable()} method.
|
* depending on if the controller is ready thorough the {@link HalAbstractController#isAvailable()} method.
|
||||||
*
|
*
|
||||||
* @param clazz is the class of the wanted object instance wanted
|
* @param clazz is the class of the wanted object instance wanted
|
||||||
* @return A singleton instance of the input clazz or null if the class is unavailable or not ready to be instantiated.
|
* @return A singleton instance of the input clazz or null if the class is unavailable or not ready to be instantiated.
|
||||||
|
|
@ -127,14 +127,14 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
|
||||||
T controller;
|
T controller;
|
||||||
|
|
||||||
if (controllerMap.containsKey(clazz)) {
|
if (controllerMap.containsKey(clazz)) {
|
||||||
|
//noinspection unchecked
|
||||||
controller = (T) controllerMap.get(clazz);
|
controller = (T) controllerMap.get(clazz);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
// Instantiate controller
|
// Instantiate controller
|
||||||
controller = clazz.getDeclaredConstructor().newInstance();
|
controller = clazz.getDeclaredConstructor().newInstance();
|
||||||
|
|
||||||
if (controller instanceof HalAutostartController &&
|
if (!controller.isAvailable()) {
|
||||||
! ((HalAutostartController) controller).isAvailable()) {
|
|
||||||
logger.warning("Controller is not ready: " + clazz.getName());
|
logger.warning("Controller is not ready: " + clazz.getName());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -142,9 +142,6 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
|
||||||
logger.info("Instantiating new controller: " + clazz.getName());
|
logger.info("Instantiating new controller: " + clazz.getName());
|
||||||
controller.initialize();
|
controller.initialize();
|
||||||
|
|
||||||
if (this instanceof HalDeviceReportListener)
|
|
||||||
controller.setListener((HalDeviceReportListener)this);
|
|
||||||
|
|
||||||
controllerMap.put(clazz, controller);
|
controllerMap.put(clazz, controller);
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
logger.log(Level.SEVERE, "Unable to instantiate controller: " + clazz.getName(), e);
|
logger.log(Level.SEVERE, "Unable to instantiate controller: " + clazz.getName(), e);
|
||||||
|
|
@ -152,6 +149,11 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Assign the manager as a listener
|
||||||
|
|
||||||
|
if (this instanceof HalDeviceReportListener)
|
||||||
|
controller.setListener((HalDeviceReportListener) this);
|
||||||
|
|
||||||
return controller;
|
return controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,4 @@ package se.hal.intf;
|
||||||
*/
|
*/
|
||||||
public interface HalAutostartController {
|
public interface HalAutostartController {
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates if the controller has all the configuration
|
|
||||||
* data and resources needed to be able to initialize correctly
|
|
||||||
*/
|
|
||||||
boolean isAvailable();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package se.hal.plugin.netscan;
|
||||||
import se.hal.HalContext;
|
import se.hal.HalContext;
|
||||||
import se.hal.intf.*;
|
import se.hal.intf.*;
|
||||||
import se.hal.struct.devicedata.AvailabilityEventData;
|
import se.hal.struct.devicedata.AvailabilityEventData;
|
||||||
import se.hal.struct.devicedata.OnOffEventData;
|
|
||||||
import zutil.InetUtil;
|
import zutil.InetUtil;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.net.InetScanner;
|
import zutil.net.InetScanner;
|
||||||
|
|
@ -91,7 +90,7 @@ public class NetScanController implements HalEventController, HalAutostartContro
|
||||||
if (listener != null)
|
if (listener != null)
|
||||||
listener.reportReceived(
|
listener.reportReceived(
|
||||||
new NetworkDevice(ip.getHostAddress()),
|
new NetworkDevice(ip.getHostAddress()),
|
||||||
new OnOffEventData(true, System.currentTimeMillis()));
|
new AvailabilityEventData(true, System.currentTimeMillis()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package se.hal.plugin.netscan;
|
||||||
import se.hal.intf.HalEventConfig;
|
import se.hal.intf.HalEventConfig;
|
||||||
import se.hal.intf.HalEventController;
|
import se.hal.intf.HalEventController;
|
||||||
import se.hal.intf.HalEventData;
|
import se.hal.intf.HalEventData;
|
||||||
import se.hal.struct.devicedata.OnOffEventData;
|
import se.hal.struct.devicedata.AvailabilityEventData;
|
||||||
import zutil.ui.conf.Configurator;
|
import zutil.ui.conf.Configurator;
|
||||||
|
|
||||||
public class NetworkDevice implements HalEventConfig {
|
public class NetworkDevice implements HalEventConfig {
|
||||||
|
|
@ -30,7 +30,7 @@ public class NetworkDevice implements HalEventConfig {
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Class<? extends HalEventData> getDeviceDataClass() {
|
public Class<? extends HalEventData> getDeviceDataClass() {
|
||||||
return OnOffEventData.class;
|
return AvailabilityEventData.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue