Search in sources :

Example 51 with TabularData

use of javax.management.openmbean.TabularData in project tdi-studio-se by Talend.

the class OverviewProperty method getValueString.

/**
     * Gets the string representation of value.
     * 
     * @return The string representation of value
     */
protected String getValueString() {
    if (value == null) {
        //$NON-NLS-1$
        return "";
    }
    if (value instanceof String[]) {
        StringBuffer buffer = new StringBuffer();
        for (String element : (String[]) value) {
            buffer.append(element).append(' ');
        }
        return buffer.toString();
    }
    if (value instanceof CompositeData) {
        if (!attributeName.contains(".")) {
            //$NON-NLS-1$
            return "";
        }
        CompositeData compositeData = (CompositeData) value;
        //$NON-NLS-1$
        String[] elements = attributeName.split("\\.");
        String compositeDataKey = elements[elements.length - 1];
        value = compositeData.get(compositeDataKey);
    }
    if (value instanceof String) {
        //$NON-NLS-1$ //$NON-NLS-2$
        return ((String) value).replace("\n", "\\n");
    }
    if (value instanceof Long) {
        if (format != null) {
            return format.format(value);
        }
    }
    if (value instanceof TabularData) {
        //$NON-NLS-1$
        return "";
    }
    return value.toString();
}
Also used : CompositeData(javax.management.openmbean.CompositeData) TabularData(javax.management.openmbean.TabularData)

Example 52 with TabularData

use of javax.management.openmbean.TabularData in project aries by apache.

the class ConfigurationAdminMBeanTest method testMBeanInterface.

@Ignore("ManagedServiceFactory tests failing.. " + "Some issues surrounding creating a factory configuration and then retrieving by pid to update.. Needs investigation")
@Test
@SuppressWarnings("unchecked")
public void testMBeanInterface() throws Exception {
    // get bundles
    Bundle a = getBundleByName("org.apache.aries.jmx.test.bundlea");
    // ManagedService operations
    assertNull(managedServiceA.getConfig());
    // create a configuration for A
    TabularData data = mbean.getProperties("org.apache.aries.jmx.test.ServiceA");
    assertEquals(0, data.size());
    PropertyData<String> p1 = PropertyData.newInstance("A1", "first");
    data.put(p1.toCompositeData());
    PropertyData<Integer> p2 = PropertyData.newInstance("A2", 2);
    data.put(p2.toCompositeData());
    mbean.update("org.apache.aries.jmx.test.ServiceA", data);
    Thread.sleep(1000);
    Dictionary<String, Object> config = managedServiceA.getConfig();
    assertNotNull(config);
    assertEquals(3, config.size());
    assertEquals("org.apache.aries.jmx.test.ServiceA", config.get(Constants.SERVICE_PID));
    assertEquals("first", config.get("A1"));
    assertEquals(2, config.get("A2"));
    //delete
    mbean.deleteForLocation("org.apache.aries.jmx.test.ServiceA", a.getLocation());
    Thread.sleep(1000);
    assertNull(managedServiceA.getConfig());
    // ManagedServiceFactory operations
    String cpid = mbean.createFactoryConfiguration("jmx.test.B.factory");
    assertNotNull(cpid);
    assertTrue(cpid.contains("jmx.test.B.factory"));
    TabularData fConfig = mbean.getProperties(cpid);
    assertNotNull(fConfig);
    assertEquals(0, fConfig.values().size());
    PropertyData<String> prop1 = PropertyData.newInstance("B1", "value1");
    fConfig.put(prop1.toCompositeData());
    PropertyData<Boolean> prop2 = PropertyData.newInstance("B2", true);
    fConfig.put(prop2.toCompositeData());
    mbean.update(cpid, fConfig);
    Thread.sleep(1000);
    InterfaceB configured = managedFactory.getConfigured(cpid);
    assertNotNull(configured);
    config = configured.getConfig();
    assertNotNull(config);
    assertTrue(config.size() >= 4);
    assertEquals("jmx.test.B.factory", config.get(ConfigurationAdmin.SERVICE_FACTORYPID));
    assertEquals(cpid, config.get(Constants.SERVICE_PID));
    assertEquals("value1", config.get("B1"));
    assertEquals("true", config.get("B2"));
    assertEquals("jmx.test.B.factory", mbean.getFactoryPid(cpid));
    mbean.delete(cpid);
    Thread.sleep(1000);
    assertNull(managedFactory.getConfigured(cpid));
    // list operations
    data = mbean.getProperties("org.apache.aries.jmx.test.ServiceA");
    assertEquals(0, data.size());
    p1 = PropertyData.newInstance("A1", "a1Value");
    data.put(p1.toCompositeData());
    mbean.update("org.apache.aries.jmx.test.ServiceA", data);
    Thread.sleep(1000);
    config = managedServiceA.getConfig();
    assertNotNull(config);
    assertEquals(2, config.size());
    assertEquals("org.apache.aries.jmx.test.ServiceA", config.get(Constants.SERVICE_PID));
    assertEquals("a1Value", config.get("A1"));
    String[][] configurations = mbean.getConfigurations("(A1=a1Value)");
    assertNotNull(configurations);
    assertEquals(1, configurations.length);
    assertEquals("org.apache.aries.jmx.test.ServiceA", configurations[0][0]);
    assertEquals(a.getLocation(), configurations[0][1]);
    // delete with filter
    mbean.deleteConfigurations("(A1=a1Value)");
    Thread.sleep(1000);
    assertNull(managedServiceA.getConfig());
}
Also used : InterfaceB(org.apache.aries.jmx.test.bundleb.api.InterfaceB) Bundle(org.osgi.framework.Bundle) TabularData(javax.management.openmbean.TabularData) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 53 with TabularData

