Advertisement
dig090

RuntimeMetadataNamingStrategy

Jul 16th, 2012
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package com.j256.jmx;
  2.  
  3. import javax.management.MalformedObjectNameException;
  4. import javax.management.ObjectName;
  5.  
  6. import org.springframework.jmx.export.naming.MetadataNamingStrategy;
  7.  
  8. /**
  9.  * Local version of the {@link MetadataNamingStrategy} which uses the {@link RuntimeJmxNames} interface to have the
  10.  * exported object set its own name.
  11.  *
  12.  * @author graywatson
  13.  */
  14. public class RuntimeMetadataNamingStrategy extends MetadataNamingStrategy {
  15.  
  16.     /**
  17.      * Overrides Spring's naming method and replaced it with our local one.
  18.      */
  19.     @Override
  20.     public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
  21.  
  22.         // get the object name from the annotation
  23.         ObjectName objectName = super.getObjectName(managedBean, beanKey);
  24.  
  25.         // now run through and see if the object implements the naming interface
  26.         if (managedBean instanceof RuntimeJmxNames) {
  27.             objectName = buildObjectName((RuntimeJmxNames) managedBean, objectName.getDomain());
  28.         }
  29.  
  30.         return objectName;
  31.     }
  32.  
  33.     /**
  34.      * Construct our object name by calling the methods in {@link RuntimeJmxNames}.
  35.      */
  36.     private ObjectName buildObjectName(RuntimeJmxNames namedObject, String domainName)
  37.             throws MalformedObjectNameException {
  38.  
  39.         String[] typeNames = namedObject.getJmxPath();
  40.         if (typeNames == null) {
  41.             throw new MalformedObjectNameException("getJmxPath() is returning null for object " + namedObject);
  42.         }
  43.  
  44.         StringBuilder nameBuilder = new StringBuilder();
  45.         nameBuilder.append(domainName);
  46.         nameBuilder.append(':');
  47.  
  48.         /*
  49.          * Ok. This is a HACK. It seems like something in the JMX mbean naming process actually sorts the names
  50.          * lexicographically. The stuff before the '=' character seems to be ignored anyway but it does look ugly.
  51.          */
  52.         boolean needComma = false;
  53.         int typeNameC = 0;
  54.         for (String typeName : typeNames) {
  55.             if (needComma) {
  56.                 nameBuilder.append(',');
  57.             }
  58.             // this will come out as 00=partition
  59.             nameBuilder.append(String.format("%02d", typeNameC));
  60.             typeNameC++;
  61.             nameBuilder.append('=');
  62.             nameBuilder.append(typeName);
  63.             needComma = true;
  64.         }
  65.         if (needComma) {
  66.             nameBuilder.append(',');
  67.         }
  68.         nameBuilder.append("name=");
  69.         nameBuilder.append(namedObject.getJmxName());
  70.         return ObjectName.getInstance(nameBuilder.toString());
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement