1 package org.mr.core.cmc; 2 3 50 51 import com.sun.jdmk.comm.HtmlAdaptorServer; 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 import org.mr.MantaAgent; 55 import org.mr.MantaException; 56 import org.mr.core.net.cmc.GetConnectionsJMX; 57 import org.mr.core.stats.cmc.GetStatsJMX; 58 import org.mr.kernel.world.cmc.GetServiceParticipationJMX; 59 import org.mr.kernel.world.cmc.GetWorldModelerVersionJMX; 60 import org.mr.plugins.discovery.ADControlSender; 61 62 import javax.management.*; 63 import javax.management.remote.JMXConnectorServer ; 64 import javax.management.remote.JMXConnectorServerFactory ; 65 import javax.management.remote.JMXServiceURL ; 66 import java.io.IOException ; 67 import java.net.MalformedURLException ; 68 import java.rmi.registry.LocateRegistry ; 69 import java.util.Set ; 70 71 78 public class MantaJMXManagement { 79 private MBeanServer server; 80 private static MantaJMXManagement instance = null; 81 protected Log log; 82 int namingPort; 84 ObjectName objectName; 85 String host = null; 86 ObjectInstance rmiInstance; 87 String jndiPath; 88 JMXServiceURL url; 89 JMXConnectorServer connectorServer; 90 ObjectName serverName; 92 ObjectName processorName; 93 ObjectInstance httpInstance; 94 97 private boolean started = false; 98 99 105 public static synchronized MantaJMXManagement getInstance() { 106 if (instance == null){ 107 instance = new MantaJMXManagement(); 108 instance.init(); 109 } 110 return instance; 111 } 113 117 118 private void init() { 119 log=LogFactory.getLog("MantaJMXManagement"); 120 boolean startHttpAdaptor= MantaAgent.getInstance().getSingletonRepository() 121 .getConfigManager().getBooleanProperty("management.jmx.httpAdaptor.enabled", false); 122 boolean startRMIConnector= MantaAgent.getInstance().getSingletonRepository() 123 .getConfigManager().getBooleanProperty("management.jmx.rmiConnector.enabled", false); 124 125 126 127 if(startHttpAdaptor == true || startRMIConnector == true){ 128 if(log.isInfoEnabled()) 129 log.info("Initializing the MantaJMXManagement component"); 130 MantaJMXManagement mantaJMXManagement = getInstance(); 131 mantaJMXManagement.server = MBeanServerFactory.createMBeanServer(); 132 ObjectName objectName = null; 133 try { 134 objectName = new ObjectName("JMImplementation:type=MBeanServerDelegate"); 135 } catch (MalformedObjectNameException e) { 136 if(log.isErrorEnabled()){ 137 log.error("Unknown object name 'JMImplementation:type=MBeanServerDelegate'",e); 138 139 } 141 } 142 NotificationListener listener = new NotificationListener() 143 { 144 public void handleNotification(Notification notification, Object handback) 145 { 146 if(log.isInfoEnabled()) 147 log.info(notification.toString()); 148 } 149 }; 150 try { 151 mantaJMXManagement.server.addNotificationListener(objectName, listener, null, null); 153 } catch (InstanceNotFoundException e1) { 154 if(log.isErrorEnabled()){ 155 log.error("addNotificationListener() had instance not found exception. ", e1); 156 158 } 159 } 160 161 162 } 163 } 165 public synchronized boolean startConnections() { 166 if (started) return true; 167 boolean startHttpAdaptor= MantaAgent.getInstance().getSingletonRepository() 168 .getConfigManager().getBooleanProperty("management.jmx.httpAdaptor.enabled", false); 169 boolean startRMIConnector= MantaAgent.getInstance().getSingletonRepository() 170 .getConfigManager().getBooleanProperty("management.jmx.rmiConnector.enabled", false); 171 try { 172 if(startHttpAdaptor) 173 makeHttpAdaptor(); 174 if(startRMIConnector) 175 makeRMIConnector(); 176 } catch (MantaException e) { 177 if (log.isErrorEnabled()){ 178 log.error("Had a problem creating an RMI connection or HTTP Adaptor: "+ e.getMessage()); 179 } 180 return false; 181 } 182 started = true; 183 return true; 184 } 186 191 private void makeRMIConnector() throws MantaException{ 192 String host =null; 194 host=ADControlSender.getValidLocalAddress(); 196 try { 197 int namingPort= MantaAgent.getInstance().getSingletonRepository() 198 .getConfigManager().getIntProperty("management.jmx.rmiConnector.rmiPort",1099); 199 200 jndiPath = "/jmxconnector"; 201 LocateRegistry.createRegistry(namingPort); 203 url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://"+host+":" + namingPort + jndiPath); 205 206 connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); 208 connectorServer.start(); 209 210 if (log.isInfoEnabled()) 211 log.info("Set port value for the JMX-RMI server. The given port: " + namingPort); 212 } catch (MalformedURLException e) { 213 if(log.isErrorEnabled()) 214 log.error("problem with the defined jmx.rmiPort. can't create an rmi connection to management system. ", e); 215 throw new MantaException(e.getMessage()+" The jmx.rmiPort defined might already be in use. Its value can be changed in configuration files. ", MantaException.ID_INVALID_ARGUMENTS); 216 } catch (IOException e) { 217 if(log.isErrorEnabled()) 218 log.error("IOEception. ", e); 219 throw new MantaException(e.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL); 220 } 221 } 223 228 private void makeHttpAdaptor() throws MantaException { 229 int port = MantaAgent.getInstance().getSingletonRepository() 231 .getConfigManager().getIntProperty("management.jmx.httpAdaptor.httpPort",8080); 232 234 HtmlAdaptorServer adaptorServer = new HtmlAdaptorServer(); 235 236 adaptorServer.setPort(port); 237 238 addManagedObject(adaptorServer, "HtmlAdaptor:type=htmladapter,port="+port); 239 if (log.isInfoEnabled()) 240 log.info("Set port value for the HTTP Adaptor. The given port: " + port); 241 adaptorServer.start(); 242 243 } 244 245 258 public void addManagedObject(Object managedOBJ, String managedObjectName) throws MantaException { 259 ObjectName objectName; 260 try { 261 objectName = new ObjectName(managedObjectName); 262 if(server!=null){ 263 server.registerMBean(managedOBJ, objectName); 264 } 265 } catch (MalformedObjectNameException e) { 266 if(log.isErrorEnabled()) 267 log.error("Unkown name of the managed object. ",e); 268 throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS); 269 }catch (InstanceAlreadyExistsException e1) { 270 if(log.isErrorEnabled()) 271 log.error("The parameters of that managed object already exsists. ",e1); 272 throw new MantaException(e1.getMessage(), MantaException.ID_INVALID_ARGUMENTS); 273 } catch (MBeanRegistrationException e1) { 274 if(log.isErrorEnabled()) 275 log.error("Problem in registration of the new manged object. ",e1); 276 throw new MantaException(e1.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL); 277 } catch (NotCompliantMBeanException e1) { 278 if(log.isErrorEnabled()) 279 log.error("Problem with the implementation/definations of the managed object. ",e1); 280 throw new MantaException(e1.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL); 281 } 282 } 284 288 public Set getManagedObjects(){ 289 Set myMBeans= server.queryMBeans(null, null); 290 return myMBeans; 291 } 293 300 public Set getManagedObjects(String topic)throws MantaException{ 301 ObjectName objectName; 302 try { 303 objectName = new ObjectName(topic+":*"); 304 } catch (MalformedObjectNameException e) { 305 if(log.isErrorEnabled()) 306 log.error("Unkown name of the managed object. ",e); 307 throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS); 308 } 309 Set myMBeans= server.queryMBeans(objectName, null); 310 return myMBeans; 311 } 313 322 public Set getManagedObject(String managedObjectName) throws MantaException{ 323 ObjectName objectName; 324 try { 325 objectName = new ObjectName(managedObjectName); 326 } catch (MalformedObjectNameException e) { 327 if(log.isErrorEnabled()) 328 log.error("Unkown name of the managed object. ",e); 329 throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS); 330 } 331 Set myMBean= server.queryMBeans(objectName, null); 332 return myMBean; 333 } 335 342 public void removeManagedObject(String managedObjectName) throws MantaException{ 343 ObjectName objectName; 344 try { 345 objectName = new ObjectName(managedObjectName); 346 if(server!=null){ 347 server.unregisterMBean(objectName); 348 server.unregisterMBean(objectName); 349 } 350 } catch (MalformedObjectNameException e) { 351 if(log.isErrorEnabled()) 352 log.error("Unkown name of the managed object. ",e); 353 throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS); 354 } catch (InstanceNotFoundException e1) { 355 if(log.isErrorEnabled()) 356 log.error("Instance not found exception. could refer to the managed object's class. ",e1); 357 throw new MantaException(e1.getMessage(), MantaException.ID_INVALID_ARGUMENTS); 358 } catch (MBeanRegistrationException e1) { 359 if(log.isErrorEnabled()) 360 log.error("Problem with the unRegistration of the new manged object. ",e1); 361 throw new MantaException(e1.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL); 362 } 363 } 365 369 public static void registerJMXBeans() { 370 try { 371 boolean startHttpAdaptor= MantaAgent.getInstance().getSingletonRepository() 372 .getConfigManager().getBooleanProperty("management.jmx.httpAdaptor.enabled", false); 373 boolean startRMIConnector= MantaAgent.getInstance().getSingletonRepository() 374 .getConfigManager().getBooleanProperty("management.jmx.rmiConnector.enabled", false); 375 376 if(startHttpAdaptor == true || startRMIConnector == true){ 377 MantaJMXManagement manager = MantaJMXManagement.getInstance(); 378 manager.addManagedObject(new GarbageCollectionJMX(),"MantaRay:function=garbadgeCollector"); 379 manager.addManagedObject(new GetCurrentTimeJMX(), "MantaRay:time=gets the time"); 380 manager.addManagedObject(new GetMemStatJMX(), "MantaRay:memory=gets the memory status"); 381 manager.addManagedObject(new GetConnectionsJMX(), "MantaRay:connections=get layers connected"); 382 manager.addManagedObject(new GetServiceParticipationJMX(), "MantaRay:services=services participation"); 383 manager.addManagedObject(new GetWorldModelerVersionJMX(), "MantaRay:worldModeler=get version"); 384 manager.addManagedObject(new GetStatsJMX(), "MantaRay:layer=get stats"); 388 manager.addManagedObject(MantaAgent.getInstance().getSingletonRepository() 389 .getConfigManager(), "MantaRay:config=manages configuration of properties"); 390 392 } 393 394 } catch (Exception e) { 395 if(LogFactory.getLog("InitManager").isErrorEnabled()) 396 LogFactory.getLog("InitManager").error("RegisterJMXMBeans had an exception: "+e); 397 } 398 } 399 } | Popular Tags |