use of javax.management.openmbean.TabularData in project aries by apache.

the class BundleStateMBeanTest method testMBeanInterface.

@Test
public void testMBeanInterface() throws Exception {
    // exportedPackages
    String[] exports = mbean.getExportedPackages(a.getBundleId());
    assertEquals(2, exports.length);
    List<String> packages = Arrays.asList(exports);
    assertTrue(packages.contains("org.apache.aries.jmx.test.bundlea.api;2.0.0"));
    assertTrue(packages.contains("org.apache.aries.jmx.test.fragmentc;0.0.0"));
    // fragments
    long[] fragments = mbean.getFragments(a.getBundleId());
    assertEquals(1, fragments.length);
    assertEquals(fragc.getBundleId(), fragments[0]);
    // headers
    TabularData headers = mbean.getHeaders(b.getBundleId());
    assertNotNull(headers);
    assertEquals(BundleStateMBean.HEADERS_TYPE, headers.getTabularType());
    assertTrue(headers.values().size() >= 4);
    assertEquals("org.apache.aries.jmx.test.bundleb", Header.from(headers.get(new Object[] { Constants.BUNDLE_SYMBOLICNAME })).getValue());
    // hosts
    long[] hosts = mbean.getHosts(fragc.getBundleId());
    assertEquals(1, hosts.length);
    assertEquals(a.getBundleId(), hosts[0]);
    //imported packages
    String[] imports = mbean.getImportedPackages(a.getBundleId());
    assertTrue(imports.length >= 3);
    List<String> importedPackages = Arrays.asList(imports);
    Version version = getPackageVersion("org.osgi.framework");
    assertTrue(importedPackages.contains("org.osgi.framework;" + version.toString()));
    assertTrue(importedPackages.contains("org.apache.aries.jmx.test.bundleb.api;1.1.0"));
    //last modified
    assertTrue(mbean.getLastModified(b.getBundleId()) > 0);
    //location
    assertEquals(b.getLocation(), mbean.getLocation(b.getBundleId()));
    //registered services
    long[] serviceIds = mbean.getRegisteredServices(a.getBundleId());
    assertEquals(1, serviceIds.length);
    //required bundles
    long[] required = mbean.getRequiredBundles(d.getBundleId());
    assertEquals(1, required.length);
    assertEquals(a.getBundleId(), required[0]);
    //requiring bundles
    long[] requiring = mbean.getRequiringBundles(a.getBundleId());
    assertEquals(2, requiring.length);
    assertTrue(fragc.getSymbolicName(), arrayContains(fragc.getBundleId(), requiring));
    assertTrue(d.getSymbolicName(), arrayContains(d.getBundleId(), requiring));
    //services in use
    long[] servicesInUse = mbean.getServicesInUse(a.getBundleId());
    assertEquals(1, servicesInUse.length);
    //start level
    long startLevel = mbean.getStartLevel(b.getBundleId());
    assertTrue(startLevel >= 0);
    //state
    assertEquals("ACTIVE", mbean.getState(b.getBundleId()));
    //isFragment
    assertFalse(mbean.isFragment(b.getBundleId()));
    assertTrue(mbean.isFragment(fragc.getBundleId()));
    //isRemovalPending
    assertFalse(mbean.isRemovalPending(b.getBundleId()));
    // isRequired
    assertTrue(mbean.isRequired(a.getBundleId()));
    assertTrue(mbean.isRequired(b.getBundleId()));
    // listBundles
    TabularData bundlesTable = mbean.listBundles();
    assertNotNull(bundlesTable);
    assertEquals(BundleStateMBean.BUNDLES_TYPE, bundlesTable.getTabularType());
    assertEquals(bundleContext.getBundles().length, bundlesTable.values().size());
    // notifications
    final List<Notification> received = new ArrayList<Notification>();
    mbeanServer.addNotificationListener(objectName, new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            received.add(notification);
        }
    }, null, null);
    assertEquals(Bundle.ACTIVE, b.getState());
    b.stop();
    assertEquals(Bundle.RESOLVED, b.getState());
    b.start();
    assertEquals(Bundle.ACTIVE, b.getState());
    int i = 0;
    while (received.size() < 2 && i < 3) {
        Thread.sleep(1000);
        i++;
    }
    assertEquals(2, received.size());
}
Also used : ArrayList(java.util.ArrayList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) TabularData(javax.management.openmbean.TabularData) Version(org.osgi.framework.Version) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 54 with TabularData

use of javax.management.openmbean.TabularData in project aries by apache.

the class ServiceStateMBeanTest method testMBeanInterface.

@Test
public void testMBeanInterface() throws Exception {
    //get bundles
    Bundle a = getBundleByName("org.apache.aries.jmx.test.bundlea");
    assertBundleStarted(a);
    Bundle b = getBundleByName("org.apache.aries.jmx.test.bundleb");
    assertBundleStarted(b);
    // get services
    ServiceReference refA = bundleContext.getServiceReference(InterfaceA.class.getName());
    assertNotNull(refA);
    long serviceAId = (Long) refA.getProperty(Constants.SERVICE_ID);
    assertTrue(serviceAId > -1);
    ServiceReference refB = bundleContext.getServiceReference(InterfaceB.class.getName());
    assertNotNull(refB);
    long serviceBId = (Long) refB.getProperty(Constants.SERVICE_ID);
    assertTrue(serviceBId > -1);
    ServiceReference[] refs = bundleContext.getServiceReferences(ManagedServiceFactory.class.getName(), "(" + Constants.SERVICE_PID + "=jmx.test.B.factory)");
    assertNotNull(refs);
    assertEquals(1, refs.length);
    ServiceReference msf = refs[0];
    // getBundleIdentifier
    assertEquals(a.getBundleId(), mbean.getBundleIdentifier(serviceAId));
    //getObjectClass
    String[] objectClass = mbean.getObjectClass(serviceAId);
    assertEquals(2, objectClass.length);
    List<String> classNames = Arrays.asList(objectClass);
    assertTrue(classNames.contains(InterfaceA.class.getName()));
    assertTrue(classNames.contains(ManagedService.class.getName()));
    // getProperties
    TabularData serviceProperties = mbean.getProperties(serviceBId);
    assertNotNull(serviceProperties);
    assertEquals(JmxConstants.PROPERTIES_TYPE, serviceProperties.getTabularType());
    assertTrue(serviceProperties.values().size() > 1);
    assertEquals("org.apache.aries.jmx.test.ServiceB", PropertyData.from(serviceProperties.get(new Object[] { Constants.SERVICE_PID })).getValue());
    // getUsingBundles
    long[] usingBundles = mbean.getUsingBundles(serviceBId);
    assertEquals(1, usingBundles.length);
    assertEquals(a.getBundleId(), usingBundles[0]);
    // listServices
    ServiceReference[] allSvsRefs = bundleContext.getAllServiceReferences(null, null);
    TabularData allServices = mbean.listServices();
    assertNotNull(allServices);
    assertEquals(allSvsRefs.length, allServices.values().size());
    // notifications
    final List<Notification> received = new ArrayList<Notification>();
    final List<AttributeChangeNotification> attributeChanges = new ArrayList<AttributeChangeNotification>();
    mbeanServer.addNotificationListener(objectName, new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            if (notification instanceof AttributeChangeNotification) {
                attributeChanges.add((AttributeChangeNotification) notification);
            } else {
                received.add(notification);
            }
        }
    }, null, null);
    assertNotNull(refB);
    assertNotNull(msf);
    b.stop();
    refB = bundleContext.getServiceReference(InterfaceB.class.getName());
    refs = bundleContext.getServiceReferences(ManagedServiceFactory.class.getName(), "(" + Constants.SERVICE_PID + "=jmx.test.B.factory)");
    assertNull(refs);
    assertNull(refB);
    b.start();
    refB = bundleContext.getServiceReference(InterfaceB.class.getName());
    refs = bundleContext.getServiceReferences(ManagedServiceFactory.class.getName(), "(" + Constants.SERVICE_PID + "=jmx.test.B.factory)");
    assertNotNull(refB);
    assertNotNull(refs);
    assertEquals(1, refs.length);
    waitForListToReachSize(received, 4);
    assertEquals(4, received.size());
    assertEquals(4, attributeChanges.size());
}
Also used : InterfaceA(org.apache.aries.jmx.test.bundlea.api.InterfaceA) InterfaceB(org.apache.aries.jmx.test.bundleb.api.InterfaceB) AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ServiceReference(org.osgi.framework.ServiceReference) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) TabularData(javax.management.openmbean.TabularData) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 55 with TabularData

