1 package org.objectweb.petals.tools.petalsadmin.utils; 2 3 import java.io.IOException ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import javax.management.Attribute ; 12 import javax.management.AttributeNotFoundException ; 13 import javax.management.MBeanServerConnection ; 14 import javax.management.ObjectName ; 15 import javax.management.remote.JMXConnector ; 16 17 public class JMXUtils { 18 19 22 private String host = ""; 23 24 27 private String password = ""; 28 29 32 private String port = ""; 33 34 37 private String username = ""; 38 39 public JMXUtils(String serverHost,String jmxPort, String jmxUser, String jmxPasswd) { 40 this.host = serverHost; 41 this.port=jmxPort; 42 this.username=jmxUser; 43 this.password=jmxPasswd; 44 } 45 46 60 80 81 87 public Map <String , JBIEntity> listComponents(String phost, String pport, String pusername, String ppassword) 88 throws Exception { 89 Map <String , JBIEntity> components = new HashMap <String , JBIEntity>(); 90 JMXConnector connector = null; 91 try { 92 connector = retrieveConnector(phost, pport, pusername, ppassword); 93 MBeanServerConnection connection = connector 94 .getMBeanServerConnection(); 95 Object result = connection.getAttribute(JBIJMXConnectorUtil 96 .getAdminServiceMBeanName(connection), "BindingComponents"); 97 if (result instanceof ObjectName []) { 98 ObjectName [] engines = (ObjectName []) result; 99 for (ObjectName objectName : engines) { 100 JBIEntity component = new JBIEntity(); 101 String objState = (String ) connection.getAttribute( 102 objectName, "CurrentState"); 103 String name = objectName.getKeyProperty("name"); 104 component.setName(name); 105 component.setState(objState); 106 component.setObjectName(objectName); 107 component.setDescription(""); 109 component.setType(JBIEntity.BINDING_COMPONENT); 110 components.put(component.getName(), component); 111 } 112 } 113 result = connection.getAttribute(JBIJMXConnectorUtil 114 .getAdminServiceMBeanName(connection), "EngineComponents"); 115 if (result instanceof ObjectName []) { 116 ObjectName [] engines = (ObjectName []) result; 117 for (ObjectName objectName : engines) { 118 JBIEntity component = new JBIEntity(); 119 String objState = (String ) connection.getAttribute( 120 objectName, "CurrentState"); 121 String name = objectName.getKeyProperty("name"); 122 component.setName(name); 123 component.setState(objState); 124 component.setObjectName(objectName); 125 component.setDescription(""); 127 component.setType(JBIEntity.SERVICE_ENGINE); 128 components.put(component.getName(), component); 129 } 130 } 131 connector.close(); 132 } catch (Exception e) { 133 try { 134 if (connector != null) { 135 connector.close(); 136 } 137 } catch (IOException e1) { 138 } 140 throw new Exception (e); 141 } 142 return components; 143 } 144 145 151 public JMXConnector retrieveConnector() throws IOException { 152 return JBIJMXConnectorUtil 153 .getConnection(host, port, username, password); 154 } 155 156 162 public JMXConnector retrieveConnector(String phost, String pport, String pusername, String ppassword) 163 throws IOException { 164 return JBIJMXConnectorUtil 165 .getConnection(phost, pport, pusername, ppassword); 166 } 167 168 @SuppressWarnings ("unchecked") 169 public List <Map <String , String >> getStartedServers() throws Exception { 170 List <Map <String , String >> serversList= new ArrayList <Map <String , String >>(); 172 JMXConnector connector = null; 173 try { 174 connector = retrieveConnector(); 175 MBeanServerConnection connection = connector 176 .getMBeanServerConnection(); 177 ObjectName petalsAdminName = new ObjectName ( 178 "Petals:name=PetalsAdmin,type=service"); 179 @SuppressWarnings ("unused") 180 Collection <Map <String , String >> serversConfigurations = (Collection <Map <String , String >>) connection 181 .getAttribute(petalsAdminName,"AllStartedServersConfiguration"); 182 190 Iterator <Map <String , String >> parcServers = serversConfigurations.iterator(); 191 while(parcServers.hasNext()) 192 { 193 Map <String , String > currentServer = parcServers.next(); 194 serversList.add(currentServer); 195 } 196 197 } catch (Exception e) { 198 throw new Exception (e); 199 } 200 return serversList; 201 } 202 203 @SuppressWarnings ("unchecked") 204 public List <Map <String , String >> getAllServers() throws Exception { 205 List <Map <String , String >> serversList= new ArrayList <Map <String , String >>(); 207 JMXConnector connector = null; 208 try { 209 connector = retrieveConnector(); 210 MBeanServerConnection connection = connector 211 .getMBeanServerConnection(); 212 ObjectName petalsAdminName = new ObjectName ( 213 "Petals:name=PetalsAdmin,type=service"); 214 Collection <Map <String , String >> serversConfigurations = (Collection <Map <String , String >>) connection 215 .getAttribute(petalsAdminName,"AllServersConfiguration"); 216 224 Iterator <Map <String , String >> parcServers = serversConfigurations.iterator(); 225 while(parcServers.hasNext()) 226 { 227 Map <String , String > currentServer = parcServers.next(); 228 serversList.add(currentServer); 229 } 230 231 } catch (Exception e) { 232 throw new Exception (e); 233 } 234 return serversList; 235 } 236 237 @SuppressWarnings ("unchecked") 238 public List <List <Map <String ,Object >>> bulkData(Map <String , String > serverConf, long sinceTime) 239 throws Exception 240 { 241 List <List <Map <String ,Object >>> list_exchanges = new ArrayList <List <Map <String ,Object >>>(); 242 JMXConnector connector = null; 243 try { 244 String serverHost = serverConf.get("host"); 245 String serverPort = serverConf.get("jmxPort"); 246 String serverLogin = serverConf.get("jmxLogin"); 247 String serverPasswd = serverConf.get("jmxPassword"); 248 249 connector = retrieveConnector(serverHost,serverPort,serverLogin,serverPasswd); 250 MBeanServerConnection connection = connector 251 .getMBeanServerConnection(); 252 ObjectName petalsAdminName = new ObjectName ( 253 "Petals:name=PetalsAdmin,type=service"); 254 list_exchanges = (List <List <Map <String ,Object >>>) connection.invoke(petalsAdminName,"bulkData", new Object []{sinceTime}, new String []{Long.TYPE.getCanonicalName()}); 255 } 256 catch (Exception e) 257 { 258 throw new Exception (e); 259 } 260 return list_exchanges; 261 } 262 263 269 public boolean monitor(String serverHost, String serverPort, String serverLogin, String serverPasswd, boolean action) 270 throws Exception , AttributeNotFoundException 271 { 272 JMXConnector connector = null; 273 try { 274 connector = retrieveConnector(serverHost,serverPort,serverLogin,serverPasswd); 275 MBeanServerConnection connection = connector 276 .getMBeanServerConnection(); 277 ObjectName petalsAdminName = new ObjectName ( 278 "Petals:name=PetalsAdmin,type=service"); 279 Attribute attAction = new Attribute ("Monitoring",action); 280 connection.setAttribute(petalsAdminName, attAction); 281 return true; 282 } 283 catch (AttributeNotFoundException anf) { 284 return true; 285 } 286 catch (Exception e) { 287 throw new Exception (e); 288 } 289 } 290 291 297 public String getAttribute(String serverHost, String serverPort, String serverLogin, String serverPasswd, String attributeName) 298 throws Exception , AttributeNotFoundException 299 { 300 JMXConnector connector = null; 301 try { 302 connector = retrieveConnector(serverHost,serverPort,serverLogin,serverPasswd); 303 MBeanServerConnection connection = connector 304 .getMBeanServerConnection(); 305 ObjectName petalsAdminName = new ObjectName ( 306 "Petals:name=PetalsAdmin,type=service"); 307 return connection.getAttribute(petalsAdminName,"Monitoring").toString(); 308 } 309 catch (AttributeNotFoundException anf) { 310 return "not found"; 311 } 312 catch (Exception e) { 313 throw new Exception (e); 314 } 315 } 316 317 public String getHost() { 318 return host; 319 } 320 321 public void setHost(String host) { 322 this.host = host; 323 } 324 325 public String getPassword() { 326 return password; 327 } 328 329 public void setPassword(String password) { 330 this.password = password; 331 } 332 333 public String getPort() { 334 return port; 335 } 336 337 public void setPort(String port) { 338 this.port = port; 339 } 340 341 public String getUsername() { 342 return username; 343 } 344 345 public void setUsername(String username) { 346 this.username = username; 347 } 348 349 } 350 | Popular Tags |