KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > mbean > CustomViewDeployer


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.mbean;
14
15
16
17 import java.io.File JavaDoc;
18
19 import java.io.InputStream JavaDoc;
20
21 import java.util.Calendar JavaDoc;
22
23 import java.util.Iterator JavaDoc;
24
25 import java.util.TreeMap JavaDoc;
26
27
28
29 import javax.management.MBeanNotificationInfo JavaDoc;
30
31 import javax.management.Notification JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36
37 import org.w3c.dom.Element JavaDoc;
38
39 import org.w3c.dom.NodeList JavaDoc;
40
41
42
43 import org.jboss.deployment.DeploymentException;
44
45 import org.jboss.deployment.DeploymentInfo;
46
47 import org.jboss.deployment.SubDeployerSupport;
48
49 import org.xml.sax.InputSource JavaDoc;
50
51
52
53 /**
54
55  * Description of the Class
56
57  *
58
59  * @author letiemble
60
61  * @created 13 d?cembre 2001
62
63  * @version $Revision: 1.5 $
64
65  * @todo Javadoc to complete
66
67  * @jmx.mbean name="user:service=CustomViewDeployer"
68
69  * extends="org.jboss.deployment.SubDeployerMBean"
70
71  */

72
73 public class CustomViewDeployer extends SubDeployerSupport implements CustomViewDeployerMBean
74
75 {
76
77    /** Description of the Field */
78
79    protected TreeMap JavaDoc views = new TreeMap JavaDoc();
80
81    /** Description of the Field */
82
83    private MBeanNotificationInfo JavaDoc[] info = null;
84
85    /** Description of the Field */
86
87    private long sequence = 0;
88
89    /** Description of the Field */
90
91    public final static String JavaDoc EXTENSION = ".jmxml";
92
93    /** Description of the Field */
94
95    public final static String JavaDoc OBJECT_NAME = "user:service=CustomViewDeployer";
96
97
98
99
100
101    /**
102
103     * Description of the Method
104
105     *
106
107     * @jmx:managed-constructor description="default constructor"
108
109     */

110
111    public CustomViewDeployer() { }
112
113
114
115
116
117    /**
118
119     * Description of the Method
120
121     *
122
123     * @param di Description of the Parameter
124
125     * @return Description of the Return Value
126
127     * @jmx.managed-operation
128
129     */

130
131    public boolean accepts(DeploymentInfo di)
132
133    {
134
135       String JavaDoc urlStr = di.url.toString();
136
137       return urlStr.endsWith(EXTENSION);
138
139    }
140
141
142
143
144
145    /**
146
147     * Description of the Method
148
149     *
150
151     * @param di Description of the Parameter
152
153     * @exception DeploymentException Description of the Exception
154
155     * @jmx.managed-operation
156
157     */

158
159    public void create(DeploymentInfo di)
160
161       throws DeploymentException
162
163    {
164
165       try
166
167       {
168
169          NodeList JavaDoc nl = null;
170
171          log.debug("Deploying Custom View, create step: url " + di.url);
172
173
174
175          String JavaDoc url = di.url.toString();
176
177          Element JavaDoc top = (Element JavaDoc) (di.document.getElementsByTagName("customview")).item(0);
178
179
180
181          View custview = new View();
182
183          custview.setName(url);
184
185          custview.setDisplayName(top.getAttribute("displayName"));
186
187          log.debug("Create view " + custview.getName());
188
189
190
191          Element JavaDoc attributes = (Element JavaDoc) (di.document.getElementsByTagName("attributes")).item(0);
192
193          Element JavaDoc operations = (Element JavaDoc) (di.document.getElementsByTagName("operations")).item(0);
194
195
196
197          nl = attributes.getElementsByTagName("query");
198
199          log.debug("About to create " + nl.getLength() + " attribute queries");
200
201
202
203          for (int i = 0; i < nl.getLength(); i++)
204
205          {
206
207             Element JavaDoc node = (Element JavaDoc) nl.item(i);
208
209
210
211             String JavaDoc exp = node.getAttribute("exp");
212
213             String JavaDoc name = node.getAttribute("name");
214
215
216
217             custview.addAttributeLine(exp, name);
218
219             log.debug("Query expression " + exp + " which has name " + name);
220
221          }
222
223
224
225          nl = operations.getElementsByTagName("query");
226
227          log.debug("About to create " + nl.getLength() + " operation queries");
228
229
230
231          for (int i = 0; i < nl.getLength(); i++)
232
233          {
234
235             Element JavaDoc node = (Element JavaDoc) nl.item(i);
236
237
238
239             String JavaDoc exp = node.getAttribute("exp");
240
241             String JavaDoc name = node.getAttribute("name");
242
243
244
245             custview.addOperationLine(exp, name);
246
247             log.debug("Query expression " + exp + " which has operation " + name);
248
249          }
250
251
252
253          views.put(url, custview);
254
255          log.debug("View added");
256
257
258
259          Notification JavaDoc notification = new Notification JavaDoc("VIEW_ADDED", this, sequence++, Calendar.getInstance().getTime().getTime(), "Custom view " + url);
260
261          this.sendNotification(notification);
262
263       }
264
265       catch (Exception JavaDoc e)
266
267       {
268
269          destroy(di);
270
271          throw new DeploymentException("create operation failed for package " + di.url, e);
272
273       }
274
275    }
276
277
278
279
280
281    /**
282
283     * Description of the Method
284
285     *
286
287     * @param di Description of the Parameter
288
289     * @jmx.managed-operation
290
291     */

292
293    public void destroy(DeploymentInfo di)
294
295    {
296
297       log.debug("Deploying Custom View, destroy step: url " + di.url);
298
299
300
301       try
302
303       {
304
305          String JavaDoc url = di.url.toString();
306
307          log.debug("Destroy view " + url);
308
309
310
311          views.remove(url);
312
313          log.debug("View removed");
314
315
316
317          Notification JavaDoc notification = new Notification JavaDoc("VIEW_REMOVED", this, sequence++, Calendar.getInstance().getTime().getTime(), "Custom view " + url);
318
319          this.sendNotification(notification);
320
321       }
322
323       catch (Exception JavaDoc e)
324
325       {
326
327       }
328
329    }
330
331
332
333
334
335    /**
336
337     * Gets the notificationInfo attribute of the CustomViewDeployer object
338
339     *
340
341     * @return The notificationInfo value
342
343     */

344
345    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
346
347    {
348
349       if (info == null)
350
351       {
352
353          info = new MBeanNotificationInfo JavaDoc[]{
354
355                new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{"VIEW_ADDED"}, "javax.management.Notification", "Notification that a view has been added"),
356
357                new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{"VIEW_REMOVED"}, "javax.management.Notification", "Notification that a view has been removed")
358
359                };
360
361       }
362
363       return info;
364
365    }
366
367
368
369
370
371    /**
372
373     * Gets the customView attribute of the CustomViewDeployer object
374
375     *
376
377     * @param index Description of the Parameter
378
379     * @return The view value
380
381     * @jmx.managed-operation
382
383     */

