Skip to content

Commit

Permalink
Merge pull request #23720 from arjantijms/clean_db_commands
Browse files Browse the repository at this point in the history
Cleaning of code in the CLI optional package (mostly DB commands)
  • Loading branch information
arjantijms authored Nov 22, 2021
2 parents a7dbf4a + 0fb88b2 commit 445f989
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 484 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,44 +17,46 @@

package com.sun.enterprise.admin.cli.optional;

import java.io.*;
import static com.sun.enterprise.util.Utility.isEmpty;
import static com.sun.enterprise.util.io.DomainDirs.getDefaultDomainsDir;

import java.io.File;
import java.io.IOException;

import org.glassfish.api.admin.*;
import org.glassfish.api.Param;
import com.sun.enterprise.admin.cli.*;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.CommandValidationException;

import com.sun.enterprise.admin.servermgmt.cli.LocalDomainCommand;
import com.sun.enterprise.backup.BackupRequest;
import com.sun.enterprise.util.ObjectAnalyzer;
import com.sun.enterprise.universal.i18n.LocalStringsImpl;

import com.sun.enterprise.util.io.DomainDirs;
import com.sun.enterprise.util.ObjectAnalyzer;

/**
* This is a local command for backing-up domains.
*
* <p>
* The Options:
* <ul>
* <li>domaindir
* </ul>
* <ul>
* <li>domaindir
* </ul>
*
* The Operand:
* <ul>
* <li>domain_name
* </ul>
* <ul>
* <li>domain_name
* </ul>
*/

