KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > MBeanView


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jmx;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.management.*;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * The view for administration.
45  */

46 public class MBeanView {
47   private static final Logger JavaDoc log = Log.open(MBeanView.class);
48   private static final L10N L = new L10N(MBeanView.class);
49
50
51   private ClassLoader JavaDoc _classLoader;
52
53   private AbstractMBeanServer _mbeanServer;
54
55   private MBeanServerDelegateImpl _delegate;
56   
57   private HashMap JavaDoc<ObjectName,MBeanWrapper> _mbeans =
58     new HashMap JavaDoc<ObjectName,MBeanWrapper>(8);
59
60   MBeanView(AbstractMBeanServer mbeanServer,
61             ClassLoader JavaDoc loader,
62             String JavaDoc agentId)
63   {
64     _mbeanServer = mbeanServer;
65
66     _classLoader = loader;
67     _delegate = new MBeanServerDelegateImpl(agentId);
68   }
69
70   /**
71    * Returns the class loader.
72    */

73   public ClassLoader JavaDoc getClassLoader()
74   {
75     return _classLoader;
76   }
77
78   /**
79    * Returns the parent local view.
80    */

81   protected MBeanView getParentView()
82   {
83     if (_classLoader == null)
84       return null;
85
86     MBeanContext context = _mbeanServer.getContext(_classLoader.getParent());
87
88     if (context.getView() == this)
89       return null;
90     else
91       return context.getView();
92
93     /*
94     if (_classLoader != null)
95       return Jmx.getLocalView(_classLoader.getParent());
96     else
97       return null;
98     */

99   }
100
101   /**
102    * Returns the parent global view.
103    */

104   protected MBeanView getParentGlobalView()
105   {
106     return null;
107     /*
108     if (_classLoader != null)
109       return Jmx.getLocalView(_classLoader.getParent());
110     else
111       return null;
112     */

113   }
114
115   /**
116    * Returns the mbean count.
117    */

118   public int getMBeanCount()
119   {
120     MBeanView parentView = getParentView();
121
122     if (parentView != null)
123       return _mbeans.size() + parentView.getMBeanCount();
124     else
125       return _mbeans.size();
126   }
127
128   /**
129    * Returns the mbean domains.
130    */

131   public String JavaDoc []getDomains()
132   {
133     ArrayList JavaDoc<String JavaDoc> domains = new ArrayList JavaDoc<String JavaDoc>();
134
135     getDomains(domains);
136     
137     return domains.toArray(new String JavaDoc[domains.size()]);
138   }
139
140   /**
141    * Returns the mbean domains.
142    */

143   protected void getDomains(ArrayList JavaDoc<String JavaDoc> domains)
144   {
145     synchronized (_mbeans) {
146       Iterator JavaDoc<ObjectName> names = _mbeans.keySet().iterator();
147       while (names.hasNext()) {
148     ObjectName name = names.next();
149
150     String JavaDoc domain = name.getDomain();
151
152     if (! domains.contains(domain))
153       domains.add(domain);
154       }
155     }
156
157     MBeanView parent = getParentView();
158
159     if (parent != null)
160       parent.getDomains(domains);
161   }
162
163   /**
164    * Finds names matching the query.
165    */

166   public Set JavaDoc<ObjectName> queryNames(ObjectName queryName, QueryExp query)
167     throws BadStringOperationException,
168        BadBinaryOpValueExpException,
169        BadAttributeValueExpException,
170        InvalidApplicationException
171   {
172     // TreeSet would be better but it causes jconsole to fail
173
HashSet JavaDoc<ObjectName> set = new HashSet JavaDoc<ObjectName>();
174
175     queryNames(set, queryName, query);
176     
177     return set;
178   }
179
180   /**
181    * Finds names matching the query.
182    */

183   protected void queryNames(Set JavaDoc<ObjectName> set,
184                 ObjectName queryName,
185                 QueryExp query)
186     throws BadStringOperationException,
187        BadBinaryOpValueExpException,
188        BadAttributeValueExpException,
189        InvalidApplicationException
190   {
191     synchronized (_mbeans) {
192       Iterator JavaDoc<ObjectName> iter = _mbeans.keySet().iterator();
193
194       while (iter.hasNext()) {
195         ObjectName name = iter.next();
196
197         if (isMatch(name, queryName, query)) {
198           set.add(name);
199         }
200       }
201     }
202
203     MBeanView parentView = getParentView();
204
205     if (parentView != null)
206       parentView.queryNames(set, queryName, query);
207   }
208
209   /**
210    * Finds names matching the query.
211    */

212   public Set JavaDoc<ObjectInstance> queryMBeans(ObjectName name, QueryExp query)
213     throws BadStringOperationException,
214        BadBinaryOpValueExpException,
215        BadAttributeValueExpException,
216        InvalidApplicationException
217   {
218     HashSet JavaDoc<ObjectInstance> set = new HashSet JavaDoc<ObjectInstance>();
219
220     queryMBeans(set, name, query);
221
222     return set;
223   }
224
225   /**
226    * Finds names matching the query.
227    */

228   protected void queryMBeans(Set JavaDoc<ObjectInstance> set,
229                  ObjectName name,
230                  QueryExp query)
231     throws BadStringOperationException,
232        BadBinaryOpValueExpException,
233        BadAttributeValueExpException,
234        InvalidApplicationException
235   {
236     synchronized (_mbeans) {
237       Iterator JavaDoc<ObjectName> iter = _mbeans.keySet().iterator();
238
239       while (iter.hasNext()) {
240         ObjectName testName = iter.next();
241
242         if (isMatch(testName, name, query)) {
243       MBeanWrapper mbean = _mbeans.get(testName);
244
245       if (mbean != null)
246         set.add(mbean.getObjectInstance());
247         }
248       }
249     }
250
251     MBeanView parentView = getParentView();
252
253     if (parentView != null)
254       parentView.queryMBeans(set, name, query);
255   }
256
257   /**
258    * Tests if the name matches.
259    *
260    * @param name the object name to match
261    * @param queryName the name of the query pattern
262    */

263   private boolean isMatch(ObjectName name,
264                           ObjectName queryName,
265                           QueryExp query)
266     throws BadStringOperationException,
267        BadBinaryOpValueExpException,
268        BadAttributeValueExpException,
269        InvalidApplicationException
270   {
271     if (queryName == null)
272       return true;
273
274     if (! queryName.isDomainPattern() &&
275         ! name.getDomain().equals(queryName.getDomain()))
276       return false;
277
278     if (queryName.isPropertyPattern()) {
279       // If the queryName has a '*' in the properties, then check
280
// the queryName properties to see if they match
281

282       Hashtable JavaDoc<String JavaDoc,String JavaDoc> map = queryName.getKeyPropertyList();
283       Iterator JavaDoc<String JavaDoc> iter = map.keySet().iterator();
284       while (iter.hasNext()) {
285         String JavaDoc key = iter.next();
286         String JavaDoc value = map.get(key);
287
288         if (! value.equals(name.getKeyProperty(key)))
289           return false;
290       }
291     }
292     else {
293       String JavaDoc testProps = name.getCanonicalKeyPropertyListString();
294       String JavaDoc queryProps = queryName.getCanonicalKeyPropertyListString();
295     
296       if (! testProps.equals(queryProps))
297         return false;
298     }
299
300     if (query != null && ! query.apply(name))
301       return false;
302
303     return true;
304   }
305
306   /**
307    * Adds an mbean instance to the view.
308    */

309   boolean add(ObjectName name, MBeanWrapper mbean)
310   {
311     return add(name, mbean, false);
312   }
313
314   /**
315    * Adds an mbean instance to the view.
316    */

317   boolean add(ObjectName name, MBeanWrapper mbean, boolean overwrite)
318   {
319     synchronized (_mbeans) {
320       if (overwrite || _mbeans.get(name) == null) {
321     _mbeans.put(name, mbean);
322
323     return true;
324       }
325       else
326     return false;
327     }
328   }
329
330   /**
331    * Removes an mbean instance from the view.
332    */

333   MBeanWrapper remove(ObjectName name)
334   {
335     synchronized (_mbeans) {
336       return _mbeans.remove(name);
337     }
338   }
339
340   /**
341    * Removes an mbean instance from the view.
342    */

343   MBeanWrapper remove(ObjectName name, MBeanWrapper mbean)
344   {
345     synchronized (_mbeans) {
346       if (mbean != null && _mbeans.get(name) != mbean)
347     return null;
348     
349       return _mbeans.remove(name);
350     }
351   }
352   
353   /**
354    * Returns the object.
355    */

356   public MBeanWrapper getMBean(ObjectName name)
357   {
358     synchronized (_mbeans) {
359       MBeanWrapper mbean = _mbeans.get(name);
360       if (mbean != null)
361     return mbean;
362
363       if (_classLoader == null)
364     return null;
365
366       MBeanView parentView = getParentView();
367
368       if (parentView != null)
369     return parentView.getMBean(name);
370       else
371     return null;
372     }
373   }
374
375   /**
376    * Closes the view.
377    */

378   void close()
379   {
380     _mbeans = null;
381   }
382
383   public String JavaDoc toString()
384   {
385     return "MBeanView[" + _classLoader + "]";
386   }
387
388 }
389
390
Popular Tags