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

41 visningar
Hoppa till det första olästa meddelandet

Craig Andrews

oläst,
30 juli 2014 10:20:582014-07-30
till 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

oläst,
31 juli 2014 22:31:152014-07-31
till 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

oläst,
1 aug. 2014 16:38:252014-08-01
till 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

oläst,
1 aug. 2014 19:11:432014-08-01
till 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

oläst,
4 aug. 2014 12:38:462014-08-04
till 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

oläst,
4 aug. 2014 12:40:402014-08-04
till grails-de...@googlegroups.com
Ops, I missed the "is String" part. :-)

--
Sérgio Michels

Guilherme Santos

oläst,
4 aug. 2014 17:19:452014-08-04
till 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.
Svara alla
Svara författaren
Vidarebefordra
0 nya meddelanden