Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Gemini/Management/Examples"

(New page: {{GeminiManagement}} Category:Gemini Management Category:Gemini Category:EclipseRT == Examples ==)
 
(Introspect a single bundle)
 
(One intermediate revision by one other user not shown)
Line 3: Line 3:
  
 
== Examples ==
 
== Examples ==
 +
 +
== Accessing Gemini Management MBeans from a Groovy Script ==
 +
 +
Create a server connection:
 +
 +
<source lang="groovy">
 +
import groovy.jmx.builder.JmxBuilder
 +
import javax.management.MBeanServerConnection
 +
 +
def client = new JmxBuilder().connectorClient(port: 1234)
 +
 +
print("connecting to JMX server...")
 +
client.connect()
 +
println("established.")
 +
 +
MBeanServerConnection serverConnection = client.getMBeanServerConnection()
 +
</source>
 +
 +
== Query the list of available bundles ==
 +
 +
<source lang="groovy">
 +
ObjectName objectName = serverConnection.queryNames(new ObjectName("osgi.core:type=bundleState,*"), null).first()
 +
TabularData data = serverConnection.invoke(objectName, "listBundles", null, null)
 +
 +
println("List of available bundles:")
 +
data.values().each { println "[" + it.get("Identifier") + "] - " + it.get("State") + " -  " + it.get("SymbolicName")}
 +
</source>
 +
Sample output:
 +
 +
<source lang="text">
 +
List of available bundles:
 +
[0] - ACTIVE - org.eclipse.osgi
 +
[1] - ACTIVE - org.eclipse.osgi.services
 +
[2] - ACTIVE - osgi.enterprise
 +
[3] - ACTIVE - org.eclipse.gemini.management
 +
</source>
 +
 +
== Query the available info per bundle ==
 +
 +
<source lang="groovy">
 +
ObjectName objectName = serverConnection.queryNames(new ObjectName("osgi.core:type=bundleState,*"), null).first()
 +
TabularData data = serverConnection.invoke(objectName, "listBundles", null, null)
 +
 +
println("List of available info:")
 +
println data.values().first().compositeType.keySet().each {println it}
 +
</source>
 +
 +
Sample output:
 +
 +
<source lang="text">
 +
List of available info:
 +
ActivationPolicyUsed
 +
ExportedPackages
 +
Fragment
 +
Fragments
 +
Headers
 +
...
 +
</source>
 +
 +
== Introspect a single bundle ==
 +
 +
The following snippet checks if the bundle with ID 3 is a fragment, then prints all bundle headers.
 +
 +
<source lang="groovy">
 +
long bundleId = 3
 +
def args = [bundleId] as Object[]
 +
def signature = ["long"] as String[]
 +
def isFragment = serverConnection.invoke(objectName, "isFragment", args, signature)
 +
println "Is bundle $bundleId a fragment? $isFragment"
 +
 +
println "Bundle $bundleId has the following bundle headers:"
 +
def headers = serverConnection.invoke(objectName, "getHeaders", args, signature)
 +
headers.values().each { println it.get("Key") + " = " + it.get("Value") }
 +
</source>
 +
 +
Sample output:
 +
 +
<source lang="text">
 +
Is bundle 3 a fragment? false
 +
Bundle 3 has the following bundle headers:
 +
Manifest-Version = 1.0
 +
Archiver-Version = Plexus Archiver
 +
Created-By = Apache Maven
 +
Built-By = cgfrost
 +
Build-Jdk = 1.6.0_45
 +
Export-Package = org.osgi.jmx;uses:="javax.management.openmbean";version="1.1", [...]
 +
Bundle-Vendor = Eclipse Gemini Management
 +
Bundle-Version = 2.0.0.RELEASE
 +
Bundle-Name = Gemini Management
 +
...
 +
</source>

Latest revision as of 09:12, 24 September 2013

Examples

Accessing Gemini Management MBeans from a Groovy Script

Create a server connection:

import groovy.jmx.builder.JmxBuilder
import javax.management.MBeanServerConnection
 
def client = new JmxBuilder().connectorClient(port: 1234)
 
print("connecting to JMX server...")
client.connect()
println("established.")
 
MBeanServerConnection serverConnection = client.getMBeanServerConnection()

Query the list of available bundles

ObjectName objectName = serverConnection.queryNames(new ObjectName("osgi.core:type=bundleState,*"), null).first()
TabularData data = serverConnection.invoke(objectName, "listBundles", null, null)
 
println("List of available bundles:")
data.values().each { println "[" + it.get("Identifier") + "] - " + it.get("State") + " -  " + it.get("SymbolicName")}

Sample output:

List of available bundles:
[0] - ACTIVE - org.eclipse.osgi
[1] - ACTIVE - org.eclipse.osgi.services
[2] - ACTIVE - osgi.enterprise
[3] - ACTIVE - org.eclipse.gemini.management

Query the available info per bundle

ObjectName objectName = serverConnection.queryNames(new ObjectName("osgi.core:type=bundleState,*"), null).first()
TabularData data = serverConnection.invoke(objectName, "listBundles", null, null)
 
println("List of available info:")
println data.values().first().compositeType.keySet().each {println it}

Sample output:

List of available info:
ActivationPolicyUsed
ExportedPackages
Fragment
Fragments
Headers
...

Introspect a single bundle

The following snippet checks if the bundle with ID 3 is a fragment, then prints all bundle headers.

long bundleId = 3
def args = [bundleId] as Object[]
def signature = ["long"] as String[]
def isFragment = serverConnection.invoke(objectName, "isFragment", args, signature)
println "Is bundle $bundleId a fragment? $isFragment"
 
println "Bundle $bundleId has the following bundle headers:"
def headers = serverConnection.invoke(objectName, "getHeaders", args, signature)
headers.values().each { println it.get("Key") + " = " + it.get("Value") }

Sample output:

Is bundle 3 a fragment? false
Bundle 3 has the following bundle headers:
Manifest-Version = 1.0
Archiver-Version = Plexus Archiver
Created-By = Apache Maven
Built-By = cgfrost
Build-Jdk = 1.6.0_45
Export-Package = org.osgi.jmx;uses:="javax.management.openmbean";version="1.1", [...]
Bundle-Vendor = Eclipse Gemini Management
Bundle-Version = 2.0.0.RELEASE
Bundle-Name = Gemini Management
...

Copyright © Eclipse Foundation, Inc. All Rights Reserved.