private List<String> createTreeForStatsProvider(TreeNode parentNode, Object statsProvider) {
    /* construct monitoring tree at PluginPoint using subTreePath */
    List<String> childNodeNames = new ArrayList();

    /* retrieve ManagedAttribute attribute id (v2 compatible) and method names */
    /* Check for custom reset method and store for later to be called instead of
    standard reset methods on Statistic classes*/
    for (Method m : statsProvider.getClass().getMethods()) {
      ManagedAttribute ma = m.getAnnotation(ManagedAttribute.class);
      Reset resetMeth = m.getAnnotation(Reset.class);
      if (resetMeth != null) {
        StatsProviderRegistryElement spre =
            this.statsProviderRegistry.getStatsProviderRegistryElement(statsProvider);
        spre.setResetMethod(m);
      }
      if (ma != null) {
        String methodName = m.getName();
        String id = ma.id();
        if ((id == null) || id.isEmpty()) { // if id not specified, derive from method name
          String methodNameLower = methodName.toLowerCase(Locale.ENGLISH);
          if (methodNameLower.startsWith("get") && methodNameLower.length() > 3) {
            id = methodNameLower.substring(3);
          }
        }

        TreeNode attrNode = TreeNodeFactory.createMethodInvoker(id, statsProvider, id, m);
        parentNode.addChild(attrNode);
        childNodeNames.add(attrNode.getName());
      }
    }
    return childNodeNames;
  }
 private void removeParentNode(TreeNode parentNode) {
   TreeNode superParentNode = parentNode.getParent();
   if (superParentNode != null) {
     superParentNode.removeChild(parentNode);
     if (!superParentNode.hasChildNodes()) removeParentNode(superParentNode);
   }
 }
 private void enableTreeNode(TreeNode treeNode) {
   if (!treeNode.isEnabled()) {
     treeNode.setEnabled(true);
     // recursevely call the enable on parent nodes, until the whole path is enabled
     if (treeNode.getParent() != null) {
       enableTreeNode(treeNode.getParent());
     }
   }
 }
  private TreeNode constructServerPP() {
    TreeNode srvrNode = mrdr.get(instanceName);
    if (srvrNode != null) {
      return srvrNode;
    }

    srvrNode = TreeNodeFactory.createTreeNode(instanceName, null, instanceName);
    srvrNode.setEnabled(false);
    mrdr.add(instanceName, srvrNode);
    return srvrNode;
  }
 private TreeNode createSubTreeNode(TreeNode parent, String child) {
   TreeNode childNode = parent.getChild(child);
   if (childNode == null) {
     childNode = TreeNodeFactory.createTreeNode(child, null, child);
     parent.addChild(childNode);
   } else {
     // the childNode is found, but ensure that its enabled
     enableTreeNode(childNode);
   }
   return childNode;
 }
 private void updateTreeNodes(StatsProviderRegistryElement spre, boolean enable) {
   // Enable/Disable the child TreeNodes
   String parentNodePath = spre.getParentTreeNodePath();
   List<String> childNodeNames = spre.getChildTreeNodeNames();
   TreeNode rootNode = mrdr.get(instanceName);
   if (rootNode != null) {
     // This has to return one node
     List<TreeNode> nodeList = rootNode.getNodes(parentNodePath, false, true);
     TreeNode parentNode = nodeList.get(0);
     // For each child Node, enable it
     Collection<TreeNode> childNodes = parentNode.getChildNodes();
     boolean hasUpdatedNode = false;
     for (TreeNode childNode : childNodes) {
       if (childNodeNames.contains(childNode.getName())) {
         // Enabling or Disabling the child node (based on enable flag)
         if (childNode.isEnabled() != enable) {
           childNode.setEnabled(enable);
           hasUpdatedNode = true;
         }
       }
     }
     if (!hasUpdatedNode) return;
     // Make sure the tree path is affected with the changes.
     if (enable) {
       enableTreeNode(parentNode);
     } else {
       disableTreeNode(parentNode);
     }
   }
 }
  private void enableStatsProvider(StatsProviderRegistryElement spre) {
    Object statsProvider = spre.getStatsProvider();
    if (logger.isLoggable(Level.FINE))
      logger.fine("Enabling the statsProvider - " + statsProvider.getClass().getName());

    /* Step 1. Create the tree for the statsProvider */
    // Check if we already have TreeNodes created
    if (spre.getParentTreeNodePath() == null) {
      /* Verify if PluginPoint exists, create one if it doesn't */
      PluginPoint pp = spre.getPluginPoint();
      String subTreePath = spre.getSubTreePath();

      TreeNode ppNode = getPluginPointNode(pp, serverNode);
      TreeNode parentNode = createSubTree(ppNode, subTreePath);
      List<String> childNodeNames = createTreeForStatsProvider(parentNode, statsProvider);
      spre.setParentTreeNodePath(parentNode.getCompletePathName());
      spre.setChildNodeNames(childNodeNames);
    } else {
      updateTreeNodes(spre, true);
    }

    /* Step 2. reset statistics (OFF --> LOW, OFF --> HIGH)*/
    resetStatistics(spre);

    /* Step 3. register the StatsProvider to the flashlight */
    if (spre.getHandles() == null) {
      // register with flashlight and save the handles
      Collection<ProbeClientMethodHandle> handles =
          registerStatsProviderToFlashlight(statsProvider);
      spre.setHandles(handles);
    } else {
      // Enable the Flashlight handles for this statsProvider
      for (ProbeClientMethodHandle handle : spre.getHandles()) {
        if (!handle.isEnabled()) handle.enable();
      }
    }

    /* Step 4. gmbal registration */
    if (AMXReady && getMbeanEnabledValue()) {
      // Create mom root using the statsProvider
      String subTreePath = spre.getSubTreePath();
      ManagedObjectManager mom = spre.getManagedObjectManager();
      if (mom == null) {
        mom = registerGmbal(statsProvider, subTreePath);
        spre.setManagedObjectManager(mom);
      }
    }

    spre.setEnabled(true);
  }
  public void unregister(Object statsProvider) {
    // Unregisters the statsProvider
    try {
      StatsProviderRegistryElement spre =
          statsProviderRegistry.getStatsProviderRegistryElement(statsProvider);
      if (spre == null) {
        logger.log(
            Level.INFO, invalidStatsProvider, new Object[] {statsProvider.getClass().getName()});
        return;
      }

      // get the Parent node and delete all children nodes (only that we know of)
      String parentNodePath = spre.getParentTreeNodePath();
      List<String> childNodeNames = spre.getChildTreeNodeNames();
      TreeNode rootNode = mrdr.get(instanceName);
      if ((rootNode != null) && (parentNodePath != null)) {
        // This has to return one node
        List<TreeNode> nodeList = rootNode.getNodes(parentNodePath, false, true);
        if (nodeList.size() > 0) {
          TreeNode parentNode = nodeList.get(0);
          // Remove each of the child nodes
          Collection<TreeNode> childNodes = parentNode.getChildNodes();
          for (TreeNode childNode : childNodes) {
            if (childNodeNames.contains(childNode.getName())) {
              parentNode.removeChild(childNode);
            }
          }
          if (!parentNode.hasChildNodes()) removeParentNode(parentNode);
        }
      }

      // get the handles and unregister the listeners from Flashlight
      Collection<ProbeClientMethodHandle> handles = spre.getHandles();
      if (handles != null) {
        for (ProbeClientMethodHandle handle : handles) {
          // handle.remove????? Mahesh?
          // TODO IMPLEMENTATION
          // For now disable the handle => remove the client from invokerlist
          handle.disable();
        }
      }

      // unregister the statsProvider from Gmbal
      if (spre.getManagedObjectManager() != null) unregisterGmbal(spre);

      // Unregister from the MonitoringDataTreeRegistry and the map entries
      statsProviderRegistry.unregisterStatsProvider(statsProvider);
    } catch (Exception ex) {
      String msg =
          MessageFormat.format(errorUnregisteringStatsProvider, statsProvider.getClass().getName());
      logger.log(Level.SEVERE, msg, ex);
    }
  }
 private void resetChildNodeStatistics(
     String parentNodePath, List<String> childNodeNames, String statsProviderName) {
   TreeNode rootNode = mrdr.get(instanceName);
   if (rootNode != null) {
     List<TreeNode> nodeList = rootNode.getNodes(parentNodePath, false, true);
     if (nodeList.size() > 0) {
       TreeNode parentNode = nodeList.get(0);
       Collection<TreeNode> childNodes = parentNode.getChildNodes();
       for (TreeNode childNode : childNodes) {
         if (childNodeNames.contains(childNode.getName())) {
           invokeStatisticResetMethod(childNode.getValue());
         }
       }
     } else {
       logger.log(Level.WARNING, nodeNotFound, new Object[] {parentNodePath, statsProviderName});
     }
   }
 }
 private void disableTreeNode(TreeNode treeNode) {
   if (treeNode.isEnabled()) {
     boolean isAnyChildEnabled = false;
     Collection<TreeNode> childNodes = treeNode.getChildNodes();
     if (childNodes != null) {
       for (TreeNode childNode : childNodes) {
         if (childNode.isEnabled()) {
           isAnyChildEnabled = true;
           break;
         }
       }
     }
     // if none of the childs are enabled, disable the parent
     if (!isAnyChildEnabled) {
       treeNode.setEnabled(false);
       // recursevely call the disable on parent nodes
       if (treeNode.getParent() != null) {
         disableTreeNode(treeNode.getParent());
       }
     }
   }
 }