Cache parsed classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Cache parsed classes

Francesco Chicchiriccò
Hi there,
at Apache Syncope we are currently working to support dynamically loaded Groovy classes as standard classes.

Essentially, there are some defined Java interfaces, and we allow users to upload their Groovy implementations which are stored as strings in our internal database.

When one of available implementations is requested, we do something as follows:

    private static final GroovyClassLoader GROOVY_CLASSLOADER = new GroovyClassLoader();

    @SuppressWarnings("unchecked")
    private static <T> T buildGroovy(final String classBody) throws InstantiationException, IllegalAccessException {
        Class<?> clazz = GROOVY_CLASSLOADER.parseClass(classBody);
        return (T) ApplicationContextProvider.getBeanFactory().
                createBean(clazz, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);
    }

I am happy to say that this works just fine.

Now I was wondering what is the best approach to cache such information, to avoid Groovy parsing the class source text at every execution request.
Could you provide any pointer? Thanks!

Regards.
Reply | Threaded
Open this post in threaded view
|

Re: Cache parsed classes

Jochen Theodorou
On 03.10.2017 10:52, Francesco Chicchiriccò wrote:
[...]

> When one of available implementations is requested, we do something as follows:
>
>      private static final GroovyClassLoader GROOVY_CLASSLOADER = new GroovyClassLoader();
>
>      @SuppressWarnings("unchecked")
>      private static <T> T buildGroovy(final String classBody) throws InstantiationException, IllegalAccessException {
>          Class<?> clazz = GROOVY_CLASSLOADER.parseClass(classBody);
>          return (T) ApplicationContextProvider.getBeanFactory().
>                  createBean(clazz, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);
>      }
>
> I am happy to say that this works just fine.
>
> Now I was wondering what is the best approach to cache such information, to avoid Groovy parsing the class source text at every execution request.

If classBody across requests for the live time of the session, then you
can save the bean in the session scope. If there is no real session,
then I need to know how the different classbody differ. And that aspect
would be for me a key for a map, that has the clazz as value.

bye Jochen

Reply | Threaded
Open this post in threaded view
|

Re: Cache parsed classes

Francesco Chicchiriccò
On 2017-10-03 18:38, Jochen Theodorou <[hidden email]> wrote:

> On 03.10.2017 10:52, Francesco Chicchiriccò wrote:
> [...]
> > When one of available implementations is requested, we do something as follows:
> >
> >      private static final GroovyClassLoader GROOVY_CLASSLOADER = new GroovyClassLoader();
> >
> >      @SuppressWarnings("unchecked")
> >      private static <T> T buildGroovy(final String classBody) throws InstantiationException, IllegalAccessException {
> >          Class<?> clazz = GROOVY_CLASSLOADER.parseClass(classBody);
> >          return (T) ApplicationContextProvider.getBeanFactory().
> >                  createBean(clazz, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);
> >      }
> >
> > I am happy to say that this works just fine.
> >
> > Now I was wondering what is the best approach to cache such information, to avoid Groovy parsing the class source text at every execution request.
>
> If classBody across requests for the live time of the session, then you
> can save the bean in the session scope. If there is no real session,
> then I need to know how the different classbody differ. And that aspect
> would be for me a key for a map, that has the clazz as value.

Thanks for your answer.

No, there is no session that can be involved here.

The classBody string is expected to vary frequently during the development phase, and rarely when the system is in production.

Managing the cache with an in-memory Map is fine to me, I just want to avoid duplicating any existing mechanism already implemented in Groovy.

Regards.