provide option to use sudo to read the file
This commit is contained in:
parent
d9a2f6f284
commit
142721a52f
|
@ -2,11 +2,18 @@ package net.hyperic.sigar;
|
||||||
|
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public abstract class FileTail extends FileWatcher {
|
public abstract class FileTail extends FileWatcher {
|
||||||
|
|
||||||
|
public static final String PROP_USE_SUDO =
|
||||||
|
"sigar.tail.sudo";
|
||||||
|
|
||||||
|
private boolean useSudo =
|
||||||
|
"true".equals(System.getProperty(PROP_USE_SUDO));
|
||||||
|
|
||||||
private HashMap offsets = new HashMap();
|
private HashMap offsets = new HashMap();
|
||||||
|
|
||||||
public abstract void tail(FileInfo info, Reader reader);
|
public abstract void tail(FileInfo info, Reader reader);
|
||||||
|
@ -15,13 +22,24 @@ public abstract class FileTail extends FileWatcher {
|
||||||
super(sigar);
|
super(sigar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void useSudo(boolean useSudo) {
|
||||||
|
this.useSudo = useSudo;
|
||||||
|
}
|
||||||
|
|
||||||
public void onChange(FileInfo info) {
|
public void onChange(FileInfo info) {
|
||||||
long len = info.size;
|
long len = info.size;
|
||||||
|
|
||||||
Reader reader = null;
|
Reader reader = null;
|
||||||
|
String name = info.getName();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
reader = new FileReader(info.getName());
|
if (this.useSudo) {
|
||||||
|
reader =
|
||||||
|
new InputStreamReader(new SudoFileInputStream(name));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
reader = new FileReader(name);
|
||||||
|
}
|
||||||
reader.skip(getOffset(info));
|
reader.skip(getOffset(info));
|
||||||
tail(info, reader);
|
tail(info, reader);
|
||||||
setOffset(info);
|
setOffset(info);
|
||||||
|
|
Loading…
Reference in New Issue