Friday, January 21, 2011

FTP in Java to an IFS folder

A couple of years ago I wrote about uploading files via FTP to a remote server, ending the post reminding about the Commons Net library. Well, the time has come for me to use it, having to upload a file to an IFS folder.

The naive approach didn't work:
@Test
public void basicTest() throws Exception {
URL url = new URL("ftp://user:pass@remoteserver/path/to/remote/folder/" + "outputFile.txt");
URLConnection urlc = url.openConnection();
OutputStream outputStream = urlc.getOutputStream();

String text = "oooooo sooooleee miiiiiiiooooooo.....";
InputStreamReader reader = new InputStreamReader(IOUtils.toInputStream(text));

int mychar = reader.read();
while (mychar != -1) {
outputStream.write(mychar);
mychar = reader.read();
}
reader.close();
outputStream.close();
}
as it returned the "550-Specified library does not exist or cannot be accessed." error. Too bad. After some investigations we found that before we could access the folder we were interested in we had to issue the "cd /" command. All right, I went for the commons:
@Test
public void commonsTest() throws Exception {

FTPClient ftp = new FTPClient();
ftp.connect("remoteserver");
ftp.login("user", "pass");

ftp.changeWorkingDirectory("/");
ftp.changeWorkingDirectory("path/to/remote/folder");

OutputStream outputStream = ftp.storeFileStream("outputFile.txt");

String text = "oooooo sooooleee miiiiiiiooooooo.....";
InputStreamReader reader = new InputStreamReader(IOUtils.toInputStream(text));

int mychar = reader.read();
while (mychar != -1) {
outputStream.write(mychar);
mychar = reader.read();
}

reader.close();
outputStream.close();

ftp.logout();
ftp.disconnect();
}
Obviously this code is simplistic and pretends that errors cannot happen, but it's just to grab the sense of it. We create an instance of the FTPClient class and connect to our remote server (after all we are dealing with sockets... does that ring a bell?), then we use the normal FTP commands to reach the folder we need. After getting an OutputStream, obtained with the storeFileStream(String filename) method, we do everything like in the previous example, then log out and disconnect from the server.

Saturday, January 8, 2011

What should we write about next?

While following a tweet I found an interesting article with links to some post-mortems (the title reads 25 but there are actually 32).

After a little a small form opened up on my screen:


I must admit I wouldn't know what to answer, this being the first time I ever landed onto this site. Yet, I think it is a very good idea, as it can give the authors interesting insights beyond analytics. I mean, numbers can give you hints (you can stop blogging about foo and bar as nobody ever reads that stuff) but nothing is as worth as an advice by someone that invests her time to follow your rants AND to give you feedback.

If someone is thinking "hey that's my blog, I know what to write next" that's ok. If someone is thinking "hey that's my blog, I know what to write next, I need no hints"... well, they don't need my advice :-)

So... what should we write about next?

Friday, January 7, 2011

Toto's Africa? They need no instruments!

Listen to Perpetuum Jazzile perform Africa!

Where does good code come from?

It is a good question, and apparently several people keep asking it. I found a a visual representation of the problem:


You can find the original here, with many other interesting comics.

The interesting thing is that it seems that coding right and coding fast are like oil and water (or, to quote from the Godfather part three, like money and friendship). But is it true?

At the beginning, it surely is. But, after applying - and applying, and applying, and repeating and repeating all over again - you will actually be able to code right AND fast. That's where katas come in handy. Who knows, you might also be able to exit the "are you done yet" switch before requirements change...

Monday, January 3, 2011

The Best Way to Use the Last Five Minutes of Your Day

The title refers to this very interesting article by Peter Bregman. The core of the post is clear, and it is the importance of retrospectives, which is well known by agile pratictioners.

The sentence that struck me the most is the following one:

I was once asked: if an organization could teach only one thing to its employees, what single thing would have the most impact? My answer was immediate and clear: teach people how to learn.


It was not the first time I read something like this, yet it was more evident to me because it perfectly agrees with what the Buzans say in How to Mind Map, which I've just finished studying (I should actually say "which I've just begun studying").

This also goes hand in hand with Tom Peters, that says that each organization should work to better the condition of its employees rather than trying to gain success, which is bound to come anyway if they followed this simple rule. He also presents a certain body of evidence, so he cannot possibly have gone nuts. All the contrary.

Is your organization teaching its employees how to learn? If it is, you're a lucky guy. If it's not, you can start looking for another company, or start working on the issue yourself. Or both.

In any case, that's not an excuse: the ultimate responsible for your education is you.

The Speed Camera Lottery

This is a funny way to try to educate people to respect speed limits:



I doubt that our administrations would allow such an initiative, that would deprive their pockets; yet I wish it were feasible, as there are far too many victims on our roads.