Reputation: 1334
I'm trying to integrate a legacy java/spring app into my grails app. This code uses a lot of custom properties which don't appear to be available when I wire the legacy app context stuff in.
If I load them individually in Config.groovy, things start to work, but I'd really like a programatic way of doing it (meaning load up the legacy properties object and insert them into the grails config).
What's the best way of doing this? Bootstrap init seems too late, the appContext's already been refreshed at that point and it's thrown an exception about an unresolved property.
Upvotes: 1
Views: 194
Reputation: 122364
Within your Config.groovy
you can do a basic trick like
Properties legacyProps = // whatever you need to do to load the legacy properties
for(String propName in legacyProps.stringPropertyNames()) {
setProperty(propName, legacyProps.getProperty(propName))
}
which would work for property names which don't include any dots. Properties that do include dots would be added to the Grails config but only as flat keys, not hierarchical, i.e.
grailsApplication.config.'property.with.dots'
as opposed to
grailsApplication.config.property.with.dots
If you want them adding in proper hierarchical form you can do that with Groovy tricks provided you can be sure that there's no cases in the legacy properties where you have one key that is a prefix of another, i.e.
my.app.foo=bar
my.app.bar=baz
is OK, but
my.app.foo=bar
my.app=baz
is not OK. If the properties satisfy this then try something like
Properties legacyProps = // whatever you need to do to load the legacy properties
for(String propName in legacyProps.stringPropertyNames()) {
String[] propParts = propName.split(/\./)
if(propParts.size() == 1) {
// no dots
setProperty(propName, legacyProps.getProperty(propName))
} else {
// we have dots - get the first segment (which is a ConfigObject)
ConfigObject co = getProperty(propParts[0])
if(propParts.size() > 2) {
// then apply all but the last segment to that to get the last parent
co = propParts[1..-2].inject(co) { o, part -> o."${part}" }
}
// then set final segment on the last parent ConfigObject
co."${propParts[-1]}" = legacyProps.getProperty(propName)
}
}
Upvotes: 5