use of javax.management.openmbean.TabularData in project aries by apache.

the class ServiceStateMBeanTest method testListServicesSelectiveItems.

@SuppressWarnings("unchecked")
@Test
public void testListServicesSelectiveItems() throws Exception {
    String filter = "(|(service.pid=org.apache.aries.jmx.test.ServiceB)(service.pid=jmx.test.B.factory))";
    ServiceReference[] refs = bundleContext.getAllServiceReferences(null, filter);
    TabularData svcData = mbean.listServices(null, filter, ServiceStateMBean.BUNDLE_IDENTIFIER);
    assertEquals(refs.length, svcData.size());
    long id = refs[0].getBundle().getBundleId();
    for (ServiceReference ref : refs) {
        assertEquals("Precondition", id, ref.getBundle().getBundleId());
    }
    for (CompositeData cd : new ArrayList<CompositeData>((Collection<CompositeData>) svcData.values())) {
        assertEquals(id, cd.get(ServiceStateMBean.BUNDLE_IDENTIFIER));
        assertNotNull(cd.get(ServiceStateMBean.IDENTIFIER));
        assertNull(cd.get(ServiceStateMBean.OBJECT_CLASS));
        assertNull(cd.get(ServiceStateMBean.USING_BUNDLES));
    }
}
Also used : CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) TabularData(javax.management.openmbean.TabularData) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Aggregations

TabularData (javax.management.openmbean.TabularData)183 CompositeData (javax.management.openmbean.CompositeData)91 TabularDataSupport (javax.management.openmbean.TabularDataSupport)67 ObjectName (javax.management.ObjectName)54 MBeanServer (javax.management.MBeanServer)50 CompositeType (javax.management.openmbean.CompositeType)47 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)43 Test (org.junit.Test)38 Map (java.util.Map)28 ArrayList (java.util.ArrayList)23 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)21 HashMap (java.util.HashMap)20 TabularType (javax.management.openmbean.TabularType)17 Bundle (org.osgi.framework.Bundle)17 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)15 Collection (java.util.Collection)13 IOException (java.io.IOException)11 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)9 List (java.util.List)8 ServiceReference (org.osgi.framework.ServiceReference)8