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

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Craig Andrews
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/fd2fcafc-f3fb-402a-a469-0a5064bfd231%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Guilherme Santos
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/26465e96-37c6-4765-b541-0aa6ce931b76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Craig Andrews
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.

On Thursday, July 31, 2014 10:31:15 PM UTC-4, Guilherme Santos wrote:
Hi Craig,

Take a look at this commit that checks if hibernate3 is present or not.
<a href="https://github.com/grails-plugins/grails-webflow-plugin/commit/26ebec5e5cda8a99304f0b280b151b3a40434932" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails-plugins%2Fgrails-webflow-plugin%2Fcommit%2F26ebec5e5cda8a99304f0b280b151b3a40434932\46sa\75D\46sntz\0751\46usg\75AFQjCNFYTRUVxZLSdsPDn3hEydDVnkbMeA';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails-plugins%2Fgrails-webflow-plugin%2Fcommit%2F26ebec5e5cda8a99304f0b280b151b3a40434932\46sa\75D\46sntz\0751\46usg\75AFQjCNFYTRUVxZLSdsPDn3hEydDVnkbMeA';return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/45171788-6dcc-4b25-9437-0c9447954d41%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Guilherme Santos
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

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

sergiomichels
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


On Fri, Aug 1, 2014 at 8:11 PM, Guilherme Santos <[hidden email]> wrote:
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAHTf1NV6MNnFGC4RE71%2BAnXobOOyWuHhGXNZWddViKAXM92KfQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

sergiomichels
Ops, I missed the "is String" part. :-)

--
Sérgio Michels


On Mon, Aug 4, 2014 at 1:38 PM, Sergio Michels <[hidden email]> wrote:
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


On Fri, Aug 1, 2014 at 8:11 PM, Guilherme Santos <[hidden email]> wrote:
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAHTf1NX6bCzvA9j1-TCbaz6PT55XYXzCxzJN8St4Fkt9hFFNaQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Guilherme Santos
Sergio, exactly, he's defining the whole class in a String and compiling at runtime. ;)


On Mon, Aug 4, 2014 at 12:40 PM, Sergio Michels <[hidden email]> wrote:
Ops, I missed the "is String" part. :-)

--
Sérgio Michels


On Mon, Aug 4, 2014 at 1:38 PM, Sergio Michels <[hidden email]> wrote:
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


On Fri, Aug 1, 2014 at 8:11 PM, Guilherme Santos <[hidden email]> wrote:
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAHTf1NX6bCzvA9j1-TCbaz6PT55XYXzCxzJN8St4Fkt9hFFNaQ%40mail.gmail.com.

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CACieg8Hd%3DXH_JU8ghfvfxV0gS0%2B7%3DCx2eLQ46Mq6uk97BnLizg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.