tags:

views:

28

answers:

2

hi,

I want to add linux based or windows based system properties in Grails as my app needs to run in the both. I know that we can add grails.config.locations location specified in Config. groovy.

But I need the if and esle condition for the file to be picked. the problem is config.grrovy has userHome grailsHome appName appVersion I would need something like osName. Either I can go ahead with syetm.properties or if soembody can tell me how these (only) properties are availble in Config.groovy (through DefaultGrailsApplication or otherwise. that woyuld be great.

Also, somewjhat moer elegant would be if where I need those properties I make my service as user-defined-spring-bean. Would that be right and feasible approach?If yes, some example

A: 

Create a custom enviorenment for both Windows and Linux. Something like the following should work if placed in config.groovy

 environments {
 productionWindows {
 filePath=c:\path
 }
 productionLinux {
 filePath=/var/dir
 }
 }

You should then be able to use the grails config object to get the value of filePath reguardless of weather your on Windows or Linux. For more details on this see section 3.2 of http://www.grails.org/doc/1.0.x/guide/3.%20Configuration.html If you wanted to create a war file to run on Linux you would execute the following command.

grails -Dgrails.env=productionLinux war

And then to get a file path you stored in config.groovy for the specific environment your running in.

def fileToOpen=Conf.config.filePath

fileToOpen will contain the value you assigned to filePath in your config.groovy based on the environment your currently running as, so when running with productionLinux as the environment it will contain the value /var/dir

Jared
Thanks @Jared. but I believe enviromnet{ dev.... and similar struure get passed as env variable in grails while run-app or war. how I gonna pass that then. I mean my problem is if grails does some more work for standard env and those wont get kicked in or if there is a better way to set this variables (productionWindows et al.. ) then instead of command line or env.
See the "packaging and running for different environments" section in the link I sent. So something like
Jared
I added more info to my answer.
Jared
A: 

You could do something like this in your Config.groovy:

environments {
    development {
        if (System.properties["os.name"] == "Linux") {
            grails.config.locations = [ "file:$basedir/grails-app/conf/linux.properties" ]
        } else {
            grails.config.locations = [ "file:$basedir/grails-app/conf/windows.properties" ]
        }
    }
    ...
}

Alternatively, for a service based approach, you could bundle up all the OS specific behavior into implementations of service interface. For example:

// OsPrinterService.groovy
interface OsPrinterService {
    void printOs();
}

// LinuxOsPrinterService.groovy
class LinuxOsPrinterService implements OsPrinterService {
    void printOs() { println "Linux" }
}

// WindowsOsPrinterService.groovy
class WindowsOsPrinterService implements OsPrinterService {
    void printOs() { println "Windows" }
}

Then instantiate the correct one in grails-app/conf/spring/resources.groovy like so:

beans = {
    if (System.properties["os.name"] == "Linux") {
        osPrinterService(LinuxOsPrinterService) {}
    } else {
        osPrinterService(WindowsOsPrinterService) {}
    }
}

Then the correct service will be automatically injected into your objects by spring.

ataylor
Thanks @ataylor, thats what i am thinking of too. Plus the bean initializaton way which I didnt have the handle of. Also, read somehwre that we can have something similar if we override DefaultGrailsApplication. But docs has more info on Plugins with that reagrd. And I am honestly new to to GnG. leave alone pligin. Was wondering if there is an eay way otherwise will go with the if...n else.