1

I have a singleton class

@Singleton
class CustomerBundleSingleton {

    def grailsApplication = Holders.getGrailsApplication()
    String projName
    private CustomerBundleSingleton() {

        line 10: projName = // how to get sub-project name here ???

    }

}

application.properties // my project is running
-----------------------
app.name = MyNewProject


application.properties // located in sub project
-----------------------
app.name = MySubProject

I tried grailsApplication.metadata['app.name'] in "line 10:" it returns "MyNewProject".Whereas I want a way to get the project name of the UserBundleSingleton located (MySubProjectName). Something like grailsApplication.current.metadata['app.name'] ???? .

So that it can give me back MySubProjectName instead of MyNewProject??

user2001627
  • 257
  • 2
  • 16
  • By "subproject" you mean a plugin? –  May 02 '13 at 19:25
  • Yes that is a plugin under the main project. – user2001627 May 02 '13 at 19:33
  • http://stackoverflow.com/questions/9809566/how-to-get-grails-application-name-within-config-groovy Try `grails.util.Metadata.current.'app.name'`. –  May 02 '13 at 20:14
  • I have grailsApplication inside my class, if i try `grailsApplication.util.Metadata.current.'app.name'` gives me "util is not a defined property". If I use `Holders.getGrailsApplication().metadata.current.'app.name'` I am getting back cannot call `app.name` on null object – user2001627 May 02 '13 at 21:15
  • The Matadata is a class, not an attribute of grailsApplication. –  May 02 '13 at 21:45
  • @SérgioMichels yeah i tried something like this : `Holders.getGrailsApplication().getMetadata().getCurrent().get("app.name")` it again gave me 'MyNewProject' instead of `MySubProject` – user2001627 May 02 '13 at 21:57
  • @user2001627 Metadata is a separate class. If it helps, you can do `import grails.util.Metadata` and then `def appVersion = Metadata.getCurrent().'app.version'`. I would say you don't need the grailsApplication at all. – lucke84 May 03 '13 at 08:58
  • @lucke84 I tried that too but it is still giving me `MyNewProject` not `MySubProject` – user2001627 May 03 '13 at 15:15

1 Answers1

1

I have 3 suggestions depending on your requirements and your 'bundling'.

1) You don't have a bundle marker/descriptor

Assuming that you know the sub-project(Grails plugin) name, your life gets easier, instead of having to loop through all plugins...

You can probably use something among these lines.

// Plugin name is 'hibernate' in this example

import org.codehaus.groovy.grails.plugins.PluginManagerHolder

def hibernateVersion = PluginManagerHolder.pluginManager.getGrailsPlugin('hibernate').version

// Loop through all plugins
// PluginManagerHolder.pluginManager.getAllPlugins()

2) Using custom plugin properties to lookup plugins of interest

Other strategy, if you must lookup the bundle dynamically.

Create a custom marker property in each of your plugin descriptors

def specialProperty = "whatever"

Then inside your CustomerBundleSingleton

PluginManagerHolder.pluginManager.getAllPlugins().each {
     if (it.properties.specialProperty) {
               def subProjectName = it.name
               def subProjectVersion = it.version
     } 
}

3) Custom bundle info resolution

You may also want to consider some metadata via META-INF/MANIFEST.MF or similar mechanism.

Hope it helps...

rimero
  • 2,383
  • 1
  • 14
  • 8
  • Thank you for the quick reply. To get the property app.name from application.properties inside a the sub-project isn't there any simple way to get it like grailsApplication.util.Metadata.current.'app.name'?? of course this line didnt work :( but I thought there could be simple way to get it. I am a new bee for grails/groovy.If there is no other way then i will start trying your inputs one by one. – user2001627 May 02 '13 at 20:15
  • I don't know any other way, but also don't know your exact requirements. You could do grailsApplication.util.Metadata.getInstalledPlugins (http://grails.org/doc/latest/api/grails/util/Metadata.html#getInstalledPlugins()) -> You still need to know what to look for though... – rimero May 02 '13 at 20:26