27 May 2009

Finding MBean names using Groovy Collections

Needed to do me some searching of the WebLogic Server MBean runtime domain looking for MBeans that matched some part of a specified string -- it's name, it's type, etc.

This was the best I could do in Groovy:
def matches(con, name) { 
con.queryNames(new ObjectName("*:*"), null).findAll{
it.toString() =~ name
}.join("\n")
}
which can be called such as:
println matches(con, "Domain")
to produce output such as:
com.bea:ServerRuntime=examplesServer,Name=weblogic.logging.DomainLogBroadcasterClient,Type=MinThreadsConstraintRuntime
com.bea:ServerRuntime=examplesServer,Name=DomainLog,Type=WLDFFileArchiveRuntime,WLDFRuntime=WLDFRuntime
com.bea:ServerRuntime=examplesServer,Name=DomainLog,Type=WLDFDataAccessRuntime,WLDFAccessRuntime=Accessor,WLDFRuntime=WLDFRuntime
com.bea:ServerRuntime=examplesServer,Name=weblogic.logging.DomainLogBroadcasterClient,Type=WorkManagerRuntime
com.bea:Name=wl_server,Type=Domain
OK, what about checking for multiple names at once?
def matchesAny(con, map) { 
map.collect() { name ->
matches(con, name)
}.join("\n")
}
which can be called such as:
println matchesAny(con, ["Domain", "Kodo"])
to produce output such as:
com.bea:ServerRuntime=examplesServer,Name=weblogic.logging.DomainLogBroadcasterClient,Type=MinThreadsConstraintRuntime
com.bea:ServerRuntime=examplesServer,Name=DomainLog,Type=WLDFFileArchiveRuntime,WLDFRuntime=WLDFRuntime
com.bea:ServerRuntime=examplesServer,Name=DomainLog,Type=WLDFDataAccessRuntime,WLDFAccessRuntime=Accessor,WLDFRuntime=WLDFRuntime
com.bea:ServerRuntime=examplesServer,Name=weblogic.logging.DomainLogBroadcasterClient,Type=WorkManagerRuntime
com.bea:Name=wl_server,Type=Domain
com.bea:ServerRuntime=examplesServer,Name=reviewService,ApplicationRuntime=ejb30,Type=KodoQueryCompilationCacheRuntime,EJBComponentRuntime=domain.jar,KodoPersistenceUnitRuntime=reviewService
com.bea:ServerRuntime=examplesServer,Name=reviewSession,ApplicationRuntime=ejb30,Type=KodoQueryCompilationCacheRuntime,EJBComponentRuntime=domain.jar,KodoPersistenceUnitRuntime=reviewSession
com.bea:ServerRuntime=examplesServer,Name=reviewService,ApplicationRuntime=ejb30,Type=KodoPersistenceUnitRuntime,EJBComponentRuntime=domain.jar
com.bea:ServerRuntime=examplesServer,Name=reviewSession,ApplicationRuntime=ejb30,Type=KodoPersistenceUnitRuntime,EJBComponentRuntime=domain.jar
Easy peasy and served my needs.

LOVE THE GROOVY

No comments: