Search in sources :

Example 96 with CompositeData

use of javax.management.openmbean.CompositeData in project karaf by apache.

the class JMXSecurityMBeanImpl method canInvoke.

public TabularData canInvoke(Map<String, List<String>> bulkQuery) throws Exception {
    TabularData table = new TabularDataSupport(CAN_INVOKE_TABULAR_TYPE);
    BulkRequestContext context = BulkRequestContext.newContext(guard.getConfigAdmin());
    for (Map.Entry<String, List<String>> entry : bulkQuery.entrySet()) {
        String objectName = entry.getKey();
        List<String> methods = entry.getValue();
        if (methods.size() == 0) {
            boolean res = canInvoke(context, objectName);
            CompositeData data = new CompositeDataSupport(CAN_INVOKE_RESULT_ROW_TYPE, CAN_INVOKE_RESULT_COLUMNS, new Object[] { objectName, "", res });
            table.put(data);
        } else {
            for (String method : methods) {
                List<String> argTypes = new ArrayList<>();
                String name = parseMethodName(method, argTypes);
                boolean res;
                if (name.equals(method)) {
                    res = canInvoke(context, objectName, name);
                } else {
                    res = canInvoke(context, objectName, name, argTypes.toArray(new String[] {}));
                }
                CompositeData data = new CompositeDataSupport(CAN_INVOKE_RESULT_ROW_TYPE, CAN_INVOKE_RESULT_COLUMNS, new Object[] { objectName, method, res });
                try {
                    table.put(data);
                } catch (KeyAlreadyExistsException e) {
                    // KeyAlreadyExistsException can happen only when methods are not empty
                    LOG.warn("{} (objectName = \"{}\", method = \"{}\")", e, objectName, method);
                }
            }
        }
    }
    return table;
}
Also used : CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) ArrayList(java.util.ArrayList) KeyAlreadyExistsException(javax.management.openmbean.KeyAlreadyExistsException) TabularData(javax.management.openmbean.TabularData) TabularDataSupport(javax.management.openmbean.TabularDataSupport) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 97 with CompositeData

use of javax.management.openmbean.CompositeData in project karaf by apache.

the class JMXSecurityMBeanImplTestCase method testCanInvokeBulk.

public void testCanInvokeBulk() throws Exception {
    MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.replay(mbs);
    ConfigurationAdmin testConfigAdmin = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl*)"))).andReturn(new Configuration[0]).anyTimes();
    EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl.whitelist)"))).andReturn(new Configuration[0]).once();
    EasyMock.replay(testConfigAdmin);
    KarafMBeanServerGuard testGuard = EasyMock.createMock(KarafMBeanServerGuard.class);
    String objectName = "foo.bar.testing:type=SomeMBean";
    final String[] la = new String[] { "long" };
    final String[] sa = new String[] { "java.lang.String" };
    EasyMock.expect(testGuard.getConfigAdmin()).andReturn(testConfigAdmin).anyTimes();
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("testMethod"), EasyMock.aryEq(la))).andReturn(true).anyTimes();
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("testMethod"), EasyMock.aryEq(sa))).andReturn(false).anyTimes();
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("otherMethod"))).andReturn(true).anyTimes();
    String objectName2 = "foo.bar.testing:type=SomeOtherMBean";
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName2)))).andReturn(true).anyTimes();
    String objectName3 = "foo.bar.foo.testing:type=SomeOtherMBean";
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName3)))).andReturn(false).anyTimes();
    EasyMock.replay(testGuard);
    JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
    mb.setMBeanServer(mbs);
    mb.setGuard(testGuard);
    Map<String, List<String>> query = new HashMap<>();
    query.put(objectName, Arrays.asList("otherMethod", "testMethod(long)", "testMethod(java.lang.String)"));
    query.put(objectName2, Collections.emptyList());
    query.put(objectName3, Collections.emptyList());
    TabularData result = mb.canInvoke(query);
    assertEquals(5, result.size());
    CompositeData cd = result.get(new Object[] { objectName, "testMethod(long)" });
    assertEquals(objectName, cd.get("ObjectName"));
    assertEquals("testMethod(long)", cd.get("Method"));
    assertEquals(true, cd.get("CanInvoke"));
    CompositeData cd2 = result.get(new Object[] { objectName, "testMethod(java.lang.String)" });
    assertEquals(objectName, cd2.get("ObjectName"));
    assertEquals("testMethod(java.lang.String)", cd2.get("Method"));
    assertEquals(false, cd2.get("CanInvoke"));
    CompositeData cd3 = result.get(new Object[] { objectName, "otherMethod" });
    assertEquals(objectName, cd3.get("ObjectName"));
    assertEquals("otherMethod", cd3.get("Method"));
    assertEquals(true, cd3.get("CanInvoke"));
    CompositeData cd4 = result.get(new Object[] { objectName2, "" });
    assertEquals(objectName2, cd4.get("ObjectName"));
    assertEquals("", cd4.get("Method"));
    assertEquals(true, cd4.get("CanInvoke"));
    CompositeData cd5 = result.get(new Object[] { objectName3, "" });
    assertEquals(objectName3, cd5.get("ObjectName"));
    assertEquals("", cd5.get("Method"));
    assertEquals(false, cd5.get("CanInvoke"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) CompositeData(javax.management.openmbean.CompositeData) ObjectName(javax.management.ObjectName) TabularData(javax.management.openmbean.TabularData) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) MBeanServer(javax.management.MBeanServer)

Example 98 with CompositeData

use of javax.management.openmbean.CompositeData in project karaf by apache.

the class HttpMBeanImpl method getServlets.

public TabularData getServlets() throws MBeanException {
    try {
        CompositeType servletType = new CompositeType("Servlet", "HTTP Servlet", new String[] { "Bundle-ID", "Servlet", "Servlet Name", "State", "Alias", "URL" }, new String[] { "ID of the bundle that registered the servlet", "Class name of the servlet", "Servlet Name", "Current state of the servlet", "Aliases of the servlet", "URL of the servlet" }, new OpenType[] { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING });
        TabularType tableType = new TabularType("Servlets", "Table of all HTTP servlets", servletType, new String[] { "Bundle-ID", "Servlet Name", "State" });
        TabularData table = new TabularDataSupport(tableType);
        List<ServletInfo> servletInfos = servletService.getServlets();
        for (ServletInfo info : servletInfos) {
            CompositeData data = new CompositeDataSupport(servletType, new String[] { "Bundle-ID", "Servlet", "Servlet Name", "State", "Alias", "URL" }, new Object[] { info.getBundleId(), info.getClassName(), info.getName(), info.getStateString(), info.getAlias(), Arrays.toString(info.getUrls()) });
            table.put(data);
        }
        return table;
    } catch (Exception e) {
        throw new MBeanException(null, e.toString());
    }
}
Also used : ServletInfo(org.apache.karaf.http.core.ServletInfo) TabularDataSupport(javax.management.openmbean.TabularDataSupport) TabularType(javax.management.openmbean.TabularType) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) CompositeType(javax.management.openmbean.CompositeType) TabularData(javax.management.openmbean.TabularData)

Example 99 with CompositeData

use of javax.management.openmbean.CompositeData in project karaf by apache.

the class InstanceToTableMapperTest method testJMXInstance2.

public void testJMXInstance2() throws Exception {
    Instance instance = EasyMock.createMock(Instance.class);
    EasyMock.expect(instance.getPid()).andReturn(1712);
    EasyMock.expect(instance.getName()).andReturn("MyInstance");
    EasyMock.expect(instance.isRoot()).andReturn(true);
    EasyMock.expect(instance.getSshPort()).andReturn(0);
    EasyMock.expect(instance.getSshHost()).andReturn("0.0.0.0");
    EasyMock.expect(instance.getRmiRegistryPort()).andReturn(0);
    EasyMock.expect(instance.getRmiRegistryHost()).andReturn("0.0.0.0");
    EasyMock.expect(instance.getRmiServerPort()).andReturn(0);
    EasyMock.expect(instance.getRmiServerHost()).andReturn("0.0.0.0");
    EasyMock.expect(instance.getState()).andReturn("Started");
    EasyMock.expect(instance.getLocation()).andReturn(null);
    EasyMock.expect(instance.getJavaOpts()).andReturn(null);
    EasyMock.replay(instance);
    TabularData td = InstanceToTableMapper.tableFrom(Collections.singletonList(instance));
    Collection<?> keys = (Collection<?>) td.keySet().iterator().next();
    Assert.assertEquals("MyInstance", keys.iterator().next());
    CompositeData cd = td.get(keys.toArray());
    Assert.assertEquals(1712, cd.get("Pid"));
    Assert.assertEquals("MyInstance", cd.get("Name"));
    Assert.assertEquals(true, cd.get("Is Root"));
    Assert.assertEquals(0, cd.get("SSH Port"));
    Assert.assertEquals("0.0.0.0", cd.get("SSH Host"));
    Assert.assertEquals(0, cd.get("RMI Registry Port"));
    Assert.assertEquals("0.0.0.0", cd.get("RMI Registry Host"));
    Assert.assertEquals(0, cd.get("RMI Server Port"));
    Assert.assertEquals("0.0.0.0", cd.get("RMI Server Host"));
    Assert.assertEquals("Started", cd.get("State"));
    Assert.assertNull(cd.get("Location"));
    Assert.assertNull(cd.get("JavaOpts"));
}
Also used : Instance(org.apache.karaf.instance.core.Instance) CompositeData(javax.management.openmbean.CompositeData) Collection(java.util.Collection) TabularData(javax.management.openmbean.TabularData)

