Example usage for javax.management Attribute getName

List of usage examples for javax.management Attribute getName

Introduction

In this page you can find the example usage for javax.management Attribute getName.

Prototype

public String getName() 

Source Link

Document

Returns a String containing the name of the attribute.

Usage

From source file:catalina.mbeans.ContextResourceMBean.java

/**
 * Set the value of a specific attribute of this MBean.
 *
 * @param attribute The identification of the attribute to be set
 *  and the new value/*from  www  .j  a  va  2s .c o m*/
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
public void setAttribute(Attribute attribute)
        throws AttributeNotFoundException, MBeanException, ReflectionException {

    // Validate the input parameters
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute is null"),
                "Attribute is null");
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name is null"),
                "Attribute name is null");

    ContextResource cr = null;
    try {
        cr = (ContextResource) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }

    if ("auth".equals(name)) {
        cr.setAuth((String) value);
    } else if ("description".equals(name)) {
        cr.setDescription((String) value);
    } else if ("name".equals(name)) {
        cr.setName((String) value);
    } else if ("scope".equals(name)) {
        cr.setScope((String) value);
    } else if ("type".equals(name)) {
        cr.setType((String) value);
    } else {
        ResourceParams rp = cr.getNamingResources().findResourceParams(cr.getName());
        if (rp != null) {
            String valueStr = "" + value;
            rp.addParameter(name, valueStr);
            cr.getNamingResources().removeResourceParams(cr.getName());
        } else {
            rp = new ResourceParams();
            rp.setName(cr.getName());
            String valueStr = "" + value;
            rp.addParameter(name, valueStr);
        }
        cr.getNamingResources().addResourceParams(rp);
    }

    // cannot use side-efects.  It's removed and added back each time 
    // there is a modification in a resource.
    NamingResources nr = cr.getNamingResources();
    nr.removeResource(cr.getName());
    nr.addResource(cr);
}

From source file:org.helios.netty.jmx.MetricCollector.java

/**
 * Returns a simple map of NIO metrics/*from  w ww . j  av a  2s .  c  o m*/
 * @return a simple map of NIO metrics
 */
