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.

Thursday, December 30, 2010

Don't work more: work better

Sustainable pace is one of the practices of XP; having provided the link, and assuming you know what it is, I won't further indulge on the subject. Why am I talking about it then?

Because, thanks to Paolo, I am reading The Little Big Things by Tom Peters, and I stumbled upon the following sentences (re-translated in English from the Italian version), that according to the author apply in hard times as well as in good ones:
  • Get to work earlier than usual.
  • Leave later than usual.
  • Work more.
  • Volunteer to do more.
Now. This sounds quite different from sustainable pace. It also sounds different from the slack periods that the very same author reports as very important just a few pages earlier.

Why should we work more? I can understand that arriving earlier and leaving later leaves you some time all by yourself in the office, which is when you tipically can perform better because you can get some uninterrupted quality time. But you cannot count on such a short amount of time to complete everything you're supposed to do during a whole day.

And how about your canonical eight hours? Are they all wasted as "the big slack time between early morning and late evening"? Nope, you're supposed to work. Does this come as a surprise? That's very strange, because it is precisely what you are being paid for. So why add the extra hours? You're not being more productive, you're just spending more time at the desk. No, really, Peters goes as far as saying "work more for less". No way. At least, things being as they are now.

Let's get back to sustainable time. Suppose you're a workaholic: you spend at least 12 hours a day at the office, trying to compensate with brute force for your laziness. You are often so tired that your judgment is hiding somewhere under your shoes, and you can't tell whether you still have to bang your head on the wall on little and useless details or you can pass to something else. Or, better yet, call it a day. This is not committment, this is a physical dependency. How long do you think you can sustain it before reaching a burnout? Not very much. You're doomed. And you've wasted your time, not to mention the money of your employer.

Moreover, this is also very bad for morale: not only yours, as you obtain very little despite your enormous efforts, but also of the people surrounding you. People will start feeling guilty because they work less than you do, so they'll feel compelled to stay just because you do, even if they don't have anything to do but keep their chairs warm. Isn't that absurd? Yet, here in Italy you are too often judged by the time you spend at your desk. Even if you don't produce anything worth your time.

I'm sure Mr. Peters didn't mean "waste your day spending more time at the office", but unluckily it is exactly what is going to happen.

Get yourself a life! Strive for excellence, and work better: if you do it you will achieve the same results in less time. Absurd? Yes, it is: actually you will get much better results in much less time.

A simplified example for programmers? concentrate and try to make your code easily readable, not only by you but also by other people (which also means you, as you will not even remember you wrote that piece of code in a couple of months). Write automated tests. Refactor. Eliminate duplications. Do I hear "it will take more"? Yes, it will take more. But just on the barrelhead. As I read today on twitter:

Programming is like sex: one mistake and you’re providing support for a lifetime. (Michael Sinz)

Do your maths...