1

I'm really newbie to groovy scripting but following some tutorial I tried to dynamically load some groovy class within my java code using parseClass() method of GroovyClassLoader. I wrote some snippet and it worked fine for me. The problem is that I don't clearly understand what groovy engine is doing beyond my view and how those scripts are compiled?
Does a new class gets creted and loaded into jvm? Or does my application uses some cached sources?

Here is the class I'm trying to parse:

  private static class MyScript {

  @Override
  public String toString()
  {
     StringBuilder builder = new StringBuilder();
     builder.append("public class SomeClass\n");
     builder.append("{\n");
     builder.append("Some code...").append("\n");
     builder.append("}\n");
     return builder.toString();
  }

The I load it with build() as below:

private Class MyGroovyBuilder {
  private Script script = new Script();
  public String build() throws TemplateCompilationException
  //
  String groovyText = script.toString();
  //
  CompilerConfiguration config = new CompilerConfiguration();
  //
  byte[] bytes;
  try
  {
     bytes = groovyText.getBytes(config.getSourceEncoding());
  }
  catch (UnsupportedEncodingException e)
  {
     throw new TemplateCompilationException(e, groovyText);
  }
  //
  InputStream in = new ByteArrayInputStream(bytes);
  GroovyCodeSource gcs = new GroovyCodeSource(in, "SomeName", "/groovy/shell");
  GroovyClassLoader loader = new
  GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
  Class<?> scriptClass;
  try
  {
     scriptClass = loader.parseClass(gcs, false);
  }
  catch (CompilationFailedException e)
  {
     throw new GroovyCompilationException(e, "SomeName", groovyText);
  }
  catch (ClassFormatError e)
  {
     throw new GroovyCompilationException(e, "SomeName", groovyText);
  }
return scriptClass.getName();

}


Any clarification is greatelly appreciated.

BR.

tmarwen
  • 15,750
  • 5
  • 43
  • 62

1 Answers1

1

After loading class it appears in your class loader, and can be accessed like any other class.

There is a simple tutorial [here], that show how to load class from string.

In simplest case, you can load class, and hold it's Class object, using it to create objects dynamically. For field access or method invokation you can rely on Groovy dynamic nature.

There is no "cached source" or smth like that behind the scene and you can forget, from where your class is loaded. You can also cache classes, that are already compiled, and save them somewhere, as described [here]. It will drastically improve performance, if you need to load same class often.

But it will be better, to dig down in topic, because dynamic class loading is advanced Java/Groovy technique, it's whole infrastructure of chained classloaders, so it's better to refer documentation about them.

Links below may be helpful.

http://javarevisited.blogspot.ru/2012/12/how-classloader-works-in-java.html

How to use URLClassLoader to load a *.class file?

Community
  • 1
  • 1
Seagull
  • 13,484
  • 2
  • 33
  • 45
  • Hi @Seaagull, thanks for the explanation, but let's get deeper and supposing I'm configuring some clusterd environement with two nodes and that I created an instance of that object in **node1** then wanted to replicate it to the **node2**; the replication process will certainlly fail as no the second node does not hold sources of this class, isn't right? How should I process in such case? – tmarwen Feb 13 '14 at 12:40
  • @tmarwen Ok, let's go deeper ;) You have severals isolated vm's, and so you have to share loaded class on each of them. For replication, you could serialise your class instance, transfer it to second node, then deserialise it there. – Seagull Feb 13 '14 at 12:47
  • So this should be done manually, it means that it is going back to me to implement some sharing-resource mechanism. There is no out-of-the-box feature that can ease those classes replication? – tmarwen Feb 13 '14 at 13:30
  • @tmarwen As I know, in Groovy - no. There are several clusterisation solutions. I heard about Terracota, a way to distribute RAM across many computers. Look http://stackoverflow.com/questions/383920/what-is-the-best-library-for-java-to-grid-cluster-enable-your-application for more info. – Seagull Feb 13 '14 at 14:25
  • Thank you very much since you helped crossing half the street. It won't be possible to consider Terracotta now since already using a cache framework `jbosscache` but will put in some migration/combination schema, TY. – tmarwen Feb 13 '14 at 14:34