public abstract class BackupCommands extends LocalDomainCommand {

BackupRequest request;
private static final LocalStringsImpl strings = new LocalStringsImpl(BackupCommands.class);

private static final LocalStringsImpl strings =
new LocalStringsImpl(BackupCommands.class);

@Param(name = "long", shortName="l", alias = "verbose", optional = true)
@Param(name = "long", shortName = "l", alias = "verbose", optional = true)
boolean verbose;

@Param(name = "domain_name", primary = true, optional = true)
String domainName;

@Param(name= "_configonly", optional = true)
@Param(name = "_configonly", optional = true)
String configonly;

@Param(optional = true)
Expand All @@ -62,74 +65,68 @@ public abstract class BackupCommands extends LocalDomainCommand {
@Param(optional = true)
String backupdir;


private String desc = null;

BackupRequest request;
private String description;
private int recycleLimit = 0;

/**
* A method that checks the options and operand that the user supplied.
* These tests are slightly different for different CLI commands
/**
* A method that checks the options and operand that the user supplied. These tests are slightly different for different
* CLI commands
*/
protected void checkOptions() throws CommandException {
if (verbose && programOpts.isTerse())
throw new CommandValidationException(
strings.get("NoVerboseAndTerseAtTheSameTime"));

if (domainDirParam == null || domainDirParam.length() <= 0) {
if (verbose && programOpts.isTerse()) {
throw new CommandValidationException(strings.get("NoVerboseAndTerseAtTheSameTime"));
}

if (isEmpty(domainDirParam)) {
try {

domainDirParam = DomainDirs.getDefaultDomainsDir().getPath();
domainDirParam = getDefaultDomainsDir().getPath();
} catch (IOException ioe) {
throw new CommandException(ioe.getMessage());
}
}

File domainsDirFile = new File(domainDirParam);

// make sure domainsDir exists and is a directory
// Make sure domainsDir exists and is a directory
if (!domainsDirFile.isDirectory()) {
throw new CommandValidationException(
strings.get("InvalidDomainPath", domainDirParam));
throw new CommandValidationException(strings.get("InvalidDomainPath", domainDirParam));
}

// if user hasn't specified domain_name, get the default one

if (domainName == null)
// If user hasn't specified domain_name, get the default one
if (domainName == null) {
domainName = getDomainName();
}

}

protected void setDescription(String d) {
desc = d;
protected void setDescription(String description) {
this.description = description;
}

protected void setBackupDir(String dir) {
backupdir = dir;
protected void setBackupDir(String backupdir) {
this.backupdir = backupdir;
}

protected void setRecycleLimit(int limit) {
recycleLimit = limit;
protected void setRecycleLimit(int recycleLimit) {
this.recycleLimit = recycleLimit;
}

protected void prepareRequest() throws CommandValidationException {

File backupdir_f = null;
File backupdirFile = null;
if (backupdir != null) {
backupdir_f = new File(backupdir);
if (!backupdir_f.isAbsolute()) {
throw new CommandValidationException(
strings.get("InvalidBackupDirPath", backupdir));
backupdirFile = new File(backupdir);
if (!backupdirFile.isAbsolute()) {
throw new CommandValidationException(strings.get("InvalidBackupDirPath", backupdir));
}
}

boolean configonlybackup = false;
if ((configonly != null) && ( Boolean.valueOf(configonly))) {
if (Boolean.valueOf(configonly)) {
configonlybackup = true;
}
request = new BackupRequest(domainDirParam, domainName, backupdir_f,
backupConfig, desc, recycleLimit,configonlybackup);

request = new BackupRequest(domainDirParam, domainName, backupdirFile, backupConfig, description, recycleLimit, configonlybackup);
request.setTerse(programOpts.isTerse());
request.setVerbose(verbose);
}
Expand All @@ -138,11 +135,11 @@ protected void prepareRequest() throws CommandValidationException {
* Method to check if the file is writable directory
*/
protected boolean isWritableDirectory(File domainFile) {
boolean result = false;
if (domainFile.isDirectory() || domainFile.canWrite()) {
result = true;
return true;
}
return result;

return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,15 +17,14 @@

package com.sun.enterprise.admin.cli.optional;

import com.sun.enterprise.admin.cli.remote.RemoteCLICommand;
import java.io.File;

import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandException;
import org.glassfish.hk2.api.PerLookup;
import org.jvnet.hk2.annotations.Service;

import com.sun.enterprise.admin.cli.remote.RemoteCommand;
import com.sun.enterprise.admin.cli.remote.RemoteCLICommand;
import com.sun.enterprise.backup.BackupException;
import com.sun.enterprise.backup.BackupManager;
import com.sun.enterprise.backup.BackupWarningException;
Expand All @@ -33,37 +33,38 @@

/**
* This is a local command for backing-up domains.
*
* <p>
* The Options:
* <ul>
* <li>domaindir
* </ul>
* <ul>
* <li>domaindir
* </ul>
* The Operand:
* <ul>
* <li>domain
* </ul>
* <ul>
* <li>domain
* </ul>
*/
@Service(name = "backup-domain")
@PerLookup
public final class BackupDomainCommand extends BackupCommands {

private static final LocalStringsImpl strings = new LocalStringsImpl(BackupDomainCommand.class);

@Param(name = "description", optional = true)
String description;

@Param(name="_force",optional=true)
@Param(name = "_force", optional = true)
String force;

@Param(name="_recyclelimit",optional=true)
@Param(name = "_recyclelimit", optional = true)
String recycleLimit;

private static final LocalStringsImpl strings =
new LocalStringsImpl(BackupDomainCommand.class);

@Override
protected void validate()
throws CommandException {
// only if domain name is not specified, it should try to find one
if (domainName == null)
protected void validate() throws CommandException {
// Only if domain name is not specified, it should try to find one
if (domainName == null) {
super.validate();
}

checkOptions();

Expand All @@ -73,58 +74,55 @@ protected void validate()
File domainFile = new File(new File(domainDirParam), domainName);

if (!isWritableDirectory(domainFile)) {
throw new CommandException(
strings.get("InvalidDirectory", domainFile.getPath()));
throw new CommandException(strings.get("InvalidDirectory", domainFile.getPath()));
}

if (force == null ) {
if (force == null) {
if (isRunning()) {
boolean suspendAvailable = canSuspend();

if (suspendAvailable && !isSuspended()) {
throw new CommandException(
strings.get("DomainIsNotSuspended", domainName));
} else if (!suspendAvailable) {
throw new CommandException(strings.get("DomainIsNotStopped",
domainName));
throw new CommandException(strings.get("DomainIsNotSuspended", domainName));
}

if (!suspendAvailable) {
throw new CommandException(strings.get("DomainIsNotStopped", domainName));
}
}
}

int limit = 0;
if (recycleLimit != null ) {
if (recycleLimit != null) {
try {
limit = Integer.parseInt(recycleLimit.trim());
} catch (NumberFormatException ex) {
limit = -1;
}
if (limit < 0) {
throw new CommandException(
strings.get("InvalidBackupRecycleLimit", recycleLimit));
throw new CommandException(strings.get("InvalidBackupRecycleLimit", recycleLimit));
}
}

setDescription(description);
setBackupDir(backupdir);
setRecycleLimit(limit);
prepareRequest();
initializeLogger(); // in case program options changed
initializeLogger(); // in case program options changed
}

/**
*/
@Override
protected int executeCommand()
throws CommandException {

protected int executeCommand() throws CommandException {
try {
BackupManager mgr = new BackupManager(request);
logger.info(mgr.backup());
BackupManager backupManager = new BackupManager(request);
logger.info(backupManager.backup());
} catch (BackupWarningException bwe) {
logger.info(bwe.getMessage());
} catch (BackupException be) {
throw new CommandException(be);
}

return 0;
}

Expand All @@ -137,17 +135,15 @@ public String toString() {
* This method determines if the DAS has the ability to suspend itself.
*/
private boolean canSuspend() {

try {
RemoteCLICommand cmd = new RemoteCLICommand("list-commands",
programOpts, env);
RemoteCLICommand cmd = new RemoteCLICommand("list-commands", programOpts, env);
String response = cmd.executeAndReturnOutput("list-commands");

if (response.indexOf("suspend-domain") >= 0)
if (response.indexOf("suspend-domain") >= 0) {
return true;
}
} catch (Exception e) {
logger.info("Exception while probing DAS (list-commands): " +
e.getMessage());
logger.info("Exception while probing DAS (list-commands): " + e.getMessage());
}

return false;
Expand All @@ -157,19 +153,15 @@ private boolean canSuspend() {
* This method determines if the DAS is currently suspended.
*/
private boolean isSuspended() {

try {
RemoteCLICommand cmd = new RemoteCLICommand("suspend-domain",
programOpts, env);
String response = cmd.executeAndReturnOutput("suspend-domain",
"--_test=true");
RemoteCLICommand cmd = new RemoteCLICommand("suspend-domain", programOpts, env);
String response = cmd.executeAndReturnOutput("suspend-domain", "--_test=true");

if (response.indexOf("SUSPENDED=TRUE") >= 0)
return true;

} catch (Exception e) {
logger.info("Exception while probing DAS (suspend-domain): " +
e.getMessage());
logger.info("Exception while probing DAS (suspend-domain): " + e.getMessage());
}

return false;
Expand Down
Loading

0 comments on commit 445f989

Please sign in to comment.