mirror of
https://github.com/apache/sqoop.git
synced 2025-05-10 22:13:07 +08:00
SQOOP-2853: Sqoop2: Refactor TableDisplayer to be used in document generation
(Jarek Jarcec Cecho via Abraham Fine)
This commit is contained in:
parent
b024e8cae5
commit
dc39a5aafe
@ -21,12 +21,14 @@
|
||||
import org.apache.sqoop.common.SqoopException;
|
||||
import org.apache.sqoop.shell.core.ShellError;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.codehaus.groovy.tools.shell.IO;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
@ -57,6 +59,18 @@ private ShellEnvironment() {
|
||||
private static boolean interactive = false;
|
||||
private static long pollTimeout = DEFAULT_POLL_TIMEOUT;
|
||||
|
||||
private static TableDisplayer tableDisplayer = new TableDisplayer(new TableDisplayer.TableDisplayerWriter() {
|
||||
@Override
|
||||
public void append(String text) {
|
||||
print(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void newLineAndFlush() {
|
||||
println();
|
||||
}
|
||||
}, TableDisplayer.DEFAULT_STYLE);
|
||||
|
||||
static ResourceBundle resource = ResourceBundle.getBundle(Constants.RESOURCE_NAME, Locale.getDefault());
|
||||
static SqoopClient client = new SqoopClient(getServerUrl());
|
||||
static IO io;
|
||||
@ -226,6 +240,10 @@ public static void print(String format, Object... args) {
|
||||
io.out.printf(format, args);
|
||||
}
|
||||
|
||||
public static void displayTable(List<String> headers, List<String> ...columns) {
|
||||
tableDisplayer.display(headers, columns);
|
||||
}
|
||||
|
||||
// for tests only
|
||||
public static void cleanup() {
|
||||
serverHost = DEFAULT_SERVER_HOST;
|
||||
|
@ -27,7 +27,6 @@
|
||||
import org.apache.sqoop.common.SqoopException;
|
||||
import org.apache.sqoop.model.MConnector;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.*;
|
||||
@ -83,7 +82,7 @@ private void showSummary() {
|
||||
supportedDirections.add(connector.getSupportedDirections().toString());
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, uniqueNames, versions, classes, supportedDirections);
|
||||
displayTable(header, uniqueNames, versions, classes, supportedDirections);
|
||||
}
|
||||
|
||||
private void showConnectors() {
|
||||
|
@ -19,12 +19,8 @@
|
||||
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.OptionBuilder;
|
||||
import org.apache.sqoop.common.Direction;
|
||||
import org.apache.sqoop.model.MConnector;
|
||||
import org.apache.sqoop.model.MJob;
|
||||
import org.apache.sqoop.model.MLink;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import java.text.DateFormat;
|
||||
@ -97,7 +93,7 @@ private void showSummary() {
|
||||
availabilities.add(String.valueOf(job.getEnabled()));
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, ids, names, fromConnectors, toConnectors, availabilities);
|
||||
displayTable(header, ids, names, fromConnectors, toConnectors, availabilities);
|
||||
}
|
||||
|
||||
private void showJobs(String jArg) {
|
||||
|
@ -19,19 +19,13 @@
|
||||
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.OptionBuilder;
|
||||
import org.apache.sqoop.common.SqoopException;
|
||||
import org.apache.sqoop.model.MConnector;
|
||||
import org.apache.sqoop.model.MLink;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.*;
|
||||
import static org.apache.sqoop.shell.utils.ConfigDisplayer.*;
|
||||
@ -86,7 +80,7 @@ private void showSummary() {
|
||||
availabilities.add(String.valueOf(link.getEnabled()));
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, names, connectorNames, availabilities);
|
||||
displayTable(header, names, connectorNames, availabilities);
|
||||
}
|
||||
|
||||
private void showLinks() {
|
||||
|
@ -22,13 +22,13 @@
|
||||
import org.apache.sqoop.model.MPrincipal;
|
||||
import org.apache.sqoop.model.MRole;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.client;
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.displayTable;
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.resourceString;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@ -70,6 +70,6 @@ private void showPrincipals(MRole role) {
|
||||
types.add(principal.getType());
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, names, types);
|
||||
displayTable(header, names, types);
|
||||
}
|
||||
}
|
||||
|
@ -25,13 +25,13 @@
|
||||
import org.apache.sqoop.model.MResource;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.core.ShellError;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.client;
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.displayTable;
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.resourceString;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@ -108,6 +108,6 @@ private void showPrivileges(MPrincipal principal, MResource resource) {
|
||||
withGrant.add(Boolean.toString(privilege.isWith_grant_option()));
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, actions, resourceNames, resourceTypes, withGrant);
|
||||
displayTable(header, actions, resourceNames, resourceTypes, withGrant);
|
||||
}
|
||||
}
|
||||
|
@ -24,13 +24,13 @@
|
||||
import org.apache.sqoop.model.MRole;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.core.ShellError;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.client;
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.displayTable;
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.resourceString;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@ -83,6 +83,6 @@ private void showRoles(MPrincipal principal) {
|
||||
names.add(role.getName());
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, names);
|
||||
displayTable(header, names);
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,6 @@
|
||||
import org.apache.sqoop.model.MSubmission;
|
||||
import org.apache.sqoop.shell.core.Constants;
|
||||
import org.apache.sqoop.shell.utils.SubmissionDisplayer;
|
||||
import org.apache.sqoop.shell.utils.TableDisplayer;
|
||||
import org.apache.sqoop.validation.Status;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.*;
|
||||
@ -91,7 +90,7 @@ private void showSummary(String jArg) {
|
||||
dates.add(submission.getLastUpdateDate().toString());
|
||||
}
|
||||
|
||||
TableDisplayer.display(header, jnames, eids, status, dates);
|
||||
displayTable(header, jnames, eids, status, dates);
|
||||
}
|
||||
|
||||
private void showSubmissions(String jArg) {
|
||||
|
@ -22,62 +22,154 @@
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.sqoop.shell.ShellEnvironment.*;
|
||||
|
||||
/**
|
||||
* Display table based data
|
||||
*/
|
||||
public class TableDisplayer {
|
||||
|
||||
public static class Style {
|
||||
// Various characters that should be used to "draw" the table's columns, lines, ...
|
||||
String headerLineSeparator;
|
||||
String lineSeparator;
|
||||
boolean linesAfterEachRow;
|
||||
|
||||
// Generated ones
|
||||
String columnLeft;
|
||||
String columnRight;
|
||||
String columnMiddle;
|
||||
|
||||
String lineLeft;
|
||||
String lineRight;
|
||||
String lineMiddle;
|
||||
|
||||
String headerLeft;
|
||||
String headerRight;
|
||||
String headerMiddle;
|
||||
|
||||
public Style(String headerLineSeparator, String lineSeparator, String columnSeparator, String lineColumnSeparator, boolean linesAfterEachRow) {
|
||||
this.headerLineSeparator = headerLineSeparator;
|
||||
this.lineSeparator = lineSeparator;
|
||||
this.linesAfterEachRow = linesAfterEachRow;
|
||||
|
||||
this.columnLeft = columnSeparator + " ";
|
||||
this.columnRight = " " + columnSeparator;
|
||||
this.columnMiddle = " " + columnSeparator + " ";
|
||||
|
||||
this.lineLeft = "+" + lineSeparator;
|
||||
this.lineRight = lineSeparator + "+";
|
||||
this.lineMiddle = lineSeparator + "+" + lineSeparator;
|
||||
|
||||
this.headerLeft = "+" + headerLineSeparator;
|
||||
this.headerRight = headerLineSeparator + "+";
|
||||
this.headerMiddle = headerLineSeparator + "+" + headerLineSeparator;
|
||||
}
|
||||
}
|
||||
|
||||
public static final Style DEFAULT_STYLE = new Style("-", "-", "|", "+", false);
|
||||
public static final Style RST_STYLE = new Style("=", "-", "|", "+", true);
|
||||
|
||||
/**
|
||||
* Interface that this displayer will use to write out formatted text.
|
||||
*/
|
||||
public interface TableDisplayerWriter {
|
||||
/**
|
||||
* Print out addition formatted text to the output
|
||||
*
|
||||
* @param text
|
||||
*/
|
||||
void append(String text);
|
||||
|
||||
/**
|
||||
* Flush and print new line
|
||||
*/
|
||||
void newLineAndFlush();
|
||||
}
|
||||
|
||||
public TableDisplayer(TableDisplayerWriter writer, Style style) {
|
||||
setWriter(writer);
|
||||
this.style = style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writer instance that we should use to write something out
|
||||
*/
|
||||
private TableDisplayerWriter writer;
|
||||
|
||||
/**
|
||||
* Style that should be used to the generated tables
|
||||
*/
|
||||
private Style style;
|
||||
|
||||
/**
|
||||
* Reset the writer if needed.
|
||||
*
|
||||
* @param writer
|
||||
*/
|
||||
public void setWriter(TableDisplayerWriter writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display given columns in nice table structure to given IO object.
|
||||
*
|
||||
* @param headers List of headers
|
||||
* @param columns Array of columns
|
||||
*/
|
||||
public static void display(List<String> headers, List<String> ...columns) {
|
||||
assert headers != null;
|
||||
public void display(List<String> headers, List<String> ...columns) {
|
||||
assert columns != null;
|
||||
assert headers.size() == columns.length;
|
||||
assert columns.length >= 1;
|
||||
assert writer != null;
|
||||
if(headers != null) {
|
||||
assert headers.size() == columns.length;
|
||||
}
|
||||
|
||||
// Count of columns
|
||||
int columnCount = headers.size();
|
||||
int columnCount = columns.length;
|
||||
|
||||
// List of all maximal widths of each column
|
||||
List<Integer> widths = new LinkedList<Integer>();
|
||||
for(int i = 0; i < columnCount; i++) {
|
||||
widths.add(getMaximalWidth(headers.get(i), columns[i]));
|
||||
widths.add(getMaximalWidth(headers != null ? headers.get(i) : null, columns[i]));
|
||||
}
|
||||
|
||||
// First line is border
|
||||
drawLine(widths);
|
||||
|
||||
// Print out header (text is centralised)
|
||||
print("| ");
|
||||
for(int i = 0 ; i < columnCount; i++) {
|
||||
print(StringUtils.center(headers.get(i), widths.get(i), ' '));
|
||||
print((i == columnCount -1) ? " |" : " | ");
|
||||
}
|
||||
println();
|
||||
if(headers != null) {
|
||||
// Print out header (text is centralised)
|
||||
print(style.columnLeft);
|
||||
for (int i = 0; i < columnCount; i++) {
|
||||
print(StringUtils.center(headers.get(i), widths.get(i), ' '));
|
||||
print((i == columnCount - 1) ? style.columnRight : style.columnMiddle);
|
||||
}
|
||||
println();
|
||||
|
||||
// End up header by border
|
||||
drawLine(widths);
|
||||
// End up header by border
|
||||
drawHeaderLine(widths);
|
||||
}
|
||||
|
||||
// Number of rows in the table
|
||||
int rows = getMaximalRows(columns);
|
||||
|
||||
// Print out each row
|
||||
for(int row = 0 ; row < rows; row++) {
|
||||
print("| ");
|
||||
print(style.columnLeft);
|
||||
for(int i = 0 ; i < columnCount; i++) {
|
||||
print(StringUtils.rightPad(columns[i].get(row), widths.get(i), ' '));
|
||||
print((i == columnCount -1) ? " |" : " | ");
|
||||
print((i == columnCount - 1) ? style.columnRight : style.columnMiddle);
|
||||
|
||||
}
|
||||
println();
|
||||
|
||||
if(style.linesAfterEachRow) {
|
||||
drawLine(widths);
|
||||
}
|
||||
}
|
||||
|
||||
// End table by final border
|
||||
drawLine(widths);
|
||||
if(!style.linesAfterEachRow) {
|
||||
drawLine(widths);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,12 +177,19 @@ public static void display(List<String> headers, List<String> ...columns) {
|
||||
*
|
||||
* @param widths List of widths of each column
|
||||
*/
|
||||
private static void drawLine(List<Integer> widths) {
|
||||
private void drawLine(List<Integer> widths) {
|
||||
drawLine(widths, style.lineLeft, style.lineRight, style.lineMiddle, style.lineSeparator);
|
||||
}
|
||||
private void drawHeaderLine(List<Integer> widths) {
|
||||
drawLine(widths, style.headerLeft, style.headerRight, style.headerMiddle, style.headerLineSeparator);
|
||||
}
|
||||
|
||||
private void drawLine(List<Integer> widths, String left, String right, String middle, String line) {
|
||||
int last = widths.size() - 1;
|
||||
print("+-");
|
||||
print(left);
|
||||
for(int i = 0; i < widths.size(); i++) {
|
||||
print(StringUtils.repeat("-", widths.get(i)));
|
||||
print((i == last) ? "-+" : "-+-");
|
||||
print(StringUtils.repeat(line, widths.get(i)));
|
||||
print((i == last) ? right : middle);
|
||||
}
|
||||
println();
|
||||
}
|
||||
@ -102,11 +201,10 @@ private static void drawLine(List<Integer> widths) {
|
||||
* @param column All column values
|
||||
* @return Maximal
|
||||
*/
|
||||
private static int getMaximalWidth(String header, List<String> column) {
|
||||
assert header != null;
|
||||
private int getMaximalWidth(String header, List<String> column) {
|
||||
assert column != null;
|
||||
|
||||
int max = header.length();
|
||||
int max = header != null ? header.length() : 0;
|
||||
|
||||
for(String value : column) {
|
||||
if(value != null && value.length() > max) {
|
||||
@ -123,7 +221,7 @@ private static int getMaximalWidth(String header, List<String> column) {
|
||||
* @param columns Array with all column values
|
||||
* @return
|
||||
*/
|
||||
private static int getMaximalRows(List<String>... columns) {
|
||||
private int getMaximalRows(List<String>... columns) {
|
||||
int max = 0;
|
||||
|
||||
for(List<String> column : columns) {
|
||||
@ -135,7 +233,12 @@ private static int getMaximalRows(List<String>... columns) {
|
||||
return max;
|
||||
}
|
||||
|
||||
private TableDisplayer() {
|
||||
// Instantiation is prohibited
|
||||
private void print(String text) {
|
||||
writer.append(text);
|
||||
}
|
||||
|
||||
private void println() {
|
||||
writer.newLineAndFlush();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user