Search in sources :

Example 16 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class RyaAdminCommands method arePCJCommandsAvailable.

/**
 * Enables commands that are available when the Shell is connected to a Rya Instance that supports PCJ Indexing.
 */
@CliAvailabilityIndicator({ CREATE_PCJ_CMD, DELETE_PCJ_CMD })
public boolean arePCJCommandsAvailable() {
    // The PCJ commands are only available if the Shell is connected to an instance of Rya
    // that is new enough to use the RyaDetailsRepository and is configured to maintain PCJs.
    final ShellState shellState = state.getShellState();
    if (shellState.getConnectionState() == ConnectionState.CONNECTED_TO_INSTANCE) {
        final GetInstanceDetails getInstanceDetails = shellState.getConnectedCommands().get().getGetInstanceDetails();
        final String ryaInstanceName = state.getShellState().getRyaInstanceName().get();
        try {
            final Optional<RyaDetails> instanceDetails = getInstanceDetails.getDetails(ryaInstanceName);
            if (instanceDetails.isPresent()) {
                return instanceDetails.get().getPCJIndexDetails().isEnabled();
            }
        } catch (final RyaClientException e) {
            return false;
        }
    }
    return false;
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) GetInstanceDetails(org.apache.rya.api.client.GetInstanceDetails) RyaDetails(org.apache.rya.api.instance.RyaDetails) CliAvailabilityIndicator(org.springframework.shell.core.annotation.CliAvailabilityIndicator)

Example 17 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class RyaAdminCommands method listInstances.