384
385    public View getView(int index)
386
387    {
388
389       Iterator JavaDoc iterator = views.values().iterator();
390
391       View view = null;
392
393       int i = 0;
394
395
396
397       while (iterator.hasNext())
398
399       {
400
401          view = (View) iterator.next();
402
403          if (index == i)
404
405          {
406
407             break;
408
409          }
410
411          i++;
412
413       }
414
415
416
417       return view;
418
419    }
420
421
422
423
424
425    /**
426
427     * Description of the Method
428
429     *
430
431     * @return The customViews value
432
433     * @jmx.managed-operation
434
435     */

436
437    public View[] getViews()
438
439    {
440
441       return (View[]) views.values().toArray(new View[0]);
442
443    }
444
445
446
447
448
449    /**
450
451     * Description of the Method
452
453     *
454
455     * @param di Description of the Parameter
456
457     * @exception DeploymentException Description of the Exception
458
459     * @jmx.managed-operation
460
461     */

462
463    public void init(DeploymentInfo di)
464
465       throws DeploymentException
466
467    {
468
469       try
470
471       {
472
473          // resolve the watch
474

475          if (di.url.getProtocol().equals("file"))
476
477          {
478
479             File JavaDoc file = new File JavaDoc(di.url.getFile());
480
481
482
483             // If not directory we watch the package
484

485             if (!file.isDirectory())
486
487             {
488
489                di.watch = di.url;
490
491             }
492
493          }
494
495          else
496
497          {
498
499             // We watch the top only, no directory support
500

501             di.watch = di.url;
502
503          }
504
505
506
507          // Get the document
508

509          parseDocument(di);
510
511       }
512
513       catch (Exception JavaDoc e)
514
515       {
516
517          throw new DeploymentException(e);
518
519       }
520
521
522
523       // invoke super-class initialization
524

525       processNestedDeployments(di);
526
527    }
528
529
530
531
532
533    /**
534
535     * Description of the Method
536
537     *
538
539     * @param di Description of the Parameter
540
541     * @exception Exception Description of the Exception
542
543     */

544
545    protected void parseDocument(DeploymentInfo di)
546
547       throws Exception JavaDoc
548
549    {
550
551       DocumentBuilder JavaDoc parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
552
553       InputStream JavaDoc stream = null;
554
555
556
557       // If we are in a xml only get the URL
558

559       if (di.isXML)
560
561       {
562
563          stream = di.localUrl.openStream();
564
565       }
566
567       // Else load from the jar or directory
568

569       else
570
571       {
572
573          throw new DeploymentException("Can only handle *" + EXTENSION);
574
575       }
576
577
578
579       // Validate that the stream is not null
580

581       if (stream == null)
582
583       {
584
585          throw new DeploymentException("Failed to find valid *" + EXTENSION);
586
587       }
588
589
590
591       InputSource JavaDoc is = new InputSource JavaDoc(stream);
592
593       di.document = parser.parse(is);
594
595    }
596
597 }
598
599
Popular Tags