Search in sources :

Example 51 with MBeanInfo

use of javax.management.MBeanInfo in project symmetric-ds by JumpMind.

the class JmxCommand method executeWithOptions.

@Override
protected boolean executeWithOptions(final CommandLine line) throws Exception {
    if (line.hasOption(OPTION_LISTBEANS)) {
        execute(new IJmxTemplate<Object>() {

            @Override
            public Object execute(String engineName, MBeanServerConnection mbeanConn) throws Exception {
                Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
                for (ObjectName objectName : beanSet) {
                    if (objectName.getDomain().startsWith("org.jumpmind.symmetric." + engineName)) {
                        System.out.println(objectName.toString());
                    }
                }
                return null;
            }
        });
    } else if (line.hasOption(OPTION_LISTMETHODS) || line.hasOption(OPTION_METHOD)) {
        if (line.hasOption(OPTION_BEAN)) {
            execute(new IJmxTemplate<Object>() {

                @Override
                public Object execute(String engineName, MBeanServerConnection mbeanConn) throws Exception {
                    String beanName = line.getOptionValue(OPTION_BEAN);
                    MBeanInfo info = mbeanConn.getMBeanInfo(new ObjectName(beanName));
                    if (info != null) {
                        if (line.hasOption(OPTION_LISTMETHODS)) {
                            MBeanOperationInfo[] operations = info.getOperations();
                            Map<String, MBeanOperationInfo> orderedMap = new TreeMap<String, MBeanOperationInfo>();
                            for (MBeanOperationInfo methodInfo : operations) {
                                orderedMap.put(methodInfo.getName(), methodInfo);
                            }
                            for (MBeanOperationInfo methodInfo : orderedMap.values()) {
                                System.out.print(methodInfo.getName() + "(");
                                MBeanParameterInfo[] params = methodInfo.getSignature();
                                int index = 0;
                                for (MBeanParameterInfo p : params) {
                                    if (index > 0) {
                                        System.out.print(", ");
                                    }
                                    System.out.print(p.getType() + " " + p.getName());
                                    index++;
                                }
                                System.out.print(")");
                                if (methodInfo.getReturnType() != null && !methodInfo.getReturnType().equals("void")) {
                                    System.out.print(" : " + methodInfo.getReturnType());
                                }
                                System.out.println();
                            }
                        } else if (line.hasOption(OPTION_METHOD)) {
                            String argsDelimiter = line.getOptionValue(OPTION_ARGS_DELIM);
                            if (isBlank(argsDelimiter)) {
                                argsDelimiter = ",";
                            } else {
                                argsDelimiter = argsDelimiter.trim();
                            }
                            String methodName = line.getOptionValue(OPTION_METHOD);
                            String[] args = null;
                            if (line.hasOption(OPTION_ARGS)) {
                                String argLine = line.getOptionValue(OPTION_ARGS);
                                args = argsDelimiter == "," ? CsvUtils.tokenizeCsvData(argLine) : argLine.split(argsDelimiter);
                                ;
                            } else {
                                args = new String[0];
                            }
                            MBeanOperationInfo[] operations = info.getOperations();
                            for (MBeanOperationInfo methodInfo : operations) {
                                MBeanParameterInfo[] paramInfos = methodInfo.getSignature();
                                if (methodInfo.getName().equals(methodName) && paramInfos.length == args.length) {
                                    String[] signature = new String[args.length];
                                    Object[] objArgs = new Object[args.length];
                                    int index = 0;
                                    for (MBeanParameterInfo paramInfo : paramInfos) {
                                        signature[index] = paramInfo.getType();
                                        if (!paramInfo.getType().equals(String.class.getName())) {
                                            Class<?> clazz = Class.forName(paramInfo.getType());
                                            Constructor<?> constructor = clazz.getConstructor(String.class);
                                            objArgs[index] = constructor.newInstance(args[index]);
                                        } else {
                                            objArgs[index] = args[index];
                                        }
                                        index++;
                                    }
                                    Object returnValue = mbeanConn.invoke(new ObjectName(beanName), methodName, objArgs, signature);
                                    if (methodInfo.getReturnType() != null && !methodInfo.getReturnType().equals("void")) {
                                        System.out.println(returnValue);
                                    }
                                    System.exit(0);
                                }
                            }
                            System.out.println("ERROR: Could not locate a JMX method named: " + methodName + " with " + args.length + " arguments on bean: " + beanName);
                            System.exit(1);
                            return null;
                        }
                    } else {
                        System.out.println("ERROR: Could not locate a JMX bean with the name of: " + beanName);
                        System.exit(1);
                    }
                    return null;
                }
            });
        } else {
            System.out.println("ERROR: Must specifiy the --bean option.");
            System.exit(1);
        }
    } else {
        return false;
    }
    return true;
}
Also used : Set(java.util.Set) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) Constructor(java.lang.reflect.Constructor) TreeMap(java.util.TreeMap) ObjectName(javax.management.ObjectName) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 52 with MBeanInfo

