resource = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse resource name into a valid URI: "+e.getMessage());
return false;
}
final Resource res = retrieve(resource);
// display document
if (res != null) {
final String data;
if ("XMLResource".equals(res.getResourceType())) {
data = (String) res.getContent();
} else {
data = new String((byte[]) res.getContent());
}
if (startGUI) {
frame.setEditable(false);
frame.display(data);
frame.setEditable(true);
} else {
final String content = data;
more(content);
}
}
return true;
} else if (args[0].equalsIgnoreCase("find")) {
// search
if (args.length < 2) {
messageln("no query argument found.");
return true;
}
messageln(args[1]);
final long start = System.currentTimeMillis();
result = find(args[1]);
if (result == null) {
messageln("nothing found");
} else {
messageln("found " + result.getSize() + " hits in "
+ (System.currentTimeMillis() - start) + "ms.");
}
nextInSet = 1;
} else if (args[0].equalsIgnoreCase("run")) {
if (args.length < 2) {
messageln("please specify a query file.");
return true;
}
try {
final BufferedReader reader = new BufferedReader(new FileReader(args[1]));
final StringBuilder buf = new StringBuilder();
String nextLine;
while ((nextLine = reader.readLine()) != null) {
buf.append(nextLine);
buf.append(EOL);
}
args[1] = buf.toString();
final long start = System.currentTimeMillis();
result = find(args[1]);
if (result == null) {
messageln("nothing found");
} else {
messageln("found " + result.getSize() + " hits in "
+ (System.currentTimeMillis() - start) + "ms.");
}
nextInSet = 1;
} catch (final Exception e) {
errorln("An error occurred: " + e.getMessage());
}
} else if (args[0].equalsIgnoreCase("show")) {
// show search results
if (result == null) {
messageln("no result set.");
return true;
}
try {
int start = nextInSet;
int count = 1;
if (args.length > 1) {
start = Integer.parseInt(args[1]);
}
if (args.length > 2) {
count = Integer.parseInt(args[2]);
}
final int s = (int) result.getSize();
if (start < 1 || start > s) {
messageln("start offset out of range");
return true;
}
--start;
if (start + count > s) {
count = s - start;
}
nextInSet = start + count + 1;
for (int i = start; i < start + count; i++) {
final Resource r = result.getResource(i);
if (startGUI) {
frame.display((String) r.getContent());
} else {
more((String) r.getContent());
}
}
messageln("displayed items " + (start + 1) + " to "
+ (start + count) + " of " + result.getSize());
} catch (final NumberFormatException nfe) {
errorln("wrong argument");
return true;
}
} else if (args[0].equalsIgnoreCase("mkcol")) {
// create collection
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final XmldbURI collUri;
try {
collUri = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse collection name into a valid URI: "+e.getMessage());
return false;
}
final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) current
.getService("CollectionManagementService", "1.0");
final Collection newCollection = mgtService.createCollection(collUri);
if (newCollection == null) {
messageln("could not create collection.");
} else {
messageln("created collection.");
}
// re-read current collection
current = DatabaseManager.getCollection(properties
.getProperty("uri")
+ path, properties.getProperty("user"), properties
.getProperty("password"));
getResources();
} else if (args[0].equalsIgnoreCase("put")) {
// put a document or directory into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final boolean r = parse(args[1]);
getResources();
return r;
} else if (args[0].equalsIgnoreCase("putzip")) {
// put the contents of a zip archive into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final boolean r = parseZip(args[1]);
getResources();
return r;
} else if (args[0].equalsIgnoreCase("putgz")) {
// put the contents of a zip archive into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final boolean r = parseGZip(args[1]);
getResources();
return r;
} else if (args[0].equalsIgnoreCase("blob")) {
// put a document or directory into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
storeBinary(args[1]);
getResources();
} else if (args[0].equalsIgnoreCase("rm")) {
// remove document
if (args.length < 2) {
messageln("missing argument.");
return true;
}
remove(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("rmcol")) {
// remove collection
if (args.length < 2) {
messageln("wrong argument count.");
return true;
}
final XmldbURI collUri;
try {
collUri = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse collection name into a valid URI: "+e.getMessage());
return false;
}
rmcol(collUri);
// re-read current collection
current = DatabaseManager.getCollection(properties
.getProperty("uri")
+ path, properties.getProperty("user"), properties
.getProperty("password"));
getResources();
} else if (args[0].equalsIgnoreCase("adduser")) {
if (args.length < 2) {
System.err.println("Usage: adduser name");
return true;
}
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")) {