From fcc9791d3e7518605d62a6b52c0ba56a08c10794 Mon Sep 17 00:00:00 2001 From: Gerd Wuetherich Date: Mon, 10 Jun 2019 22:21:48 +0200 Subject: [PATCH] GH-2 First draft shell commands --- slizaa-poms/slizaa-parent/pom.xml | 1 + .../serverconfig/ServerConfigMutation.java | 8 +- slizaa-server/slizaa-server-main/pom.xml | 5 + .../slizaa/server/ExtensionIdentifier.java | 44 ++++ .../slizaa/server/SlizaaBackendCommands.java | 124 +++++++++++ .../server/SlizaaGraphDatabaseCommands.java | 119 ++++++++++ .../codekontor/slizaa/server/SlizaaQuit.java | 49 ++++ .../src/main/resources/application.properties | 2 + .../src/main/resources/banner.txt | 13 ++ .../service/backend/IBackendService.java | 10 - .../backend/IModifiableBackendService.java | 31 +++ .../backend/impl/DummyBackendService.java | 8 +- .../impl/OfflineSlizaaServerBackendImpl.java | 209 ++++++++++++++++++ .../backend/impl/SlizaaServerBackendImpl.java | 10 +- .../ServerBackend_InstallExtensionsTest.java | 2 +- .../slizaa/internal/SlizaaServiceImpl.java | 9 +- .../slizaa/server/descr/ContentDefinition.kt | 20 ++ .../slizaa/server/descr/GraphDatabase.kt | 27 +++ .../slizaa/server/descr/HierarchicalGraph.kt | 20 ++ 19 files changed, 684 insertions(+), 27 deletions(-) create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/ExtensionIdentifier.java create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaBackendCommands.java create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseCommands.java create mode 100644 slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaQuit.java create mode 100644 slizaa-server/slizaa-server-main/src/main/resources/banner.txt create mode 100644 slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IModifiableBackendService.java create mode 100644 slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/OfflineSlizaaServerBackendImpl.java create mode 100644 slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/ContentDefinition.kt create mode 100644 slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/GraphDatabase.kt create mode 100644 slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/HierarchicalGraph.kt diff --git a/slizaa-poms/slizaa-parent/pom.xml b/slizaa-poms/slizaa-parent/pom.xml index e011c88..0064e61 100644 --- a/slizaa-poms/slizaa-parent/pom.xml +++ b/slizaa-poms/slizaa-parent/pom.xml @@ -193,6 +193,7 @@ LICENSE.txt + src/main/resources/banner.txt **/content-mapstruct.txt slizaa-work/** app/build/** diff --git a/slizaa-server/slizaa-server-graphql/src/main/java/io/codekontor/slizaa/server/graphql/serverconfig/ServerConfigMutation.java b/slizaa-server/slizaa-server-graphql/src/main/java/io/codekontor/slizaa/server/graphql/serverconfig/ServerConfigMutation.java index 24e5d53..03de655 100644 --- a/slizaa-server/slizaa-server-graphql/src/main/java/io/codekontor/slizaa/server/graphql/serverconfig/ServerConfigMutation.java +++ b/slizaa-server/slizaa-server-graphql/src/main/java/io/codekontor/slizaa/server/graphql/serverconfig/ServerConfigMutation.java @@ -20,6 +20,8 @@ import java.util.List; import java.util.stream.Collectors; +import io.codekontor.slizaa.server.service.backend.IBackendService; +import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -51,7 +53,11 @@ public List installServerExtensions(List serve List extensions = slizaaService.getExtensionService().getExtensions(extensionIds); // ... and install it - slizaaService.getBackendService().installExtensions(extensions); + IBackendService backendService = slizaaService.getBackendService(); + + if (backendService instanceof IModifiableBackendService) { + ((IModifiableBackendService) backendService).installExtensions(extensions); + } // return extensions.stream().map(ext -> new ServerExtension(ext.getSymbolicName(), ext.getVersion().toString())) diff --git a/slizaa-server/slizaa-server-main/pom.xml b/slizaa-server/slizaa-server-main/pom.xml index 5cb6158..907a057 100644 --- a/slizaa-server/slizaa-server-main/pom.xml +++ b/slizaa-server/slizaa-server-main/pom.xml @@ -47,6 +47,11 @@ slizaa-server-graphql ${project.version} + + io.codekontor.slizaa + slizaa-server-spec + ${project.version} + com.graphql-java-kickstart graphql-spring-boot-starter diff --git a/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/ExtensionIdentifier.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/ExtensionIdentifier.java new file mode 100644 index 0000000..c334e57 --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/ExtensionIdentifier.java @@ -0,0 +1,44 @@ +/** + * 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 com.google.common.base.Preconditions; +import io.codekontor.slizaa.server.service.extensions.IExtensionIdentifier; +import io.codekontor.slizaa.server.service.extensions.Version; + +public class ExtensionIdentifier implements IExtensionIdentifier { + + private String symbolicName; + + private Version version; + + public ExtensionIdentifier(String symbolicName, String version) { + this.symbolicName = Preconditions.checkNotNull(symbolicName); + this.version = new Version(Preconditions.checkNotNull(version)); + } + + @Override + public String getSymbolicName() { + return symbolicName; + } + + @Override + public Version getVersion() { + return version; + } +} 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 new file mode 100644 index 0000000..e946431 --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaBackendCommands.java @@ -0,0 +1,124 @@ +/** + * 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.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.standard.ShellCommandGroup; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellOption; + +import java.util.ArrayList; +import java.util.List; + +@ShellComponent +@ShellCommandGroup("Slizaa Backend Commands") +public class SlizaaBackendCommands { + + @Autowired(required = false) + private IModifiableBackendService _modifiableBackendService; + + @Autowired + private IBackendService _backendService; + + @Autowired + private IExtensionService _extensionService; + + @ShellMethod(value = "List all available backend extensions.", key="listAvailableExtensions") + public String listAvailableExtensions() { + + // + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Available Backend Extensions:\n"); + _extensionService.getExtensions().forEach(extension -> { + stringBuffer.append(String.format(" - %1$s_%2$s (Symbolic name: %1$s, version: %2$s)\n", extension.getSymbolicName(), extension.getVersion())); + }); + return stringBuffer.toString(); + } + + @ShellMethod(value = "List installed extensions.", key="listInstalledExtensions") + public String listInstalledExtensions() { + + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Installed Backend Extensions:\n"); + _backendService.getInstalledExtensions().forEach(extension -> { + stringBuffer.append(extension + "\n"); + }); + + + return stringBuffer.toString(); + } + + @ShellMethod(value = "Install backend extensions.", key="installExtensions") + public String installExtensions(@ShellOption({"-e", "--extensions"}) String[] extensions) { + + // fail fast + if (_modifiableBackendService == null) { + return cannotExecuteCommand("Backend is not modifiable."); + } + + // + List extensionIdList = new ArrayList<>(); + + for (int i = 0; i < extensions.length; i++) { + String extension = extensions[i]; + String[] split = extension.split("_"); + if (split.length != 2) { + return cannotExecuteCommand(String.format("Invalid parameter value '%s'.", extension)); + } + extensionIdList.add(new ExtensionIdentifier(split[0], split[1])); + } + + // + List extensionsToInstall = _extensionService.getExtensions(extensionIdList); + _modifiableBackendService.installExtensions(extensionsToInstall); + + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Installed Backend Extensions:\n"); + _modifiableBackendService.getInstalledExtensions().forEach(extension -> { + stringBuffer.append(extension + "\n"); + }); + + + return stringBuffer.toString(); + } + + /** + * 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(); + } +} \ 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 new file mode 100644 index 0000000..336798f --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaGraphDatabaseCommands.java @@ -0,0 +1,119 @@ +/** + * 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.IModifiableBackendService; +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 java.util.Collections; + +@ShellComponent +@ShellCommandGroup("Slizaa Graph Databases Commands") +public class SlizaaGraphDatabaseCommands { + + @Autowired(required = false) + private IModifiableBackendService _modifiableBackendService; + + @Autowired + private ISlizaaService _slizaaService; + + @ShellMethod(value = "List all configured graph databases.", key="listDBs") + public String listDBs() { + + // check the backend configuration + String checkBackendConfigured = checkBackendConfigured(); + if (checkBackendConfigured != null) { + return checkBackendConfigured; + } + + // + return dumpGraphDatabases(); + } + + @ShellMethod(value = "Create a new graph databases.", key="createDB") + public String createDB(String identifier) { + + // check the backend configuration + String checkBackendConfigured = checkBackendConfigured(); + if (checkBackendConfigured != null) { + return checkBackendConfigured; + } + + // + _slizaaService.newGraphDatabase(identifier); + + // return the result + return dumpGraphDatabases(); + + } + + @NotNull + private String dumpGraphDatabases() { + // + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Graph Databases:\n"); + + _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"); + }); + + 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 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/SlizaaQuit.java b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaQuit.java new file mode 100644 index 0000000..6b815eb --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/java/io/codekontor/slizaa/server/SlizaaQuit.java @@ -0,0 +1,49 @@ +/** + * 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 org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.shell.standard.ShellCommandGroup; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import org.springframework.shell.standard.commands.Quit; + +@ShellComponent +@ShellCommandGroup("Built-In Commands") +public class SlizaaQuit implements Quit.Command { + + @Autowired + private ConfigurableApplicationContext applicationContext; + + @ShellMethod( + value = "Exit the shell.", + key = {"quit", "exit"} + ) + public void quit() { + new Thread(() -> { + try { + SpringApplication.exit(applicationContext, () -> 0); + System.exit(0); + } catch (Exception e) { + + } + }).start(); + } +} \ No newline at end of file diff --git a/slizaa-server/slizaa-server-main/src/main/resources/application.properties b/slizaa-server/slizaa-server-main/src/main/resources/application.properties index b79fbcf..0a67b0a 100644 --- a/slizaa-server/slizaa-server-main/src/main/resources/application.properties +++ b/slizaa-server/slizaa-server-main/src/main/resources/application.properties @@ -16,6 +16,8 @@ # along with this program. If not, see . # +# spring.profiles.active=offline + server.compression.enabled=true server.compression.min-response-size=2048 server.compression.mime-types=application/json,application/xml diff --git a/slizaa-server/slizaa-server-main/src/main/resources/banner.txt b/slizaa-server/slizaa-server-main/src/main/resources/banner.txt new file mode 100644 index 0000000..ddf73b8 --- /dev/null +++ b/slizaa-server/slizaa-server-main/src/main/resources/banner.txt @@ -0,0 +1,13 @@ + + _____ _____ ____________ _____ ______ _____ _____ + _____\ \|\ \ / \ / / / /| / |_ / |_ + / / \ |\\ \ |\___/\ \\___/|| |/ / | / \ / \ + | | /___/| \\ \ \|____\ \___|/|\____\\ / / | /\ \ | /\ \ + ____\ \ | || \| | ______ | | \|___|/ / / | | | \ | | | \ + / /\ \|___|/ | |/ \ __ / / __ / /_/____ | \/ \ | \/ \ +| |/ \ \ / | / \/ /_/ | / /\ ||\ /\ \ |\ /\ \ +|\____\ /____/| /_____/\_____/||____________/| /_____/ /_____/|| \_____\ \_____\| \_____\ \_____\ +| | || | | | | | ||| | / | |/| | || | | | || | | | | + \|___||____|/ |______|/|____|/|___________|/ |____| |_____|/ \|_____|\|_____| \|_____|\|_____| + +Copyright © 2019 Code-Kontor GmbH and others (slizaa@codekontor.io) diff --git a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IBackendService.java b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IBackendService.java index 7967193..5cb3de6 100644 --- a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IBackendService.java +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IBackendService.java @@ -38,17 +38,7 @@ public interface IBackendService { */ List getInstalledExtensions(); - /** - * - * @param extensions - */ - void installExtensions(List extensions); - /** - * - * @param extensions - */ - void installAllExtensions(); /** * 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 new file mode 100644 index 0000000..330f76f --- /dev/null +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/IModifiableBackendService.java @@ -0,0 +1,31 @@ +/** + * slizaa-server-service-backend - 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.service.backend; + +import io.codekontor.slizaa.server.service.extensions.IExtension; + +import java.util.List; + +public interface IModifiableBackendService extends IBackendService { + + /** + * + * @param extensions + */ + void installExtensions(List extensions); +} 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 b9712cb..72633dd 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 @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.List; +import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; import org.springframework.core.io.FileSystemResource; import com.google.common.io.ByteStreams; @@ -35,7 +36,7 @@ import io.codekontor.slizaa.server.service.backend.IBackendServiceInstanceProvider; import io.codekontor.slizaa.server.service.extensions.IExtension; -public class DummyBackendService implements IBackendService, IBackendServiceInstanceProvider { +public class DummyBackendService implements IModifiableBackendService, IBackendServiceInstanceProvider { @Override public boolean hasInstalledExtensions() { @@ -52,11 +53,6 @@ public void installExtensions(List extensions) { // ignore } - @Override - public void installAllExtensions() { - // ignore - } - @Override public ClassLoader getCurrentExtensionClassLoader() { return this.getClass().getClassLoader(); diff --git a/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/OfflineSlizaaServerBackendImpl.java b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/OfflineSlizaaServerBackendImpl.java new file mode 100644 index 0000000..2612fc4 --- /dev/null +++ b/slizaa-server/slizaa-server-service-backend/src/main/java/io/codekontor/slizaa/server/service/backend/impl/OfflineSlizaaServerBackendImpl.java @@ -0,0 +1,209 @@ +/** + * slizaa-server-service-backend - 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.service.backend.impl; + +import com.google.common.io.ByteStreams; +import io.codekontor.slizaa.hierarchicalgraph.graphdb.mapping.spi.IMappingProvider; +import io.codekontor.slizaa.scanner.api.cypherregistry.ICypherStatementRegistry; +import io.codekontor.slizaa.scanner.api.graphdb.IGraphDbFactory; +import io.codekontor.slizaa.scanner.api.importer.IModelImporterFactory; +import io.codekontor.slizaa.scanner.spi.parser.IParserFactory; +import io.codekontor.slizaa.server.service.backend.IBackendService; +import io.codekontor.slizaa.server.service.backend.IBackendServiceCallback; +import io.codekontor.slizaa.server.service.backend.IBackendServiceInstanceProvider; +import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; +import io.codekontor.slizaa.server.service.backend.impl.dao.ISlizaaServerBackendDao; +import io.codekontor.slizaa.server.service.extensions.IExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + +/** + *

+ *

+ */ +@Component +@Profile("offline") +public class OfflineSlizaaServerBackendImpl implements IBackendService, IBackendServiceInstanceProvider { + + /* the dynamically loaded extensions */ + private DynamicallyLoadedExtensions _dynamicallyLoadedExtensions; + + @PostConstruct + public void initialize() { + + // + Path workingDirectory = Paths.get("").toAbsolutePath(); + + System.out.println("Offline installation:"); + System.out.println(workingDirectory); + +// // +// if (!installedExtensions.isEmpty()) { +// configureBackendFromDao(); +// } + } + + @Override + public ClassLoader getCurrentExtensionClassLoader() { + return _dynamicallyLoadedExtensions != null ? _dynamicallyLoadedExtensions.getClassLoader() : null; + } + + @Override + public List getInstalledExtensions() { + return Collections.EMPTY_LIST; + } + + @Override + public boolean hasInstalledExtensions() { + return _dynamicallyLoadedExtensions != null; + } + + @Override + public boolean hasModelImporterFactory() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.hasModelImporterFactory(); + } + + @Override + public IModelImporterFactory getModelImporterFactory() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.getModelImporterFactory(); + } + + @Override + public boolean hasGraphDbFactory() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.hasGraphDbFactory(); + } + + @Override + public IGraphDbFactory getGraphDbFactory() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.getGraphDbFactory(); + } + + @Override + public ICypherStatementRegistry getCypherStatementRegistry() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.getCypherStatementRegistry(); + } + + @Override + public List getParserFactories() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.getParserFactories(); + } + + @Override + public List getMappingProviders() { + checkState(hasInstalledExtensions(), "SlizaaServerBackend has no installed extensions."); + return _dynamicallyLoadedExtensions.getMappingProviders(); + } + + @Override + public byte[] loadResourceFromExtensions(String path) { + + if (hasInstalledExtensions()) { + + ClassPathResource imgFile = new ClassPathResource(path, getCurrentExtensionClassLoader()); + + if (imgFile.exists()) { + try { + return ByteStreams.toByteArray(imgFile.getInputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; + } + +// protected boolean configureBackendFromDao() { +// +// try { +// +// DynamicallyLoadedExtensions newDynamicallyLoadedExtensions = new DynamicallyLoadedExtensions( +// dynamicallyLoadExtensions(_slizaaServerBackendDao.getInstalledExtensions())); +// +// this._dynamicallyLoadedExtensions = newDynamicallyLoadedExtensions; +// this._dynamicallyLoadedExtensions.initialize(); +// +// return true; +// +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } +// } + +// protected void updateBackendConfiguration(List extensionsToInstall) { +// +// try { +// +// DynamicallyLoadedExtensions newDynamicallyLoadedExtensions = null; +// +// if (!extensionsToInstall.isEmpty()) { +// newDynamicallyLoadedExtensions = new DynamicallyLoadedExtensions( +// dynamicallyLoadExtensions(extensionsToInstall)); +// } +// +// if (this._dynamicallyLoadedExtensions != null) { +// this._dynamicallyLoadedExtensions.dispose(); +// this._dynamicallyLoadedExtensions = null; +// } +// +// if (!extensionsToInstall.isEmpty()) { +// this._dynamicallyLoadedExtensions = newDynamicallyLoadedExtensions; +// this._dynamicallyLoadedExtensions.initialize(); +// } +// +// _slizaaServerBackendDao.saveInstalledExtensions(extensionsToInstall); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } + + /** + *

+ *

+ * + * @return + */ + private static ClassLoader dynamicallyLoadExtensions(List extensionToDynamicallyLoad) { + + List urlList = checkNotNull(extensionToDynamicallyLoad).stream() + .flatMap(ext -> ext.resolvedArtifactsToInstall().stream()).distinct().collect(Collectors.toList()); + + return new URLClassLoader(urlList.toArray(new URL[0]), OfflineSlizaaServerBackendImpl.class.getClassLoader()); + } +} 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 2e989d9..3110768 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 @@ -29,7 +29,9 @@ import javax.annotation.PostConstruct; +import io.codekontor.slizaa.server.service.backend.IModifiableBackendService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; @@ -51,7 +53,8 @@ *

*/ @Component -public class SlizaaServerBackendImpl implements IBackendService, IBackendServiceInstanceProvider { +@Profile("!offline") +public class SlizaaServerBackendImpl implements IModifiableBackendService, IBackendServiceInstanceProvider { @Autowired private ISlizaaServerBackendDao _slizaaServerBackendDao; @@ -96,11 +99,6 @@ public void installExtensions(List extensionsToInstall) { updateBackendConfiguration(extensionsToInstall); } - @Override - public void installAllExtensions() { - installExtensions(Collections.emptyList()); - } - @Override public boolean hasInstalledExtensions() { return _dynamicallyLoadedExtensions != null; diff --git a/slizaa-server/slizaa-server-service-backend/src/test/java/io/codekontor/slizaa/server/service/backend/ServerBackend_InstallExtensionsTest.java b/slizaa-server/slizaa-server-service-backend/src/test/java/io/codekontor/slizaa/server/service/backend/ServerBackend_InstallExtensionsTest.java index 576a42e..9ceb7c1 100644 --- a/slizaa-server/slizaa-server-service-backend/src/test/java/io/codekontor/slizaa/server/service/backend/ServerBackend_InstallExtensionsTest.java +++ b/slizaa-server/slizaa-server-service-backend/src/test/java/io/codekontor/slizaa/server/service/backend/ServerBackend_InstallExtensionsTest.java @@ -41,7 +41,7 @@ public class ServerBackend_InstallExtensionsTest extends AbstractServerBackendTe .withDependency(new MvnDependency("NOT_THERE:NOT_THERE:1.0.0-SNAPSHOT")); // - private IBackendService _backend; + private SlizaaServerBackendImpl _backend; @Before public void before() { diff --git a/slizaa-server/slizaa-server-service-slizaa/src/main/java/io/codekontor/slizaa/server/service/slizaa/internal/SlizaaServiceImpl.java b/slizaa-server/slizaa-server-service-slizaa/src/main/java/io/codekontor/slizaa/server/service/slizaa/internal/SlizaaServiceImpl.java index ff44069..fa91533 100644 --- a/slizaa-server/slizaa-server-service-slizaa/src/main/java/io/codekontor/slizaa/server/service/slizaa/internal/SlizaaServiceImpl.java +++ b/slizaa-server/slizaa-server-service-slizaa/src/main/java/io/codekontor/slizaa/server/service/slizaa/internal/SlizaaServiceImpl.java @@ -227,11 +227,14 @@ public IGraphDatabase newGraphDatabase(String identifier) { throw new RuntimeException(); } - // + // create the result + IGraphDatabase result = createStructureDatabaseIfAbsent(identifier, SlizaaSocketUtils.findAvailableTcpPort()); + + // store the configuration storeConfig(); - // - return createStructureDatabaseIfAbsent(identifier, SlizaaSocketUtils.findAvailableTcpPort()); + // and return the result + return result; } @Override diff --git a/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/ContentDefinition.kt b/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/ContentDefinition.kt new file mode 100644 index 0000000..31a3f67 --- /dev/null +++ b/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/ContentDefinition.kt @@ -0,0 +1,20 @@ +/** + * slizaa-server-spec - 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.descr + +data class ContentDefinition(val type: String, val definition: String) \ No newline at end of file diff --git a/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/GraphDatabase.kt b/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/GraphDatabase.kt new file mode 100644 index 0000000..bd9eb5e --- /dev/null +++ b/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/GraphDatabase.kt @@ -0,0 +1,27 @@ +/** + * slizaa-server-spec - 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.descr + +data class GraphDatabase( + val identifier: String, + val contentDefinition: ContentDefinition?, + val hierarchicalGraphSpec: List, + val state: String, + val port : Int, + val availableActions : List) { +} \ No newline at end of file diff --git a/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/HierarchicalGraph.kt b/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/HierarchicalGraph.kt new file mode 100644 index 0000000..619a3fe --- /dev/null +++ b/slizaa-server/slizaa-server-spec/src/main/kotlin/io/codekontor/slizaa/server/descr/HierarchicalGraph.kt @@ -0,0 +1,20 @@ +/** + * slizaa-server-spec - 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.descr + +data class HierarchicalGraph(val identifier: String) \ No newline at end of file