protected Map<String, Long> getNio() {
    Map<String, Long> map = new HashMap<String, Long>(NIO_ATTRS.length);
    try {
        AttributeList attrs = ManagementFactory.getPlatformMBeanServer().getAttributes(directNio, NIO_ATTRS);
        for (Attribute attr : attrs.asList()) {
            map.put(attr.getName(), (Long) attr.getValue());
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    return map;
}

From source file:org.fishwife.jrugged.spring.jmx.TestWebMBeanAdapter.java

@Test
public void testGetAttributeValues() throws Exception {
    MBeanAttributeInfo[] attributeInfoArray = new MBeanAttributeInfo[2];
    MBeanAttributeInfo mockAttributeInfo1 = createMock(MBeanAttributeInfo.class);
    MBeanAttributeInfo mockAttributeInfo2 = createMock(MBeanAttributeInfo.class);
    attributeInfoArray[0] = mockAttributeInfo1;
    attributeInfoArray[1] = mockAttributeInfo2;
    expect(mockMBeanInfo.getAttributes()).andReturn(attributeInfoArray);

    String attributeName1 = "attribute_name_1";
    String attributeName2 = "attribute_name_2";
    expect(mockAttributeInfo1.getName()).andReturn(attributeName1);
    expect(mockAttributeInfo2.getName()).andReturn(attributeName2);

    AttributeList mockAttributeList = createMock(AttributeList.class);
    expect(mockMBeanServer.getAttributes(eq(mockObjectName), anyObject(String[].class)))
            .andReturn(mockAttributeList);

    List<Attribute> attributeList = new ArrayList<Attribute>();
    Attribute mockAttribute1 = createMock(Attribute.class);
    Attribute mockAttribute2 = createMock(Attribute.class);

    attributeList.add(mockAttribute1);//  w  ww  .j a v  a 2  s .com
    attributeList.add(mockAttribute2);

    expect(mockAttributeList.asList()).andReturn(attributeList);

    String name1 = "name 1";
    String value1 = "value 1";
    expect(mockAttribute1.getName()).andReturn(name1);
    expect(mockAttribute1.getValue()).andReturn(value1);
    expect(mockSanitizer.escapeValue(value1)).andReturn(value1);

    String name2 = "name 2";
    String value2 = "value 2";
    expect(mockAttribute2.getName()).andReturn(name2);
    expect(mockAttribute2.getValue()).andReturn(value2);
    expect(mockSanitizer.escapeValue(value2)).andReturn(value2);

    replay(mockMBeanServer, mockSanitizer, mockObjectName, mockMBeanInfo, mockAttributeInfo1,
            mockAttributeInfo2, mockAttributeList, mockAttribute1, mockAttribute2);

    Map<String, Object> attributeValueMap = webMBeanAdapter.getAttributeValues();

    assertEquals(2, attributeValueMap.size());
    assertEquals(value1, attributeValueMap.get(name1));
    assertEquals(value2, attributeValueMap.get(name2));

    verify(mockMBeanServer, mockSanitizer, mockObjectName, mockMBeanInfo, mockAttributeInfo1,
            mockAttributeInfo2, mockAttributeList, mockAttribute1, mockAttribute2);
}

From source file:org.apache.tomcat.util.mx.DynamicMBeanProxy.java

public void setAttribute(Attribute attribute)
        throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    if (methods == null)
        init();// w  w w.  j a  va 2 s. c  o  m
    // XXX Send notification !!!
    Method m = (Method) setAttMap.get(attribute.getName());
    if (m == null)
        throw new AttributeNotFoundException(attribute.getName());

    try {
        log.info(real.getClass().getName() + "setAttribute " + attribute.getName());
        m.invoke(real, new Object[] { attribute.getValue() });
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        throw new MBeanException(ex);
    } catch (InvocationTargetException ex1) {
        ex1.printStackTrace();
        throw new MBeanException(ex1);
    }
}

From source file:org.rifidi.edge.configuration.DefaultConfigurationImpl.java

@Override
public void setAttribute(final Attribute attribute)
        throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    assert (attribute != null);
    RifidiService service = target.get();
    if (service != null) {
        service.setAttribute(attribute);
    }//from w  w w  . j  a v a  2  s. c  om

    attributes.set(nameToPos.get(attribute.getName()), attribute);
    notifierService.attributesChanged(getServiceID(), (AttributeList) attributes.clone());
}

From source file:com.tc.stats.DSO.java

@Override
public Map<ObjectName, Map<String, Object>> getAttributeMap(Map<ObjectName, Set<String>> attributeMap,
        long timeout, TimeUnit unit) {
    Map<ObjectName, Map<String, Object>> result = new HashMap<ObjectName, Map<String, Object>>();
    List<Callable<SourcedAttributeList>> tasks = new ArrayList<Callable<SourcedAttributeList>>();
    Iterator<Entry<ObjectName, Set<String>>> entryIter = attributeMap.entrySet().iterator();
    while (entryIter.hasNext()) {
        Entry<ObjectName, Set<String>> entry = entryIter.next();
        tasks.add(new AttributeListTask(entry.getKey(), entry.getValue()));
    }/*  www . j  ava2  s .c  o  m*/
    try {
        List<Future<SourcedAttributeList>> results = pool.invokeAll(tasks, timeout, unit);
        Iterator<Future<SourcedAttributeList>> resultIter = results.iterator();
        while (resultIter.hasNext()) {
            Future<SourcedAttributeList> future = resultIter.next();
            if (future.isDone() && !future.isCancelled()) {
                try {
                    SourcedAttributeList sal = future.get();
                    Iterator<Object> attrIter = sal.attributeList.iterator();
                    Map<String, Object> onMap = new HashMap<String, Object>();
                    while (attrIter.hasNext()) {
                        Attribute attr = (Attribute) attrIter.next();
                        onMap.put(attr.getName(), attr.getValue());
                    }
                    result.put(sal.objectName, onMap);
                } catch (CancellationException ce) {
                    /**/
                } catch (ExecutionException ee) {
                    /**/
                }
            }
        }
    } catch (InterruptedException ie) {/**/
    }
    return result;
}

