KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > model > Server


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.jmx.browser.model;
8
9 import java.beans.beancontext.BeanContextServices;
10 import java.util.Collection;
11 import java.util.Enumeration;
12 import java.util.Hashtable;
13 import java.util.Iterator;
14 import java.util.ResourceBundle;
15 import java.util.Set;
16
17 import javax.management.ObjectInstance;
18 import javax.management.ObjectName;
19 import javax.management.QueryExp;
20
21 import org.apache.log4j.Logger;
22 import org.ejtools.jmx.browser.model.service.CacheService;
23 import org.ejtools.jmx.browser.model.service.ConnectionMetaData;
24 import org.ejtools.jmx.browser.model.service.ConnectionService;
25 import org.ejtools.util.service.Profile;
26 import org.ejtools.util.service.ProfileHolder;
27
28 /**
29  * Description of the Class
30  *
31  * @author letiemble
32  * @version $Revision: 1.6 $
33  * @todo Javadoc to complete
34  * @todo Add log4j logs
35  * @javabean:class displayName="JMX Server"
36  * shortDescription="JMX Server"
37  * @javabean:icons color16="/toolbarButtonGraphics/development/Server16.gif"
38  * @javabean:property name="name"
39  * class="java.lang.String"
40  * displayName="Connection"
41  * shortDescription="Connection Name"
42  * @javabean:property name="defaultDomain"
43  * class="java.lang.String"
44  * displayName="Default Domain"
45  * shortDescription="Default Domain"
46  * @javabean:property name="count"
47  * class="int"
48  * displayName="MBean number"
49  * shortDescription="Number of MBean registered"
50  * @javabean:property name="factory"
51  * class="java.lang.String"
52  * displayName="Factory"
53  * shortDescription="JNDI context factory"
54  * @javabean:property name="packages"
55  * class="java.lang.String"
56  * displayName="Packages"
57  * shortDescription="Packages for context"
58  * @javabean:property name="uRL"
59  * class="java.lang.String"
60  * displayName="URL"
61  * shortDescription="JNDI Server URL"
62  * @javabean:property name="binding"
63  * class="java.lang.String"
64  * displayName="JNDI Binding"
65  * shortDescription="JNDI Binding to find the connector"
66  * @javabean:property name="principal"
67  * class="java.lang.String"
68  * displayName="Principal"
69  * shortDescription="Principal to connect"
70  * @javabean:property name="credentials"
71  * class="java.lang.String"
72  * displayName="Credentials"
73  * shortDescription="Credentials to connect"
74  * @javabean:property name="service"
75  * class="java.lang.String"
76  * displayName="Service URL"
77  * shortDescription="JMX Remoting Service URL beginning with 'service:jmx:'"
78  */

