« Return to Thread: GORM outside Grails

Re: GORM outside Grails

by Gregory W. Bond :: Rate this Message:

Reply to Author | View in Thread

playing around some more i've managed to get burt's entire example to
run in a jar file without any groovy scripts in the jar (!) -
unfortunately it's not a general solution but perhaps what i've done
will help achieve one

the major change i made is to explicitly load the domain classes into
the application - i've attached a zip with the updated code but here's
a quick overview of what i've done (it's not much really):

1) i moved Main.groovy from the scripts dir to src/groovy in order to
get it to be compiled

2) i've replaced occurrences of classLoader.getResource(...groovy
files...) with appropriate variations on classLoader.loadClass()

3) instead of creating the application this way:

application = new DefaultGrailsApplication(groovyFiles)

i'm using this (cumbersome, non-general, but working) approach:

       Class bookClass =
GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Book")
       Class carClass =
GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Car")
       Class personClass =
GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Person")
       Class songClass =
GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Song")
       Class[] domainClasses = new Class[4]
       domainClasses[0] = bookClass
       domainClasses[1] = carClass
       domainClasses[2] = personClass
       domainClasses[3] = songClass
               application = new DefaultGrailsApplication(domainClasses, new
GroovyClassLoader(GormHelper.classLoader))

(as a sidenote that probably highlights my ignorance of grails and
groovy: i kept getting compilation errors when i tried to initialize
the domain class array when i declared it e.g. using syntax 'Class[]
domainClasses = new Class[] { bookClass, .... }')

greg

On Tue, Sep 2, 2008 at 12:05 PM, Burt Beckwith <burt@...> wrote:

> Right, I mentioned the problem with the metaclass methods in the blog post. I
> won't have time today to look at this but I'll take a look this evening.
>
> Burt
>
> On Tuesday 02 September 2008 11:03:52 am Gregory W. Bond wrote:
>> i've been playing around with this code for a couple of days now trying to
>> get it to work in a self-contained jar file (since i eventually want to use
>> the code to access gorm from java) - the problem i run into is that the
>> dynamic gorm 'save()' method isn't found at runtime (see stack trace below)
>> - the problem disappears if i include the 'grails-app' directory containing
>> the domain class definitions in the same directory as the jar file
>> containing the compiled class files
>>
>> my jar file ('beckwith.jar') includes all the class files generated by
>> 'grails compile' as well as the 'grails-app' directory contents and the
>> 'application.properties' file - at the moment i have to include the file
>> 'scripts/Main.groovy' in the same directory as the jar file until i figure
>> out how to load it as a resource from the jar file
>>
>> here's the command i'm using to run the program
>>
>> java -classpath
>> beckwith.jar:$GRAILS_HOME/lib/groovy-all-1.5.6.jar:$GRAILS_HOME/lib/spring-
>>2.5.4.jar:$GRAILS_HOME/lib/commons-logging-1.1.jar:$GRAILS_HOME/dist/grails-
>>core-1.0.3.jar:$GRAILS_HOME/lib/javaee.jar:$GRAILS_HOME/dist/grails-spring-1
>>.0.3.jar:$GRAILS_HOME/dist/grails-gorm-1.0.3.jar:$GRAILS_HOME/lib/hibernate-
>>annotations.jar:$GRAILS_HOME/lib/hibernate3.jar:$GRAILS_HOME/lib/dom4j-1.6.1
>>.jar:$GRAILS_HOME/lib/hibernate-commons-annotations.jar:.:$GRAILS_HOME/lib/c
>>ommons-lang-2.1.jar:$GRAILS_HOME/lib/commons-collections-3.2.jar:lib/mysql-c
>>onnector-java-5.0.8-bin.jar:$GRAILS_HOME/lib/cglib-nodep-2.1_3.jar Tester
>>
>> here's the exception i get - any idea how to get around this?
>>
>> Exception in thread "main" groovy.lang.MissingMethodException: No signature
>> of method: com.burtbeckwith.gorm.domain.Book.save() is applicable for
>> argument types: () values: {}
>>         at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdap
>>ter.java:55) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdap
>>ter.java:60) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytec
>>odeAdapter.java:172) at
>> com.burtbeckwith.gorm.domain.Book.invokeMethod(Book.groovy) at
>> org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.ja
>>va:784) at
>> org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:7
>>58) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytec
>>odeAdapter.java:170) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytec
>>odeAdapter.java:198) at Main.run(Main.groovy:7)
>>         at groovy.lang.GroovyShell.evaluate(GroovyShell.java:473)
>>         at groovy.lang.GroovyShell.evaluate(GroovyShell.java:508)
>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>         at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>>9) at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
>>         at
>> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
>>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
>>         at
>> groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
>>         at
>> org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.ja
>>va:778) at
>> org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:7
>>58) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytec
>>odeAdapter.java:170) at Tester$_main_closure1.doCall(Tester.groovy:12)
>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>         at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>>9) at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
>>         at
>> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
>>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
>>         at
>> groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
>>         at groovy.lang.Closure.call(Closure.java:292)
>>         at groovy.lang.Closure.call(Closure.java:305)
>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>         at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>>9) at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
>>         at
>> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
>>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
>>         at
>> groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
>>         at
>> org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.ja
>>va:778) at
>> org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:7
>>58) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytec
>>odeAdapter.java:170) at
>> com.burtbeckwith.gorm.GormHelper$_withTransaction_closure1.doCall(GormHelpe
>>r.groovy:133) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>>9) at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
>>         at
>> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
>>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
>>         at
>> groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
>>         at groovy.lang.Closure.call(Closure.java:292)
>>         at
>> org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.
>>java:48) at
>> org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java
>>:72) at $Proxy0.doInTransaction(Unknown Source)
>>         at
>> org.springframework.transaction.support.TransactionTemplate.execute(Transac
>>tionTemplate.java:128) at
>> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>>9) at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
>>         at
>> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
>>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
>>         at
>> groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
>>         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
>>         at
>> org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.ja
>>va:766) at
>> org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:7
>>54) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytec
>>odeAdapter.java:170) at
>> com.burtbeckwith.gorm.GormHelper.withTransaction(GormHelper.groovy:132)
>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>         at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>>9) at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
>>         at
>> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
>>         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
>>         at
>> groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1105)
>>         at
>> org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:7
>>49) at
>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytec
>>odeAdapter.java:170) at Tester.main(Tester.groovy:10)
>>
>> burtbeckwith wrote:
>> > I spent some time on this and got it working. I've posted a writeup and a
>> > zip
>> > file here: http://burtbeckwith.com/blog/?p=66
>> >
>> > Burt
>> >
>> > On Saturday 23 August 2008 3:59:22 pm Christopher Mason wrote:
>> >> Clarifying:
>> >>
>> >> So there are two projects that purport to allow one to use grails
>> >> without having a web server around:
>> >>
>> >> http://docs.codehaus.org/display/GRAILS/GORM+-+StandAlone+Gorm
>> >> which is based on:
>> >> http://jweldin.com/blog/?p=6
>> >>
>> >> However, both are over a year old and don't compile.  Both use this
>> >> constructor:
>> >>
>> >>        application =
>> >>           new DefaultGrailsApplication( groovyFiles, new
>> >> GormInjectionOperation() )
>> >>
>> >> which, it seems, no longer exists.
>> >>
>> >> My suspicion is that this configuration is all done now via Spring.  Is
>> >> this true?
>> >>
>> >> I'm trying to read through the grails Gant scripts source to figure out
>> >> how grails starts up, but it's not clear yet.  I've tried various
>> >> methods on GrailsUtil like:
>> >>
>> >>    GrailsUtil.bootstrapGrailsFromApplication(grailsApplication)
>> >> or
>> >>    ApplicationContext appContext = GrailsUtil
>> >>    .bootstrapGrailsFromClassPath()
>> >>    DefaultGrailsApplication grailsApplication =
>> >>         (DefaultGrailsApplication)appContext
>> >>            .getBean("grailsApplication", D
>> >>              efaultGrailsApplication.class);
>> >>
>> >> but this seems to result in class not found errors, I assume because the
>> >> Gant scripts are responsible for classpathy stuff that I'm not yet doing
>> >> (eg Init.groovy:setClasspath()). Am I headed in the right direction?
>> >>
>> >> If I simply omit the GormInjectionOperation from the code posted above,
>> >> obviously, none of the GORM methods are available on doman objects.
>> >>
>> >> Any hints would be most appreciated.  My goal is something I can run via
>> >> java -jar that will execute a groovy script using GORM domain classes.
>> >>
>> >> I offer beer to anyone who can help me get this working!  And I promise
>> >> to post it somewhere for all to read!
>> >>
>> >> -c
>> >>
>> >> Christopher Mason wrote:
>> >> > Can anyone help update this code for grails 1.0.3?
>> >> >
>> >> > http://jweldin.com/blog/?p=6
>> >> >
>> >> > Does anyone have this working?  (I see several posts but none of them
>> >> > seem to have successful resolutions.)
>> >> >
>> >> > Thanks so much!
>> >> >
>> >> > -c
>> >> >
>> >> >
>> >> > import groovy.lang.GroovyClassLoader;
>> >> > import groovy.lang.GroovyObject;
>> >> > import org.codehaus.groovy.grails.commons.*;
>> >> > import org.codehaus.groovy.grails.orm.hibernate.cfg.*;
>> >> > import org.hibernate.SessionFactory;
>> >> > import org.springframework.core.io.*;
>> >> > import org.springframework.core.io.support.*
>> >> > import
>> >> > org.codehaus.groovy.grails.compiler.injection.GrailsAwareClassLoader
>> >> > import org.codehaus.groovy.grails.compiler.injection.ClassInjector;
>> >> >
>> >> > class RunScript
>> >> > {
>> >> >     public static void main(String[] args)
>> >> >     {
>> >> >
>> >> >         if (args.length < 2) {
>> >> >             println "Usage: java -jar profiler-script.jar <db.props>
>> >> > <script name> <script args>"
>> >> >         }
>> >> >
>> >> >         GrailsApplication grailsApplication = null;
>> >> >         SessionFactory sessionFactory = null;
>> >> >
>> >> >         PathMatchingResourcePatternResolver resolver = new
>> >> > PathMatchingResourcePatternResolver();
>> >> >
>> >> >         Resource[] groovyFiles =
>> >> > resolver.getResources("classpath*:**grails-app/**/*.groovy");
>> >> >
>> >> >         grailsApplication = new DefaultGrailsApplication(groovyFiles);
>> >> >
>> >> >         GrailsAwareClassLoader gcl = new GrailsAwareClassLoader()
>> >> >         gcl.setClassInjectors([ new DefaultGrailsDomainClassInjector()
>> >>
>> >> ]
>> >>
>> >> > as ClassInjector[])
>> >> >
>> >> >         DefaultGrailsDomainConfiguration config = new
>> >> > DefaultGrailsDomainConfiguration();
>> >> >         config.setGrailsApplication(grailsApplication);
>> >> >
>> >> >         Properties props = new Properties();
>> >> >         props.load(new FileInputStream(args[0]));
>> >> >         config.setProperties(props);
>> >> >
>> >> >         sessionFactory = config.buildSessionFactory();
>> >> >
>> >> >         GrailsHibernateUtil.configureDynamicMethods(sessionFactory,
>> >> > grailsApplication);
>> >> >
>> >> >         GroovyClassLoader cl = gcl
>> >> > //grailsApplication.getClassLoader()
>> >> >
>> >> >         println "\n-----------\nBefore Main\n-----------\n"
>> >> >
>> >> >         Binding b = new Binding(args[3..args.length-1] as String[])
>> >> >         def TestDomainScript =
>> >> > (Script)cl.loadClass(args[1],false,true).newInstance(b)
>> >> >         TestDomainScript.run()
>> >> >
>> >> >         println "\n-----------\nAfter Main\n-----------\n"
>> >> >         println "\nDone";
>> >> >
>> >> >     }
>> >> > }
>> >> >
>> >> >
>> >> > import
>> >> > org.codehaus.groovy.grails.compiler.injection.GrailsInjectionOperation
>> >> >; import org.codehaus.groovy.grails.compiler.injection.*;
>> >> > import org.apache.commons.logging.Log;
>> >> > import org.apache.commons.logging.LogFactory;
>> >> > import org.codehaus.groovy.ast.ClassNode;
>> >> > import org.codehaus.groovy.classgen.GeneratorContext;
>> >> > import org.codehaus.groovy.control.CompilationFailedException;
>> >> > import org.codehaus.groovy.control.CompilationUnit;
>> >> > import org.codehaus.groovy.control.Phases;
>> >> > import org.codehaus.groovy.control.SourceUnit;
>> >> > import
>> >>
>> >> org.codehaus.groovy.grails.compiler.support.GrailsResourceLoader;
>> >>
>> >> > import org.codehaus.groovy.grails.commons.GrailsResourceUtils
>> >> > import org.codehaus.groovy.grails.compiler.GrailsClassLoader;
>> >> >
>> >> > public class MyInjectionOperation extends GrailsInjectionOperation
>> >> > {
>> >> >
>> >> >         private static GrailsDomainClassInjector injector = new
>> >> > DefaultGrailsDomainClassInjector();;
>> >> >     private GrailsResourceLoader resourceLoader;
>> >> >
>> >> >     public void call(SourceUnit source, GeneratorContext context,
>> >> > ClassNode classNode) throws CompilationFailedException
>> >> >         {
>> >> >
>> >> >                 if(resourceLoader != null)
>> >> >                 {
>> >> >             try {
>> >> >                 URL url;
>> >> >                 if(GrailsResourceUtils.isGrailsPath(source.getName()))
>> >> >                                 {
>> >> >                     url =
>> >>
>> >> resourceLoader.loadGroovySource(GrailsResourceUtils.getClassName(source.
>> >>g
>> >>
>> >> >etName()));
>> >> >
>> >> >                 }
>> >> >                 else
>> >> >                                 {
>> >> >                     url =
>> >> > resourceLoader.loadGroovySource(source.getName());
>> >> >                 }
>> >> >                 if(GrailsResourceUtils.isDomainClass(url))
>> >> >                                 {
>> >> >
>> >> > this.injector.performInjection(source,context,classNode);
>> >> >                 }
>> >> >
>> >> >             }
>> >> >                         catch (MalformedURLException e)
>> >> >                         {
>> >> >                 println("Error loading URL during addition of compile
>> >> > time properties: " + e.getMessage(),e);
>> >> >                 throw new
>> >> > CompilationFailedException(Phases.CONVERSION,source,e);
>> >> >             }
>> >> >         }
>> >> >     }
>> >> >
>> >> >     public void setResourceLoader(GrailsResourceLoader resourceLoader)
>> >> >         {
>> >> >         this.resourceLoader = resourceLoader;
>> >> >     }
>> >> > }
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > ---------------------------------------------------------------------
>> >> > To unsubscribe from this list, please visit:
>> >> >
>> >> >    http://xircles.codehaus.org/manage_email
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe from this list, please visit:
>> >>
>> >>     http://xircles.codehaus.org/manage_email
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list, please visit:
>> >
>> >     http://xircles.codehaus.org/manage_email
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

gorm.zip (22K) Download Attachment

 « Return to Thread: GORM outside Grails