# 5
Here are a couple of the methods that do most of the work. This assumes that you already have a reference to the managed bean server to iterate through the beans that it controls.
/** Retrieves the runtime name of a service
* @param collabName Name of service to retrieve runtime name for
* @return String Runtime deployment name
* @throws Exception
*/
public String getBeanRuntimeName(final String collabName)
throws Exception {
final String METHOD_NAME = "getBeanRuntimeName";
final String J2EE_APPLICATION = "J2EEApplication";
System.out.println(new StringBuffer(METHOD_NAME).append(" >>").toString());
String runtimeName = null;
String correctBeanName = new StringBuffer(collabName).append(".jar").toString();
final ObjectName objectName = new ObjectName(new StringBuffer("com.sun.appserv:j2eeType=StatelessSessionBean,name=").append(collabName)
.append(",EJBModule=").append(correctBeanName)
.append(",*,J2EEServer=server,category=runtime").toString());
final Set beanNames = this.mbeanServer.queryNames(objectName,null);
if (beanNames.isEmpty()){
System.out.println(new StringBuffer(METHOD_NAME).append(" Error: Did not locate any beans matching query").toString());
} else {
for (Iterator iterator = beanNames.iterator(); iterator.hasNext();) {
ObjectName objRetrieved = (ObjectName) iterator.next();
runtimeName = (String)this.mbeanServer.getAttribute(objRetrieved,J2EE_APPLICATION);
}
}
System.out.println(new StringBuffer(METHOD_NAME).append(" Retrieved J2EEAppliation name:").append(runtimeName).toString());
System.out.println(new StringBuffer(METHOD_NAME).append(" <<").toString());
return runtimeName;
}
/** Retrieves a Managed Bean
* @param domain Name of domain
* @param runtimeName Name of runtime project for bean
* @param deploymentProfile Name of deployment profile
* @param serviceName Name of service
* @throws MalformedObjectNameException, MalformedObjectNameException, Exception
*/
public ObjectName findManagedBean(final String domain, final String runtimeName, final String deploymentProfile, final String serviceName) throws MalformedObjectNameException, Exception {
final String METHOD_NAME = "findManagedBean";
System.out.println(new StringBuffer(METHOD_NAME).append(" >>").toString());
// create pattern to match the key
final String patternString = new StringBuffer( "^(" ).append( runtimeName ).append( ").*(" ).append( deploymentProfile ).append( "\\|" ).append( serviceName ).append( ")$" ).toString();
System.out.println(new StringBuffer(METHOD_NAME).append(" Searching for pattern:").append(patternString).toString());
final Pattern p = Pattern.compile( patternString);
final String objectNamePattern = new StringBuffer(domain).append(":*").toString();
System.out.println(new StringBuffer(METHOD_NAME).append(" Using object name pattern:").append(objectNamePattern).toString());
final ObjectName searchObjectName = new ObjectName( objectNamePattern);
final Set beanNames = this.mbeanServer.queryNames( searchObjectName, null );
ObjectName objectName = null;
String fullBeanPathName = null;
if (beanNames.isEmpty()) {
System.out.println(new StringBuffer(METHOD_NAME).append("Error: Did not locate any beans matching query").toString());
} else {
for (Iterator iterator = beanNames.iterator(); iterator.hasNext();) {
objectName = (ObjectName) iterator.next();
fullBeanPathName = objectName.getKeyProperty( "Name" );
if (fullBeanPathName != null) {
Matcher m = p.matcher( fullBeanPathName );
if (m.find()) {
System.out.println(new StringBuffer(METHOD_NAME).append(" Found bean:").append(objectName.getKeyPropertyListString()).toString());
break;
}
}
}
}
System.out.println(new StringBuffer(METHOD_NAME).append(" <<").toString());
return objectName;
}
The code to invoke the desired operation is as follows (we pass an integer to indicate the operation since the eDesigner doesn't support 1.5 in which case, we would use enums as they were meant to be used). The ServiceControl object is a class we created to control our services:
/*
Executes the requested operation on the bean
* @param objectName Bean to operate on
* @throws Exception
*/
protected void execute( final ObjectName objectName )
throws Exception
{
final String METHOD_NAME = "execute";
final int operationToInvoke = Integer.parseInt( this.inputMessage.retrieveUserProperty( "operation" ) );
final String logicalHostName = InetAddress.getLocalHost().getHostName();
final ServiceControl serviceControl = ServiceControlFactory.getInstance( collabContext, logicalHostName );
final MBeanServer mbeanServer = this.serviceControl.getManagedBeanServer();
boolean isValidOperation = false;
final String status = mbeanServer.getAttribute( objectName, "Status" ).toString().toUpperCase();
switch (operationToInvoke) {
case ServiceControl.STOP_MBEAN :
{
isValidOperation = true;
mbeanServer.invoke( objectName, "stop", null, null );
break;
}
case ServiceControl.START_MBEAN :
{
isValidOperation = true;
mbeanServer.invoke( objectName, "start", null, null );
break;
}
case ServiceControl.RESTART_MBEAN :
{
isValidOperation = true;
mbeanServer.invoke( objectName, "stop", null, null );
mbeanServer.invoke( objectName, "start", null, null );
break;
}
}
if (!isValidOperation) {
System.out.println( "Invalid operation" );
}