Skip to content

Commit

Permalink
HDDS-12058. Use CommandLine out/err in GenericCli subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai committed Jan 9, 2025
1 parent a4f4821 commit 652ed6b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ protected void printError(Throwable error) {
//message could be null in case of NPE. This is unexpected so we can
//print out the stack trace.
if (verbose || Strings.isNullOrEmpty(error.getMessage())) {
error.printStackTrace(System.err);
error.printStackTrace(cmd.getErr());
} else {
System.err.println(error.getMessage().split("\n")[0]);
cmd.getErr().println(error.getMessage().split("\n")[0]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public Void call() throws Exception {
versionProvider = HddsVersionProvider.class)
public void generateClusterId() {
commonInit();
System.out.println("Generating new cluster id:");
System.out.println(receiver.generateClusterId());
getCmd().getOut().println("Generating new cluster id:");
getCmd().getOut().println(receiver.generateClusterId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.hadoop.ozone.conf;

import java.io.PrintStream;

import org.apache.hadoop.hdds.cli.GenericCli;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
Expand All @@ -45,31 +43,17 @@
OzoneManagersCommandHandler.class
})
public class OzoneGetConf extends GenericCli {
private final PrintStream out; // Stream for printing command output
private final PrintStream err; // Stream for printing error
private OzoneConfiguration conf;

protected OzoneGetConf(OzoneConfiguration conf) {
this(conf, System.out, System.err);
}

protected OzoneGetConf(OzoneConfiguration conf, PrintStream out,
PrintStream err) {
this.conf = conf;
this.out = out;
this.err = err;
}

void printError(String message) {
err.println(message);
getCmd().getErr().println(message);
}

void printOut(String message) {
out.println(message);
getCmd().getOut().println(message);
}

OzoneConfiguration getConf() {
return this.conf;
return getOzoneConf();
}

public static void main(String[] argv) {
Expand All @@ -79,8 +63,6 @@ public static void main(String[] argv) {
.addAppender(new ConsoleAppender(new PatternLayout("%m%n")));
Logger.getLogger(NativeCodeLoader.class).setLevel(Level.ERROR);

OzoneConfiguration conf = new OzoneConfiguration();
conf.addResource(new OzoneConfiguration());
new OzoneGetConf(conf).run(argv);
new OzoneGetConf().run(argv);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.PicocliException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
Expand Down Expand Up @@ -68,50 +67,25 @@ public final class GenerateOzoneRequiredConfigurations extends GenericCli implem
"template, update Kerberos principal and keytab file before use.")
private boolean genSecurityConf;

/**
* Entry point for using genconf tool.
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
new GenerateOzoneRequiredConfigurations().run(args);
}

@Override
public Void call() throws Exception {
generateConfigurations(path, genSecurityConf);
generateConfigurations();
return null;
}

/**
* Generate ozone-site.xml at specified path.
* @param path
* @throws PicocliException
* @throws JAXBException
*/
public static void generateConfigurations(String path) throws
PicocliException, JAXBException, IOException {
generateConfigurations(path, false);
}

/**
* Generate ozone-site.xml at specified path.
* @param path
* @param genSecurityConf
* @throws PicocliException
* @throws JAXBException
*/
public static void generateConfigurations(String path,
boolean genSecurityConf) throws
PicocliException, JAXBException, IOException {
private void generateConfigurations() throws
JAXBException, IOException {

if (!isValidPath(path)) {
throw new PicocliException("Invalid directory path.");
throw new IllegalArgumentException("Invalid directory path.");
}

if (!canWrite(path)) {
throw new PicocliException("Insufficient permission.");
throw new IllegalArgumentException("Insufficient permission.");
}

OzoneConfiguration oc = new OzoneConfiguration();
Expand Down Expand Up @@ -171,9 +145,9 @@ public static void generateConfigurations(String path,
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(generatedConfig, output);

System.out.println("ozone-site.xml has been generated at " + path);
getCmd().getOut().println("ozone-site.xml has been generated at " + path);
} else {
System.out.printf("ozone-site.xml already exists at %s and " +
getCmd().getOut().printf("ozone-site.xml already exists at %s and " +
"will not be overwritten%n", path);
}

Expand All @@ -182,21 +156,19 @@ public static void generateConfigurations(String path,
/**
* Check if the path is valid directory.
*
* @param path
* @return true, if path is valid directory, else return false
*/
public static boolean isValidPath(String path) {
try {
return Files.isDirectory(Paths.get(path));
} catch (InvalidPathException | NullPointerException ex) {
return Boolean.FALSE;
return false;
}
}

/**
* Check if user has permission to write in the specified path.
*
* @param path
* @return true, if the user has permission to write, else returns false
*/
public static boolean canWrite(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void printError(Throwable errorArg) {
if (omException != null && !isVerbose()) {
// In non-verbose mode, reformat OMExceptions as error messages to the
// user.
System.err.println(String.format("%s %s", omException.getResult().name(),
getCmd().getErr().println(String.format("%s %s", omException.getResult().name(),
omException.getMessage()));
} else {
// Prints the stack trace when in verbose mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public Void call() throws Exception {
isalLoaded = true;
}
}
System.out.println("Native library checking:");
System.out.printf("hadoop: %b %s%n", nativeHadoopLoaded,
getCmd().getOut().println("Native library checking:");
getCmd().getOut().printf("hadoop: %b %s%n", nativeHadoopLoaded,
hadoopLibraryName);
System.out.printf("ISA-L: %b %s%n", isalLoaded, isalDetail);
getCmd().getOut().printf("ISA-L: %b %s%n", isalLoaded, isalDetail);

// Attempt to load the rocks-tools lib
boolean nativeRocksToolsLoaded = NativeLibraryLoader.getInstance().loadLibrary(
Expand All @@ -70,7 +70,7 @@ public Void call() throws Exception {
if (nativeRocksToolsLoaded) {
rocksToolsDetail = NativeLibraryLoader.getJniLibraryFileName();
}
System.out.printf("rocks-tools: %b %s%n", nativeRocksToolsLoaded, rocksToolsDetail);
getCmd().getOut().printf("rocks-tools: %b %s%n", nativeRocksToolsLoaded, rocksToolsDetail);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
*/
package org.apache.hadoop.ozone.conf;

import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
import org.apache.hadoop.ozone.om.OMConfigKeys;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -32,22 +30,23 @@
import java.io.UnsupportedEncodingException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests the ozone getconf command.
*/
public class TestGetConfOptions {
private static OzoneConfiguration conf;
private static ByteArrayOutputStream bout;
private static PrintStream psBackup;
private static final String DEFAULT_ENCODING = UTF_8.name();
private static OzoneGetConf subject;

@BeforeAll
public static void init() throws UnsupportedEncodingException {
conf = new OzoneConfiguration();
conf.set(OMConfigKeys.OZONE_OM_NODE_ID_KEY, "1");
conf.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, "service1");
conf.set(ScmConfigKeys.OZONE_SCM_NAMES, "localhost");
subject = new OzoneGetConf();
subject.getConf().set(OMConfigKeys.OZONE_OM_NODE_ID_KEY, "1");
subject.getConf().set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, "service1");
subject.getConf().set(ScmConfigKeys.OZONE_SCM_NAMES, "localhost");
psBackup = System.out;
bout = new ByteArrayOutputStream();
PrintStream psOut = new PrintStream(bout, false, DEFAULT_ENCODING);
Expand All @@ -67,32 +66,30 @@ public static void tearDown() {
@Test
public void testGetConfWithTheOptionConfKey()
throws UnsupportedEncodingException {
new OzoneGetConf(conf)
.run(new String[] {"-confKey", ScmConfigKeys.OZONE_SCM_NAMES});
subject.run(new String[] {"-confKey", ScmConfigKeys.OZONE_SCM_NAMES});
assertEquals("localhost\n", bout.toString(DEFAULT_ENCODING));
bout.reset();
new OzoneGetConf(conf)
.run(new String[] {"confKey", OMConfigKeys.OZONE_OM_NODE_ID_KEY});
subject.run(new String[] {"confKey", OMConfigKeys.OZONE_OM_NODE_ID_KEY});
assertEquals("1\n", bout.toString(DEFAULT_ENCODING));
}

@Test
public void testGetConfWithTheOptionStorageContainerManagers()
throws UnsupportedEncodingException {
new OzoneGetConf(conf).run(new String[] {"-storagecontainermanagers"});
subject.execute(new String[] {"-storagecontainermanagers"});
assertEquals("localhost\n", bout.toString(DEFAULT_ENCODING));
bout.reset();
new OzoneGetConf(conf).run(new String[] {"storagecontainermanagers"});
subject.execute(new String[] {"storagecontainermanagers"});
assertEquals("localhost\n", bout.toString(DEFAULT_ENCODING));
}

@Test
public void testGetConfWithTheOptionOzoneManagers()
throws UnsupportedEncodingException {
new OzoneGetConf(conf).run(new String[] {"-ozonemanagers"});
subject.execute(new String[] {"-ozonemanagers"});
assertEquals("", bout.toString(DEFAULT_ENCODING));
bout.reset();
new OzoneGetConf(conf).run(new String[] {"ozonemanagers"});
subject.execute(new String[] {"ozonemanagers"});
assertEquals("", bout.toString(DEFAULT_ENCODING));
}
}

0 comments on commit 652ed6b

Please sign in to comment.