79 public class Server extends Node implements ProfileHolder
80 {
81    /** Description of the Field */
82    protected int count = 0;
83    /** Description of the Field */
84    protected String defaultDomain = "";
85    /** Description of the Field */
86    protected Profile profile = null;
87    /** Description of the Field */
88    private static Logger logger = Logger.getLogger(Server.class);
89    /** Description of the Field */
90    private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources");
91
92
93    /** Constructor for the Server object */
94    public Server()
95    {
96       super();
97       this.setName(resources.getString("connection.text.untitled"));
98    }
99
100
101    /**
102     * Description of the Method
103     *
104     * @javabean:method name="connect"
105     * displayName="Connect"
106     * shortDescription="Connect to the JMX Server"
107     */

108    public void connect()
109    {
110       logger.debug("In connect");
111       this.refresh();
112    }
113
114
115    /**
116     * Description of the Method
117     *
118     * @param className Description of the Parameter
119     * @param name Description of the Parameter
120     * @param loaderName Description of the Parameter
121     * @param params Description of the Parameter
122     * @param signature Description of the Parameter
123     * @return Description of the Return Value
124     * @exception Exception Description of the Exception
125     */

126    public ObjectInstance createMBean(java.lang.String className, ObjectName name, ObjectName loaderName, java.lang.Object[] params, java.lang.String[] signature)
127       throws Exception
128    {
129       ObjectInstance result = null;
130
131       BeanContextServices context = (BeanContextServices) getBeanContext();
132
133       if (context.hasService(ConnectionService.class))
134       {
135          try
136          {
137             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
138
139             if (loaderName == null)
140             {
141                if ((params == null) || (signature == null))
142                {
143                   logger.debug("Creating MBean with <" + className + "," + name + ">");
144                   result = service.getMBeanServer().createMBean(className, name);
145                }
146                else
147                {
148                   logger.debug("Creating MBean with <" + className + "," + name + "," + params + "," + signature + ">");
149                   result = service.getMBeanServer().createMBean(className, name, params, signature);
150                }
151             }
152             else
153             {
154                if ((params == null) || (signature == null))
155                {
156                   logger.debug("Creating MBean with <" + className + "," + name + "," + loaderName + ">");
157                   result = service.getMBeanServer().createMBean(className, name, loaderName);
158                }
159                else
160                {
161                   logger.debug("Creating MBean with <" + className + "," + name + "," + loaderName + "," + params + "," + signature + ">");
162                   result = service.getMBeanServer().createMBean(className, name, loaderName, params, signature);
163                }
164             }
165
166             context.releaseService(this, this, ConnectionService.class);
167          }
168          catch (Exception e)
169          {
170             logger.error("Error during utilisation of service ConnectionService", e);
171             throw e;
172          }
173       }
174
175       return result;
176    }
177
178
179    /**
180     * Description of the Method
181     *
182     * @todo Use queryMBeans instance method instead direct call to service
183     * @javabean:method name="disconnect"
184     * displayName="Disconnect"
185     * shortDescription="Disconnect from the JMX Server"
186     */

187    public void disconnect()
188    {
189       logger.debug("In disconnect");
190       BeanContextServices context = (BeanContextServices) getBeanContext();
191
192       if (context.hasService(ConnectionService.class) && context.hasService(CacheService.class))
193       {
194          try
195          {
196             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
197             CacheService cache = (CacheService) context.getService(this, this, CacheService.class, this, this);
198
199             // Remove old mbeans from server
200
Enumeration enum = cache.keys(CacheService.RESOURCE_TYPE);
201             while (enum.hasMoreElements())
202             {
203                String name = (String) enum.nextElement();
204
205                Resource resource = (Resource) cache.get(CacheService.RESOURCE_TYPE, name);
206                resource.getBeanContext().remove(resource);
207             }
208
209             // Remove domains from server
210
enum = cache.keys(CacheService.DOMAIN_TYPE);
211             while (enum.hasMoreElements())
212             {
213                String name = (String) enum.nextElement();
214                Domain domain = (Domain) cache.get(CacheService.DOMAIN_TYPE, name);
215
216                if (domain.isEmpty())
217                {
218                   domain.getBeanContext().remove(domain);
219                }
220             }
221
222             // When everything is done, disconnect
223
if (service.isConnected())
224             {
225                service.disconnect();
226             }
227
228             this.setCount(0);
229             this.setDefaultDomain("");
230
231             context.releaseService(this, this, CacheService.class);
232             context.releaseService(this, this, ConnectionService.class);
233          }
234          catch (Exception e)
235          {
236             logger.error("Error during utilisation of service ConnectionService or CacheService", e);
237          }
238       }
239    }
240
241
242    /**
243     * Returns the binding.
244     *
245     * @return String
246     */

247    public String getBinding()
248    {
249       return this.profile.getProperty(ConnectionMetaData.CONTEXT);
250    }
251
252
253    /**
254     * Getter for the count attribute
255     *
256     * @return The value
257     */

258    public int getCount()
259    {
260       return this.count;
261    }
262
263
264    /**
265     * Returns the credentials.
266     *
267     * @return String
268     */

269    public String getCredentials()
270    {
271       return this.profile.getProperty(ConnectionMetaData.CREDENTIALS);
272    }
273
274
275    /**
276     * Getter for the defaultDomain attribute
277     *
278     * @return The value
279     */

280    public String getDefaultDomain()
281    {
282       return this.defaultDomain;
283    }
284
285
286    /**
287     * Gets the factory attribute of the Server object
288     *
289     * @return The factory value
290     */

291    public String getFactory()
292    {
293       return this.profile.getProperty(ConnectionMetaData.FACTORY);
294    }
295
296    public String getService()
297    {
298       return this.profile.getProperty(ConnectionMetaData.SERVICE);
299    }
300
301    /**
302     * Gets the packages attribute of the Server object
303     *
304     * @return The packages value
305     */

306    public String getPackages()
307    {
308       return this.profile.getProperty(ConnectionMetaData.PACKAGES);
309    }
310
311
312    /**
313     * Returns the principal.
314     *
315     * @return String
316     */

317    public String getPrincipal()
318    {
319       return this.profile.getProperty(ConnectionMetaData.PRINCIPAL);
320    }
321
322
323    /**
324     * Gets the url attribute of the Server object
325     *
326     * @return The url value
327     */

328    public String getURL()
329    {
330       return this.profile.getProperty(ConnectionMetaData.URL);
331    }
332
333
334    /**
335     * Gets the connected attribute of the Server object
336     *
337     * @return The connected value
338     */

339    public boolean isConnected()
340    {
341       BeanContextServices context = (BeanContextServices) getBeanContext();
342       boolean result = false;
343       if (context.hasService(ConnectionService.class))
344       {
345          try
346          {
347             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
348             result = service.isConnected();
349             context.releaseService(this, this, ConnectionService.class);
350          }
351          catch (Exception e)
352          {
353             logger.error("Error during utilisation of service ConnectionService", e);
354          }
355       }
356       return result;
357    }
358
359
360    /**
361     * Description of the Method
362     *
363     * @param name Description of the Parameter
364     * @param exp Description of the Parameter
365     * @return Description of the Return Value
366     * @exception Exception Description of the Exception
367     */

368    public Set query(ObjectName name, QueryExp exp)
369       throws Exception
370    {
371       Set result = null;
372
373       BeanContextServices context = (BeanContextServices) getBeanContext();
374
375       if (context.hasService(ConnectionService.class))
376       {
377          try
378          {
379             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
380
381             result = service.getMBeanServer().queryNames(name, exp);
382
383             context.releaseService(this, this, ConnectionService.class);
384          }
385          catch (Exception e)
386          {
387             logger.error("Error during utilisation of service ConnectionService", e);
388             throw e;
389          }
390       }
391
392       return result;
393    }
394
395
396    /**
397     * Description of the Method
398     *
399     * @todo Use queryMBeans instance method instead direct call to service
400     * @javabean:method name="refresh"
401     * displayName="Refresh"
402     * shortDescription="Refresh the content of JMX Server"
403     */

404    public void refresh()
405    {
406       logger.debug("In refresh");
407       BeanContextServices context = (BeanContextServices) getBeanContext();
408
409       if (context.hasService(ConnectionService.class) && context.hasService(CacheService.class))
410       {
411          try
412          {
413             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
414             CacheService cache = (CacheService) context.getService(this, this, CacheService.class, this, this);
415
416             if (!service.isConnected())
417             {
418                service.connect(this.profile);
419             }
420
421             this.setDefaultDomain(service.getMBeanServer().getDefaultDomain());
422
423             Hashtable newContent = new Hashtable();
424             Collection result = service.getMBeanServer().queryMBeans(null, null);
425
426             int counter = result.size();
427             this.setCount(counter);
428
429             // Build the new content
430
Iterator it = result.iterator();
431             while (it.hasNext())
432             {
433                ObjectInstance oi = (ObjectInstance) it.next();
434                newContent.put(oi.getObjectName().getCanonicalName(), oi);
435 // logger.debug("MBean found = " + oi.getObjectName().getCanonicalName());
436
}
437
438             // Remove old mbeans from server
439
Enumeration enum = cache.keys(CacheService.RESOURCE_TYPE);
440             while (enum.hasMoreElements())
441             {
442                String name = (String) enum.nextElement();
443                Object o = newContent.get(name);
444
445                if (o == null)
446                {
447                   Resource resource = (Resource) cache.get(CacheService.RESOURCE_TYPE, name);
448                   resource.getBeanContext().remove(resource);
449 // logger.debug("MBean to remove = " + resource);
450
}
451             }
452
453             // Remove empty domains from server
454
enum = cache.keys(CacheService.DOMAIN_TYPE);
455             while (enum.hasMoreElements())
456             {
457                String name = (String) enum.nextElement();
458                Domain domain = (Domain) cache.get(CacheService.DOMAIN_TYPE, name);
459
460                if (domain.isEmpty())
461                {
462                   domain.getBeanContext().remove(domain);
463 // logger.debug("Domain to remove = " + domain);
464
}
465             }
466
467             // Add new MBeans to the server
468
it = result.iterator();
469             while (it.hasNext())
470             {
471                ObjectInstance oi = (ObjectInstance) it.next();
472
473                String domainName = oi.getObjectName().getDomain();
474                Domain d = (Domain) cache.get(CacheService.DOMAIN_TYPE, domainName);
475
476                if (d == null)
477                {
478                   d = new Domain();
479                   d.setName(domainName);
480                   add(d);
481 // logger.debug("Domain to add = " + d);
482
}
483
484                String name = oi.getObjectName().getCanonicalName();
485                Resource resource = (Resource) cache.get(CacheService.RESOURCE_TYPE, name);
486
487                if (resource == null)
488                {
489                   resource = new Resource();
490                   resource.setReference(oi);
491                   d.add(resource);
492 // logger.debug("ManagedObject to add = " + resource);
493
}
494             }
495
496             context.releaseService(this, this, CacheService.class);
497             context.releaseService(this, this, ConnectionService.class);
498          }
499          catch (Exception e)
500          {
501             logger.error("Error during utilisation of service ConnectionService or CacheService", e);
502          }
503       }
504    }
505
506
507
508    /**
509     * Sets the binding.
510     *
511     * @param binding The binding to set
512     */

513    public void setBinding(String binding)
514    {
515       this.profile.setProperty(ConnectionMetaData.CONTEXT, binding);
516    }
517
518
519    /**
520     * Sets the credentials.
521     *
522     * @param credentials The credentials to set
523     */

524    public void setCredentials(String credentials)
525    {
526       this.profile.setProperty(ConnectionMetaData.CREDENTIALS, credentials);
527    }
528
529
530    /**
531     * Sets the factory attribute of the Server object
532     *
533     * @param factory The new factory value
534     */

535    public void setFactory(String factory)
536    {
537       this.profile.setProperty(ConnectionMetaData.FACTORY, factory);
538    }
539
540    public void setService(String service)
541    {
542       this.profile.setProperty(ConnectionMetaData.SERVICE, service);
543    }
544
545
546    /**
547     * Sets the name attribute of the Server object
548     *
549     * @param name The new name value
550     */

551    public void setName(String name)
552    {
553       super.setName(name);
554    }
555
556
557    /**
558     * Sets the packages attribute of the Server object
559     *
560     * @param pkgs The new packages value
561     */

562    public void setPackages(String pkgs)
563    {
564       this.profile.setProperty(ConnectionMetaData.PACKAGES, pkgs);
565    }
566
567
568    /**
569     * Sets the principal.
570     *
571     * @param principal The principal to set
572     */

573    public void setPrincipal(String principal)
574    {
575       this.profile.setProperty(ConnectionMetaData.PRINCIPAL, principal);
576    }
577
578
579    /**
580     * Sets the profile.
581     *
582     * @param profile The profile to set
583     */

584    public void setProfile(Profile profile)
585    {
586       this.profile = profile;
587    }
588
589
590    /**
591     * Sets the url attribute of the Server object
592     *
593     * @param url The new uRL value
594     */

595    public void setURL(String url)
596    {
597       this.profile.setProperty(ConnectionMetaData.URL, url);
598    }
599
600
601    /**
602     * Setter for the mBeanCount attribute
603     *
604     * @param count The new count value
605     */

606    protected void setCount(int count)
607    {
608       int oldCount = this.count;
609       this.count = count;
610       this.firePropertyChange("count", new Integer(oldCount), new Integer(count));
611    }
612
613
614    /**
615     * Setter for the defaultDomain attribute
616     *
617     * @param defaultDomain The new defaultDomain value
618     */

619    protected void setDefaultDomain(String defaultDomain)
620    {
621       String oldDomain = this.defaultDomain;
622       this.defaultDomain = defaultDomain;
623       this.firePropertyChange("defaultDomain", oldDomain, defaultDomain);
624    }
625 }
626
Popular Tags