Example 100 with CompositeData

use of javax.management.openmbean.CompositeData in project karaf by apache.

the class ServicesMBeanImpl method getServices.

public TabularData getServices(long bundleId, boolean inUse) throws MBeanException {
    try {
        CompositeType serviceType = new CompositeType("Service", "OSGi Service", new String[] { "Interfaces", "Properties" }, new String[] { "Interfaces class name of the service", "Properties of the service" }, new OpenType[] { new ArrayType(1, SimpleType.STRING), new ArrayType(1, SimpleType.STRING) });
        TabularType tableType = new TabularType("Services", "Table of OSGi Services", serviceType, new String[] { "Interfaces", "Properties" });
        TabularData table = new TabularDataSupport(tableType);
        Bundle[] bundles;
        if (bundleId >= 0) {
            bundles = new Bundle[] { bundleContext.getBundle(bundleId) };
        } else {
            bundles = bundleContext.getBundles();
        }
        for (Bundle bundle : bundles) {
            try {
                ServiceReference[] serviceReferences;
                if (inUse) {
                    serviceReferences = bundle.getServicesInUse();
                } else {
                    serviceReferences = bundle.getRegisteredServices();
                }
                if (serviceReferences != null) {
                    for (ServiceReference reference : serviceReferences) {
                        String[] interfaces = (String[]) reference.getProperty("objectClass");
                        List<String> properties = new ArrayList<>();
                        for (String key : reference.getPropertyKeys()) {
                            properties.add(key + " = " + reference.getProperty(key));
                        }
                        CompositeData data = new CompositeDataSupport(serviceType, new String[] { "Interfaces", "Properties" }, new Object[] { interfaces, properties.toArray(new String[0]) });
                        table.put(data);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return table;
    } catch (Exception e) {
        throw new MBeanException(null, e.toString());
    }
}
Also used : Bundle(org.osgi.framework.Bundle) TabularType(javax.management.openmbean.TabularType) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) ArrayList(java.util.ArrayList) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) TabularData(javax.management.openmbean.TabularData) ServiceReference(org.osgi.framework.ServiceReference) ArrayType(javax.management.openmbean.ArrayType) TabularDataSupport(javax.management.openmbean.TabularDataSupport) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) CompositeType(javax.management.openmbean.CompositeType)

Aggregations

CompositeData (javax.management.openmbean.CompositeData)229 TabularData (javax.management.openmbean.TabularData)91 Test (org.junit.Test)73 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)68 TabularDataSupport (javax.management.openmbean.TabularDataSupport)51 CompositeType (javax.management.openmbean.CompositeType)50 HashMap (java.util.HashMap)31 ArrayList (java.util.ArrayList)27 Map (java.util.Map)27 Bundle (org.osgi.framework.Bundle)24 ObjectName (javax.management.ObjectName)21 OpenDataException (javax.management.openmbean.OpenDataException)18 IOException (java.io.IOException)17 Collection (java.util.Collection)16 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)16 AuditEvent (org.nhindirect.common.audit.AuditEvent)15 TabularType (javax.management.openmbean.TabularType)13 MBeanServer (javax.management.MBeanServer)12 DefaultAuditContext (org.nhindirect.common.audit.DefaultAuditContext)11 MBeanException (javax.management.MBeanException)8