use of javax.management.MBeanServerConnection in project ats-framework by Axway.
the class SystemOperations method getJvmMbeans.
/**
* @param host the address of the host machine
* @param jmxPort the jmx port
*
* @return all MBeans with their attributes and type
* @throws SystemOperationException
*/
@PublicAtsApi
public String getJvmMbeans(String host, String jmxPort) {
JMXConnector jmxCon = null;
try {
// Connect to JMXConnector
JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi");
jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, null);
jmxCon.connect();
// Access the MBean
MBeanServerConnection con = jmxCon.getMBeanServerConnection();
Set<ObjectName> queryResults = con.queryNames(null, null);
StringBuilder results = new StringBuilder();
for (ObjectName theName : queryResults) {
results.append("\n---");
results.append("\nMBean name: " + theName.getCanonicalName());
MBeanAttributeInfo[] attributes = con.getMBeanInfo(theName).getAttributes();
for (MBeanAttributeInfo attribute : attributes) {
if (attribute.getType() != null) {
if (!"javax.management.openmbean.CompositeData".equals(attribute.getType())) {
if ("java.lang.Long".equals(attribute.getType()) || "java.lang.Integer".equals(attribute.getType()) || "int".equals(attribute.getType()) || "long".equals(attribute.getType()))
results.append("\r " + attribute.getName() + " | " + attribute.getType());
} else {
results.append("\r " + attribute.getName() + " | " + attribute.getType());
CompositeData comdata = (CompositeData) con.getAttribute(theName, attribute.getName());
if (comdata != null) {
for (String key : comdata.getCompositeType().keySet()) {
Object value = comdata.get(key);
if (value instanceof Integer || value instanceof Double || value instanceof Long)
results.append("\r " + key + " | " + value.getClass());
}
}
}
}
}
}
return results.toString();
} catch (Exception e) {
throw new SystemOperationException("MBeans with their attributes cannot be get.", e);
} finally {
if (jmxCon != null)
try {
jmxCon.close();
} catch (IOException e) {
log.error("JMX connection was not closed!");
}
}
}
use of javax.management.MBeanServerConnection in project geode by apache.
the class MBeanSecurityJUnitTest method testNoAccessWithWhoever.
/**
* No user can call createBean or unregisterBean of GemFire Domain
*/
@Test
@ConnectionConfiguration(user = "super-user", password = "1234567")
public void testNoAccessWithWhoever() throws Exception {
MBeanServerConnection con = connectionRule.getMBeanServerConnection();
assertThatThrownBy(() -> con.createMBean("FakeClassName", new ObjectName("GemFire", "name", "foo"))).isInstanceOf(SecurityException.class);
assertThatThrownBy(() -> con.unregisterMBean(new ObjectName("GemFire", "name", "foo"))).isInstanceOf(SecurityException.class);
// user is allowed to create beans of other domains
assertThatThrownBy(() -> con.createMBean("FakeClassName", new ObjectName("OtherDomain", "name", "foo"))).isInstanceOf(ReflectionException.class);
}
use of javax.management.MBeanServerConnection in project geode by apache.
the class MBeanSecurityJUnitTest method testQueryBean.
/**
* looks like everyone can query for beans, but the AccessControlMXBean is filtered from the
* result
*/
@Test
@ConnectionConfiguration(user = "stranger", password = "1234567")
public void testQueryBean() throws MalformedObjectNameException, IOException {
MBeanServerConnection con = connectionRule.getMBeanServerConnection();
Set<ObjectInstance> objects = con.queryMBeans(ObjectName.getInstance(ResourceConstants.OBJECT_NAME_ACCESSCONTROL), null);
// no AccessControlMBean in the query result
assertThat(objects.size()).isEqualTo(0);
objects = con.queryMBeans(ObjectName.getInstance("GemFire:service=CacheServer,*"), null);
assertThat(objects.size()).isEqualTo(1);
}
use of javax.management.MBeanServerConnection in project geode by apache.
the class GemFireAuthentication method populateAuthorities.
public static ArrayList<GrantedAuthority> populateAuthorities(JMXConnector jmxc) {
ObjectName name;
ArrayList<GrantedAuthority> authorities = new ArrayList<>();
try {
name = new ObjectName(PulseConstants.OBJECT_NAME_ACCESSCONTROL_MBEAN);
MBeanServerConnection mbeanServer = jmxc.getMBeanServerConnection();
for (String role : PulseConstants.PULSE_ROLES) {
Object[] params = role.split(":");
String[] signature = new String[] { String.class.getCanonicalName(), String.class.getCanonicalName() };
boolean result = (Boolean) mbeanServer.invoke(name, "authorize", params, signature);
if (result) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
}
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
return authorities;
}
use of javax.management.MBeanServerConnection in project karaf by apache.
the class DeployMojo method deployWithJmx.
protected void deployWithJmx(List<String> locations) throws MojoExecutionException {
try {
JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/" + instance);
ArrayList<String> list = new ArrayList<>();
if (user != null)
list.add(user);
if (password != null)
list.add(password);
HashMap env = new HashMap();
String[] credentials = list.toArray(new String[list.size()]);
env.put(JMXConnector.CREDENTIALS, credentials);
JMXConnector jmxConnector = null;
if (credentials.length > 0)
jmxConnector = JMXConnectorFactory.connect(jmxServiceURL, env);
else
jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
for (String location : locations) {
mBeanServerConnection.invoke(new ObjectName("org.apache.karaf:type=bundle,name=*"), "install", new Object[] { location, true }, new String[] { "java.lang.String", "boolean" });
}
} catch (Exception e) {
throw new MojoExecutionException("Can't deploy using JMX", e);
}
}
Aggregations