Brain Flush

July 31, 2008

Grails Quick Tip #2: Getting Access to SessionFactory and MessageSource

Filed under: Software Development & Programming — Tags: , — Matthias @ 9:52 am

Sometimes it is necessary to reach beyond the dynamics of Grails’ finder methods and other injected features, and instead make use of the underlying objects that implement this behavior directly. An example would be the need to access a Hibernate session directly, maybe because  you are writing a service object in plain Java, which means none of the features that rely on the dynamic nature of Groovy are available to you. These include:

  • the current Hibernate session
  • the Spring message source for accessing text in various translations
  • a logger object

All these objects are injected by Grails into your services automatically — that is, if you follow the conventions and are programming in Groovy. If you’re not, you’re pretty much on your own, because the Grails reference guide does not make any mention of these objects and how to access them.

However, I figured they’d be simply Spring beans as well (with the exception of log objects, see below), and due to the consistent conventions in Grails I soon discovered that they are called sessionFactory and messageSource respectively. So in order to access them directly from within a Groovy service object, just do this:

class MyService {
   def sessionFactory
   def messageSource

   def serviceMethod = {
      def session = sessionFactory.getCurrentSession()
      ...
   }
}

That doesn’t work with Java services of course. In that case, go to resources.groovy and add a bean declaration:

 myService(com.example.services.MyService) {
    sessionFactory = ref("sessionFactory")
    messageSource = ref("messageSource")
 }

Note that log objects have to be created manually, because they are not handled by the Spring container. You could for instance create a Log4J object, as shown below. Do not forget to add the setters for the Spring beans, so Grails can set them for you automatically:

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.context.MessageSource;

public class MyService {
    // instantiates a Log4J logger object
    private Logger log = Logger.getLogger(getClass().getName());

    // a Hibernate SessionFactory
    private SessionFactory sessionFactory;

    // a Spring MessageSource
    private MessageSource messageSource;

    [...]

    // used by the Spring IoC container to inject the session factory bean
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    // used by the Spring IoC container to inject the message source bean
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
}

It may be a good idea to use a class like this as a base class for all your plain Java services, so they can benefit from these objects, too.

Advertisements

July 25, 2008

Web 2.0 Buzzword Map… v2.0

Filed under: Graphics & Design — Tags: , — Matthias @ 4:08 pm

I guess you probably all know the wonderful Web 2.0 buzzword cloud by Markus Angermeier.

I found it was too dark in color when printing it on paper, so I did a re-coloring. I simply inverted the colors and adjusted the color curves with GIMP. Here’s the result:

Web 2.0 Map - recolored

Web 2.0 Map - recolored

I felt it’d be a lot easier on the eyes… and does not stress your printer’s toner as much. Please name the original author, Markus Angermeier, when using this graphic.

July 17, 2008

Nielsen Survey shows: Mobile Web adoption is picking up the pace

Filed under: Mobile Devices — Tags: — Matthias @ 2:15 pm

I have to admit I am currently somewhat hyped about the mobile Web, particularly in face of its opportunities for mobile social media. A recent survey by Nielsen Mobile shows that especially in the U.S. the adoption of the mobile Web experiences rapid growth:

We believe mobile Internet has reached a critical mass as an advertising medium in the US. As of May 2008, there were 40 million active users of the mobile Internet in the US, with individual sites that attract millions of unique users.

Source: Nielsen Mobile

Since my thesis focuses on mobile social media, I can hardly wait to see similar growth (and price drops) here in Germany — fixed price subscription plans are still too expensive.

Create a free website or blog at WordPress.com.