package org.jboss.fresh.shell.commands;
import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import java.util.HashMap;
// EX OK
public class SetAttrExe extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SetAttrExe.class);
/**
Sets or defines a new attribute.
*/
private static final String ADD = "-a";
public void process(String exename, String[] params) throws Exception {
log.debug("entered");
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
if (helpRequested()) {
out.println("Usage: setattr [-ex] [--help] [-a] FILE_NAME ATTR_NAME ATTR_VALUE [ATTR_NAME ATTR_VALUE]");
out.println(" ATTR_NAME : name of the attribute to be set.");
out.println(" ATTR_VALUE : new value of the specified attribute.");
out.println(" -a : add the attribute instead of replacing an existing one.");
out.println(" --help : this help.");
out.close();
log.debug("done");
return;
}
VFS vfs = shell.getVFS();
FileName pwd = new FileName(shell.getEnvProperty("PWD"));
FileName path = null;
boolean add = false;
HashMap attrs = new HashMap();
for (int i = 0; i < params.length; i++) {
String param = params[i];
if ((i == 0) && param.equals(ADD))
add = true;
else if (add && (i == 1) || (i == 0)) {
FileName filepath = new FileName(param);
if (filepath.isRelative())
filepath = pwd.absolutize(filepath);
// filepath = vfs.resolve(shell.getUserCtx(), filepath, true);
if (vfs.exists(shell.getUserCtx(), filepath, true)) {
path = filepath;
} else {
if (canThrowEx()) {
throw new RuntimeException(filepath + ": Path does not exist!");
} else {
out.println(filepath + ": Path does not exist!");
}
}
} else {
String name = param;
String value = null;
if (params.length > ++i)
value = params[i];
attrs.put(name, value);
}
}
if (path == null) {
out.println("Usage: setattr [-ex] [-a] FILE NAME VALUE {NAME VALUE}");
out.println(" -a = add attributes insted of set");
} else {
FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
if (add) {
HashMap oldAttrs = info.getAttributes();
oldAttrs.putAll(attrs);
} else
info.setAttributes(attrs);
vfs.updateFile(shell.getUserCtx(), info);
}
out.close();
log.debug("done");
}
}