« Return to Thread: GORM outside Grails

Re: GORM outside Grails

by cjmason :: Rate this Message:

Reply to Author | View in Thread

Burt-

This is excellent, thanks.

Could someone with access perhaps add this link to

   http://docs.codehaus.org/display/GRAILS/GORM+-+StandAlone+Gorm
and/or
   http://grails.org/GORM+-+StandAlone+Gorm

Let me know where to send the beer!

-c


Burt Beckwith 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


 « Return to Thread: GORM outside Grails