Skip to content

Commit

Permalink
GH-2 moved commands to their own classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerd Wuetherich committed Jun 18, 2019
1 parent e64321b commit 0dbcd96
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* slizaa-server-main - Slizaa Static Software Analysis Tools
* Copyright © 2019 Code-Kontor GmbH and others ([email protected])
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.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 <code>true</code> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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")
Expand All @@ -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();
}
Expand All @@ -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));
});


Expand All @@ -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.");
}

Expand All @@ -90,45 +77,26 @@ public String installExtensions(String[] extensions) {
}

//
List<IExtension> extensionsToInstall = _extensionService.getExtensions(extensionIdList);
_modifiableBackendService.installExtensions(extensionsToInstall);
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(format(extension));
modifiableBackendService().getInstalledExtensions().forEach(extension -> {
stringBuffer.append(formatExtension(extension));
});


return stringBuffer.toString();
}

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 <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();
}

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());
}
}
Loading

0 comments on commit 0dbcd96

Please sign in to comment.