Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.mortbay.jetty.jmx.ws.service.impl;
  
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
 
 import  javax.ws.rs.core.UriInfo;
 
 
 
 
 public class AggregateServiceImpl implements AggregateService
 {
     private static final String[] HEAP_MEMORY_KEYS = new String[]
     { "init""used""committed""max" };
     public static final String ID_REPLACE_REGEX = ",id=\\d+";
 
     private JMXService _jmxService;
     // TODO: IOC
     private JMXNodeService _jmxNodeService = new JMXNodeServiceImpl();
 
     public AggregateServiceImpl(JMXService jmxService)
     {
         this. = jmxService;
     }
 
     public Set<NodeJaxBeangetNodes()
     {
         Set<NodeJaxBeannodes = new TreeSet<NodeJaxBean>();
         for (JMXNode jmxNode : .getNodes())
         {
             JMXServiceURL jmxServiceURL = jmxNode.getJmxServiceURL();
             String jettyVersion;
             try
             {
                 jettyVersion = getJettyVersions(jmxServiceURL);
                 Map<StringLongnodeMemoryUsageMap = getMemoryByNode(jmxNode);
                 NodeJaxBean NodeJaxBean = new NodeJaxBean.NodeJaxBeanBuilder(jmxNode.getNodeName(),jettyVersion,jmxServiceURL).memory(nodeMemoryUsageMap)
                         .threadCount(getThreadCount(jmxServiceURL)).peakThreadCount(getPeakThreadCount(jmxServiceURL)).build();
                 nodes.add(NodeJaxBean);
             }
             catch (InstanceNotFoundException e)
             {
                 throw new IllegalStateException("While fetching data for: " + jmxNode.getNodeName(),e);
             }
         }
         return nodes;
     }
 
     private String getJettyVersions(JMXServiceURL jmxServiceURLthrows InstanceNotFoundException
     {
         return (String).getAttribute(jmxServiceURL,.,"version");
     }
 
     private Map<StringLonggetMemoryByNode(JMXNode jmxNodethrows InstanceNotFoundException
     {
         JMXServiceURL jmxServiceURL = jmxNode.getJmxServiceURL();
         CompositeData memoryCompositeData = (CompositeData).getAttribute(jmxServiceURL,.,
                 .);
         Map<StringLongnodeMemoryUsageMap = new HashMap<StringLong>();
         for (String memoryKey : )
         {
             if (memoryCompositeData == null)
                 continue;
             Long heapUsage = (Long)memoryCompositeData.get(memoryKey);
             nodeMemoryUsageMap.put(memoryKey,heapUsage);
         }
         return nodeMemoryUsageMap;
     }
 
     private int getThreadCount(JMXServiceURL jmxServiceURLthrows InstanceNotFoundException
     {
         Object value = .getAttribute(jmxServiceURL,.,"ThreadCount");
        if (value == null)
            return -1;
        return (Integer)value;
    }
    private int getPeakThreadCount(JMXServiceURL jmxServiceURLthrows InstanceNotFoundException
    {
        Object value = .getAttribute(jmxServiceURL,.,"PeakThreadCount");
        if (value == null)
            return -1;
        return (Integer)value;
    }
    public MBeanShortJaxBeans getMBeanShortJaxBeans(UriInfo uriInfoCollection<JMXNodejmxNodes)
    {
        Set<ObjectNamecommonObjectNames = new HashSet<ObjectName>();
        Set<MBeanShortJaxBeanmBeanShortJaxBeans = new TreeSet<MBeanShortJaxBean>();
        for (JMXNode jmxNode : jmxNodes)
        {
            Set<ObjectNamenodeObjectNames = .getObjectNames(jmxNode.getJmxServiceURL());
            if (commonObjectNames.isEmpty())
                commonObjectNames.addAll(nodeObjectNames);
            else
                removeObjectNamesWhichDoNotExistOnCurrentNode(commonObjectNames,nodeObjectNames);
        }
        for (String objectNameString : getObjectNamesWithoutId(commonObjectNames))
        {
            mBeanShortJaxBeans.add(new MBeanShortJaxBean(uriInfo,objectNameString));
        }
        return new MBeanShortJaxBeans(mBeanShortJaxBeans);
    }
    private void removeObjectNamesWhichDoNotExistOnCurrentNode(Set<ObjectNamecommonObjectNamesSet<ObjectNamenodeObjectNames)
    {
        for (Iterator<ObjectNameit = commonObjectNames.iterator(); it.hasNext();)
        {
            ObjectName objectName = it.next();
            if (!nodeObjectNames.contains(objectName))
                it.remove();
        }
    }
    private Set<StringgetObjectNamesWithoutId(Set<ObjectNameobjectNames)
    {
        Set<StringobjectNameStrings = new TreeSet<String>();
        for (ObjectName objectName : objectNames)
        {
            objectNameStrings.add(objectName.toString().replaceFirst(,""));
        }
        return objectNameStrings;
    }
    {
        Set<MBeanAttributeValueJaxBeanmBeanAttributeValueJaxBeans = new TreeSet<MBeanAttributeValueJaxBean>();
        Map<StringMBeanAttributeInfomBeanAttributeInfos = aggregateMBeanAttributeInfos(jmxNodes,objectName);
        for (JMXNode jmxNode : jmxNodes)
        {
            JMXServiceURL jmxServiceURL = jmxNode.getJmxServiceURL();
            Set<StringobjectNames = parseObjectNameToAggregateMBeansWithMultipleIDs(objectName,jmxServiceURL);
            try
            {
                mBeanAttributeValueJaxBeans.addAll(getAttributeValues(objectNames,mBeanAttributeInfos,jmxNode));
            }
            catch (InstanceNotFoundException e)
            {
                objectNames = .getObjectNamesByPrefix(jmxServiceURL,objectName);
                mBeanAttributeValueJaxBeans.addAll(getAttributeValues(objectNames,mBeanAttributeInfos,jmxNode));
            }
        }
        return new MBeanAttributeValueJaxBeans(mBeanAttributeValueJaxBeans);
    }
    private Set<MBeanAttributeValueJaxBeangetAttributeValues(Set<StringobjectNamesMap<StringMBeanAttributeInfomBeanAttributeInfos,
            JMXNode jmxNodethrows InstanceNotFoundException
    {
        Set<MBeanAttributeValueJaxBeanmBeanAttributeValueJaxBeans = new TreeSet<MBeanAttributeValueJaxBean>();
        for (String attributeName : mBeanAttributeInfos.keySet())
        {
            getAttributeValuesForNode(attributeName,mBeanAttributeValueJaxBeans,jmxNode,objectNames);
        }
        return mBeanAttributeValueJaxBeans;
    }
    public MBeanAttributeValueJaxBeans getAttributeValues(Collection<JMXNodejmxNodesString objectNameString attributeName)
            throws InstanceNotFoundException
    {
        Set<MBeanAttributeValueJaxBeanmBeanAttributeValueJaxBeans = new TreeSet<MBeanAttributeValueJaxBean>();
        for (JMXNode jmxNode : jmxNodes)
        {
            JMXServiceURL jmxServiceURL = jmxNode.getJmxServiceURL();
            Set<StringobjectNames = parseObjectNameToAggregateMBeansWithMultipleIDs(objectName,jmxServiceURL);
            try
            {
                getAttributeValuesForNode(attributeName,mBeanAttributeValueJaxBeans,jmxNode,objectNames);
            }
            catch (InstanceNotFoundException e)
            {
                objectNames = .getObjectNamesByPrefix(jmxServiceURL,objectName);
                getAttributeValuesForNode(attributeName,mBeanAttributeValueJaxBeans,jmxNode,objectNames);
            }
        }
        return new MBeanAttributeValueJaxBeans(mBeanAttributeValueJaxBeans);
    }
    private void getAttributeValuesForNode(String attributeNameSet<MBeanAttributeValueJaxBeanmBeanAttributeValueJaxBeans,
            JMXNode jmxNodeSet<StringobjectNamesthrows InstanceNotFoundException
    {
        for (String aggregatedObjectName : objectNames)
        {
            Object value = .getAttribute(jmxNode.getJmxServiceURL(),aggregatedObjectName,attributeName);
            mBeanAttributeValueJaxBeans.add(new MBeanAttributeValueJaxBean(attributeName,jmxNode.getNodeName(),aggregatedObjectName,value));
        }
    }
    public MBeanOperationJaxBeans getOperationsMetaData(UriInfo uriInfoCollection<JMXNodejmxNodesString objectName)
            throws InstanceNotFoundException
    {
        Set<MBeanOperationJaxBeanmBeanOperationJaxBeans = new TreeSet<MBeanOperationJaxBean>();
        Map<StringMBeanOperationInfomBeanOperations = aggregateOperations(jmxNodes,objectName);
        for (MBeanOperationInfo mBeanOperationInfo : mBeanOperations.values())
            mBeanOperationJaxBeans.add(new MBeanOperationJaxBean(uriInfo,mBeanOperationInfo));
        return new MBeanOperationJaxBeans(objectName,mBeanOperationJaxBeans);
    }
    {
        Map<StringMBeanOperationInfomBeanOperations = new HashMap<StringMBeanOperationInfo>();
        Set<MBeanOperationInfononEqualOperationsToRemove = new HashSet<MBeanOperationInfo>();
        // TODO: three nested for loops and some if clauses...too high cyclomatic complexity
        for (JMXNode jmxNode : jmxNodes)
        {
            JMXServiceURL jmxServiceURL = jmxNode.getJmxServiceURL();
            Set<StringobjectNames = parseObjectNameToAggregateMBeansWithMultipleIDs(objectName,jmxServiceURL);
            try
            {
                if (!aggregateMBeanOperationInfosForNode(objectNames,mBeanOperations,nonEqualOperationsToRemove,jmxServiceURL))
                    return Collections.emptyMap();
            }
            catch (InstanceNotFoundException e)
            {
                objectNames = .getObjectNamesByPrefix(jmxServiceURL,objectName);
                if (!aggregateMBeanOperationInfosForNode(objectNames,mBeanOperations,nonEqualOperationsToRemove,jmxServiceURL))
                    return Collections.emptyMap();
            }
        }
        removeNonEqualOperations(mBeanOperations,nonEqualOperationsToRemove);
        return mBeanOperations;
    }
    private boolean aggregateMBeanOperationInfosForNode(Set<StringobjectNamesMap<StringMBeanOperationInfomBeanOperations,
            Set<MBeanOperationInfononEqualOperationsToRemoveJMXServiceURL jmxServiceURLthrows InstanceNotFoundException
    {
        for (String aggregatedObjectName : objectNames)
        {
            MBeanOperationInfo[] mBeanOperationInfoArray = .getOperations(jmxServiceURL,aggregatedObjectName);
            if (mBeanOperationInfoArray != null)
                for (MBeanOperationInfo mBeanOperationInfo : mBeanOperationInfoArray)
                {
                    if (mBeanOperations.containsKey(mBeanOperationInfo.getName())
                            && !mBeanOperations.get(mBeanOperationInfo.getName()).equals(mBeanOperationInfo))
                        nonEqualOperationsToRemove.add(mBeanOperationInfo);
                    else
                        mBeanOperations.put(mBeanOperationInfo.getName(),mBeanOperationInfo);
                }
            else
                return false;
        }
        return true;
    }
    private void removeNonEqualOperations(Map<StringMBeanOperationInfomBeanOperationsSet<MBeanOperationInfononEqualOperationsToRemove)
    {
        for (MBeanOperationInfo mBeanOperationInfo : nonEqualOperationsToRemove)
        {
            mBeanOperations.remove(mBeanOperationInfo.getName());
        }
    }
    public MBeanAttributeJaxBeans getAttributesMetaData(UriInfo uriInfoCollection<JMXNodejmxNodesString objectName)
            throws InstanceNotFoundException
    {
        Set<MBeanAttributeJaxBeanmBeanAttributeJaxBeans = new TreeSet<MBeanAttributeJaxBean>();
        Map<StringMBeanAttributeInfomBeanAttributeInfos = aggregateMBeanAttributeInfos(jmxNodes,objectName);
        for (String attributeName : mBeanAttributeInfos.keySet())
            mBeanAttributeJaxBeans.add(new MBeanAttributeJaxBean(uriInfo,mBeanAttributeInfos.get(attributeName)));
        return new MBeanAttributeJaxBeans(mBeanAttributeJaxBeans);
    }
            throws InstanceNotFoundException
    {
        Map<StringMBeanAttributeInfomBeanAttributeInfos = new HashMap<StringMBeanAttributeInfo>();
        Set<StringnonEqualAttributeNamesToRemove = new HashSet<String>();
        for (JMXNode jmxNode : jmxNodes)
        {
            JMXServiceURL jmxServiceURL = jmxNode.getJmxServiceURL();
            Set<StringobjectNames = parseObjectNameToAggregateMBeansWithMultipleIDs(objectName,jmxServiceURL);
            try
            {
                if (!aggregateMBeanAttributeInfosForNode(mBeanAttributeInfos,nonEqualAttributeNamesToRemove,jmxServiceURL,objectNames))
                    return Collections.emptyMap();
            }
            catch (InstanceNotFoundException e)
            {
                objectNames = .getObjectNamesByPrefix(jmxServiceURL,objectName);
                if (!aggregateMBeanAttributeInfosForNode(mBeanAttributeInfos,nonEqualAttributeNamesToRemove,jmxServiceURL,objectNames))
                    return Collections.emptyMap();
            }
        }
        removeDuplicates(mBeanAttributeInfos,nonEqualAttributeNamesToRemove);
        return mBeanAttributeInfos;
    }
    private boolean aggregateMBeanAttributeInfosForNode(Map<StringMBeanAttributeInfomBeanAttributeInfosSet<StringnonEqualAttributeNamesToRemove,
            JMXServiceURL jmxServiceURLSet<StringobjectNamesthrows InstanceNotFoundException
    {
        for (String aggregatedObjectName : objectNames)
        {
            MBeanAttributeInfo[] mBeanAttributeInfoArray = .getAttributes(jmxServiceURL,aggregatedObjectName);
            if (mBeanAttributeInfoArray != null)
                addAttributeInfosToMap(mBeanAttributeInfos,nonEqualAttributeNamesToRemove,mBeanAttributeInfoArray);
            else
                return false;
        }
        return true;
    }
    {
        Set<StringobjectNames = new TreeSet<String>();
        objectNames.add(objectName);
        String string = objectName.toString();
        String idMatchRegex = ".*?" + ;
        if (string.matches(idMatchRegex))
        {
            String objectNamePrefix = objectName.replaceFirst(,"");
            objectNames = .getObjectNamesByPrefix(jmxServiceURL,objectNamePrefix);
        }
        return objectNames;
    }
    private void addAttributeInfosToMap(Map<StringMBeanAttributeInfomBeanAttributeInfosSet<StringduplicatesToRemove,
            MBeanAttributeInfo[] mBeanAttributeInfoArray)
    {
        for (MBeanAttributeInfo mBeanAttributeInfo : mBeanAttributeInfoArray)
        {
            String attributeName = mBeanAttributeInfo.getName();
            if (mBeanAttributeInfos.containsKey(attributeName) && !mBeanAttributeInfo.equals(mBeanAttributeInfos.get(attributeName)))
                duplicatesToRemove.add(attributeName);
            else
                mBeanAttributeInfos.put(attributeName,mBeanAttributeInfo);
        }
    }
    private void removeDuplicates(Map<StringMBeanAttributeInfomBeanAttributeInfosSet<StringduplicatesToRemove)
    {
        for (String attributeName : duplicatesToRemove)
            mBeanAttributeInfos.remove(attributeName);
    }
    public OperationReturnValueJaxBeans invokeOperation(Collection<JMXNodejmxNodesString objectNameString operationName)
    {
        Set<OperationReturnValueJaxBeanoperationReturnValueJaxBeans = new TreeSet<OperationReturnValueJaxBean>();
        for (JMXNode jmxNode : jmxNodes)
        {
            Object returnValue = .invoke(jmxNode.getJmxServiceURL(),objectName,operationName,null,null);
            operationReturnValueJaxBeans.add(new OperationReturnValueJaxBean(jmxNode.getNodeName(),returnValue));
        }
        return new OperationReturnValueJaxBeans(operationReturnValueJaxBeans);
    }
    public OperationReturnValueJaxBeans invokeOperation(Collection<JMXNodejmxNodesString objectNameString operationNameObject[] params,
            String[] signature)
    {
        Set<OperationReturnValueJaxBeanoperationReturnValueJaxBeans = new TreeSet<OperationReturnValueJaxBean>();
        for (JMXNode jmxNode : jmxNodes)
        {
            Object returnValue = .invoke(jmxNode.getJmxServiceURL(),objectName,operationName,params,signature);
            operationReturnValueJaxBeans.add(new OperationReturnValueJaxBean(jmxNode.getNodeName(),returnValue));
        }
        return new OperationReturnValueJaxBeans(operationReturnValueJaxBeans);
    }
New to GrepCode? Check out our FAQ X