Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails base.dir system property

I have a simple grails file upload app.

I am using transferTo to save the file to the file system.

To get the base path in my controller I am using

def basePath = System.properties['base.dir'] // HERE IS HOW I GET IT

        println "Getting new file"
        println "copying file to "+basePath+"/files"
        def f = request.getFile('file')
        def okcontents = ['application/zip','application/x-zip-compressed']
        if (! okcontents.contains(f.getContentType())) {
            flash.message = "File must be of a valid zip archive"
            render(view:'create', model:[zone:create])
            return;
        }
              if(!f.empty) {
                  f.transferTo( new File(basePath+"/files/"+zoneInstance.title+".zip") )
              }
              else 
              {
                  flash.message = 'file cannot be empty'
                      redirect(action:'upload')
              }
        println "Done getting new file"

For some reason this is always null when deployed to my WAS 6.1 server.

Why does it work when running dev but not in prod on the WAS server? Should I be accessing this information in a different way?

like image 249
branchgabriel Avatar asked Feb 20 '09 18:02

branchgabriel


3 Answers

Thanks j,

I found the best dynamic solution possible. As a rule I never like to code absolute paths into any piece of software. Property file or no.

So here is how it is done:

def basePath = grailsAttributes.getApplicationContext().getResource("/files/").getFile().toString() 

grailsAttributes is available in any controller.

getResource(some relative dir) will look for anything inside of the web-app folder.

So for example in my dev system it will toString out to "C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\ with the relative dir concated to the end

like so in my example above C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\files

I tried it in WAS 6.1 and it worked in the container no problems. You have to toString it or it will try to return the object.

mugafuga

like image 194
branchgabriel Avatar answered Sep 21 '22 23:09

branchgabriel


There's a definitive way...

grailsApplication.parentContext.getResource("dir/or/file").file.toString()

Out of controllers (ex. bootstrap)? Just inject..

def grailsApplication

Best regards!

like image 44
Wanderson Santos Avatar answered Sep 24 '22 23:09

Wanderson Santos


Grails, when it's run in dev mode, provides a whole host of environment properties to its Gant scripts and the app in turn, including basedir.

Take a look at the grails.bat or grails.sh script and you will find these lines:

Unix: -Dbase.dir="." \
Windows: set JAVA_OPTS=%JAVA_OPTS% -Dbase.dir="."

When these scripts start your environment in dev mode you get these thrown in for free.

When you take the WAR and deploy you no longer use these scripts and therefore you need to solve the problem another way; you can either

  1. Specify the property yourself to the startup script for the app server, eg: -Dbase.dir=./some/dir .. however
    • ... it usually makes more sense to use the Grails Config object which allows for per-environment properties
like image 35
j pimmel Avatar answered Sep 22 '22 23:09

j pimmel