From source file:dk.netarkivet.harvester.harvesting.controller.BnfHeritrixController.java

/**
 * Retrieve the values of the crawl service attributes and
 * add them to the CrawlProgressMessage being put together.
 * @param cpm the crawlProgress message being prepared
 *///  w  ww .j  a  v a 2  s .  co m
private void getCrawlServiceAttributes(CrawlProgressMessage cpm) {
    List<Attribute> heritrixAtts = getMBeanAttributes(
            new CrawlServiceAttribute[] { CrawlServiceAttribute.AlertCount, CrawlServiceAttribute.IsCrawling,
                    CrawlServiceAttribute.CurrentJob });

    CrawlServiceInfo hStatus = cpm.getHeritrixStatus();
    for (Attribute att : heritrixAtts) {
        Object value = att.getValue();
        CrawlServiceAttribute crawlServiceAttribute = CrawlServiceAttribute.fromString(att.getName());
        switch (crawlServiceAttribute) {
        case AlertCount:
            Integer alertCount = -1;
            if (value != null) {
                alertCount = (Integer) value;
            }
            hStatus.setAlertCount(alertCount);
            break;
        case CurrentJob:
            String newCurrentJob = "";
            if (value != null) {
                newCurrentJob = (String) value;
            }
            hStatus.setCurrentJob(newCurrentJob);
            break;
        case IsCrawling:
            Boolean newCrawling = false;
            if (value != null) {
                newCrawling = (Boolean) value;
            }
            hStatus.setCrawling(newCrawling);
            break;
        default:
            log.debug("Unhandled attribute: " + crawlServiceAttribute);
        }
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java

public AttributeList setAttributes(AttributeList attrs) {
    log.debug("setAttributes");

    AttributeList results = new AttributeList(attrs.size());
    Iterator iter = attrs.iterator();
    while (iter.hasNext()) {
        try {/*from   w w  w  .j av  a  2 s  .  c om*/
            Attribute attr = (Attribute) iter.next();
            setAttribute(attr);
            results.add(new Attribute(attr.getName(), getAttribute(attr.getName())));
        } catch (Exception e) {
            log.warn(LogSupport.EXCEPTION, e);
        }
    }
    return results;
}

From source file:dk.netarkivet.harvester.harvesting.controller.BnfHeritrixController.java

/**
 * Retrieve the values of the crawl service job attributes and
 * add them to the CrawlProgressMessage being put together.
 * @param cpm the crawlProgress message being prepared
 *///from   w w  w .j a va  2  s  . c  o m
private void fetchCrawlServiceJobAttributes(CrawlProgressMessage cpm) {
    String progressStats = (String) executeMBeanOperation(CrawlServiceJobOperation.progressStatistics);
    CrawlServiceJobInfo jStatus = cpm.getJobStatus();
    String newProgressStats = "?";
    if (progressStats != null) {
        newProgressStats = progressStats;
    }
    jStatus.setProgressStatistics(newProgressStats);

    if (progressStatisticsLegend == null) {
        progressStatisticsLegend = (String) executeMBeanOperation(
                CrawlServiceJobOperation.progressStatisticsLegend);
    }

    List<Attribute> jobAtts = getMBeanAttributes(CrawlServiceJobAttribute.values());

    for (Attribute att : jobAtts) {
        Object value = att.getValue();
        CrawlServiceJobAttribute aCrawlServiceJobAttribute = CrawlServiceJobAttribute.fromString(att.getName());
        switch (aCrawlServiceJobAttribute) {
        case CrawlTime:
            Long elapsedSeconds = -1L;
            if (value != null) {
                elapsedSeconds = (Long) value;
            }
            jStatus.setElapsedSeconds(elapsedSeconds);
            break;
        case CurrentDocRate:
            Double processedDocsPerSec = new Double(-1L);
            if (value != null) {
                processedDocsPerSec = (Double) value;
            }
            jStatus.setCurrentProcessedDocsPerSec(processedDocsPerSec);
            break;
        case CurrentKbRate:
            // NB Heritrix seems to store the average value in
            // KbRate instead of CurrentKbRate...
            // Inverse of doc rates.
            Long processedKBPerSec = -1L;
            if (value != null) {
                processedKBPerSec = (Long) value;
            }
            jStatus.setProcessedKBPerSec(processedKBPerSec);
            break;
        case DiscoveredCount:
            Long discoveredCount = -1L;
            if (value != null) {
                discoveredCount = (Long) value;
            }
            jStatus.setDiscoveredFilesCount(discoveredCount);
            break;
        case DocRate:
            Double docRate = new Double(-1L);
            if (value != null) {
                docRate = (Double) value;
            }
            jStatus.setProcessedDocsPerSec(docRate);
            break;
        case DownloadedCount:
            Long downloadedCount = -1L;
            if (value != null) {
                downloadedCount = (Long) value;
            }
            jStatus.setDownloadedFilesCount(downloadedCount);
            break;
        case FrontierShortReport:
            String frontierShortReport = "?";
            if (value != null) {
                frontierShortReport = (String) value;
            }
            jStatus.setFrontierShortReport(frontierShortReport);
            break;
        case KbRate:
            // NB Heritrix seems to store the average value in
            // KbRate instead of CurrentKbRate...
            // Inverse of doc rates.
            Long kbRate = -1L;
            if (value != null) {
                kbRate = (Long) value;
            }
            jStatus.setCurrentProcessedKBPerSec(kbRate);
            break;
        case Status:
            String newStatus = "?";
            if (value != null) {
                newStatus = (String) value;
            }
            jStatus.setStatus(newStatus);
            if (value != null) {
                String status = (String) value;
                if (CrawlController.PAUSING.equals(status)) {
                    cpm.setStatus(CrawlStatus.CRAWLER_PAUSING);
                } else if (CrawlController.PAUSED.equals(status)) {
                    cpm.setStatus(CrawlStatus.CRAWLER_PAUSED);
                } else {
                    cpm.setStatus(CrawlStatus.CRAWLER_ACTIVE);
                }
            }
            break;
        case ThreadCount:
            Integer currentActiveToecount = -1;
            if (value != null) {
                currentActiveToecount = (Integer) value;
            }
            jStatus.setActiveToeCount(currentActiveToecount);
            break;
        default:
            log.debug("Unhandled attribute: " + aCrawlServiceJobAttribute);
        }
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java

public void setAttribute(Attribute attr)
        throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    if (attr == null)
        return;/*from ww w  . j  av a2  s.  c om*/

    if (log.isDebugEnabled())
        log.debug("setAttribute " + attr.getName() + "=" + attr.getValue());
    Method setter = (Method) _setter.get(attr.getName());
    if (setter == null)
        throw new AttributeNotFoundException(attr.getName());
    try {
        Object o = _object;
        if (setter.getDeclaringClass().isInstance(this))
            o = this;
        setter.invoke(o, new Object[] { attr.getValue() });
    } catch (IllegalAccessException e) {
        log.warn(LogSupport.EXCEPTION, e);
        throw new AttributeNotFoundException(e.toString());
    } catch (InvocationTargetException e) {
        log.warn(LogSupport.EXCEPTION, e);
        throw new ReflectionException((Exception) e.getTargetException());
    }
}