You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Frazer Irving <su...@clumsyjedi.com> on 2012/09/25 14:12:00 UTC

Thread safety of plain FTP VFS using UserAuthenticator

Hi,

I have the following code:

@Autowired
public DeltaBackend(FileSystemManager fsManager, CredentialsRegistry
credentialsRegistry,
                        @Value("${delta.ftp.hostname}") String hostname,
                        @Value("${delta.ftp.port}") long port)  {
        this.fsOptions = new FileSystemOptions();
        this.credentialsRegistry = credentialsRegistry;
        this.hostname = hostname;
        this.port = port;

        FtpFileSystemConfigBuilder.getInstance().setPassiveMode(fsOptions,
true);

FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fsOptions, false);

        this.fsManager = fsManager;

}


    private FileObject getFileObjectForUri (URI uri) throws IOException {
        try {
            String profile = uri.getHost();
            Credentials creds = credentialsRegistry.get(profile);
            UserAuthenticator auth = new
StaticUserAuthenticator(AUTH_DOMAIN, creds.username, creds.password);

DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(fsOptions,
auth);

            URI ftpUri = new URI(String.format(ftpUriTemplate, hostname,
port, uri.getPath()));
            return fsManager.resolveFile(ftpUri.toString(), fsOptions);
        } catch (URISyntaxException e) {
            throw new IllegalStateException(e);
        }
    }

Which I expect to be run within many concurrent threads. DeltaBackend is a
singleton. fsManager is created by spring by way of VFS.getManager()

The code above works in single threaded mode, but I'm wondering if it's
thread safe. At a glance I would think the whole method may benefit from
being synchronized. Any thoughts?