if (startGUI) {
messageln("command not supported in GUI mode. Please use the \"Edit users\" menu option.");
return true;
}
try {
final UserManagementService mgtService = (UserManagementService) current
.getService("UserManagementService", "1.0");
String p1;
String p2;
while (true) {
p1 = console.readLine("password: ", Character.valueOf('*'));
p2 = console.readLine("re-enter password: ", Character.valueOf('*'));
if (p1.equals(p2)) {
break;
}
System.out.println(EOL + "entered passwords differ. Try again...");
}
final UserAider user = new UserAider(args[1]);
user.setPassword(p1);
final String groups = console.readLine("enter groups: ");
final StringTokenizer tok = new StringTokenizer(groups, " ,");
while (tok.hasMoreTokens()) {
final String group = tok.nextToken();
if (group.length() > 0) {
user.addGroup(group);
}
}
mgtService.addAccount(user);
System.out.println("user " + user + " created.");
} catch (final Exception e) {
System.out.println("ERROR: " + e.getMessage());
e.printStackTrace();
}
} else if (args[0].equalsIgnoreCase("users")) {
final UserManagementService mgtService = (UserManagementService) current
.getService("UserManagementService", "1.0");
final Account users[] = mgtService.getAccounts();
System.out.println("User\t\tGroups");
System.out.println("-----------------------------------------");
for (int i = 0; i < users.length; i++) {
System.out.print(users[i].getName() + "\t\t");
final String[] groups = users[i].getGroups();
for (int j = 0; j < groups.length; j++) {
System.out.print(groups[j]);
if (j + 1< groups.length) {
System.out.print(", ");
}
}
System.out.println();
}
} else if (args[0].equalsIgnoreCase("passwd")) {
if (startGUI) {
messageln("command not supported in GUI mode. Please use the \"Edit users\" menu option.");
return true;
}
if (args.length < 2) {
System.out.println("Usage: passwd username");
return true;
}
try {
final UserManagementService mgtService = (UserManagementService) current
.getService("UserManagementService", "1.0");
final Account user = mgtService.getAccount(args[1]);
if (user == null) {
System.out.println("no such user.");
return true;
}
String p1;
String p2;
while (true) {
p1 = console.readLine("password: ", Character.valueOf('*'));
p2 = console.readLine("re-enter password: ", Character.valueOf('*'));
if (p1.equals(p2)) {
break;
}
System.out.println(EOL + "entered passwords differ. Try again...");
}
user.setPassword(p1);
mgtService.updateAccount(user);
properties.setProperty("password", p1);
} catch (final Exception e) {
System.err.println("ERROR: " + e.getMessage());
}
} else if (args[0].equalsIgnoreCase("chmod")) {
if (args.length < 2) {
System.out.println("Usage: chmod [resource] mode");
return true;
}
final Collection temp;
if (args.length == 3) {
System.out.println("trying collection: " + args[1]);
temp = current.getChildCollection(args[1]);
if (temp == null) {
System.out.println(EOL + "trying resource: " + args[1]);
final Resource r = current.getResource(args[1]);
if (r != null) {
final UserManagementService mgtService = (UserManagementService) current
.getService("UserManagementService", "1.0");
mgtService.chmod(r, args[2]);
} else {
System.err.println("Resource " + args[1]
+ " not found.");
}
} else {
final UserManagementService mgtService = (UserManagementService) temp
.getService("UserManagementService", "1.0");
mgtService.chmod(args[2]);
}
} else {
final UserManagementService mgtService = (UserManagementService) current
.getService("UserManagementService", "1.0");
mgtService.chmod(args[1]);
}
// re-read current collection
current = DatabaseManager.getCollection(properties
.getProperty("uri")
+ path, properties.getProperty("user"), properties
.getProperty("password"));
getResources();
} else if (args[0].equalsIgnoreCase("chown")) {
if (args.length < 3) {
System.out
.println("Usage: chown username group [resource]");
return true;
}
final Collection temp;
if (args.length == 4) {
temp = current.getChildCollection(args[3]);
} else {
temp = current;
}
if (temp != null) {
final UserManagementService mgtService = (UserManagementService) temp
.getService("UserManagementService", "1.0");
final Account u = mgtService.getAccount(args[1]);
if (u == null) {
System.out.println("unknown user");
return true;
}
mgtService.chown(u, args[2]);
System.out.println("owner changed.");
getResources();
return true;
}
final Resource res = current.getResource(args[3]);
if (res != null) {
final UserManagementService mgtService = (UserManagementService) current
.getService("UserManagementService", "1.0");
final Account u = mgtService.getAccount(args[1]);
if (u == null) {
System.out.println("unknown user");
return true;
}
mgtService.chown(res, u, args[2]);
getResources();
return true;
}
System.err.println("Resource " + args[3] + " not found.");
} else if (args[0].equalsIgnoreCase("lock") || args[0].equalsIgnoreCase("unlock")) {
if(args.length < 2) {
messageln("Usage: lock resource");
return true;
}
final Resource res = current.getResource(args[1]);
if (res != null) {
final UserManagementService mgtService = (UserManagementService)
current.getService("UserManagementService", "1.0");
final Account user = mgtService.getAccount(properties.getProperty("user", "guest"));
if(args[0].equalsIgnoreCase("lock")) {
mgtService.lockResource(res, user);
} else {
mgtService.unlockResource(res);
}
}
} else if (args[0].equalsIgnoreCase("elements")) {
System.out.println("Element occurrences in collection "
+ current.getName());
System.out
.println("--------------------------------------------"
+ "-----------");
final IndexQueryService service = (IndexQueryService) current
.getService("IndexQueryService", "1.0");
final Occurrences[] elements = service.getIndexedElements(true);
for (int i = 0; i < elements.length; i++) {
System.out
.println(formatString(elements[i].getTerm().toString(),
Integer.toString(elements[i]
.getOccurrences()), 50));
}
return true;
} else if (args[0].equalsIgnoreCase("terms")) {
if (args.length < 3) {
System.out
.println("Usage: terms [xpath] sequence-start sequence-end");
return true;
}
final IndexQueryService service = (IndexQueryService) current
.getService("IndexQueryService", "1.0");
Occurrences[] terms;
if (args.length == 3) {
terms = service.scanIndexTerms(args[1], args[2], true);
} else {
terms = service.scanIndexTerms(args[1], args[2], args[3]);
}
System.out.println("Element occurrences in collection " + current.getName());
System.out.println("-------------------------------------------------------");
for (int i = 0; i < terms.length; i++) {
System.out.println(formatString(terms[i].getTerm().toString(), Integer.toString(terms[i].getOccurrences()), 50));
}
} else if (args[0].equalsIgnoreCase("xupdate")) {
if (startGUI) {
messageln("command not supported in GUI mode.");
return true;
}
String lastLine, command = "";
try {
while (true) {
lastLine = console.readLine("| ");
if (lastLine == null || lastLine.length() == 0) {
break;
}
command += lastLine;
}
} catch (final EOFException e) {
//TODO report error?
} catch (final IOException e) {
//TODO report error?
}
final String xupdate = "<xu:modifications version=\"1.0\" "
+ "xmlns:xu=\"http://www.xmldb.org/xupdate\">"
+ command + "</xu:modifications>";
final XUpdateQueryService service = (XUpdateQueryService) current
.getService("XUpdateQueryService", "1.0");
final long mods = service.update(xupdate);
System.out.println(mods + " modifications processed.");
} else if (args[0].equalsIgnoreCase("map")) {
final StringTokenizer tok = new StringTokenizer(args[1], "= ");
final String prefix;
if (args[1].startsWith("=")) {
prefix = "";
} else {
if (tok.countTokens() < 2) {
messageln("please specify a namespace/prefix mapping as: prefix=namespaceURI");
return true;
}
prefix = tok.nextToken();
}
final String uri = tok.nextToken();
namespaceMappings.put(prefix, uri);
} else if (args[0].equalsIgnoreCase("set")) {
if (args.length == 1) {
properties.list(System.out);
} else {
try {
final StringTokenizer tok = new StringTokenizer(args[1], "= ");
if (tok.countTokens() < 2) {
System.err.println("please specify a key=value pair");
return true;
}
final String key = tok.nextToken();
final String val = tok.nextToken();
properties.setProperty(key, val);
current.setProperty(key, val);
getResources();
} catch (final Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
} else if (args[0].equalsIgnoreCase("shutdown")) {
final DatabaseInstanceManager mgr = (DatabaseInstanceManager) current
.getService("DatabaseInstanceManager", "1.0");
if (mgr == null) {
messageln("Service is not available");
return true;
}
mgr.shutdown();
return true;
} else if (args[0].equalsIgnoreCase("help") || "?".equals(args[0])) {
displayHelp();
} else if (args[0].equalsIgnoreCase("quit")) {
return false;
//XXX:make it pluggable
} else if (havePluggableCommands) {
final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) current.getService("CollectionManagementService", "1.0");
mgtService.runCommand(args);
//****************************************************************
} else {
messageln("unknown command");
return true;
}