P. 1
Using Ftp Service With Spring 2 and spring 3

Using Ftp Service With Spring 2 and spring 3


|Views: 4,583|Likes:
Опубликованmartin nad
How to use ftpclient with in the Spring framework. spring 2 and spring 3
and some discation about clean code and quality and dead line.....
How to use ftpclient with in the Spring framework. spring 2 and spring 3
and some discation about clean code and quality and dead line.....

More info:

Published by: martin nad on Mar 29, 2009
Авторские права:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC or read online from Scribd
See more
See less



by Martin Nad martin_m_nad@yahoo.com

This documentation is about mostly to use Spring in right way for example how we can using FTPService inside the Spring

Using Ftp with Spring

For me it is very important if you are using a technology as j2ee and Spring, understand the technology and writing code in right way to adapted with the technology. I had gotten hurry by deadline and ask my college if he had written any ftp-client or not, and he get very happy and send me the ftpclient that he wrote. Recently I read an article on internet and stands there “one good programmer is better than 10 bad programmesr and 5 bad programmer is better than 10 bad programmers”. I’m not agree with this. How you can define a good programmer and bad programmer, i don’t believe we have bad and good programmer, we have experience programmer and not very experience programmer. And if you want to write an application you cannot put how much time that you want to an application. The quality of an application as I said before it’s depending to the size of application and how much time we put on it. And time is mony. And I’m certainly believed that we should get chances to inexperienced programmer to develop them self and get a good or experienced programmer.

But when i looked closed at the codes i found that he didn’t follow the roles of the Spring. I just change some lines and send it back the right way to use FTP-with the Spring to him. I will thank him for his kindness to help me to keep me the dead line in my project. Here it is the file: public class FtpIoImpl implements FtpIo { private static final Logger logger = Logger.getLogger(FtpIoImpl.class); private static final int transferMode = FTP.BINARY_FILE_TYPE; protected FTPClient ftpClient; public void connect(String server, String username, String password) throws IOException {

if (ftpClient != null && ftpClient.isConnected()) { a FTP"); } if (server == null || username == null || password == null) { throw new IOException("No server/username/password server: " + server + " username: " + username + " password: " + password); } ftpClient = new FTPClient(); ftpClient.connect(server); int reply = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) { logger.warn("FTP server " + server + " refused connection"); disconnect(); throw new IOException("FTP server " + server + " refused connection"); } if (!ftpClient.login(username, password)) { logger.warn("FTP server " + server + " didnt throw new IOException("Already connected to

accept login with username " + username + " and password " + password); disconnect(); throw new IOException("FTP server " + server + " didnt accept login with username " + username + " and password " + password); } ftpClient.setFileType(FtpIoImpl.transferMode); ftpClient.enterLocalPassiveMode(); } public void uploadFile(String filename, String content) throws IOException { if (ftpClient == null || !ftpClient.isConnected()) { throw new IOException("Try to send file without having a connection"); } OutputStream os = ftpClient.storeFileStream(filename); if (os != null) { OutputStreamWriter dos = null; try { dos = new OutputStreamWriter(os);

dos.write( content, 0, content.length()); dos.flush(); } finally { if (dos != null) { dos.close(); } } boolean result = ftpClient.completePendingCommand(); if (result) { return; } else { sending a file"); logger.info("Something went wrong when throw new IOException("Something went wrong when sending a file"); } } logger.info("Could not create file"); throw new IOException("Could not create file");

} public void disconnect() throws IOException { if (ftpClient != null && ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } }

That was the whole ftpClient.... And There are lot of the refactoring to do with file. But the important thing, as i said before, to understand the technology and use the technology in the rigth way. As 12 years experiments as software developer and develop lot of (more then 100 different application) small (3000-10000 lines, and) application and medium (up to 500 000lines) , and develop part of the big applications and using different programming languages mostly Java, Perl, Php, both area as backend and frontend programmer, I think that we have practical difficulty too keep a program clean and with well design and with heigh quality when we have just very limited time. You can put how much time to keep to write a very good application. But in real-time you haven’t so much time, there is dead line to keep it. As we know to write a high quality code, needs time, and time cost money.

But how much you should spend time to keep a good quality of an application is mostly about the size of the application and length of life of the application and how much your application should be flexible. It can be a small application but it needs a wide flexibility to changing standing and updating standing the inputs parameters. And it can be a medium application but you don't need too much flexibility on your application, and you don’t change it standing. It is just with your excrement, you can conclude how much flexibility the application needs and absolutely the budget has big effect on it. And as my experience you should more time to keep a good quality of your code, because soon or later you should change, debug, and refractory your application. Here you can get benefit of quality of your code. if you have written in right way, it should take lesser time to apply changing and debug your code and even refactory your application. As my experience the mostly time in a software-life length, it is just about debugging and changing. This part take most of the time. And it is better you keep your code with relative good quality, to easier change and debug and refactory. Application context Now back to the our originally discussion.... The ftpClient should declare in your application-context as follows: <!-- ftpService -->

<bean id="ftpService" class=“org.martin.service.utils.impl.FtpImpl"> <property name="serverName" value="${ftp.serverName}" /> <property name="loginName" value="${ftp.loginName}" /> <property name="password" value="${ftp.passwd}" /> </bean> Pom.xml <ftp.loginName>login_name</ftp.loginName> <ftp.serverName>ftp.martin.com</ftp.serverName> <ftp.passwd>somepassword</ftp.passwd> If you have several environment as i have, local, test, staging, production,You can easily define your profile and have different login-name, servername, password for example: <profile> <id>prod</id> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <outputDirectory>dist/prod</outputDirectory> </configuration> </plugin> </plugins>

</build> <properties> <db.user>dbusername</db.user> <db.url>jdbc:mysql://martin/test</db.url> <db.passwd>mypassword</db.passwd> <ftp.loginName>test</ftp.loginName> <ftp.serverName>ftp2maritn.se</ftp.serverName> <ftp.passwd>ftppassword</ftp.passwd> </properties> </profile> (You can read more about POM and MAVEN in my documentation about MAVEN2.) Exactly in the same way you do for the database or other properties. And now you should have a interface and implement of your ftpclientservice as follows.

import java.io.IOException;

/** * @author martin nad * */ public interface FTPClientService { public void putFile(String path, String fileName) throws IOException; public void putFileContent(String filename, String content) throws IOException; } And you can by yourself write the implementation of this ftpClientService by yourself, by look at to this interface and the ftp-file in the beginning of this documentation I have just put those two functions that i needed maybe you want to put more function. I think you can very easy write a new one. But remember that the connection and disconnection should happen inside the implementation You can also download the source code from following repository also an example by Spring version 3:
https://svn2.sliksvn.com/jawepjava_free User: freeone password:onefree15

Properties in Java and Spring by Martin Nad Spring and Cxf by Example

Jboss and Perm Gen Error Using Ftp Service With Spring JunIt How to use Maven ReFactoring Maven and Custome Archetype How to Write Effective Code

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->