use of javax.management.MBeanInfo in project deltaspike by apache.

the class SimpleRegistrationTest method checkMBean.

@Test
public void checkMBean() throws Exception {
    assertEquals(0, myMBean.getCounter());
    myMBean.resetTo(2);
    final ObjectName on = new ObjectName("org.apache.deltaspike:type=MBeans,name=" + MyMBean.class.getName());
    assertTrue(server.isRegistered(on));
    assertEquals(2, server.getAttribute(on, "counter"));
    assertEquals(6, server.invoke(on, "multiply", new Object[] { 3 }, new String[0]));
    myMBean.resetTo(5);
    assertEquals(5, server.getAttribute(on, "counter"));
    assertEquals(20, server.invoke(on, "multiply", new Object[] { 4 }, new String[0]));
    server.setAttribute(on, new Attribute("counter", 10));
    assertEquals(10, myMBean.getCounter());
    final Collection<Notification> notifications = new ArrayList<Notification>();
    server.addNotificationListener(on, new NotificationListener() {

        @Override
        public void handleNotification(final Notification notification, final Object handback) {
            notifications.add(notification);
        }
    }, null, null);
    myMBean.broadcast();
    assertEquals(1, notifications.size());
    assertEquals(10L, notifications.iterator().next().getSequenceNumber());
    MBeanInfo mBeanInfo = server.getMBeanInfo(on);
    Assert.assertNotNull(mBeanInfo);
    MBeanOperationInfo[] operations = mBeanInfo.getOperations();
    Assert.assertNotNull(operations);
    Assert.assertTrue(operations.length > 0);
    Assert.assertTrue("Empty Signature on operation: " + operations[1], operations[1].getSignature().length > 0);
    MBeanParameterInfo parameterInfo = operations[1].getSignature()[0];
    assertEquals("multiplier", parameterInfo.getName());
    assertEquals("the multiplier", parameterInfo.getDescription());
    {
        // table support - through map
        Object table = server.getAttribute(on, "table");
        assertTrue(TabularData.class.isInstance(table));
        final TabularData data = TabularData.class.cast(table);
        assertEquals(1, data.size());
        final CompositeData compositeData = CompositeData.class.cast(data.values().iterator().next());
        assertEquals(2, compositeData.values().size());
        assertEquals("value1", compositeData.get("key1"));
        assertEquals("value2", compositeData.get("key2"));
    }
    {
        // table support - through Table
        Object table = server.getAttribute(on, "table2");
        assertTrue(TabularData.class.isInstance(table));
        final TabularData data = TabularData.class.cast(table);
        assertEquals(2, data.size());
        final Iterator<?> iterator = data.values().iterator();
        {
            final CompositeData compositeData = CompositeData.class.cast(iterator.next());
            assertEquals(3, compositeData.values().size());
            assertEquals("1", compositeData.get("a"));
            assertEquals("2", compositeData.get("b"));
            assertEquals("3", compositeData.get("c"));
        }
        {
            final CompositeData compositeData = CompositeData.class.cast(iterator.next());
            assertEquals(3, compositeData.values().size());
            assertEquals("alpha", compositeData.get("a"));
            assertEquals("beta", compositeData.get("b"));
            assertEquals("gamma", compositeData.get("c"));
        }
    }
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) MBeanOperationInfo(javax.management.MBeanOperationInfo) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) TabularData(javax.management.openmbean.TabularData) Iterator(java.util.Iterator) NotificationListener(javax.management.NotificationListener) MBeanParameterInfo(javax.management.MBeanParameterInfo) Test(org.junit.Test)

Example 53 with MBeanInfo

use of javax.management.MBeanInfo in project tomee by apache.

