KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > frame > ServerInternalFrame


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

12
13 package org.ejtools.jmx.browser.frame;
14
15
16
17 import java.awt.BorderLayout;
18
19 import java.beans.PropertyChangeEvent;
20
21 import java.beans.PropertyEditor;
22
23 import java.util.Arrays;
24
25 import java.util.Enumeration;
26
27 import java.util.ResourceBundle;
28
29 import java.util.StringTokenizer;
30
31 import java.util.Vector;
32
33
34
35 import javax.management.ObjectName;
36
37 import javax.swing.JMenuBar;
38
39
40
41 import org.apache.log4j.Logger;
42
43 import org.ejtools.adwt.BeanContextTreePanel;
44
45 import org.ejtools.adwt.action.Command;
46
47 import org.ejtools.adwt.action.action.RefreshAction;
48
49 import org.ejtools.adwt.service.BeanContextInternalFrame;
50
51 import org.ejtools.adwt.service.MenuBarServiceProvider;
52
53 import org.ejtools.adwt.service.ToolBarServiceProvider;
54
55 import org.ejtools.beans.CustomPropertyEditorManager;
56
57 import org.ejtools.jmx.browser.action.CreateMBeanAction;
58
59 import org.ejtools.jmx.browser.action.ShowNotificationsAction;
60
61 import org.ejtools.jmx.browser.dialog.CreateMBeanDialog;
62
63 import org.ejtools.jmx.browser.model.Resource;
64
65 import org.ejtools.jmx.browser.model.Server;
66
67 import org.ejtools.jmx.browser.model.service.CacheService;
68
69 import org.ejtools.jmx.browser.model.service.CacheServiceProvider;
70
71 import org.ejtools.jmx.browser.model.service.ConnectionServiceProvider;
72
73 import org.ejtools.jmx.browser.model.service.CustomizationServiceProvider;
74
75 import org.ejtools.jmx.browser.model.service.NotificationServiceProvider;
76
77 import org.ejtools.util.service.Profile;
78
79 import org.ejtools.util.service.ProfileHolder;
80
81
82
83 /**
84
85  * Description of the Class
86
87  *
88
89  * @author <a HREF="mailto:letiemble@users.sourceforge.net">Laurent Etiemble</a>
90
91  * @version $Revision: 1.6 $
92
93  * @created 5 septembre 2002
94
95  */

