From 0dbcd966a7cc1ec0fe8d805465e843e4b93ee413 Mon Sep 17 00:00:00 2001 From: Gerd Wuetherich Date: Tue, 18 Jun 2019 20:32:05 +0200 Subject: [PATCH] GH-2 moved commands to their own classes --- ...AbstractGraphDatabaseCommandComponent.java | 149 +++++++++++++ .../slizaa/server/SlizaaBackendCommands.java | 56 ++--- .../server/SlizaaGraphDatabaseCommands.java | 201 ++---------------- ...raphDatabaseContentDefinitionCommands.java | 60 ++++++ ...raphDatabaseHierarchicalGraphCommands.java | 86 ++++++++ .../backend/IModifiableBackendService.java | 2 +- .../impl/AbstractSlizaaServerBackendImpl.java | 4 +- .../backend/impl/DummyBackendService.java | 3 +- .../backend/impl/SlizaaServerBackendImpl.java | 13 +- 9 files changed, 343 insertions(+), 231 deletions(-) create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/AbstractGraphDatabaseCommandComponent.java create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseContentDefinitionCommands.java create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseHierarchicalGraphCommands.java diff --git a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/AbstractGraphDatabaseCommandComponent.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/AbstractGraphDatabaseCommandComponent.java new file mode 100644 index 0000000..4c10cc2 --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/AbstractGraphDatabaseCommandComponent.java @@ -0,0 +1,149 @@ +/** + * slizaa-server-main - Slizaa Static Software Analysis Tools + * Copyright © 2019 Code-Kontor GmbH and others (slizaa@codekontor.io) + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + *

+ * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package io.codekontor.slizaa.server; + +import io.codekontor.slizaa.server.descr.ContentDefinition; +import io.codekontor.slizaa.server.descr.GraphDatabase; +import io.codekontor.slizaa.server.service.backend.IBackendService; +import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; +import io.codekontor.slizaa.server.service.extensions.IExtensionService; +import io.codekontor.slizaa.server.service.slizaa.IGraphDatabase; +import io.codekontor.slizaa.server.service.slizaa.ISlizaaService; +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Collections; + +public abstract class AbstractGraphDatabaseCommandComponent { + + @Autowired + private ISlizaaService _slizaaService; + + @Autowired(required = false) + private IModifiableBackendService _modifiableBackendService; + + @Autowired + private IBackendService _backendService; + + @Autowired + private IExtensionService _extensionService; + + protected ISlizaaService slizaaService() { + return _slizaaService; + } + + protected boolean hasModifiableBackendService() { + return _modifiableBackendService != null; + } + + protected IModifiableBackendService modifiableBackendService() { + return _modifiableBackendService; + } + + protected IBackendService backendService() { + return _backendService; + } + + protected IExtensionService extensionService() { + return _extensionService; + } + + @NotNull + protected String dumpGraphDatabases() { + + StringBuffer stringBuffer = new StringBuffer("Graph Databases:\n"); + + if (_slizaaService.hasGraphDatabases()) { + + _slizaaService.getGraphDatabases().forEach(db -> { + + ContentDefinition contentDefinition = + db.getContentDefinition() != null ? + new ContentDefinition( + db.getContentDefinition().getContentDefinitionProviderFactory().getFactoryId(), + db.getContentDefinition().toExternalRepresentation()) : + null; + + GraphDatabase graphDatabase = new GraphDatabase( + db.getIdentifier(), + contentDefinition, + Collections.emptyList(), + db.getState().name(), + db.getPort(), + Collections.emptyList()); + + stringBuffer.append(" - " + graphDatabase.toString() + "\n"); + }); + } else { + stringBuffer.append("No database configured.\n"); + } + + return stringBuffer.toString(); + } + + @NotNull + protected String dumpContentDefinitionProviderFactories() { + + StringBuffer stringBuffer = new StringBuffer("Content Definition Provider Factories:\n"); + + if (!_slizaaService.getContentDefinitionProviderFactories().isEmpty()) { + _slizaaService.getContentDefinitionProviderFactories().forEach(factory -> { + stringBuffer.append(" - " + factory.getFactoryId() + "\n"); + }); + } else { + stringBuffer.append("No Content Definition Provider Factories available.\n"); + } + + return stringBuffer.toString(); + } + + /** + * Checks if the backend is configured properly. + * + * @return true if the backend is configured properly. + */ + protected String checkBackendConfigured() { + if (!_slizaaService.getBackendService().hasInstalledExtensions()) { + return cannotExecuteCommand("The Slizaa Server has not been configured properly: There are not installed backend extensions.\n"); + } + return null; + } + + protected String checkDatabaseExists(String identifier) { + IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(identifier); + if (graphDatabase == null) { + return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", identifier)); + } + return null; + } + + protected String checkDatabaseDoesNotExist(String identifier) { + IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(identifier); + if (graphDatabase != null) { + return cannotExecuteCommand(String.format("The specified database '%s' already exists.\n", identifier)); + } + return null; + } + + protected String cannotExecuteCommand(String msg) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Can not execute command.\n"); + stringBuffer.append(msg + "\n"); + return stringBuffer.toString(); + } +} \ No newline at end of file diff --git a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaBackendCommands.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaBackendCommands.java index 1562903..a8bd5fa 100644 --- a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaBackendCommands.java +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaBackendCommands.java @@ -17,12 +17,8 @@ */ package io.codekontor.slizaa.server; -import io.codekontor.slizaa.server.service.backend.IBackendService; -import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; import io.codekontor.slizaa.server.service.extensions.IExtension; import io.codekontor.slizaa.server.service.extensions.IExtensionIdentifier; -import io.codekontor.slizaa.server.service.extensions.IExtensionService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.shell.Availability; import org.springframework.shell.standard.*; @@ -31,16 +27,7 @@ @ShellComponent @ShellCommandGroup("Slizaa Backend Commands") -public class SlizaaBackendCommands { - - @Autowired(required = false) - private IModifiableBackendService _modifiableBackendService; - - @Autowired - private IBackendService _backendService; - - @Autowired - private IExtensionService _extensionService; +public class SlizaaBackendCommands extends AbstractGraphDatabaseCommandComponent{ @ShellMethod(value = "List all available backend extensions.", key="listAvailableExtensions") @ShellMethodAvailability("availabilityCheck") @@ -49,8 +36,8 @@ public String listAvailableExtensions() { // StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Available Backend Extensions:\n"); - _extensionService.getExtensions().forEach(extension -> { - stringBuffer.append(format(extension)); + extensionService().getExtensions().forEach(extension -> { + stringBuffer.append(formatExtension(extension)); }); return stringBuffer.toString(); } @@ -60,8 +47,8 @@ public String listInstalledExtensions() { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Installed Backend Extensions:\n"); - _backendService.getInstalledExtensions().forEach(extension -> { - stringBuffer.append(format(extension)); + backendService().getInstalledExtensions().forEach(extension -> { + stringBuffer.append(formatExtension(extension)); }); @@ -73,7 +60,7 @@ public String listInstalledExtensions() { public String installExtensions(String[] extensions) { // fail fast - if (_modifiableBackendService == null) { + if (!hasModifiableBackendService()) { return cannotExecuteCommand("Backend is not modifiable."); } @@ -90,13 +77,13 @@ public String installExtensions(String[] extensions) { } // - List extensionsToInstall = _extensionService.getExtensions(extensionIdList); - _modifiableBackendService.installExtensions(extensionsToInstall); + List extensionsToInstall = extensionService().getExtensions(extensionIdList); + modifiableBackendService().installExtensions(extensionsToInstall); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Installed Backend Extensions:\n"); - _modifiableBackendService.getInstalledExtensions().forEach(extension -> { - stringBuffer.append(format(extension)); + modifiableBackendService().getInstalledExtensions().forEach(extension -> { + stringBuffer.append(formatExtension(extension)); }); @@ -104,31 +91,12 @@ public String installExtensions(String[] extensions) { } public Availability availabilityCheck() { - return _modifiableBackendService != null + return modifiableBackendService() != null ? Availability.available() : Availability.unavailable("an offline backend is not modifiable."); } - /** - * Checks if the backend is configured properly. - * - * @return true if the backend is configured properly. - */ - private String checkBackendConfigured() { - if (!_backendService.hasInstalledExtensions()) { - return cannotExecuteCommand("The Slizaa Server has not been configured properly: There are not installed backend extensions.\n"); - } - return null; - } - - private String cannotExecuteCommand(String msg) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("Can not execute command.\n"); - stringBuffer.append(msg + "\n"); - return stringBuffer.toString(); - } - - private String format(IExtension extension) { + private String formatExtension(IExtension extension) { return String.format(" - %1$s_%2$s (Symbolic name: %1$s, version: %2$s)\n", extension.getSymbolicName(), extension.getVersion()); } } \ No newline at end of file diff --git a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseCommands.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseCommands.java index 8497837..43cd321 100644 --- a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseCommands.java +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseCommands.java @@ -17,31 +17,18 @@ */ package io.codekontor.slizaa.server; -import io.codekontor.slizaa.server.descr.ContentDefinition; -import io.codekontor.slizaa.server.descr.GraphDatabase; -import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; import io.codekontor.slizaa.server.service.slizaa.IGraphDatabase; import io.codekontor.slizaa.server.service.slizaa.IHierarchicalGraph; -import io.codekontor.slizaa.server.service.slizaa.ISlizaaService; -import org.jetbrains.annotations.NotNull; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.shell.standard.ShellCommandGroup; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; import java.io.IOException; -import java.util.Collections; @ShellComponent @ShellCommandGroup("Slizaa Graph Databases Commands") -public class SlizaaGraphDatabaseCommands { - - @Autowired(required = false) - private IModifiableBackendService _modifiableBackendService; - - @Autowired - private ISlizaaService _slizaaService; +public class SlizaaGraphDatabaseCommands extends AbstractGraphDatabaseCommandComponent { @ShellMethod(value = "List all configured graph databases.", key = "listDBs") public String listDBs() { @@ -72,19 +59,14 @@ public String createDB(String identifier) { } // - _slizaaService.newGraphDatabase(identifier); + slizaaService().newGraphDatabase(identifier); // return the result return dumpGraphDatabases(); } - @ShellMethod(value = "List available content definition provider factories.", key = {"listContentDefinitionProviderFactories"}, group = "Slizaa Graph Databases Commands - Content Definition") - public String listContentDefinitionProviderFactories() { - return dumpContentDefinitionProviderFactories(); - } - - @ShellMethod(value = "Define the content that should be analyzed.", key = "setContentDefinitionProvider", group = "Slizaa Graph Databases Commands - Content Definition") - public String setContentDefinitionProvider(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier, @ShellOption({"-f", "--factoryId"}) String contentDefinitionProviderFactoryId, @ShellOption({"-c", "--contentDefinition"}) String contentDefinition) { + @ShellMethod(value = "Delete an existing graph database.", key = "deleteDB") + public String deleteDB(String identifier) { // check the backend configuration String checkBackendConfigured = checkBackendConfigured(); @@ -93,22 +75,23 @@ public String setContentDefinitionProvider(@ShellOption({"-d", "--databaseId"}) } // check that the requested database exists - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(databaseIdentifier); - if (graphDatabase == null) { - return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); + String checkDatabaseExists = checkDatabaseExists(identifier); + if (checkDatabaseExists != null) { + return checkDatabaseExists; } - // TODO: - // _slizaaService.getContentDefinitionProviderFactories(); - // - graphDatabase.setContentDefinitionProvider(contentDefinitionProviderFactoryId, contentDefinition); + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(identifier); + graphDatabase.terminate(); // return the result - return dumpGraphDatabases(); + StringBuffer result = new StringBuffer(); + result.append(String.format("Successfully deleted graph database '%s'.\n", identifier)); + result.append(dumpGraphDatabases()); + return result.toString(); } - @ShellMethod(value = "Parse the definied content.", key = "parseDB", group = "Slizaa Graph Databases Commands - Lifecycle") + @ShellMethod(value = "Parse the definied content.", key = "parseDB") public String parseDB(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier) { // check the backend configuration @@ -118,13 +101,13 @@ public String parseDB(@ShellOption({"-d", "--databaseId"}) String databaseIdenti } // check that the requested database exists - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(databaseIdentifier); + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(databaseIdentifier); if (graphDatabase == null) { return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); } // TODO: - // _slizaaService.getContentDefinitionProviderFactories(); + // slizaaService().getContentDefinitionProviderFactories(); // try { @@ -138,7 +121,7 @@ public String parseDB(@ShellOption({"-d", "--databaseId"}) String databaseIdenti return dumpGraphDatabases(); } - @ShellMethod(value = "Start the specified database.", key = "startDB", group = "Slizaa Graph Databases Commands - Lifecycle") + @ShellMethod(value = "Start the specified database.", key = "startDB") public String startDB(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier) { // check the backend configuration @@ -148,13 +131,13 @@ public String startDB(@ShellOption({"-d", "--databaseId"}) String databaseIdenti } // check that the requested database exists - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(databaseIdentifier); + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(databaseIdentifier); if (graphDatabase == null) { return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); } // TODO: - // _slizaaService.getContentDefinitionProviderFactories(); + // slizaaService().getContentDefinitionProviderFactories(); // graphDatabase.start(); @@ -163,7 +146,7 @@ public String startDB(@ShellOption({"-d", "--databaseId"}) String databaseIdenti return dumpGraphDatabases(); } - @ShellMethod(value = "Stop the specified database.", key = "stopDB", group = "Slizaa Graph Databases Commands - Lifecycle") + @ShellMethod(value = "Stop the specified database.", key = "stopDB") public String stopDB(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier) { // check the backend configuration @@ -173,13 +156,13 @@ public String stopDB(@ShellOption({"-d", "--databaseId"}) String databaseIdentif } // check that the requested database exists - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(databaseIdentifier); + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(databaseIdentifier); if (graphDatabase == null) { return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); } // TODO: - // _slizaaService.getContentDefinitionProviderFactories(); + // slizaaService().getContentDefinitionProviderFactories(); // graphDatabase.stop(); @@ -187,144 +170,4 @@ public String stopDB(@ShellOption({"-d", "--databaseId"}) String databaseIdentif // return the result return dumpGraphDatabases(); } - - - @ShellMethod(value = "Create a new hierarchical graph.", key = "createHierarchicalGraph", group = "Slizaa Graph Databases Commands - Hierarchical Graphs") - public String createHierarchicalGraph(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier, @ShellOption({"-h", "--hierarchicalGraphId"}) String hierarchicalGraphIdentifier) { - - // check the backend configuration - String checkBackendConfigured = checkBackendConfigured(); - if (checkBackendConfigured != null) { - return checkBackendConfigured; - } - - // check that the requested database exists - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(databaseIdentifier); - if (graphDatabase == null) { - return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); - } - - // check that the hierarchical graph does not exist - IHierarchicalGraph hierarchicalGraph = graphDatabase.getHierarchicalGraph("hierarchicalGraphIdentifier"); - if (hierarchicalGraph != null) { - return cannotExecuteCommand(String.format("The specified hierarchical graph '%s' already exists.\n", hierarchicalGraphIdentifier)); - } - - // - graphDatabase.newHierarchicalGraph(hierarchicalGraphIdentifier); - - // return the result - return dumpGraphDatabases(); - } - - @ShellMethod(value = "Delete an existing graph database.", key = "deleteDB") - public String deleteDB(String identifier) { - - // check the backend configuration - String checkBackendConfigured = checkBackendConfigured(); - if (checkBackendConfigured != null) { - return checkBackendConfigured; - } - - // check that the requested database exists - String checkDatabaseExists = checkDatabaseExists(identifier); - if (checkDatabaseExists != null) { - return checkDatabaseExists; - } - - // - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(identifier); - graphDatabase.terminate(); - - // return the result - StringBuffer result = new StringBuffer(); - result.append(String.format("Successfully deleted graph database '%s'.\n", identifier)); - result.append(dumpGraphDatabases()); - return result.toString(); - - } - - @NotNull - private String dumpGraphDatabases() { - - StringBuffer stringBuffer = new StringBuffer("Graph Databases:\n"); - - if (_slizaaService.hasGraphDatabases()) { - - _slizaaService.getGraphDatabases().forEach(db -> { - - ContentDefinition contentDefinition = - db.getContentDefinition() != null ? - new ContentDefinition( - db.getContentDefinition().getContentDefinitionProviderFactory().getFactoryId(), - db.getContentDefinition().toExternalRepresentation()) : - null; - - GraphDatabase graphDatabase = new GraphDatabase( - db.getIdentifier(), - contentDefinition, - Collections.emptyList(), - db.getState().name(), - db.getPort(), - Collections.emptyList()); - - stringBuffer.append(" - " + graphDatabase.toString() + "\n"); - }); - } else { - stringBuffer.append("No database configured.\n"); - } - - return stringBuffer.toString(); - } - - @NotNull - private String dumpContentDefinitionProviderFactories() { - - StringBuffer stringBuffer = new StringBuffer("Content Definition Provider Factories:\n"); - - if (!_slizaaService.getContentDefinitionProviderFactories().isEmpty()) { - _slizaaService.getContentDefinitionProviderFactories().forEach(factory -> { - stringBuffer.append(" - " + factory.getFactoryId() + "\n"); - }); - } else { - stringBuffer.append("No Content Definition Provider Factories available.\n"); - } - - return stringBuffer.toString(); - } - - /** - * Checks if the backend is configured properly. - * - * @return true if the backend is configured properly. - */ - private String checkBackendConfigured() { - if (!_slizaaService.getBackendService().hasInstalledExtensions()) { - return cannotExecuteCommand("The Slizaa Server has not been configured properly: There are not installed backend extensions.\n"); - } - return null; - } - - private String checkDatabaseExists(String identifier) { - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(identifier); - if (graphDatabase == null) { - return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", identifier)); - } - return null; - } - - private String checkDatabaseDoesNotExist(String identifier) { - IGraphDatabase graphDatabase = _slizaaService.getGraphDatabase(identifier); - if (graphDatabase != null) { - return cannotExecuteCommand(String.format("The specified database '%s' already exists.\n", identifier)); - } - return null; - } - - private String cannotExecuteCommand(String msg) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("Can not execute command.\n"); - stringBuffer.append(msg + "\n"); - return stringBuffer.toString(); - } } \ No newline at end of file diff --git a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseContentDefinitionCommands.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseContentDefinitionCommands.java new file mode 100644 index 0000000..b9666c1 --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseContentDefinitionCommands.java @@ -0,0 +1,60 @@ +/** + * slizaa-server-main - Slizaa Static Software Analysis Tools + * Copyright © 2019 Code-Kontor GmbH and others (slizaa@codekontor.io) + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + *

+ * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package io.codekontor.slizaa.server; + +import io.codekontor.slizaa.server.service.slizaa.IGraphDatabase; +import io.codekontor.slizaa.server.service.slizaa.IHierarchicalGraph; +import org.springframework.shell.standard.ShellCommandGroup; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellOption; + +@ShellComponent +@ShellCommandGroup("Slizaa Graph Databases Commands") +public class SlizaaGraphDatabaseContentDefinitionCommands extends AbstractGraphDatabaseCommandComponent { + + @ShellMethod(value = "List available content definition provider factories.", key = {"listContentDefinitionProviderFactories"}, group = "Slizaa Graph Databases Commands - Content Definition") + public String listContentDefinitionProviderFactories() { + return dumpContentDefinitionProviderFactories(); + } + + @ShellMethod(value = "Define the content that should be analyzed.", key = "setContentDefinitionProvider", group = "Slizaa Graph Databases Commands - Content Definition") + public String setContentDefinitionProvider(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier, @ShellOption({"-f", "--factoryId"}) String contentDefinitionProviderFactoryId, @ShellOption({"-c", "--contentDefinition"}) String contentDefinition) { + + // check the backend configuration + String checkBackendConfigured = checkBackendConfigured(); + if (checkBackendConfigured != null) { + return checkBackendConfigured; + } + + // check that the requested database exists + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(databaseIdentifier); + if (graphDatabase == null) { + return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); + } + + // TODO: + // slizaaService().getContentDefinitionProviderFactories(); + + // + graphDatabase.setContentDefinitionProvider(contentDefinitionProviderFactoryId, contentDefinition); + + // return the result + return dumpGraphDatabases(); + } +} \ No newline at end of file diff --git a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseHierarchicalGraphCommands.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseHierarchicalGraphCommands.java new file mode 100644 index 0000000..f301533 --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseHierarchicalGraphCommands.java @@ -0,0 +1,86 @@ +/** + * slizaa-server-main - Slizaa Static Software Analysis Tools + * Copyright © 2019 Code-Kontor GmbH and others (slizaa@codekontor.io) + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + *

+ * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package io.codekontor.slizaa.server; + +import io.codekontor.slizaa.server.service.slizaa.IGraphDatabase; +import io.codekontor.slizaa.server.service.slizaa.IHierarchicalGraph; +import org.springframework.shell.standard.ShellCommandGroup; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellOption; + +@ShellComponent +@ShellCommandGroup("Slizaa Graph Databases Commands - Hierarchical Graphs") +public class SlizaaGraphDatabaseHierarchicalGraphCommands extends AbstractGraphDatabaseCommandComponent { + + @ShellMethod(value = "Create a new hierarchical graph.", key = "createHierarchicalGraph", group = "Slizaa Graph Databases Commands - Hierarchical Graphs") + public String createHierarchicalGraph(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier, @ShellOption({"-h", "--hierarchicalGraphId"}) String hierarchicalGraphIdentifier) { + + // check the backend configuration + String checkBackendConfigured = checkBackendConfigured(); + if (checkBackendConfigured != null) { + return checkBackendConfigured; + } + + // check that the requested database exists + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(databaseIdentifier); + if (graphDatabase == null) { + return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); + } + + // check that the hierarchical graph does not exist + IHierarchicalGraph hierarchicalGraph = graphDatabase.getHierarchicalGraph("hierarchicalGraphIdentifier"); + if (hierarchicalGraph != null) { + return cannotExecuteCommand(String.format("The specified hierarchical graph '%s' already exists.\n", hierarchicalGraphIdentifier)); + } + + // new hierarchical graph + graphDatabase.newHierarchicalGraph(hierarchicalGraphIdentifier); + + // return the result + return dumpGraphDatabases(); + } + + @ShellMethod(value = "Delete an existing hierarchical graph.", key = "deleteHierarchicalGraph") + public String deleteHierarchicalGraph(@ShellOption({"-d", "--databaseId"}) String databaseIdentifier, @ShellOption({"-h", "--hierarchicalGraphId"}) String hierarchicalGraphIdentifier) { + + // check the backend configuration + String checkBackendConfigured = checkBackendConfigured(); + if (checkBackendConfigured != null) { + return checkBackendConfigured; + } + + // check that the requested database exists + IGraphDatabase graphDatabase = slizaaService().getGraphDatabase(databaseIdentifier); + if (graphDatabase == null) { + return cannotExecuteCommand(String.format("The specified database '%s' does not exist.\n", databaseIdentifier)); + } + + // check that the hierarchical graph does exist + IHierarchicalGraph hierarchicalGraph = graphDatabase.getHierarchicalGraph("hierarchicalGraphIdentifier"); + if (hierarchicalGraph == null) { + return cannotExecuteCommand(String.format("The specified hierarchical graph '%s' does not exist.\n", hierarchicalGraphIdentifier)); + } + + // remove hierarchical graph + graphDatabase.removeHierarchicalGraph(hierarchicalGraphIdentifier); + + // return the result + return dumpGraphDatabases(); + } +} \ No newline at end of file diff --git a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IModifiableBackendService.java b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IModifiableBackendService.java index 330f76f..6b78c6a 100644 --- a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IModifiableBackendService.java +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IModifiableBackendService.java @@ -27,5 +27,5 @@ public interface IModifiableBackendService extends IBackendService { * * @param extensions */ - void installExtensions(List extensions); + boolean installExtensions(List extensions); } diff --git a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/AbstractSlizaaServerBackendImpl.java b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/AbstractSlizaaServerBackendImpl.java index 315a577..bff778f 100644 --- a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/AbstractSlizaaServerBackendImpl.java +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/AbstractSlizaaServerBackendImpl.java @@ -147,7 +147,7 @@ protected final ClassLoader dynamicallyLoadExtensions(List extension * * @param extensionsToInstall */ - protected void updateBackendConfiguration(List extensionsToInstall) { + protected boolean updateBackendConfiguration(List extensionsToInstall) { checkNotNull(extensionsToInstall); @@ -168,8 +168,10 @@ protected void updateBackendConfiguration(List extensionsToInstall) this._dynamicallyLoadedExtensions = newDynamicallyLoadedExtensions; this._dynamicallyLoadedExtensions.initialize(); } + return true; } catch (Exception exception) { LOGGER.error("Could not load extensions.", exception); + return false; } } } diff --git a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/DummyBackendService.java b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/DummyBackendService.java index 72633dd..cc8a881 100644 --- a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/DummyBackendService.java +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/DummyBackendService.java @@ -49,8 +49,9 @@ public List getInstalledExtensions() { } @Override - public void installExtensions(List extensions) { + public boolean installExtensions(List extensions) { // ignore + return false; } @Override diff --git a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/SlizaaServerBackendImpl.java b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/SlizaaServerBackendImpl.java index c99418e..4305c28 100644 --- a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/SlizaaServerBackendImpl.java +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/SlizaaServerBackendImpl.java @@ -82,7 +82,7 @@ public List getInstalledExtensions() { } @Override - public void installExtensions(List extensionsToInstall) { + public boolean installExtensions(List extensionsToInstall) { checkNotNull(extensionsToInstall); @@ -90,7 +90,7 @@ public void installExtensions(List extensionsToInstall) { _backendServiceCallback.beforeInstallExtensions(extensionsToInstall); } - updateBackendConfiguration(extensionsToInstall); + return updateBackendConfiguration(extensionsToInstall); } // protected boolean configureBackendFromDao() { @@ -112,8 +112,11 @@ public void installExtensions(List extensionsToInstall) { // } @Override - protected void updateBackendConfiguration(List extensionsToInstall) { - super.updateBackendConfiguration(extensionsToInstall); - _slizaaServerBackendDao.saveInstalledExtensions(extensionsToInstall); + protected boolean updateBackendConfiguration(List extensionsToInstall) { + boolean result = super.updateBackendConfiguration(extensionsToInstall); + if (result) { + _slizaaServerBackendDao.saveInstalledExtensions(extensionsToInstall); + } + return result; } }