restConnector.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright IBM Corp. 2012, 2013
  2. # The source code for this program is not published or other-
  3. # wise divested of its trade secrets, irrespective of what has
  4. # been deposited with the U.S. Copyright Office.
  5. from java.lang import String
  6. from java.lang import System
  7. from java.util import HashMap
  8. from javax.management import NotificationListener
  9. from javax.management import NotificationFilter
  10. from javax.management.remote import JMXConnector
  11. from javax.management.remote import JMXConnectorFactory
  12. from javax.management.remote import JMXServiceURL
  13. from com.ibm.websphere.jmx.connector.rest import ConnectorSettings
  14. import jarray
  15. class BaseNotificationListener(NotificationListener):
  16. # The client applications can subclass BaseNotificationListener,
  17. # and override handleNotification() method.
  18. def __init__(self):
  19. pass
  20. def handleNotification(self,notification,handback):
  21. # A user can override this method.
  22. pass
  23. class BaseNotificationFilter(NotificationFilter):
  24. # The client applications can subclass BaseNotificationFilter,
  25. # and override isNotificationEnabled() method.
  26. def __init__(self):
  27. pass
  28. def isNotificationEnabled(self,notification):
  29. # A user can override this method.
  30. return True
  31. class JMXRESTConnector(object):
  32. connector = None
  33. mbeanConnection = None
  34. trustStore = None
  35. trustStorePassword = None
  36. trustStoreType = None
  37. def __init__(self):
  38. pass
  39. def connect(self, host, port, *args):
  40. if len(args)==2:
  41. self.connectBasic(host, port, args[0], args[1])
  42. else:
  43. self.connectAdvanced(host, port, args[0])
  44. def connectAdvanced(self,host,port,map):
  45. print("Connecting to the server...")
  46. System.setProperty("javax.net.ssl.trustStore", self.trustStore)
  47. System.setProperty("javax.net.ssl.trustStorePassword", self.trustStorePassword)
  48. System.setProperty("javax.net.ssl.trustStoreType", "PKCS12");
  49. url = JMXServiceURL("REST", host, port, "/IBMJMXConnectorREST")
  50. self.connector = JMXConnectorFactory.newJMXConnector(url, map)
  51. self.connector.connect()
  52. print("Successfully connected to the server " + '"' + host + ':%i"' % port)
  53. def connectBasic(self,host,port,user,password):
  54. map = HashMap()
  55. map.put("jmx.remote.provider.pkgs", "com.ibm.ws.jmx.connector.client")
  56. map.put(JMXConnector.CREDENTIALS, jarray.array([user, password], String))
  57. map.put(ConnectorSettings.READ_TIMEOUT, 2*60*1000)
  58. map.put(ConnectorSettings.DISABLE_HOSTNAME_VERIFICATION, True)
  59. self.connectAdvanced(host, port, map)
  60. def disconnect(self):
  61. if(self.connector==None):
  62. pass
  63. else:
  64. self.connector.close()
  65. self.connector = None
  66. self.mbeanConnection = None
  67. def getMBeanServerConnection(self):
  68. # This method can be called after the above connect() is executed successfully.
  69. self.mbeanConnection = self.connector.getMBeanServerConnection()
  70. return self.mbeanConnection