KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > remoting > event > ClassQueryExp


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.mx.remoting.event;
8
9 import java.io.Serializable JavaDoc;
10 import javax.management.BadAttributeValueExpException JavaDoc;
11 import javax.management.BadBinaryOpValueExpException JavaDoc;
12 import javax.management.BadStringOperationException JavaDoc;
13 import javax.management.InvalidApplicationException JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.management.QueryExp JavaDoc;
17
18 /**
19  * ClassQueryExp is a QueryExp implementation that allows you
20  * to check the ObjectName on a query against one or more
21  * class names to make sure that they are the instanceof one or more
22  * classes. <P>
23  * <p/>
24  * Example code:
25  * <p/>
26  * <CODE><pre>
27  * ClassQueryExp query=new ClassQueryExp(MyMBean.class);
28  * Set beans=mbeanserver.queryMBeans(new ObjectName("*:*"),query);
29  * </pre></CODE>
30  * <p/>
31  * The query in the above example will only return MBean ObjectInstances that
32  * are an instanceof <tt>MyMBean</tt> class.
33  *
34  * @author <a HREF="jhaynie@vocalocity.net">Jeff Haynie</a>
35  * @version $Revision: 30251 $
36  */

37 public class ClassQueryExp implements QueryExp JavaDoc, Serializable JavaDoc
38 {
39    private static final long serialVersionUID = -4099952623687795850L;
40
41    public static final int AND = 1;
42    public static final int OR = 2;
43
44    String JavaDoc classes[];
45    int operator;
46    transient MBeanServer JavaDoc mBeanServer;
47
48    /**
49     * default will create using a AND operator
50     *
51     * @param cl
52     */

53    public ClassQueryExp(Class JavaDoc cl[])
54    {
55       this(cl, OR);
56    }
57
58    /**
59     * default will create using a AND operator
60     *
61     * @param cl
62     */

63    public ClassQueryExp(Class JavaDoc cl)
64    {
65       this(new Class JavaDoc[]{cl}, AND);
66    }
67
68    public ClassQueryExp(Class JavaDoc cl, int operator)
69    {
70       this(new Class JavaDoc[]{cl}, operator);
71    }
72
73    public ClassQueryExp(Class JavaDoc cl[], int operator)
74    {
75       this.classes = new String JavaDoc[cl.length];
76       for(int c = 0; c < cl.length; c++)
77       {
78          this.classes[c] = cl[c].getName();
79       }
80       this.operator = operator;
81    }
82
83    public boolean apply(ObjectName JavaDoc objectName) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc, BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc
84    {
85       try
86       {
87          for(int c = 0; c < classes.length; c++)
88          {
89             boolean value = mBeanServer.isInstanceOf(objectName, classes[c]);
90             if(value && operator == OR)
91             {
92                return true;
93             }
94             else if(!value && operator == AND)
95             {
96                return false;
97             }
98          }
99          return (operator == OR ? false : true);
100       }
101       catch(Exception JavaDoc ex)
102       {
103          return false;
104       }
105    }
106
107    /**
108     * called by MBeanServer prior to apply
109     *
110     * @param mBeanServer
111     */

112    public void setMBeanServer(MBeanServer JavaDoc mBeanServer)
113    {
114       this.mBeanServer = mBeanServer;
115    }
116 }
117
Popular Tags