in gateway-shell/src/main/java/org/apache/knox/gateway/shell/commands/DataSourceCommand.java [42:125]
public Object execute(List<String> args) {
Map<String, KnoxDataSource> dataSources =
getDataSources();
if (args.isEmpty()) {
args.add("list");
}
if (args.get(0).equalsIgnoreCase("add")) {
KnoxDataSource ds = new KnoxDataSource(args.get(1),
args.get(2),
args.get(3),
args.get(4));
dataSources.put(ds.getName(), ds);
getVariables().put(KNOXDATASOURCES, dataSources);
persistDataSources();
}
else if (args.get(0).equalsIgnoreCase("remove")) {
if (dataSources == null || dataSources.isEmpty()) {
return "No datasources to remove.";
}
// if the removed datasource is currently selected, unselect it
dataSources.remove(args.get(1));
if (getVariables().get(KNOXDATASOURCE) != null) {
if (args.get(1) != null) {
if (((String)getVariables().get(KNOXDATASOURCE)).equals(args.get(1))) {
System.out.println("unselecting datasource.");
getVariables().put(KNOXDATASOURCE, "");
}
}
else {
System.out.println("Missing datasource name to remove.");
}
}
getVariables().put(KNOXDATASOURCES, dataSources);
persistDataSources();
}
else if (args.get(0).equalsIgnoreCase("list")) {
// valid command no additional work needed though
}
else if(args.get(0).equalsIgnoreCase("select")) {
if (dataSources == null || dataSources.isEmpty()) {
return "No datasources to select from.";
}
KnoxDataSource dsValue = dataSources.get(args.get(1));
Connection conn = getConnectionFromSession(dsValue);
try {
if (conn == null || conn.isClosed()) {
String username = null;
char[] pass = null;
if (dsValue.getAuthnType().equalsIgnoreCase("basic")) {
CredentialCollector dlg;
try {
dlg = login();
} catch (CredentialCollectionException e) {
e.printStackTrace();
return "Error: Credential collection failure.";
}
username = dlg.name();
pass = dlg.chars();
}
try {
getConnection(dsValue, username, new String(pass));
} catch (Exception e) {
e.printStackTrace();
return "Error: Connection creation failure.";
}
}
} catch (SQLException e) {
e.printStackTrace();
}
if (dataSources.containsKey(args.get(1))) {
getVariables().put(KNOXDATASOURCE, args.get(1));
}
KnoxShellTable datasource = new KnoxShellTable();
datasource.title("Knox DataSource Selected");
datasource.header("Name").header("Connect String").header("Driver").header("Authn Type");
datasource.row().value(dsValue.getName()).value(dsValue.getConnectStr()).value(dsValue.getDriver()).value(dsValue.getAuthnType());
return datasource;
}
else {
return "ERROR: unknown datasources command.";
}
return buildTable();
}