Changed controllers to be singleton in the manager and moved isAvailable to AbstractController

This commit is contained in:
Ziver Koc 2021-06-09 23:27:42 +02:00
parent a80a1c5cdc
commit 267054e4ba
5 changed files with 31 additions and 27 deletions

View file

@ -27,6 +27,14 @@ package se.hal.intf;
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
* resource initialization should be handled in this method
@ -35,13 +43,13 @@ public interface HalAbstractController {
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);
/**
* 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);
@ -51,7 +59,7 @@ public interface HalAbstractController {
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);

View file

@ -19,7 +19,7 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
private static final Logger logger = LogUtil.getLogger();
/** A map of all instantiated controllers **/
protected static Map<Class, HalAbstractController> controllerMap = new ConcurrentHashMap<>();
protected static Map<Class, HalAbstractController> controllerMap;
/** All available sensor plugins **/
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());
}
for (Class<? extends C> deviceConfig : getAvailableDeviceConfigs()){
try {
@SuppressWarnings("unchecked")
Class<T> controllerClass = (Class<T>) deviceConfig.getDeclaredConstructor().newInstance().getDeviceControllerClass();
// Instantiate autostart controllers, but only the first time
if (controllerClass.isAssignableFrom(HalAutostartController.class)) {
getControllerInstance(controllerClass); // Instantiate controller
synchronized (this) {
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.
* 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
* @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;
if (controllerMap.containsKey(clazz)) {
//noinspection unchecked
controller = (T) controllerMap.get(clazz);
} else {
try {
// Instantiate controller
controller = clazz.getDeclaredConstructor().newInstance();
if (controller instanceof HalAutostartController &&
! ((HalAutostartController) controller).isAvailable()) {
if (!controller.isAvailable()) {
logger.warning("Controller is not ready: " + clazz.getName());
return null;
}
@ -142,9 +142,6 @@ public abstract class HalAbstractControllerManager<T extends HalAbstractControll
logger.info("Instantiating new controller: " + clazz.getName());
controller.initialize();
if (this instanceof HalDeviceReportListener)
controller.setListener((HalDeviceReportListener)this);
controllerMap.put(clazz, controller);
} catch (Exception 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;
}

View file

@ -6,9 +6,4 @@ package se.hal.intf;
*/
public interface HalAutostartController {
/**
* Indicates if the controller has all the configuration
* data and resources needed to be able to initialize correctly
*/
boolean isAvailable();
}

View file

@ -3,7 +3,6 @@ package se.hal.plugin.netscan;
import se.hal.HalContext;
import se.hal.intf.*;
import se.hal.struct.devicedata.AvailabilityEventData;
import se.hal.struct.devicedata.OnOffEventData;
import zutil.InetUtil;
import zutil.log.LogUtil;
import zutil.net.InetScanner;
@ -91,7 +90,7 @@ public class NetScanController implements HalEventController, HalAutostartContro
if (listener != null)
listener.reportReceived(
new NetworkDevice(ip.getHostAddress()),
new OnOffEventData(true, System.currentTimeMillis()));
new AvailabilityEventData(true, System.currentTimeMillis()));
}

View file

@ -3,7 +3,7 @@ package se.hal.plugin.netscan;
import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventController;
import se.hal.intf.HalEventData;
import se.hal.struct.devicedata.OnOffEventData;
import se.hal.struct.devicedata.AvailabilityEventData;
import zutil.ui.conf.Configurator;
public class NetworkDevice implements HalEventConfig {
@ -30,7 +30,7 @@ public class NetworkDevice implements HalEventConfig {
}
@Override
public Class<? extends HalEventData> getDeviceDataClass() {
return OnOffEventData.class;
return AvailabilityEventData.class;
}