96
97 public class ServerInternalFrame extends BeanContextInternalFrame implements ProfileHolder
98
99 {
100
101    /** Description of the Field */
102
103    protected CacheServiceProvider cacheProvider;
104
105    /** Description of the Field */
106
107    protected ConnectionServiceProvider connectionProvider;
108
109    /** Description of the Field */
110
111    protected CustomizationServiceProvider customizationProvider;
112
113    /** Description of the Field */
114
115    protected MenuBarServiceProvider menubarProvider;
116
117    /** Description of the Field */
118
119    protected NotificationsInternalFrame notificationFrame;
120
121    /** Description of the Field */
122
123    protected NotificationServiceProvider notificationProvider;
124
125    /** Description of the Field */
126
127    protected Profile profile = null;
128
129    /** Description of the Field */
130
131    protected Server server = null;
132
133    /** Description of the Field */
134
135    protected ToolBarServiceProvider toolbarProvider;
136
137    /** Description of the Field */
138
139    private static Logger logger = Logger.getLogger(ServerInternalFrame.class);
140
141    /** Description of the Field */
142
143    private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources");
144
145
146
147
148
149    /**Constructor for the ServerInternalFrame object */
150
151    public ServerInternalFrame() { }
152
153
154
155
156
157    /**
158
159     * Gets the profile attribute of the ServerInternalFrame object
160
161     *
162
163     * @return The profile value
164
165     */

166
167    public Profile getProfile()
168
169    {
170
171       return this.profile;
172
173    }
174
175
176
177
178
179    /**
180
181     * @param event Description of the Parameter
182
183     */

184
185    public void propertyChange(PropertyChangeEvent event)
186
187    {
188
189       if (event.getPropertyName().equals("name"))
190
191       {
192
193          String newName = (String) event.getNewValue();
194
195          this.setTitle(resources.getString("connection.text.prefix") + " : " + newName);
196
197       }
198
199    }
200
201
202
203
204
205    /**
206
207     * Description of the Method
208
209     *
210
211     * @param objectName Description of the Parameter
212
213     * @return Description of the Return Value
214
215     */

216
217    public Resource queryMBean(String objectName)
218
219    {
220
221       Resource resource = (Resource) this.cacheProvider.get(CacheService.RESOURCE_TYPE, objectName);
222
223       try
224
225       {
226
227          if ((resource != null) && (resource.isRegistered()))
228
229          {
230
231             return resource;
232
233          }
234
235       }
236
237       catch (Exception e)
238
239       {
240
241          logger.warn("Cannot find ObjectName " + objectName, e);
242
243       }
244
245       return null;
246
247    }
248
249
250
251
252
253    public void setProfile(Profile profile)
254
255    {
256
257       this.profile = profile;
258
259    }
260
261
262
263
264
265    /**
266
267     * Sets the server attribute of the ServerInternalFrame object
268
269     *
270
271     * @param server The new server value
272
273     */

274
275    public void setServer(Server server)
276
277    {
278
279       this.connectionProvider = new ConnectionServiceProvider(this.profile);
280
281       this.cacheProvider = new CacheServiceProvider();
282
283       this.customizationProvider = new CustomizationServiceProvider();
284
285       this.notificationProvider = new NotificationServiceProvider();
286
287       this.menubarProvider = new MenuBarServiceProvider();
288
289       this.toolbarProvider = new ToolBarServiceProvider();
290
291
292
293       this.server = server;
294
295       this.server.setProfile(this.profile);
296
297
298
299       this.add(this.connectionProvider);
300
301       this.add(this.cacheProvider);
302
303       this.add(this.notificationProvider);
304
305       this.add(this.customizationProvider);
306
307       this.add(this.menubarProvider);
308
309       this.add(this.toolbarProvider);
310
311
312
313       this.add(this.server);
314
315       this.notificationFrame = new NotificationsInternalFrame(this.notificationProvider);
316
317
318
319       this.server.addPropertyChangeListener("name", this);
320
321       this.server.addPropertyChangeListener("name", this.notificationFrame);
322
323
324
325       this.add(new RefreshAction(
326
327          new Command()
328
329          {
330
331             public void execute()
332
333             {
334
335                ServerInternalFrame.this.server.refresh();
336
337             }
338
339          }));
340
341
342
343       this.add(new CreateMBeanAction(
344
345          new Command()
346
347          {
348
349             public void execute()
350
351             {
352
353                ServerInternalFrame.this.createMBean();
354
355             }
356
357          }));
358
359
360
361       this.add(new ShowNotificationsAction(
362
363          new Command()
364
365          {
366
367             public void execute()
368
369             {
370
371                ServerInternalFrame.this.showNotificationFrame();
372
373             }
374
375          }));
376
377
378
379       BeanContextTreePanel panel = new BeanContextTreePanel(this.server);
380
381       panel.setIndexer(new ResourceIndexer());
382
383       panel.selectRoot();
384
385
386
387       this.frame.setJMenuBar((JMenuBar) this.menubarProvider.getContainer());
388
389       this.frame.getContentPane().add(this.toolbarProvider.getContainer(), BorderLayout.NORTH);
390
391       this.frame.getContentPane().add(panel, BorderLayout.CENTER);
392
393    }
394
395
396
397
398
399    /** Description of the Method */
400
401    protected void createMBean()
402
403    {
404
405       Vector domains = new Vector();
406
407       for (Enumeration enum = cacheProvider.keys(CacheService.DOMAIN_TYPE); enum.hasMoreElements(); )
408
409       {
410
411          domains.add(enum.nextElement() + ":");
412
413       }
414
415       Object[] domainValues = domains.toArray();
416
417       Arrays.sort(domainValues);
418
419
420
421       Vector classes = new Vector();
422
423       for (Enumeration enum = cacheProvider.keys(CacheService.RESOURCE_TYPE); enum.hasMoreElements(); )
424
425       {
426
427          Resource resource = (Resource) cacheProvider.get(CacheService.RESOURCE_TYPE, enum.nextElement());
428
429          if (!classes.contains(resource.getClassName()))
430
431          {
432
433             classes.add(resource.getClassName());
434
435          }
436
437       }
438
439       Object[] classValues = classes.toArray();
440
441       Arrays.sort(classValues);
442
443
444
445       CreateMBeanDialog dialog = new CreateMBeanDialog(null, domainValues, classValues);
446
447       dialog.show();
448
449
450
451       Object object = dialog.getObjectName();
452
453       if (object == null)
454
455       {
456
457          return;
458
459       }
460
461       try
462
463       {
464
465          String className = null;
466
467          ObjectName name = null;
468
469          ObjectName loaderName = null;
470
471          String sParameters = null;
472
473          String sSignature = null;
474
475
476
477          Object[] parameters = null;
478
479          String[] signature = null;
480
481
482
483          name = new ObjectName(object.toString());
484
485          className = dialog.getObjectClass();
486
487
488
489          if ("".equals(dialog.getClassLoader()))
490
491          {
492
493             loaderName = null;
494
495          }
496
497          else
498
499          {
500
501             loaderName = new ObjectName(dialog.getClassLoader());
502
503          }
504
505
506
507          sParameters = dialog.getParameters();
508
509          sSignature = dialog.getSignature();
510
511          if ((!"".equals(sParameters)) && (!"".equals(sSignature)))
512
513          {
514
515             StringTokenizer stParameters = new StringTokenizer(sParameters, ",");
516
517             StringTokenizer stSignature = new StringTokenizer(sSignature, ",");
518
519             if (stParameters.countTokens() == stSignature.countTokens())
520
521             {
522
523                try
524
525                {
526
527                   parameters = new String[stParameters.countTokens()];
528
529                   signature = new String[stSignature.countTokens()];
530
531
532
533                   for (int i = 0; stParameters.hasMoreTokens(); i++)
534
535                   {
536
537                      signature[i] = stSignature.nextToken();
538
539                      Class cl = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
540
541                      PropertyEditor pe = CustomPropertyEditorManager.findEditor(cl);
542
543                      pe.setAsText(stParameters.nextToken());
544
545                      parameters[i] = pe.getValue();
546
547                   }
548
549                }
550
551                catch (Exception e)
552
553                {
554
555                   logger.error("Exception while parsing parameters", e);
556
557                }
558
559             }
560
561          }
562
563
564
565          this.server.createMBean(className, name, loaderName, parameters, signature);
566
567       }
568
569       catch (Exception e)
570
571       {
572
573          logger.error("Exception while creating MBean", e);
574
575       }
576
577    }
578
579
580
581
582
583    /** Description of the Method */
584
585    protected void initializeBeanContextResources()
586
587    {
588
589       super.initializeBeanContextResources();
590
591
592
593       // Hack to update the window title
594

595         this.server.setName("");
596
597         this.server.setName(resources.getString("connection.text.untitled"));
598
599    }
600
601
602
603
604
605    /** Description of the Method */
606
607    protected void releaseBeanContextResources()
608
609    {
610
611       this.notificationFrame.close();
612
613       this.server.clear();
614
615       super.releaseBeanContextResources();
616
617    }
618
619
620
621
622
623    /** Description of the Method */
624
625    protected void showNotificationFrame()
626
627    {
628
629       NotificationsInternalFrame frame = ServerInternalFrame.this.notificationFrame;
630
631       if (!this.getBeanContext().contains(frame))
632
633       {
634
635          this.getBeanContext().add(frame);
636
637       }
638
639       frame.activate();
640
641    }
642
643 }
644
645
Popular Tags