KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > panoptes > component > jmx > model > MBeanQuery


1 /*
2  * Created on 08.mar.2003
3  *
4  * To change this generated comment go to Window>Preferences>Java>Code
5  * Generation>Code and Comments
6  */

7 package net.sf.panoptes.component.jmx.model;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.List JavaDoc;
12
13 import javax.management.ObjectName JavaDoc;
14 import javax.management.QueryExp JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import net.sf.panoptes.model.node.DefaultNodeDescriber;
20 import net.sf.panoptes.model.node.Node;
21 import net.sf.panoptes.model.node.NodeDescriber;
22 import net.sf.panoptes.model.node.NodeDescriptor;
23 import net.sf.panoptes.model.node.NodeSupport;
24
25 /**
26  * Represents a component query node which will list the query results as it's
27  * children.
28  *
29  * @see net.sf.panoptes.model.component.IComponent
30  * @see net.sf.panoptes.model.component.registry.ComponentName
31  * @see net.sf.panoptes.model.node.NodeSupport
32  * @author Dag Liodden
33  *
34  */

35 public class MBeanQuery extends NodeSupport {
36     
37     /**
38      * For an extremely quick cache hack
39      */

40     private static final long REFRESH_RATE = 1000;
41     private long cacheExpire = 0;
42     
43     private ObjectName JavaDoc objectName = null;
44     private QueryExp JavaDoc queryExp = null;
45     private Log log = LogFactory.getLog(getClass());
46     private List JavaDoc mbeanCache = null;
47
48     /**
49      * Allow multiple queries with the same query pattern in the registry
50      */

51     private static int nextId = 1;
52
53     private NodeDescriber nodeDescriber = new DefaultNodeDescriber();
54     private NodeFactory childFactory = new DefaultSymLinkFactory();
55
56     public MBeanQuery(NodeSupport parent) {
57         super(parent);
58     }
59
60     public void setQuery(ObjectName JavaDoc objectName, QueryExp JavaDoc queryExp) {
61         this.objectName = objectName;
62         this.queryExp = queryExp;
63         setConfigDescriptor(
64             new NodeDescriptor(
65                 objectName.toString(),
66                 "Group of components in query " + objectName.toString(),
67                 Node.ICON_QUERY_FOLDER));
68     }
69
70     /**
71      * @param childClassName
72      */

73     public void setChildFactoryClass(Class JavaDoc factoryClass)
74         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
75         Object JavaDoc temp = factoryClass.newInstance();
76         if (!(temp instanceof NodeFactory))
77             throw new UnsupportedOperationException JavaDoc(
78                 "Given class " + factoryClass + " is not a SymLinkFactory");
79         childFactory = (NodeFactory) temp;
80     }
81
82     public void setChildDescriber(NodeDescriber describer) {
83         this.nodeDescriber = describer;
84     }
85
86     public void init() {
87
88     }
89
90     public List JavaDoc getChildren() {
91         return getMBeans();
92     }
93
94     public List JavaDoc getMBeans() {
95         if (System.currentTimeMillis() > cacheExpire) {
96             mbeanCache = null;
97         }
98         if (mbeanCache == null) {
99             mbeanCache = new ArrayList JavaDoc();
100             synchronized (this) {
101                 try {
102                     Collection JavaDoc mbeansFromServer;
103                     if (getContext().get(MBeanServerNode.MBEANSERVERNODE_KEY) == null)
104                         mbeansFromServer =
105                             ServerRegistry.getInstance().queryMBeans(
106                                 objectName,
107                                 queryExp,
108                                 this,
109                                 childFactory);
110                     else {
111                         mbeansFromServer =
112                             (
113                                 (MBeanServerNode) getContext().get(
114                                     MBeanServerNode.MBEANSERVERNODE_KEY)).queryMBeans(
115                                 objectName,
116                                 null,
117                                 this,
118                                 childFactory);
119                     }
120                     mbeanCache.addAll(mbeansFromServer);
121
122                 } catch (Exception JavaDoc e) {
123                     log.error("Unable to execute query", e);
124                     mbeanCache = new ArrayList JavaDoc();
125                 }
126             }
127             cacheExpire = System.currentTimeMillis() + REFRESH_RATE;
128         }
129         return mbeanCache;
130     }
131
132     public int getChildCount() {
133         return getMBeans().size();
134     }
135
136     /**
137      * @return
138      */

139     public ObjectName JavaDoc getObjectName() {
140         return objectName;
141     }
142
143     public QueryExp JavaDoc getQueryExp() {
144         return queryExp;
145     }
146
147     public boolean mightHaveChildren() {
148         return true;
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see net.sf.panoptes.model.node.NodeSupport#refresh()
155      */

156     public void refresh() {
157         mbeanCache = null;
158         getMBeans();
159     }
160
161     public Object JavaDoc getBean() {
162         return this;
163     }
164
165 }
166
Popular Tags