1 22 23 package org.objectweb.petals.system.launch; 24 25 import java.io.IOException ; 26 import java.util.HashMap ; 27 import java.util.Hashtable ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import javax.jbi.management.DeploymentServiceMBean; 32 import javax.jbi.management.InstallationServiceMBean; 33 import javax.management.MBeanServer ; 34 import javax.management.MBeanServerConnection ; 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.ObjectName ; 37 import javax.management.remote.JMXConnector ; 38 import javax.management.remote.JMXConnectorFactory ; 39 import javax.management.remote.JMXServiceURL ; 40 41 import org.objectweb.fractal.api.Component; 42 import org.objectweb.fractal.api.control.ContentController; 43 import org.objectweb.fractal.api.control.LifeCycleController; 44 import org.objectweb.fractal.jmx.agent.AdminAttributes; 45 import org.objectweb.fractal.jmx.agent.Introspector; 46 import org.objectweb.fractal.util.Fractal; 47 48 import org.objectweb.petals.jbi.management.service.AdminServiceMBean; 49 import org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean; 50 import org.objectweb.petals.util.SystemUtil; 51 52 53 59 public class MBeanHelper { 60 public static void registerMBeans(Component petalsComposite) throws Exception { 61 62 String componentName = "agent-jmx"; 63 ContentController contentController = Fractal 64 .getContentController(petalsComposite); 65 Component agent = FractalHelper.getComponentByName(contentController, componentName); 66 67 String interfaceName = "admin"; 68 Introspector.CURRENCY_TIME_LIMIT = "-1"; 69 interfaceName = "attribute-controller"; 70 AdminAttributes adminAttributes = (AdminAttributes) agent 71 .getFcInterface(interfaceName); 72 73 MBeanServer jmxServer = adminAttributes.getRawMBeanServer(); 74 75 LifeCycleController lcc = Fractal 77 .getLifeCycleController(petalsComposite); 78 79 jmxServer.registerMBean(Introspector.createMBean(lcc), new ObjectName ( 80 "FC/Petals@" + lcc.hashCode() + ":itf=lifecycle-controller")); 81 82 Component jbi = FractalHelper.getComponentByName(contentController, "jbi"); 84 ContentController contentControllerForJBI = Fractal 85 .getContentController(jbi); 86 registerComponent(contentControllerForJBI, "admin-impl", jmxServer, 87 AdminServiceMBean.class, "Petals:name=Admin,type=service"); 88 89 registerComponent(contentControllerForJBI, "deployment-impl", 91 jmxServer, DeploymentServiceMBean.class, 92 "Petals:name=Deployment,type=service"); 93 94 registerComponent(contentControllerForJBI, "installation-impl", 96 jmxServer, InstallationServiceMBean.class, 97 "Petals:name=Installation,type=service"); 98 99 registerComponent(contentController, "petalsadmin-service", jmxServer, 101 PetalsAdminServiceMBean.class, 102 "Petals:name=PetalsAdmin,type=service"); 103 } 104 105 121 protected static void registerComponent(ContentController contentController, 122 String fractalName, MBeanServer jmxServer, Class mbeanClass, 123 String mbeanName) throws Exception { 124 Component systemState = FractalHelper.getComponentByName(contentController, 125 fractalName); 126 Object object = systemState.getFcInterface("service"); 127 128 jmxServer.registerMBean(Introspector.createMBean(object, mbeanClass), 129 new ObjectName (mbeanName)); 130 } 131 132 133 139 public static MBeanServerConnection retrieveJMXServer() throws IOException { 140 String jmxHost = SystemUtil.getHost(); 143 String jmxPort = SystemUtil.getJmxPort(); 144 String username = SystemUtil.getJmxUser(); 145 String password = SystemUtil.getJmxPassword(); 146 147 JMXConnector connector = null; 148 Map <String , Object > args = new HashMap <String , Object >(); 149 if (!"".equals(username)) { 150 if (password == null) { 151 password = ""; 152 } 153 String [] credentials = new String [] {username, password}; 154 args.put("jmx.remote.credentials", credentials); 155 } 156 String urlStr = "service:jmx:rmi:///jndi/rmi://" + jmxHost + ":" 157 + jmxPort + "/management/rmi-jmx-connector"; 158 JMXServiceURL url = new JMXServiceURL (urlStr); 159 connector = retrieveJMXConnector(url, args); 160 return connector.getMBeanServerConnection(); 161 } 162 163 173 public static ObjectName retrieveServiceMBean(String serviceName, 174 MBeanServerConnection jmxServer) throws MalformedObjectNameException , 175 NullPointerException , IOException { 176 177 ObjectName result = null; 178 179 Hashtable <String , String > attributes = new Hashtable <String , String >(); 181 attributes.put("name", serviceName); 182 attributes.put("type", "service"); 183 ObjectName objName = new ObjectName ("Petals", attributes); 184 185 Set objNames = jmxServer.queryNames(objName, null); 186 if (objNames != null && objNames.size() == 1) { 187 result = (ObjectName ) objNames.iterator().next(); 188 } 189 return result; 190 } 191 192 195 protected static JMXConnector retrieveJMXConnector(JMXServiceURL url, 196 Map <String , ?> args) throws IOException { 197 return JMXConnectorFactory.connect(url, args); 198 } 199 200 201 } 202 | Popular Tags |