Using Struts 2’s native dependency injection support

Since I knew that Struts 2 used Guice internally, I was curious if the native DI support in S2 could be used in a simple application. A little bit of digging and fiddling around showed what is possible.

struts.xml

...
 <constant name="myVal" value="9" />
 <bean name="myService" type="prac.MyService" class="prac.MyServiceImpl" />
...

MyServiceImpl.java

...
 @Inject("myVal")
 public void setMyVal(String myVal) {
 this.myVal = Integer.parseInt(myVal);
 }
...

MyAction.java

...
 @Inject("myService")
 public void setMyService(MyService myService) {
 this.myService = myService;
 }
...

This will create a single instance of MyService with myVal injected into it.
And on every invocation of MyAction, that MyService instance will be injected into it.
Sweet!

Of course, this is part of Struts internals, so these can change at any time. It is probably not a good idea to use it on a real world system.

Object equality in Java

The first time I paid attention to the equals() method was when preparing for SCJP exam, where you have to know what the output of some lines of code will be. That was simple enough – follow the logic of the code, and make sure that you are not accidentally overloading equals instead of overriding it.

A few years later, I revisited equals() ( and hashcode() ) when reading Joshua Bloch’s Effective Java. Chapter 3, “Methods Common to All Objects” shows how to, and why you should, properly implement equals() method. This must be required reading for every Java programmer, even if you ever only write CRUD applications in Struts, and are never going to use any of it.

Recently, I read Angelika Langer‘s article on this subject. If, like me, you have read the Effective Java book, and think that you know all that you need to about Object equality, think again!

Angelika Langer and Klaus Kreft disect various approaches to implementing equals() – Secrets of equals().

The gist of it is that although implementing equals() for a final class, or a class with a final equals() method is reasonably straightforward, there is no one correct way of doing that with mixed-type comparisons allowed between objects of different types in the same class hierarchy.
The second thing to keep in mind is that if you are overriding a class that implements equals(), and you override the equals() method you have to pay particular attention to whether the superclass’s equals() method allows mixed-type comparisons or not –

If the designer of such a non-final class decides in favor of implementing equals() using instanceof , then no subclass can ever add fields and override equals() without violating the transitivity requirement of the equals() contract.
If the designer decides in favor of implementing equals() using getClass() , then no subclass object will ever be comparable to a superclass object and trivial extensions may not make a lot of sense.

There is a follow up article showing one way of Implementing equals() To Allow Mixed-Type Comparison.

Creating a daily log in Evernote from Quicksilver

Recently I started using Evernote, and I am loving it so far.
If you are looking for a way to organize the “stuff” on your computer, check it out.

One of the things I am trying to use it for is to keep a daily log of things I do, so that, at the end of the day or the week, I can check how productive (or not) I have been.

Taking inspiration from this story on Mac OS X Hints ;), and with some help from the kind folks at Evernote developer forum, I put together this simple script to create a note in my daily diary from Quicksilver –


property nb : "Daily Diary"

using terms from application "Quicksilver"
 on process text qtxt
 my CreateDailyEvernote(qtxt)
 end process text
end using terms from

on CreateDailyEvernote(txt)
 set t to do shell script "date +'%Y/%m/%d'"
 tell application "Evernote"
 set foundNotes to find notes "notebook:"" & nb & """ & " intitle:"" & t & """
 set found to ((length of foundNotes) is not 0)
 if not found then
 create note with text txt title t notebook nb
 end if
 activate
 end tell
end CreateDailyEvernote

This will create a note with title as current date in the format yyyy/mm/dd in “Daily Diary” notebook.
At the moment, Evernote Mac scripting API does not allow appending text to an existing note.
Once that is added (a very helpful Andrew McGeachie at the Evernote developer forum has already created a feature request for that), this would be a perfect way for me to keep a log of everything I do during the day.

Security against malicious websites

Most people know about computer viruses, but very few people are aware of the danger posed by malicious websites.
If you are not careful, malicious websites can steal your personal data by using vulnerabilities in certain websites. These kind of attacks are generally referred to as cross site scripting or XSS, and in general, it is very hard to be sure that an website you visit is not vulnerable to such attacks.

Logo Designer David Airey lost his domain as a result of an XSS attack, and a while ago Friendster suffered from a similar attack.

One precaution you can take against such attacks is to have multiple browsers on your computer, and use separate browsers to access sites with different trust levels. I divide up the sites I visit into three trust levels and use three different browsers to visit each category. One is for my primary email and banking etc. The second is for my secondary email (no personal stuff), blogs and other known sites. And finally the third where I visit sites from search engine results or other untrusted sources. This might sound paranoid, but when it comes to computer security, a certain amount of paranoia is essential, especially if you are using it for your business or professional use.

Another common mistake is to log in to your mail and other accounts from internet cafes while travelling. You can never be sure when a computer at an internet cafe has a key logger or other malicious software installed, either by an unscrupulous employee or by another user or by a downloaded virus. Set up a temporary email account for use while you are traveling, and have your other email accounts forward the email to this account. This does not guarantee safety, but at least it will minimize the risk.

For higher security, you really need to do dig deeper into computer security issues. This article at Wikipedia is a good start. But for most people, following a few reasonable precautions like the ones mentioned above can offer a good enough safety net.

The meaning of drive

This has to be one of the most touching personal anecdotes I have ever read.

A. C. Lyles, a producer at Paramount who worked at Paramount for over 60 years –

When I was 10 [in 1928] I wanted to make movies…

After four years in the job [he was then 14] I eventually met Adolph Zukor… when he came to Jacksonville. I asked him to let me come to Hollywood to work for him. He said, “Well, you’re just a kid, but you’ve been working for Paramount now for four years at the theater. So you finish high school, keep in touch, and I’ll hire you when you get out of high school.”…

So I wrote him every Sunday for four years.

Read the rest at Marc Andreessen’s blog.

Managing CruiseControl configuration

One of the pains of using CruiseControl is that setting up and configuring it is not very intuitive, especially when you only need to change it every six months or after a hard disk crash.
Julian Simpson shows how to keep the CC configuration file in the version control system, and use a bootstrapper to apply the configuration whenever it changes – Configuring CruiseControl the CruiseControl way.

Or, you can switch to Hudson 😉