Commit e0ef0d48 authored by Cristina Cocho's avatar Cristina Cocho

Now Configura uses data classes fro NomadCommandSystem to simplify the future...

Now Configura uses data classes fro NomadCommandSystem to simplify the future integration in NomadGUI
parent 43f1d4d7
......@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.ill.ics</groupId>
<artifactId>nomadcommandsystem</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.1-SNAPSHOT</version>
<name>NomadCommandSystem</name>
<description>Java bridge for the communication with the Nomad server</description>
<scm>
......
......@@ -46,6 +46,9 @@ public class ControllerManager implements ServerConfigurationChangeListener {
private HashMap<String, LinkedHashSet<String>> visibleControllerNamesByType; // KEY=controller type VALUE=list of matching controller names
private HashMap<String, LinkedHashSet<String>> allControllerNamesByType; // KEY=controller type VALUE=list of matching controller names
// Keep all the ControllerServants created. KEY = servant ID
private Map<Integer, ControllerServant> controllerServants = new HashMap<Integer, ControllerServant>();
private static ControllerManager instance = null;
public static ControllerManager getInstance() {
......@@ -88,31 +91,68 @@ public class ControllerManager implements ServerConfigurationChangeListener {
fillControllerNamesByTypeMaps();
}
private ControllerServant createController(ClientServantDescriptor descriptor) {
ControllerServant controller = new ControllerServant(0, descriptor.getId(), descriptor);
controllerServants.put(descriptor.getId(), controller);
return controller;
}
/**
*
*/
public ControllerServant getController(String controllerName) throws ControllerNotFoundException {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getControllerDescriptor(controllerName);
if (descriptor != null) {
ControllerServant controller = ServantManagerAccessor.getInstance(serverId).getController(descriptor);
return controller;
// Former solution consisted on accessing the ServantManagerAccessor class
// ControllerServant controller = ServantManagerAccessor.getInstance(serverId).getController(descriptor);
// Search the controller in local map
ControllerServant controllerServant = null;
if (controllerServants.get(descriptor.getId()) != null) {
controllerServant = controllerServants.get(descriptor.getId());
} else {
// ServantDatabase is updated with descriptor but the manager does not contain the ControllerServant instance yet. Create it.
controllerServant = createController(descriptor);
}
return controllerServant;
}
ControllerNotFoundException cnfe = new ControllerNotFoundException(controllerName, "", getClass().getCanonicalName(), "getController");
throw cnfe;
}
public ControllerServant getController(int controllerId) {
if (controllerServants.containsKey(controllerId)) {
return controllerServants.get(controllerId);
} else {
// ServantDatabase uploaded but ControllerServant instance not created
ClientServantDescriptor clientDescriptor = ServantDatabase.getInstance().getServantDescriptorWithId(controllerId);
if (clientDescriptor != null) {
return createController(clientDescriptor);
}
}
return null;
}
public ControllerServant cloneController(String controllerName) throws ControllerNotFoundException {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getControllerDescriptor(controllerName);
if (descriptor != null) {
ControllerServant controller = ServantManagerAccessor.getInstance(serverId).cloneController(descriptor);
// NOTE: Not sure about the cloning procedure... to avoid problems and assuming any time we clone we obtain a descriptor which can be different to the former one, we create a new instance of ControllerServant.
// Clone in Server
ServantManagerAccessor.getInstance(serverId).cloneController(descriptor);
// Create ControllerServant (ServantDatabase, PropertyDatabase and CommandDatabase are updated)
ControllerServant controller = new ControllerServant(0, descriptor.getId(), descriptor);
controllerServants.put(descriptor.getId(), controller); // Replacing former instance
return controller;
}
ControllerNotFoundException cnfe = new ControllerNotFoundException(controllerName, "", getClass().getCanonicalName(), "cloneController");
throw cnfe;
}
/*
* Is this method used?
*/
public void removeController(String controllerName) {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getControllerDescriptor(controllerName);
if (descriptor != null) {
......@@ -120,7 +160,6 @@ public class ControllerManager implements ServerConfigurationChangeListener {
}
}
/**
*
*/
......@@ -128,22 +167,18 @@ public class ControllerManager implements ServerConfigurationChangeListener {
return ServantDatabase.getInstance().getControllersOfType(type, onlyVisibleOnes);
}
public Set<String> getControllerTypes() {
return ServantDatabase.getInstance().getControllerTypes();
}
public boolean controllerIsEnabled(String controllerName) {
return ServantDatabase.getInstance().controllerIsEnabled(controllerName);
}
public TreeMap<String, LinkedHashMap<String, String>> getPropertiesForGeneric() {
return ServantManagerAccessor.getInstance(serverId).getPropertiesForGeneric();
}
public String getType(String controllerName) {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getControllerDescriptor(controllerName);
if (descriptor != null) {
......@@ -153,9 +188,7 @@ public class ControllerManager implements ServerConfigurationChangeListener {
}
private Map<String, Map<String, List<String>>> getInstalledControllers(boolean testCommand) {
Map<String, Map<String, List<String>>> installedControllers = new TreeMap<String, Map<String, List<String>>>();
Iterator<ClientServantDescriptor> it = ServantDatabase.getInstance().getControllers().iterator();
while (it.hasNext()) {
ClientServantDescriptor descriptor = it.next();
......@@ -167,7 +200,6 @@ public class ControllerManager implements ServerConfigurationChangeListener {
}
if (testCommandOk && descriptor.isEnabled() && descriptor.isVisible()) {
String family = descriptor.getFamily();
String subfamily = descriptor.getSubfamily();
......@@ -178,27 +210,21 @@ public class ControllerManager implements ServerConfigurationChangeListener {
}
if (subfamily.equals("none")) {
familyMembers.put(descriptor.getName(), new ArrayList<String>());
} else {
List<String> subfamilyMembers = familyMembers.get(subfamily);
if (subfamilyMembers == null) {
subfamilyMembers = new ArrayList<String>();
familyMembers.put(subfamily, subfamilyMembers);
}
subfamilyMembers.add(descriptor.getName());
}
}
}
return installedControllers;
}
public Map<String, Map<String, List<String>>> getInstalledControllers() {
return getInstalledControllers(false);
}
......@@ -207,7 +233,6 @@ public class ControllerManager implements ServerConfigurationChangeListener {
return getInstalledControllers(true);
}
/**
*
*/
......@@ -244,9 +269,8 @@ public class ControllerManager implements ServerConfigurationChangeListener {
}
}
public Set getStartedControllers() {
Set startedCommands = new HashSet();
public Set<ControllerServant> getStartedControllers() {
Set<ControllerServant> startedCommands = new HashSet<ControllerServant>();
try {
Iterator<ClientServantDescriptor> it = ServantDatabase.getInstance().getControllers().iterator();
while (it.hasNext()) {
......@@ -262,8 +286,8 @@ public class ControllerManager implements ServerConfigurationChangeListener {
return startedCommands;
}
public Set getCommandsOfType(String type) {
Set commandsOfType = new HashSet();
public Set<String> getCommandsOfType(String type) {
Set<String> commandsOfType = new HashSet<String>();
Iterator<ClientServantDescriptor> it = ServantDatabase.getInstance().getControllers().iterator();
while (it.hasNext()) {
ClientServantDescriptor descriptor = it.next();
......@@ -271,11 +295,9 @@ public class ControllerManager implements ServerConfigurationChangeListener {
commandsOfType.add(descriptor.getName());
}
}
return commandsOfType;
}
public String getFamily(String controllerName) {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getControllerDescriptor(controllerName);
if (descriptor != null) {
......@@ -292,7 +314,6 @@ public class ControllerManager implements ServerConfigurationChangeListener {
return null;
}
public LinkedHashSet<String> getControllersOfTypeInDisplayOrder(String controllerType, boolean onlyVisibleOnes) {
if (onlyVisibleOnes) {
return visibleControllerNamesByType.get(controllerType);
......
......@@ -19,6 +19,7 @@
package fr.ill.ics.bridge;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
......@@ -29,6 +30,7 @@ import java.util.TreeMap;
import fr.ill.ics.nscclient.dataprovider.ServantDatabase;
import fr.ill.ics.nscclient.dataprovider.ServantManagerAccessor;
import fr.ill.ics.nscclient.servant.ClientDriverServantDescriptor;
import fr.ill.ics.nscclient.servant.ClientServantDescriptor;
import fr.ill.ics.nscclient.servant.DriverServant;
......@@ -40,6 +42,9 @@ public class DriverManager {
private static DriverManager instance = null;
// Keep all the DriverServants created. KEY = servant ID
private Map<Integer, DriverServant> driverServants = new HashMap<Integer, DriverServant>();
public static DriverManager getInstance() {
return instance;
}
......@@ -57,18 +62,67 @@ public class DriverManager {
this.serverId = serverId;
}
protected void init() {
private void init() {
// At this point the ServantDatabase should be loaded with the Nomad configuration. We can fill our driverServants map.
Iterator<ClientServantDescriptor> it = ServantDatabase.getInstance().getDrivers().iterator();
while (it.hasNext()) {
ClientServantDescriptor descriptor = it.next();
int driverID = descriptor.getId();
// Think about a common use of DatabaseID
DriverServant driverServant = new DriverServant(0, descriptor.getId(), descriptor);
driverServants.put(driverID, driverServant);
}
}
private DriverServant createDriver(ClientDriverServantDescriptor descriptor) {
// Use ServantDatabase to get the descriptor of the driver
int driverID = descriptor.getId();
DriverServant driverServant = new DriverServant(0, driverID, descriptor);
driverServant.setChannel(descriptor.getChannel());
driverServant.setMode(descriptor.getMode());
driverServants.put(driverID, driverServant);
return driverServant;
}
public DriverServant getDriver(String driverName) {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getDriverDescriptor(driverName);
ClientDriverServantDescriptor descriptor = (ClientDriverServantDescriptor)ServantDatabase.getInstance().getDriverDescriptor(driverName);
if (descriptor != null) {
DriverServant driver = ServantManagerAccessor.getInstance(serverId).getDriver(descriptor);
return driver;
// Former solution consisted on accessing the ServantManagerAccessor class
// DriverServant driver = ServantManagerAccessor.getInstance(serverId).getDriver(descriptor);
// Search the driver in local map
DriverServant driverServant = null;
if (driverServants.get(descriptor.getId()) != null) {
driverServant = driverServants.get(descriptor.getId());
} else {
// ServantDatabase is updated with descriptor but the manager does not contain the DriverServant instance yet. Create it.
driverServant = createDriver(descriptor);
}
return driverServant;
}
return null;
}
public DriverServant getDriver(int driverID) {
// Check if local map contains the driver
if (driverServants.get(driverID) != null) {
return driverServants.get(driverID);
} else {
// ServantDatabase uploaded but DriverServant instance not created
ClientDriverServantDescriptor clientDescriptor = (ClientDriverServantDescriptor)ServantDatabase.getInstance().getServantDescriptorWithId(driverID);
if (clientDescriptor != null) {
return createDriver(clientDescriptor);
}
}
return null;
}
public void removeDriver(int driverID) {
driverServants.remove(driverID);
}
public String getType(String driverName) {
ClientServantDescriptor descriptor = ServantDatabase.getInstance().getDriverDescriptor(driverName);
if (descriptor != null) {
......@@ -77,26 +131,24 @@ public class DriverManager {
return null;
}
public Map getInstalledDrivers() {
public Map<Integer, DriverServant> getDrivers() {
return driverServants;
}
public Map<String, Map<String, Map<String, String>>> getInstalledDrivers() {
Map<String, Map<String, Map<String, String>>> installedDrivers = new TreeMap<String, Map<String, Map<String, String>>>();
Iterator<ClientServantDescriptor> it = ServantDatabase.getInstance().getDrivers().iterator();
while (it.hasNext()) {
ClientServantDescriptor descriptor = it.next();
if (descriptor.isEnabled() && descriptor.isRoot()) {
Map<String, Map<String, String>> children = getChildren(descriptor);
installedDrivers.put(descriptor.getName(), children);
}
}
return installedDrivers;
}
private Map getChildren(ClientServantDescriptor descriptor) {
Map<String, Map<String, String>> allChildren = new LinkedHashMap<String, Map<String, String>>();
List<String> children = ServantManagerAccessor.getInstance(serverId).getChildServants(descriptor.getId());
......@@ -109,17 +161,15 @@ public class DriverManager {
allChildren.put(childName, getChildren(childDescriptor));
}
}
return allChildren;
}
public Collection getControllersOfType(String driverType) {
public Collection<String> getControllersOfType(String driverType) {
return ServantDatabase.getInstance().getDriversOfType(driverType, false);
}
public Set getStartedControllers() {
Set startedCommands = new HashSet();
public Set<DriverServant> getStartedControllers() {
Set<DriverServant> startedCommands = new HashSet<DriverServant>();
Iterator<ClientServantDescriptor> it = ServantDatabase.getInstance().getDrivers().iterator();
while (it.hasNext()) {
ClientServantDescriptor descriptor = it.next();
......@@ -131,18 +181,15 @@ public class DriverManager {
return startedCommands;
}
public void setPaused(boolean isPaused) {
ServantManagerAccessor.getInstance(serverId).pauseSpies(isPaused);
}
public Long getChannel(String driverName) {
DriverServant driver = getDriver(driverName);
if (driver != null) {
return getDriver(driverName).getChannel();
}
return null;
}
......
......@@ -35,7 +35,7 @@ import fr.ill.ics.util.exception.ConfigurationException.PluginType;
public class CommandManager {
private static CommandManager instance = null;
private Map controllerCommands; // key = AC instance, value=Map of Commands (key = command name, value = Command instance)
private Map<Controller, Map<String, Command>> controllerCommands; // key = AC instance, value=Map of Commands (key = command name, value = Command instance)
private CommandManager() {
......@@ -52,11 +52,11 @@ public class CommandManager {
// Create a new Map of commandName:Command instance if it doesn't exist for the abstract controller
if (controllerCommands == null) {
controllerCommands = new HashMap();
controllerCommands = new HashMap<Controller, Map<String, Command>>();
}
if (!controllerCommands.containsKey(controller)) {
controllerCommands.put(controller, new HashMap());
controllerCommands.put(controller, new HashMap<String, Command>());
}
int servantId;
......@@ -68,7 +68,7 @@ public class CommandManager {
}
// Create a new command if it doesn't already exist or just retrieve it from the Map
Map allCommands = (HashMap)controllerCommands.get(controller);
Map<String, Command> allCommands = controllerCommands.get(controller);
Command command = null;
if (allCommands.containsKey(commandName)) {
command = (Command)allCommands.get(commandName);
......@@ -90,16 +90,16 @@ public class CommandManager {
return getCommand(controller, commandName, false, PluginType.GENERAL);
}
public Map getAllCommands(Controller controller) {
public Map<String, Command> getAllCommands(Controller controller) {
if (controllerCommands == null) {
controllerCommands = new HashMap();
controllerCommands = new HashMap<Controller, Map<String, Command>>();
}
if (!controllerCommands.containsKey(controller)) {
controllerCommands.put(controller, new HashMap());
controllerCommands.put(controller, new HashMap<String, Command>());
}
else {
return (HashMap) controllerCommands.get(controller);
return controllerCommands.get(controller);
}
int servantId;
......@@ -113,16 +113,16 @@ public class CommandManager {
databaseId = ((AtomicCommandWrapper)controller).getDatabaseId();
}
Map allCommands = (HashMap)controllerCommands.get(controller);
Map<String, Command> allCommands = controllerCommands.get(controller);
Command command = null;
HashMap<String, Integer> states = ControllerManager.getInstance().getServantCommandStates(databaseId, servantId);
Set names = controller.getCommandNames();
Set<String> names = controller.getCommandNames();
if (names != null) {
Iterator it = names.iterator();
Iterator<String> it = names.iterator();
String commandName;
int id;
while (it.hasNext()) {
commandName = (String)it.next();
commandName = it.next();
id = CommandDatabase.getInstance().getCommandId(servantId, commandName);
if (id != -1) {
command = new Command(controller.getDatabaseId(), id, commandName, false);
......@@ -133,5 +133,11 @@ public class CommandManager {
}
return allCommands;
}
public void removeAllCommands(Controller controller) {
if (controllerCommands != null) {
controllerCommands.remove(controller);
}
}
}
\ No newline at end of file
......@@ -186,6 +186,18 @@ public class PropertyManager {
return clientFilesExist;
}
/*
* Remove all the properties of a Driver or Controller given its ID
*/
public void removeAllPropertiesForServant(int servantID) {
// TODO: code controller case
Servant servant = DriverManager.getInstance().getDriver(servantID);
if (servant == null) {
// Obtain controller servant...
}
propertyMap.remove(servant.getId());
}
public void createInheritedProperties(String type, String superType) {
try {
if (!propertiesByControllerType.containsKey(superType)) {
......
......@@ -81,7 +81,6 @@ public class ServantDatabase {
driversByName.put(descriptor.getName(), descriptor);
}
public boolean isController(int servantId) {
return controllers.containsKey(servantId);
}
......
package fr.ill.ics.nscclient.servant;
import fr.ill.ics.nscclient.servant.DriverServant.Mode;
public class ClientDriverServantDescriptor extends ClientServantDescriptor {
private Mode mode;
private long channel;
public ClientDriverServantDescriptor(int ID, String name, String type, String family, String subfamily, boolean isCommand, boolean isVisible, boolean isEnabled, boolean isFavourite, boolean isRoot, Mode mode, long channel) {
super(ID, name, type, family, subfamily, isCommand, isVisible, isEnabled, isFavourite, isRoot);
this.mode = mode;
this.channel = channel;
}
public Mode getMode() {
return mode;
}
public long getChannel() {
return channel;
}
}
......@@ -62,28 +62,30 @@ public class Servant implements Controller {
return servantDescriptor.getType();
}
public boolean isRoot() {
return servantDescriptor.isRoot();
}
public boolean isEnabled() {
return servantDescriptor.isEnabled();
}
public ClientCommandState getCommandState(int databaseId, String commandName) throws UnknownCommandException {
int commandId = CommandDatabase.getInstance().getCommandId(servantId, commandName);
if (commandId != -1) {
String serverId = DataAccessor.getServerId(databaseId);
return DataAccessor.getInstance(serverId).getCommandState(0, commandId);
}
throw new UnknownCommandException();
}
public boolean isStarted() {
try {
return (getCommandState(databaseId, CommandAction.START_COMMAND_NAME) == ClientCommandState.ACTIVE);
} catch (UnknownCommandException e) {
return false;
}
}
public String getControllerNameWithRole(String role) {
......@@ -161,4 +163,7 @@ public class Servant implements Controller {
ControllerManager.getInstance().updateDynamicProperties(this);
}
public boolean isDriver() {
return (this instanceof DriverServant);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment