1 package com.codehaus.mojo.vfs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.vfs2.FileSystemException;
23 import org.apache.commons.vfs2.FileSystemOptions;
24 import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
25 import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
26 import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder;
27 import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
28 import org.codehaus.plexus.util.StringUtils;
29
30 public class FileSystemOptionsFactory
31 {
32
33 private FtpSettings ftpSettings = new FtpSettings();
34
35 public void setFtpSettings( FtpSettings ftpSettings )
36 {
37 this.ftpSettings = ftpSettings;
38 }
39
40 private SftpSettings sftpSettings = new SftpSettings();
41
42 public void setSftpSettings( SftpSettings sftpSettings )
43 {
44 this.sftpSettings = sftpSettings;
45 }
46
47 public FileSystemOptions getFileSystemOptions( String url, String username, String password )
48 throws FileSystemException
49 {
50
51 FileSystemOptions opts = new FileSystemOptions();
52
53 String[] tokens = StringUtils.split( url, ":" );
54
55 String protocol = tokens[0];
56
57 if ( "ftp".equals( protocol ) )
58 {
59 FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
60 builder.setPassiveMode( opts, ftpSettings.isPassiveMode() );
61 builder.setUserDirIsRoot( opts, ftpSettings.isUserDirIsRoot() );
62 }
63 if ( "sftp".equals( protocol ) )
64 {
65 SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
66 builder.setUserDirIsRoot( opts, sftpSettings.isUserDirIsRoot() );
67 }
68
69 String domain = null;
70 tokens = StringUtils.split( "\\" );
71 if ( tokens.length == 2 )
72 {
73 domain = tokens[0];
74 username = tokens[1];
75 }
76
77 StaticUserAuthenticator auth = new StaticUserAuthenticator( domain, username, password );
78
79 DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator( opts, auth );
80
81 return opts;
82 }
83
84 }