2525import java .nio .charset .StandardCharsets ;
2626import java .util .Iterator ;
2727import java .util .Set ;
28+ import java .util .regex .Pattern ;
29+
2830import javax .management .AttributeNotFoundException ;
2931import javax .management .InstanceNotFoundException ;
3032import javax .management .IntrospectionException ;
4042import javax .management .openmbean .CompositeData ;
4143import javax .management .openmbean .CompositeType ;
4244import javax .management .openmbean .TabularData ;
43- import org .apache .yetus .audience .InterfaceAudience ;
44- import org .slf4j .Logger ;
45- import org .slf4j .LoggerFactory ;
4645
4746import org .apache .hbase .thirdparty .com .google .gson .Gson ;
4847import org .apache .hbase .thirdparty .com .google .gson .stream .JsonWriter ;
48+ import org .apache .yetus .audience .InterfaceAudience ;
49+ import org .slf4j .Logger ;
50+ import org .slf4j .LoggerFactory ;
4951
5052/**
5153 * Utility for doing JSON and MBeans.
5254 */
5355@ InterfaceAudience .Private
5456public class JSONBean {
57+ private static final String COMMA = "," ;
58+ private static final String ASTERICK = "*" ;
5559 private static final Logger LOG = LoggerFactory .getLogger (JSONBean .class );
5660 private static final Gson GSON = GsonUtil .createGson ().create ();
5761
@@ -125,11 +129,12 @@ public int write(MBeanServer mBeanServer, ObjectName qry, String attribute,
125129 */
126130 private static int write (JsonWriter writer , MBeanServer mBeanServer , ObjectName qry ,
127131 String attribute , boolean description ) throws IOException {
128- LOG .trace ("Listing beans for " + qry );
132+ LOG .debug ("Listing beans for {}" , qry );
129133 Set <ObjectName > names = null ;
130134 names = mBeanServer .queryNames (qry , null );
131135 writer .name ("beans" ).beginArray ();
132136 Iterator <ObjectName > it = names .iterator ();
137+ Pattern [] matchingPattern = null ;
133138 while (it .hasNext ()) {
134139 ObjectName oname = it .next ();
135140 MBeanInfo minfo ;
@@ -149,8 +154,24 @@ private static int write(JsonWriter writer, MBeanServer mBeanServer, ObjectName
149154 code = (String ) mBeanServer .getAttribute (oname , prs );
150155 }
151156 if (attribute != null ) {
152- prs = attribute ;
153- attributeinfo = mBeanServer .getAttribute (oname , prs );
157+ String [] patternAttr = null ;
158+ if (attribute .contains (ASTERICK )) {
159+ if (attribute .contains (COMMA )) {
160+ patternAttr = attribute .split (COMMA );
161+ } else {
162+ patternAttr = new String [1 ];
163+ patternAttr [0 ] = attribute ;
164+ }
165+ matchingPattern = new Pattern [patternAttr .length ];
166+ for (int i = 0 ; i < patternAttr .length ; i ++) {
167+ matchingPattern [i ] = Pattern .compile (patternAttr [i ]);
168+ }
169+ // nullify the attribute
170+ attribute = null ;
171+ } else {
172+ prs = attribute ;
173+ attributeinfo = mBeanServer .getAttribute (oname , prs );
174+ }
154175 }
155176 } catch (RuntimeMBeanException e ) {
156177 // UnsupportedOperationExceptions happen in the normal course of business,
@@ -216,7 +237,7 @@ private static int write(JsonWriter writer, MBeanServer mBeanServer, ObjectName
216237 } else {
217238 MBeanAttributeInfo [] attrs = minfo .getAttributes ();
218239 for (int i = 0 ; i < attrs .length ; i ++) {
219- writeAttribute (writer , mBeanServer , oname , description , attrs [i ]);
240+ writeAttribute (writer , mBeanServer , oname , description , matchingPattern , attrs [i ]);
220241 }
221242 }
222243 writer .endObject ();
@@ -226,7 +247,7 @@ private static int write(JsonWriter writer, MBeanServer mBeanServer, ObjectName
226247 }
227248
228249 private static void writeAttribute (JsonWriter writer , MBeanServer mBeanServer , ObjectName oname ,
229- boolean description , MBeanAttributeInfo attr ) throws IOException {
250+ boolean description , Pattern pattern [], MBeanAttributeInfo attr ) throws IOException {
230251 if (!attr .isReadable ()) {
231252 return ;
232253 }
@@ -237,6 +258,21 @@ private static void writeAttribute(JsonWriter writer, MBeanServer mBeanServer, O
237258 if (attName .indexOf ("=" ) >= 0 || attName .indexOf (":" ) >= 0 || attName .indexOf (" " ) >= 0 ) {
238259 return ;
239260 }
261+
262+ if (pattern != null ) {
263+ boolean matchFound = false ;
264+ for (Pattern compile : pattern ) {
265+ // check if we have any match
266+ if (compile .matcher (attName ).find ()) {
267+ matchFound = true ;
268+ break ;
269+ }
270+ }
271+ if (!matchFound ) {
272+ return ;
273+ }
274+ }
275+
240276 String descriptionStr = description ? attr .getDescription () : null ;
241277 Object value = null ;
242278 try {
0 commit comments