Wednesday, April 30, 2008

Lightbox gone crazy...

If you think there are too many Java web-frameworks out there - check out the market for JavaScript Lightbox libraries:

http://planetozh.com/projects/lightbox-clones/

Tuesday, April 22, 2008

Learning Spanish

I started reading my second Spanish book: Pueblos del Caribe y la Amazonia. The book describes the life and customs of the indigenous people in pre-hispanic times. So far it seems to be a relatively easy informative read and I like to learn more about Latin-American history anyway. My next Spanish book is already waiting - Diarios de Motocicleta...Good ol' Che. I have been taking Spanish classes for the last one and a half years and finally things start making more and more sense. 

Which reminds me - my last level (12) at the Latin American Association here in Atlanta starts this week.

Monday, April 21, 2008

Converting Daos from Hibernate to JPA

Over the weekend I converted the Daos of my pet project jRecruiter from Hibernate to the Java Persistence API (JPA) using Hibernate as persistence provider. The project is using the latest Spring version (2.5.3) and I also incorporated Spring's latest annotation features. This allowed me to remove all the Dao-related Spring bean declarations in my context.xml file. The actual conversion from pure Hibernate to JPA was quite straight forward. I was struggling a bit with the Spring configuration of the entityManagerFactory but got it to work eventually.

Here is my Spring context file with the JPA setup.































${hibernate.dialect}
true 'Y', false 'N'
true
org.hibernate.cache.EhCacheProvider
true
true









The modifications to the daos itself were rather modest. One difference I noticed is that executing a query using query.getSingleResult(); throws a NoResultException. I prefer returning null - therefore I had to do the following:


try {
user = (User) query.getSingleResult();
} catch(NoResultException e) {
user = null;
}


Also, the Dao classes are annotated with @Repository e.g.:


...
@Repository("userDao")
public class UserDaoJpa extends GenericDaoJpa<>
implements UserDao {
...


Also, I use some of the new Spring features that allow me to get rid of quite a bit of XML:







Using context:component-scan Spring will auto-detect my daos and registered them with the context without any XML necessary. I think this is kind of neat especially for smaller projects.

Lastly, in case you need to access Hibernatenate's SessionFactory directly:

Session session = (Session)entityManager.getDelegate();

Friday, April 18, 2008

Save the Sharks!

I just watched Sharkwater. In my opinion this is the best documentary I have seen in a while. It shows some amazing underwater footage. Seeing the hammerhead sharks congregate near Cocos Island is truly impressive. Nevertheless, the movie delivers an important message: Sharks are not the dangerous animals as you might see on the mass media. More people are killed by vending machines every year than by sharks. 
In fact, sharks are an important keystone species without which many marine ecosystems may collapse. For example research indicates that the ongoing removal of sharks contributes to the decline of coral reefs. I am a certified diver and would like to continue enjoying the mystique and beauty of the world's coral reefs.

SHARKWATER

Sharks are are literally in danger of being wiped off the map by overfishing (shark finning) and habitat destruction – Every year 50-100 million sharks are killed. The documentary also explains and shows the effects of longline fishing, a really nasty practice.
Thus, even if you don't love sharks, they are critical for the marine environment and need to be protected. We cannot afford loosing them. Please spread the word! 

Wednesday, April 16, 2008

Using Annotations in Spring 2.5 to the Extreme

For the past couple of years I have been using Spring in almost all of my projects. It has been mostly a great experience. However, there was always a lot of XML involved in order to wire all those beans together. This held true also for Hibernate as well as other frameworks. Did I mention Acegi Security?...

Well, then the community decided that annotations were an awesome way of cutting down on all this configuration clutter. Paired with the concept of sensible defaults brought over from the Rails community, live started to get easier. Guice introduced annotations-based dependency injection (DI), which has also been adopted by Spring. Thus, if you use JPA or Hibernate Annotations combined with all the whiz-bang offered by Spring you can virtually eliminate all your XML glue-code.

Well, with Spring 2.5 you can take DI to the extreme by also eliminating all your dependency-setters. For example, in your service methods you can annotate your private data access object variable declarations with annotations (e.g. @Autowired) that will inject the necessary dependencies for you. No need to declare public setters.

While this sounded kind of exciting and works well, I started scratching my head when dealing with unit-testing. How do I mock my service class' dao objects when there are neither public setters nor respective constructors?

For this issue Spring comes to the rescue with the following helper class, which provides helper methods for setting non-public variables or setters:

org.springframework.test.util.ReflectionTestUtils

Here is an example using EasyMock for creating a partial mock object:

...
final User Dao userDao = EasyMock.createStrictMock(UserDao.class);
EasyMock.expect(userDao.save(user)).andReturn(user);
ReflectionTestUtils.setField(userService, "userDao", userDao, UserDao.class);
EasyMock.replay(userDao);
...

And in my Service class I declare:

...
/** User Dao. */
private @Autowired UserDao userDao;
...

For more information regarding the new features in Spring 2.5 check out this article at InfoQ as well as this blog entry.

This very streamlined way of doing dependency injection without setters is very interesting, but to me it poses the following question:

Is this usage of annotations pushing the envelope a little too far?

It certainly binds you code more to the underlying framework (Spring) but on the other hand it feels kind of nice and works well. In my understanding this might be a really productive way of implementing smaller projects but it might be problematic for bigger ones...

Saturday, April 12, 2008

AJUG's Server found a new Home

I moved the server of the Atlanta Java User Group from the Midtown Atlanta to its new location in Suwanee, Ga. For many years the server was hosted by a data center facility in Midtown but last weekend we got the surprising news that due to policy changes we had to move out. Therefore, Burr Sutter, our president, sent out a message to our community asking for help. Thanks to many, many responses from AJUG's members, we had a few options by the middle of the week. At the end we chose 4t Networks who offered use sponsored hosting. 

By the way, the data-center in Suwanee, Ga. The data-center is really quite impressive - you can find more information here

Wednesday, April 9, 2008

Social Networking and the Atlanta Java User Group

The Atlanta Java User Group has certainly a very healthy membership basis. We have 1400 subscribers to our announcement mailing list as well as 600 subscribers to the general membership mailing list. 11 days ago I created a group on LinkedIn for the Atlanta Java User Group in order to increase the visibility of the group. Previously, I had already become a member of the Java Posse LinkedIn Group and wanted to provide the same exposure to the Atlanta Java User Group. It has been quite exciting to see the rapid adoption rate. Within 48 hours we had 100 sign-ups and yesterday we hit 155 subscribers. It seems that social networking is becoming an essential tool to IT professionals these days.