KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > ftp > FtpClientFactory


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.ftp;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.commons.net.SocketFactory;
21
22 import org.apache.commons.net.ftp.FTP;
23 import org.apache.commons.net.ftp.FTPClient;
24 import org.apache.commons.net.ftp.FTPReply;
25 import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
26 import org.apache.commons.vfs.FileSystemException;
27 import org.apache.commons.vfs.FileSystemOptions;
28
29 /**
30  * Create a FtpClient instance
31  *
32  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
33  */

34 public class FtpClientFactory
35 {
36     private FtpClientFactory()
37     {
38     }
39
40     /**
41      * Creates a new connection to the server.
42      */

43     public static FTPClient createConnection(String JavaDoc hostname, int port, String JavaDoc username, String JavaDoc password, String JavaDoc workingDirectory, FileSystemOptions fileSystemOptions) throws FileSystemException
44     {
45         // Determine the username and password to use
46
if (username == null)
47         {
48             username = "anonymous";
49         }
50
51         if (password == null)
52         {
53             password = "anonymous";
54         }
55
56         try
57         {
58             final FTPClient client = new FTPClient();
59
60             FTPFileEntryParserFactory myFactory = FtpFileSystemConfigBuilder.getInstance().getEntryParserFactory(fileSystemOptions);
61             if (myFactory != null)
62             {
63                 client.setParserFactory(myFactory);
64             }
65
66             try
67             {
68                 if(System.getProperty("org.apache.commons.net.socketFactory", null)!=null) {
69                                         
70                     try {
71                     SocketFactory socketFactory = (SocketFactory) Class.forName(System.getProperty("org.apache.commons.net.socketFactory")).newInstance();
72                     client.setSocketFactory(socketFactory);
73                     } catch(Throwable JavaDoc t) {
74                         throw new FileSystemException(t);
75                     }
76                 }
77                 client.connect(hostname, port);
78
79                 int reply = client.getReplyCode();
80                 if (!FTPReply.isPositiveCompletion(reply))
81                 {
82                     throw new FileSystemException("vfs.provider.ftp/connect-rejected.error", hostname);
83                 }
84
85                 // Login
86
if (!client.login(username, password))
87                 {
88                     throw new FileSystemException("vfs.provider.ftp/login.error", new Object JavaDoc[]{hostname, username}, null);
89                 }
90
91                 // Set binary mode
92
if (!client.setFileType(FTP.BINARY_FILE_TYPE))
93                 {
94                     throw new FileSystemException("vfs.provider.ftp/set-binary.error", hostname);
95                 }
96
97                 // Set dataTimeout value
98
Integer JavaDoc dataTimeout = FtpFileSystemConfigBuilder.getInstance().getDataTimeout(fileSystemOptions);
99                 if (dataTimeout != null)
100                 {
101                     client.setDataTimeout(dataTimeout.intValue());
102                 }
103
104                 // Change to root by default
105
// All file operations a relative to the filesystem-root
106
// String root = getRoot().getName().getPath();
107

108                 Boolean JavaDoc userDirIsRoot = FtpFileSystemConfigBuilder.getInstance().getUserDirIsRoot(fileSystemOptions);
109                 if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue()))
110                 {
111                     if (!client.changeWorkingDirectory(workingDirectory))
112                     {
113                         throw new FileSystemException("vfs.provider.ftp/change-work-directory.error", workingDirectory);
114                     }
115                 }
116
117                 Boolean JavaDoc passiveMode = FtpFileSystemConfigBuilder.getInstance().getPassiveMode(fileSystemOptions);
118                 if (passiveMode != null && passiveMode.booleanValue())
119                 {
120                     client.enterLocalPassiveMode();
121                 }
122             }
123             catch (final IOException JavaDoc e)
124             {
125                 if (client.isConnected())
126                 {
127                     client.disconnect();
128                 }
129                 throw e;
130             }
131
132             return client;
133         }
134         catch (final Exception JavaDoc exc)
135         {
136             throw new FileSystemException("vfs.provider.ftp/connect.error", new Object JavaDoc[]{hostname}, exc);
137         }
138     }
139 }
Popular Tags