final String host = HOST_ARG.getValue();
final String user = USERNAME_ARG.getValue();
final String password = PASSWORD_ARG.getValue();
boolean ok = false;
final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
FTPFileSystemType type = fss.getFileSystemType(FTPFileSystemType.ID);
final DeviceManager dm = DeviceUtils.getDeviceManager();
final FTPFSDevice dev = new FTPFSDevice(host, user, password);
dev.setDriver(new FTPFSDriver());
FTPFileSystem fs = null;
try {
dm.register(dev);
fs = type.create(dev, true);
fss.registerFileSystem(fs);
fss.mount(mountPoint.getAbsolutePath(), fs, null);
ok = true;
} catch (Exception ex) {
getError().getPrintStream().println("FTP mount failed: " + ex.getLocalizedMessage());
throw ex;
} finally {
if (!ok) {
try {
// If we failed, try to undo the changes that we managed to make
if (fs != null) {
fss.unregisterFileSystem(dev);
}
dm.unregister(dev);
} catch (Exception ex) {
Logger log = Logger.getLogger(FTPMountCommand.class);
log.fatal("Cannot undo failed mount attempt", ex);