Skip to content

Commit

Permalink
GH-2 First draft shell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerd Wuetherich committed Jun 10, 2019
1 parent 588af82 commit fcc9791
Show file tree
Hide file tree
Showing 19 changed files with 684 additions and 27 deletions.
1 change: 1 addition & 0 deletions slizaa-poms/slizaa-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
</properties>
<excludes>
<exclude>LICENSE.txt</exclude>
<exclude>src/main/resources/banner.txt</exclude>
<exclude>**/content-mapstruct.txt</exclude>
<exclude>slizaa-work/**</exclude>
<exclude>app/build/**</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,7 +53,11 @@ public List<ServerExtension> installServerExtensions(List<ServerExtension> serve
List<IExtension> 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()))
Expand Down
5 changes: 5 additions & 0 deletions slizaa-server/slizaa-server-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>slizaa-server-graphql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.codekontor.slizaa</groupId>
<artifactId>slizaa-server-spec</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* slizaa-server-main - Slizaa Static Software Analysis Tools
* Copyright © 2019 Code-Kontor GmbH and others ([email protected])
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* slizaa-server-main - Slizaa Static Software Analysis Tools
* Copyright © 2019 Code-Kontor GmbH and others ([email protected])
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<IExtensionIdentifier> 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<IExtension> 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 <code>true</code> 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* slizaa-server-main - Slizaa Static Software Analysis Tools
* Copyright © 2019 Code-Kontor GmbH and others ([email protected])
*
* 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 <http://www.gnu.org/licenses/>.
*/
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 <code>true</code> 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* slizaa-server-main - Slizaa Static Software Analysis Tools
* Copyright © 2019 Code-Kontor GmbH and others ([email protected])
*
* 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 <http://www.gnu.org/licenses/>.
*/
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# spring.profiles.active=offline

server.compression.enabled=true
server.compression.min-response-size=2048
server.compression.mime-types=application/json,application/xml
Expand Down
Loading

0 comments on commit fcc9791

Please sign in to comment.