the class EjbdJmxTest method test.

@Test
public void test() throws Exception {
    System.setProperty("openejb.jmx.active", "true");
    final MBeanServer server = LocalMBeanServer.get();
    OpenEJB.init(new Properties());
    final Properties p = new Properties();
    p.put("server", "org.apache.openejb.server.ejbd.EjbServer");
    p.put("bind", "127.0.0.1");
    p.put("port", "0");
    p.put("disabled", "false");
    p.put("threads", "10");
    p.put("backlog", "200");
    p.put("discovery", "ejb:ejbd://{bind}:{port}");
    final ServerService service = ServiceManager.manage("ejbd", p, new EjbServer());
    service.init(p);
    service.start();
    ServiceManager.register("ejbd", service, server);
    ObjectName invocationsName = new ObjectName("openejb:type=ServerService,name=ejbd");
    MBeanInfo beanInfo = server.getMBeanInfo(invocationsName);
    for (MBeanAttributeInfo info : beanInfo.getAttributes()) {
        System.out.println(info);
    }
    service.stop();
    OpenEJB.destroy();
}
Also used : MBeanInfo(javax.management.MBeanInfo) ServerService(org.apache.openejb.server.ServerService) Properties(java.util.Properties) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 54 with MBeanInfo

use of javax.management.MBeanInfo in project kernel by exoplatform.

the class TestCacheService method testCacheService.

public void testCacheService() throws Exception {
    assertNotNull(service_.getAllCacheInstances());
    int size = service_.getAllCacheInstances().size();
    // -----nocache info is retrived from test-configuration(nocache 0bject)
    ExoCache<String, Object> nocache = service_.getCacheInstance("nocache");
    assertTrue("expect find nocache configuaration", nocache instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of nocache is", 5, nocache.getMaxSize());
    assertEquals("expect 'liveTime' of nocache' is", 0, nocache.getLiveTime());
    nocache.put("key1", "object 1");
    assertEquals("expect 'nocache' is not lived(LiveTime=0)", 0, nocache.getCacheSize());
    // -----cacheLiveTime2s's info is retrived from test-configuration
    // (cacheLiveTime2s object)
    ExoCache<String, Object> cacheLiveTime2s = service_.getCacheInstance("cacheLiveTime2s");
    assertTrue("expect find cacheLiveTime2s configuaration", cacheLiveTime2s instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 5, cacheLiveTime2s.getMaxSize());
    assertEquals("expect 'liveTime' of nocache' is", 2, cacheLiveTime2s.getLiveTime());
    cacheLiveTime2s.put("key", "object2s");
    String obj2s = (String) cacheLiveTime2s.get("key");
    assertTrue("expect found 'object' in cache", obj2s != null && obj2s.equals("object2s"));
    assertEquals("expect found object in this cache", 1, cacheLiveTime2s.getCacheSize());
    Thread.sleep(2500);
    assertTrue("expect no found 'object' in this cache", cacheLiveTime2s.get("key") == null);
    assertEquals("expect cache size is ", 0, cacheLiveTime2s.getCacheSize());
    // -----cacheMaxSize0's info retrived from test-configuration (cacheMaxSize0
    // object)
    ExoCache<String, Object> cacheMaxSize0 = service_.getCacheInstance("cacheMaxSize0");
    assertTrue("expect find cacheMaxSize0 configuaration", cacheMaxSize0 instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 0, cacheMaxSize0.getMaxSize());
    assertEquals("expect 'liveTime' of nocache' is", 4, cacheMaxSize0.getLiveTime());
    cacheMaxSize0.put("mkey", "maxsize object");
    assertTrue("expect can't put any object to  cache", cacheMaxSize0.get("mkey") == null);
    // -----default cache's info is retrived if no cache's info is found
    ExoCache<String, Object> cache = service_.getCacheInstance("exo");
    assertTrue("expect find defaul cache configuaration", cache instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 100, cache.getMaxSize());
    assertEquals("expect 'liveTime' of this cache' is", 300, cache.getLiveTime());
    cache.put("test", "this is a test");
    String ret = (String) cache.get("test");
    assertTrue("expect object is cached", ret != null);
    /* ----------FIFOExoCache--------------- */
    ExoCache<String, Object> fifoCache = service_.getCacheInstance("fifocache");
    assertTrue("expect find fifo cache configuration", fifoCache instanceof FIFOExoCache);
    assertEquals("expect 'maxsize' of this cache is", 3, fifoCache.getMaxSize());
    assertEquals("expect 'liveTime' of this cache' is", 4, fifoCache.getLiveTime());
    fifoCache.put("key1", "object 1");
    fifoCache.put("key2", "object 2");
    assertEquals("expect FIFOExoCache size is:", 2, fifoCache.getCacheSize());
    String obj1 = (String) fifoCache.get("key1");
    String obj2 = (String) fifoCache.get("key2");
    assertTrue("expect found 'key1' object", obj1 != null && obj1.equals("object 1"));
    assertTrue("expect found 'key2' object", obj2 != null && obj2.equals("object 2"));
    fifoCache.put("skey", "serializable object");
    assertEquals("expect FIFOExoCache size is:", 3, fifoCache.getCacheSize());
    String sobj = (String) fifoCache.get("skey");
    assertTrue("expect found serializable key and it's value", sobj != null && sobj.equals("serializable object"));
    fifoCache.put("key4", "object 4");
    // because maxsize of cache is 3, 'object 1' associated with 'key1' is
    // remove form FIFOExoCache
    assertEquals("expect cache size is still:", 3, fifoCache.getCacheSize());
    String obj4 = (String) fifoCache.get("key4");
    assertTrue("expect object has 'key4' is put in cache", obj4 != null && obj4.equals("object 4"));
    assertTrue("expect object has key is 'key1' is remove automatically", fifoCache.get("key1") == null);
    // -------remove a object in cache by key
    fifoCache.remove("key2");
    assertEquals("now, expect cache size is", 2, fifoCache.getCacheSize());
    assertEquals("now, expect number of object in cache is:", 2, fifoCache.getCachedObjects().size());
    assertTrue("expect object has 'key2' is removed", fifoCache.get("key2") == null);
    // -------remove a object in cache by serializable name
    fifoCache.remove(new String("skey"));
    assertEquals("now, expect cache size is", 1, fifoCache.getCacheSize());
    assertEquals("now, expect number of object in cache is:", 1, fifoCache.getCachedObjects().size());
    assertTrue("expect serializable object with name 'skey' is remove", fifoCache.get(new String("skey")) == null);
    // --------------clear cache
    fifoCache.clearCache();
    assertEquals("now, expect cache is clear", 0, fifoCache.getCacheSize());
    assertEquals("now, expect number of object in cache is:", 0, fifoCache.getCachedObjects().size());
    /* --------------test cache service with add extenal component plugin------ */
    ExoCache simpleCachePlugin = service_.getCacheInstance("simpleCachePlugin");
    assertTrue("expect found simpleCache from extenal plugin", simpleCachePlugin instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 8, simpleCachePlugin.getMaxSize());
    assertEquals("expect 'LiveTime' of this cache is", 5, simpleCachePlugin.getLiveTime());
    ExoCache fifoCachePlugin = service_.getCacheInstance("fifoCachePlugin");
    assertTrue("expect found fifoCache from extenal plugin", fifoCachePlugin instanceof FIFOExoCache);
    assertEquals("expect 'maxsize' of this cache is", 6, fifoCachePlugin.getMaxSize());
    assertEquals("expect 'LiveTime' of this cache is", 10, fifoCachePlugin.getLiveTime());
    // ----all cache instances---
    Collection<ExoCache<? extends Serializable, ?>> caches = service_.getAllCacheInstances();
    assertEquals("expect number of cache instanse is ", size + 7, caches.size());
    hasObjectInCollection(nocache, caches, new ExoCacheComparator());
    hasObjectInCollection(cacheLiveTime2s, caches, new ExoCacheComparator());
    hasObjectInCollection(cacheMaxSize0, caches, new ExoCacheComparator());
    hasObjectInCollection(fifoCache, caches, new ExoCacheComparator());
    hasObjectInCollection(cache, caches, new ExoCacheComparator());
    hasObjectInCollection(simpleCachePlugin, caches, new ExoCacheComparator());
    hasObjectInCollection(fifoCachePlugin, caches, new ExoCacheComparator());
    // Managed tests
    MBeanServerLocator locator = (MBeanServerLocator) PortalContainer.getInstance().getComponentInstanceOfType(MBeanServerLocator.class);
    MBeanServer server = locator.server;
    assertNotNull(locator.server);
    ObjectName name = new ObjectName("exo:service=cache,name=cacheLiveTime2s,portal=portal");
    MBeanInfo info = server.getMBeanInfo(name);
    assertNotNull(info);
    Map<String, MBeanAttributeInfo> infoMap = new HashMap<String, MBeanAttributeInfo>();
    for (MBeanAttributeInfo attributeInfo : info.getAttributes()) {
        infoMap.put(attributeInfo.getName(), attributeInfo);
    }
    assertTrue(infoMap.containsKey("Name"));
    assertTrue(infoMap.containsKey("Size"));
    assertTrue(infoMap.containsKey("Capacity"));
    assertTrue(infoMap.containsKey("TimeToLive"));
    assertTrue(infoMap.containsKey("HitCount"));
    assertTrue(infoMap.containsKey("MissCount"));
    assertEquals(6, infoMap.size());
    assertEquals(5, server.getAttribute(name, "Capacity"));
    assertEquals(size + 7, service_.getAllCacheInstances().size());
}
Also used : FIFOExoCache(org.exoplatform.services.cache.FIFOExoCache) ExoCache(org.exoplatform.services.cache.ExoCache) SimpleExoCache(org.exoplatform.services.cache.SimpleExoCache) ConcurrentFIFOExoCache(org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache) Serializable(java.io.Serializable) MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) SimpleExoCache(org.exoplatform.services.cache.SimpleExoCache) ObjectName(javax.management.ObjectName) FIFOExoCache(org.exoplatform.services.cache.FIFOExoCache) ConcurrentFIFOExoCache(org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache) MBeanServer(javax.management.MBeanServer)

Example 55 with MBeanInfo

use of javax.management.MBeanInfo in project hive by apache.

the class JMXJsonServlet method listBeans.

// --------------------------------------------------------- Private Methods
private void listBeans(JsonGenerator jg, ObjectName qry, String attribute, HttpServletResponse response) throws IOException {
    LOG.debug("Listing beans for " + qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    jg.writeArrayFieldStart("beans");
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        MBeanInfo minfo;
        String code = "";
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    prs = attribute;
                    attributeinfo = mBeanServer.getAttribute(oname, prs);
                }
            } catch (AttributeNotFoundException e) {
                // If the modelerType attribute was not found, the class name is used
                // instead.
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (MBeanException e) {
                // The code inside the attribute getter threw an exception so log it,
                // and fall back on the class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (RuntimeException e) {
                // For some reason even with an MBeanException available to them
                // Runtime exceptions can still find their way through, so treat them
                // the same as MBeanException
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (ReflectionException e) {
                // This happens when the code inside the JMX bean (setter?? from the
                // java docs) threw an exception, so log it and fall back on the
                // class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            }
        } catch (InstanceNotFoundException e) {
            // Ignored for some reason the bean was not found so don't output it
            continue;
        } catch (IntrospectionException e) {
            // This is an internal error, something odd happened with reflection so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        } catch (ReflectionException e) {
            // This happens when the code inside the JMX bean threw an exception, so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        }
        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        jg.writeStringField("modelerType", code);
        if ((attribute != null) && (attributeinfo == null)) {
            jg.writeStringField("result", "ERROR");
            jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
            jg.writeEndObject();
            jg.writeEndArray();
            jg.close();
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (attribute != null) {
            writeAttribute(jg, attribute, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, oname, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Aggregations

MBeanInfo (javax.management.MBeanInfo)154 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)87 ObjectName (javax.management.ObjectName)79 MBeanOperationInfo (javax.management.MBeanOperationInfo)38 MBeanServer (javax.management.MBeanServer)27 Test (org.junit.Test)27 Attribute (javax.management.Attribute)19 ArrayList (java.util.ArrayList)17 IntrospectionException (javax.management.IntrospectionException)16 ReflectionException (javax.management.ReflectionException)16 HashMap (java.util.HashMap)15 InstanceNotFoundException (javax.management.InstanceNotFoundException)15 IOException (java.io.IOException)12 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)12 MBeanParameterInfo (javax.management.MBeanParameterInfo)12 MBeanServerConnection (javax.management.MBeanServerConnection)10 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 AttributeList (javax.management.AttributeList)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)9 Descriptor (javax.management.Descriptor)8