@CliCommand(value = LIST_INSTANCES_CMD, help = "List the names of the installed Rya instances.")
public String listInstances() {
    // Fetch the command that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient commands = shellState.getConnectedCommands().get();
    final Optional<String> ryaInstance = shellState.getRyaInstanceName();
    try {
        final List<String> instanceNames = commands.getListInstances().listInstances();
        // Return a special message when there are no instances installed.
        if (instanceNames.isEmpty()) {
            return "There are no Rya instances installed on this storage.";
        }
        // Sort the names alphabetically.
        Collections.sort(instanceNames);
        // Return a pretty print of the instances that have been installed.
        final String report;
        final InstanceNamesFormatter formatter = new InstanceNamesFormatter();
        if (ryaInstance.isPresent()) {
            report = formatter.format(instanceNames, ryaInstance.get());
        } else {
            report = formatter.format(instanceNames);
        }
        return report;
    } catch (final RyaClientException e) {
        throw new RuntimeException("Can not list the Rya instances. Reason: " + e.getMessage(), e);
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) InstanceNamesFormatter(org.apache.rya.shell.util.InstanceNamesFormatter) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 18 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class RyaAdminCommands method printInstanceDetails.

@CliCommand(value = PRINT_INSTANCE_DETAILS_CMD, help = "Print information about how the Rya instance is configured.")
public String printInstanceDetails() {
    // Fetch the command that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient commands = shellState.getConnectedCommands().get();
    final String ryaInstance = shellState.getRyaInstanceName().get();
    try {
        final Optional<RyaDetails> details = commands.getGetInstanceDetails().getDetails(ryaInstance);
        if (details.isPresent()) {
            return new RyaDetailsFormatter().format(shellState.getStorageType().get(), details.get());
        } else {
            return "This instance of Rya does not have a Rya Details table. Consider migrating to a newer version of Rya.";
        }
    } catch (final InstanceDoesNotExistException e) {
        throw new RuntimeException(String.format("A Rya instance named '%s' does not exist.", ryaInstance), e);
    } catch (final RyaClientException e) {
        throw new RuntimeException("Could not get the instance details. Reason: " + e.getMessage(), e);
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaDetailsFormatter(org.apache.rya.shell.util.RyaDetailsFormatter) RyaDetails(org.apache.rya.api.instance.RyaDetails) RyaClient(org.apache.rya.api.client.RyaClient) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 19 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class RyaAdminCommands method addUser.

@CliCommand(value = ADD_USER_CMD, help = "Adds an authorized user to the Rya instance.")
public void addUser(@CliOption(key = { "username" }, mandatory = true, help = "The username of the user that will be granted access.") final String username) {
    // Fetch the Rya client that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient ryaClient = shellState.getConnectedCommands().get();
    final String ryaInstance = shellState.getRyaInstanceName().get();
    try {
        ryaClient.getAddUser().get().addUser(ryaInstance, username);
    } catch (final InstanceDoesNotExistException e) {
        throw new RuntimeException(String.format("A Rya instance named '%s' does not exist.", ryaInstance), e);
    } catch (final RyaClientException e) {
        throw new RuntimeException("The user's access could not be granted. Provided reason: " + e.getMessage(), e);
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 20 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class RyaCommands method loadData.

@CliCommand(value = LOAD_DATA_CMD, help = "Loads RDF Statement data from a local file to the connected Rya instance.")
public String loadData(@CliOption(key = { "file" }, mandatory = true, help = "A local file containing RDF Statements that is to be loaded.") final String file, @CliOption(key = { "format" }, mandatory = false, help = "The format of the supplied RDF Statements file. [RDF/XML, N-Triples, Turtle, N3, TriX, TriG, BinaryRDF, N-Quads, JSON-LD, RDF/JSON, RDFa]") final String format) {
    // Fetch the command that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient commands = shellState.getConnectedCommands().get();
    final Optional<String> ryaInstanceName = shellState.getRyaInstanceName();
    try {
        final long start = System.currentTimeMillis();
        // If the provided path is relative, then make it rooted in the user's home.
        // Make sure the path is formatted with Unix style file
        // separators('/') before using it as a regex replacement string.
        // Windows file separators('\') will not work unless escaped.
        final String userHome = FilenameUtils.separatorsToUnix(System.getProperty("user.home"));
        final Path rootedFile = Paths.get(file.replaceFirst("^~", userHome));
        RDFFormat rdfFormat = null;
        // If a format was provided, then go with that.
        if (format != null) {
            rdfFormat = RDFFormat.valueOf(format);
            if (rdfFormat == null) {
                throw new RuntimeException("Unsupported RDF Statement data input format: " + format);
            }
        } else // Otherwise try to figure it out using the filename.
        if (rdfFormat == null) {
            rdfFormat = RDFFormat.forFileName(rootedFile.getFileName().toString());
            if (rdfFormat == null) {
                throw new RuntimeException("Unable to detect RDF Statement data input format for file: " + rootedFile);
            } else {
                consolePrinter.println("Detected RDF Format: " + rdfFormat);
                consolePrinter.flush();
            }
        }
        commands.getLoadStatementsFile().loadStatements(ryaInstanceName.get(), rootedFile, rdfFormat);
        final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0);
        return "Loaded the file: '" + file + "' successfully in " + seconds + " seconds.";
    } catch (final RyaClientException | IOException e) {
        log.error("Error", e);
        throw new RuntimeException("Can not load the RDF Statement data. Reason: " + e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) DecimalFormat(java.text.DecimalFormat) RyaClient(org.apache.rya.api.client.RyaClient) IOException(java.io.IOException) RDFFormat(org.openrdf.rio.RDFFormat) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Aggregations

ShellState (org.apache.rya.shell.SharedShellState.ShellState)24 RyaClient (org.apache.rya.api.client.RyaClient)16 RyaClientException (org.apache.rya.api.client.RyaClientException)15 CliCommand (org.springframework.shell.core.annotation.CliCommand)13 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)9 Test (org.junit.Test)8 IOException (java.io.IOException)6 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)4 RyaDetails (org.apache.rya.api.instance.RyaDetails)3 GetInstanceDetails (org.apache.rya.api.client.GetInstanceDetails)2 InstallConfiguration (org.apache.rya.api.client.Install.InstallConfiguration)2 InstallPrompt (org.apache.rya.shell.util.InstallPrompt)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Bootstrap (org.springframework.shell.Bootstrap)2 CommandResult (org.springframework.shell.core.CommandResult)2 JLineShellComponent (org.springframework.shell.core.JLineShellComponent)2 CliAvailabilityIndicator (org.springframework.shell.core.annotation.CliAvailabilityIndicator)2 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 File (java.io.File)1