Dynamically defining a class in a plugin in order to work with Hibernate 3 or Hibernate 4

41 views
Skip to first unread message

Craig Andrews

unread,
Jul 30, 2014, 10:20:58 AM7/30/14
to grails-de...@googlegroups.com
In an effort to make the grails cache ehcache plugin work with both Hibernate 3 and 4, I dynamically load and define a class. I need to dynamically define and load the class as the Hibernate 3 class won't compile if Hibernate 3 isn't available, and the Hibernate 4 class won't compile if Hibernate 4 isn't available, and one cannot have both Hibernate 3 and Hibernate 4 available at the same time.

 I feel like I've made this harder than it should be. Would one of you mind taking a look to see if there's a better way to accomplish this goal? https://github.com/grails-plugins/grails-cache-ehcache/blob/v1.0.2/CacheEhcacheGrailsPlugin.groovy#L165

I'm also curious if there are any potential problems with what I've done.

Thank you,
~Craig

Guilherme Santos

unread,
Jul 31, 2014, 10:31:15 PM7/31/14
to grails-de...@googlegroups.com
Hi Craig,

Take a look at this commit that checks if hibernate3 is present or not.
https://github.com/grails-plugins/grails-webflow-plugin/commit/26ebec5e5cda8a99304f0b280b151b3a40434932

I think you can use the same logic on your code to decide which class to compile.

Craig Andrews

unread,
Aug 1, 2014, 4:38:25 PM8/1/14
to grails-de...@googlegroups.com
That approach works when you need to get an instance of a class that may or may not be available (in that case, org.springframework.orm.hibernate3.SessionHolder may or may not be there), but it won't work when you need to extend a class that or may not be there (in my case, I need to extend net.sf.ehcache.hibernate.EhCacheRegionFactory when using Hibernate 3, but extend org.hibernate.cache.ehcache.EhCacheRegionFactory when using Hibernate 4). If you try to extend a class that doesn't exist, Groovy (just like Java) gives an error.

Guilherme Santos

unread,
Aug 1, 2014, 7:11:43 PM8/1/14
to grails-de...@googlegroups.com
This line here https://github.com/grails-plugins/grails-cache-ehcache/blob/v1.0.2/CacheEhcacheGrailsPlugin.groovy#L47 is part of a string.

If I understood you're problem correctly you need to either
import net.sf.ehcache.hibernate.EhCacheRegionFactory or
import org.hibernate.cache.ehcache.EhCacheRegionFactory depending on which version of hibernate you have on.

So I think you can use the same logic I posted on my previous comment to know which version of hibernate you have installed.

you could check if that class you want to import is present or not.

if (ClassUtils.isPresent("net.sf.ehcache.hibernate.EhCacheRegionFactory", CacheEhRegionPlugin.class.getClassLoader())) {
     
import net.sf.ehcache.hibernate.EhCacheRegionFactory
} else {
    
import org.hibernate.cache.ehcache.EhCacheRegionFactory
}

you always extend EhCacheRegionFactory without the package name and the class that'll actually be extended will depend on your import.

This way you can eliminate having to define two classes that do basically the same thing.

Sergio Michels

unread,
Aug 4, 2014, 12:38:46 PM8/4/14
to grails-de...@googlegroups.com
Guilherme, I don't think that you can do dynamic imports like this.

Not sure if this helps you, but how about providing the class as one Spring Bean?

--
Sérgio Michels



--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/783f1d39-3af1-43ef-be71-b75320ca624e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Sergio Michels

unread,
Aug 4, 2014, 12:40:40 PM8/4/14
to grails-de...@googlegroups.com
Ops, I missed the "is String" part. :-)

--
Sérgio Michels

Guilherme Santos

unread,
Aug 4, 2014, 5:19:45 PM8/4/14
to grails-de...@googlegroups.com
Sergio, exactly, he's defining the whole class in a String and compiling at runtime. ;)


--
You received this message because you are subscribed to a topic in the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grails-dev-discuss/izj5BQ_dAlU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grails-dev-disc...@googlegroups.com.

To post to this group, send email to grails-de...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages