}
private static boolean isInSafeMode(FileSystem fs) throws IOException {
if (!(fs instanceof DistributedFileSystem))
return false;
DistributedFileSystem dfs = (DistributedFileSystem)fs;
// So this: if (!dfs.setSafeMode(SafeModeAction.SAFEMODE_GET))
// Becomes this:
Class<?> safeModeAction;
try {
// hadoop 2.0
safeModeAction = Class.forName("org.apache.hadoop.hdfs.protocol.HdfsConstants$SafeModeAction");
} catch (ClassNotFoundException ex) {
// hadoop 1.0
try {
safeModeAction = Class.forName("org.apache.hadoop.hdfs.protocol.FSConstants$SafeModeAction");
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot figure out the right class for Constants");
}
}
Object get = null;
for (Object obj : safeModeAction.getEnumConstants()) {
if (obj.toString().equals("SAFEMODE_GET"))
get = obj;
}
if (get == null) {
throw new RuntimeException("cannot find SAFEMODE_GET");
}
try {
Method setSafeMode = dfs.getClass().getMethod("setSafeMode", safeModeAction);
return (Boolean) setSafeMode.invoke(dfs, get);
} catch (IllegalArgumentException exception) {
/* Send IAEs back as-is, so that those that wrap UnknownHostException can be handled in the same place as similar sources of failure. */
throw exception;
} catch (Exception ex) {