View Javadoc

1   /**************************************************************************
2   Copyright 2005 Webstersmalley
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  /*
17   * Created on 04-Jun-2005
18   */
19  package com.webstersmalley.picweb.offline.deploy;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.net.ftp.FTPClient;
28  import org.apache.commons.net.ftp.FTPFile;
29  import org.apache.commons.net.ftp.FTPReply;
30  
31  /***
32   * Implements an FTP deployer.
33   * 
34   * @author Matthew Smalley
35   */
36  public class FTPDeployer implements Deployer
37  {
38  	/*** Logger for the class. */
39  	private static Log log = LogFactory.getLog(FTPDeployer.class);
40  			
41  	private FTPClient ftp = new FTPClient();
42  	
43  	public FTPDeployer(String hostname, int port, String remoteDir, String username, String password)
44  	{
45  		this.hostname = hostname;
46  		this.port = port;
47  		this.remoteDir = remoteDir;
48  		this.username = username;
49  		this.password = password;
50  	}
51  
52  	private String hostname;
53  	private int port;
54  	private String remoteDir;
55  	private String username;
56  	private String password;
57  	
58  	public void deploy(String baseDir) throws IOException
59  	{
60  		deploy(baseDir, false);
61  	}
62  	
63  	public void deploy(String baseDir, boolean clean) throws IOException
64  	{
65  		try
66  		{
67  			ftp.connect(hostname, port);
68  			log.debug("Connected to: " + hostname + ":" + port);
69  			ftp.login(username, password);
70  			int reply = ftp.getReplyCode();
71  			if(!FTPReply.isPositiveCompletion(reply)) 
72  			{
73  				throw new IOException("Error logging onto ftp server. FTPClient returned code: " + reply);
74  			}
75  			log.debug("Logged in");
76  			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
77  			if (clean)
78  			{
79  				deleteDir(remoteDir);
80  			}
81  			storeFolder(baseDir, remoteDir);
82  		}
83  		finally
84  		{
85  			ftp.disconnect();
86  		}
87  	}
88  	
89  	private void storeFolder(String folder, String remotePath) throws IOException
90  	{
91  		log.debug("Storing folder: " + folder + " at remote path: " + remotePath);
92  		ftp.changeWorkingDirectory(remotePath);
93  		File local = new File(folder);
94  		if (!local.isDirectory())
95  		{
96  			throw new RuntimeException("Was asked to send a folder, but a file has been given");
97  		}
98  		File[] children = local.listFiles();
99  		for (int i = 0; i < children.length; i++)
100 		{
101 			File child = children[i];
102 			if (child.isDirectory())
103 			{
104 				storeFolder(child.getAbsolutePath(), remotePath + "/" + child.getName());
105 			}
106 			else
107 			{
108 				storeFile(child, remotePath);
109 			}
110 		}
111 	}
112 	
113 	private void storeFile(File f, String remotePath) throws IOException
114 	{
115 		log.debug("Storing file: " + f.getAbsolutePath() + " at remote path: " + remotePath);
116 		makeDirs(remotePath);
117 		ftp.changeWorkingDirectory(remotePath);
118 		FileInputStream fis = new FileInputStream(f);
119 		ftp.storeFile(f.getName(), fis);
120 		fis.close();
121 	}
122 	
123 	private void makeDirs(String remotePath) throws IOException
124 	{
125 		log.debug("Attempting to make directory: " + remotePath);
126 		File dir = new File(remotePath);
127 		String parentPath = dir.getParent();
128 		if (parentPath == null)
129 		{
130 			return;
131 		}
132 		try
133 		{
134 			ftp.changeWorkingDirectory(parentPath);
135 		}
136 		catch (IOException e)
137 		{
138 			makeDirs(parentPath);
139 			ftp.changeWorkingDirectory(parentPath);
140 		}
141 		ftp.makeDirectory(dir.getName());
142 	}
143 	
144 	private void deleteDir(String remotePath) throws IOException
145 	{
146 		log.debug("Deleting: " + remotePath);
147 		try
148 		{
149 			ftp.changeWorkingDirectory(remotePath);
150 		}
151 		catch (IOException e)
152 		{
153 			return;
154 		}
155 		deleteAll();
156 		ftp.changeToParentDirectory();
157 		ftp.removeDirectory(remotePath);
158 	}
159 	
160 	private void deleteAll() throws IOException
161 	{
162 		FTPFile[] children = ftp.listFiles();
163 		for (int i = 0; i < children.length; i++)
164 		{
165 			FTPFile child = children[i];
166 			if (child.isDirectory())
167 			{
168 				ftp.changeWorkingDirectory(child.getName());
169 				deleteAll();
170 				ftp.changeToParentDirectory();
171 				log.debug("Removing directory: " + child.getName());
172 				ftp.removeDirectory(child.getName());
173 			}
174 			else if (child.isFile())
175 			{
176 				log.debug("Removing file: " + child.getName());
177 				ftp.deleteFile(child.getName());
178 			}
179 		}
180 	}
181 	
182 	public static void main(String[] args)
183 	{
184 
185 	}
186 }