Wednesday, May 21, 2008

RSS Feed Aggregators

For the Atlanta Java User Group I have been looking into providing an RSS feed aggregation service for blogs of AJUG members. Since AJUG is running its own server, I wanted to provide a server-based solution. Interestingly, it seems the choices are rather slim when looking for something that is OSS. Pratik mentioned groovyblogs. It looks interesting, got it running very quickly, but I saw one too many stacktraces when clicking through the application. Maybe just bad luck...Once I find the time to dive deeper into Groovy and Grails, I may return to groovyblogs, though. The source code is available here.

Barry Hawkins pointed me to Planet, a Python-based feed aggregator. To see it in action, take a look at http://planet.python.org/. Well, after looking at it, I found Planet Venus its successor. Compared to the original Planet, Planet Venus actually has some documentation (I like documentation!) and a fairly active mailing list. Furthermore, nature.com is using Planet Venus. You can find it at http://planet.nature.com/. There is also blog entry by Sam Ruby that provides a little more background information regarding Planet Venus.

Anyway, the actual installation has been trivial - it is basically just a command line script, that parses RSS feeds, applies a template and than spits out html pages. Thus, adding the script's execution to your crontab is all that is needed to feed your aggregator with new data.

Monday, May 19, 2008

Spring Security 2.0

For my home project, I updated ACEGI security to Spring Security 2.0.1. My experience was similar to Matt Raible's - positive. I was able to cut down the XML configuration quite substantially. At the end only 49 lines remained (Including XML namespace declarations).

The migration was very easy except for 2 smaller issues (See further below). First, I updated web.xml to rename the security filter to Spring Security's one:

springSecurityFilterChain

org.springframework.web.filter.DelegatingFilterProxy

...

springSecurityFilterChain
/*



Also, since I am using my own "user service" to pull users and roles from the database, I had to rename a few imports in my classes to reflect the package names of Spring Security 2.0.

The biggest configuration change was updating the bean definition in my context configuration file which contains all the necessary XML to wire all the beans together. Spring Security 2.0 is now providing its own XML Namespaces, which drastically simplifies configuration.

Thsu, here is the final application context configuration file for Spring Security 2.0 as used by my project:














































As mentioned above, I encountered two smaller issues. First, I am using the Jasypt library which I am using for digesting passwords. It provides a password encoder that plugs into ACEGI security. Unfortunately, it has not yet been updated to also work in conjunction of Spring Security 2.0. The aforementioned implemented password still uses the old package structure. Thanks to open source this is an easy fix :-)

Thus, I checked out Jasypt into my Eclipse Ide, updated the package reference and the pom to pull in the Spring Security 2.0 jars. I also created a ticked on the Jasypt project website and submitted a patch with the changes. Let's see whether it will find its way into the prokject soon.

My second problem had to do with the rolePrefix of Spring Security. Spring Security by default pre-fixes roles names with "ROLE_". Since my project does not use prefixes for role names I had created rolevoter bean that set the rolePrefix property.

Interestingly you can't do this configuration using namespaces. With the current version you need to fall back to the traditional bean configuration support which involves creating 3 additional beans:

  • roleVoter

  • accessDecisionManager

  • authenticatedVoter
There is a little more information available on the Spring forums about this. Hopefully this maybe be simplified in the future - thus, I opened a ticket for this on the Spring Jira.

Wednesday, May 14, 2008

If Tomcat is Running out of Memory...

If your Tomcat is running out of memory, take a look at the following issue posted by Atlassian. It affects basically all Tomcat versions including Tomcat 6.

The fix is to set the following JVM parameter:

-Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true

And, just for completeness - if you need to specifiy JVM parameters for Tomcat, create a file called setenv.sh in your Tomcat's bin directory.

e.g.

#!/bin/sh

CATALINA_OPTS=" \
-Xms512m -Xmx512m \
-XX:PermSize=128m -XX:MaxPermSize=256m \
-server \
-Djava.awt.headless=true \
-Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true"

export CATALINA_OPTS

Tomcat will pick up the file automatically when it starts up. Regarding Permspace issues, there was a good discussion about it over at Matt Raible's Blog.

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.