KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > plugins > AOPLister


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.plugins;
23
24 import gnu.trove.TLongObjectHashMap;
25 import org.jboss.aop.AspectManager;
26 import org.jboss.aop.CallerConstructorInfo;
27 import org.jboss.aop.CallerMethodInfo;
28 import org.jboss.aop.ClassAdvisor;
29 import org.jboss.aop.MethodInfo;
30 import org.jboss.aop.advice.AdviceBinding;
31 import org.jboss.aop.advice.Interceptor;
32 import org.jboss.aop.advice.AbstractAdvice;
33 import org.jboss.aop.advice.CFlowInterceptor;
34 import org.jboss.aop.introduction.InterfaceIntroduction;
35 import org.jboss.aop.standalone.Package;
36 import org.jboss.console.manager.interfaces.ManageableResource;
37 import org.jboss.console.manager.interfaces.TreeNode;
38 import org.jboss.console.plugins.helpers.AbstractPluginWrapper;
39
40 import java.io.PrintWriter JavaDoc;
41 import java.lang.reflect.Constructor JavaDoc;
42 import java.lang.reflect.Field JavaDoc;
43 import java.lang.reflect.Method JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import javax.servlet.ServletConfig JavaDoc;
51
52 /**
53  * As the number of MBeans is very big, we use a real Java class which is far
54  * faster than beanshell
55  *
56  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
57  * @version $Revision: 58165 $
58  *
59  * <p><b>Revisions:</b>
60  *
61  * <p><b>2 janv. 2003 Sacha Labourey:</b>
62  * <ul>
63  * <li> First implementation </li>
64  * </ul>
65  */

66 public class AOPLister
67         extends AbstractPluginWrapper
68 {
69    Thread JavaDoc refreshPoller;
70
71    
72    public AOPLister()
73    {
74       super();
75    }
76
77    TreeNode[] createMetaDataTree(org.jboss.aop.metadata.SimpleMetaData metaData, String JavaDoc description, String JavaDoc baseUrl) throws Exception JavaDoc
78    {
79       HashSet JavaDoc groups = metaData.tags();
80       if (groups.size() == 0)
81       {
82          return null;
83       }
84
85       TreeNode[] nodes = new TreeNode[groups.size()];
86       Iterator JavaDoc it = groups.iterator();
87       for (int i = 0; it.hasNext(); i++)
88       {
89          String JavaDoc group = (String JavaDoc) it.next();
90          nodes[i] = createTreeNode(
91                  group, // name
92
description,
93                  "images/database.gif", // Icon URL
94
baseUrl + "&group=" + group,
95                  null, // menu
96
null, // sub nodes
97
null // Sub-Resources
98
);
99       }
100       return nodes;
101    }
102
103    TreeNode[] loadDefaultMetaData(ClassAdvisor advisor, String JavaDoc classname) throws Exception JavaDoc
104    {
105       org.jboss.aop.metadata.SimpleMetaData metaData = advisor.getDefaultMetaData();
106       return createMetaDataTree(metaData,
107               "Default metadata for " + classname,
108               "AOPDefaultMetaData.jsp?classname=" + classname);
109    }
110
111    TreeNode[] loadClassMetaData(ClassAdvisor advisor, String JavaDoc classname) throws Exception JavaDoc
112    {
113       org.jboss.aop.metadata.SimpleMetaData metaData = advisor.getClassMetaData();
114       return createMetaDataTree(metaData,
115               "Class metadata for " + classname,
116               "AOPClassMetaData.jsp?classname=" + classname);
117    }
118
119    TreeNode[] loadMethodMetaData(ClassAdvisor advisor, String JavaDoc classname) throws Exception JavaDoc
120    {
121       org.jboss.aop.metadata.MethodMetaData metaData = advisor.getMethodMetaData();
122
123       Iterator JavaDoc it = metaData.getMethods();
124       if (!it.hasNext()) return null;
125       ArrayList JavaDoc methods = new ArrayList JavaDoc();
126       while (it.hasNext())
127       {
128          String JavaDoc method = (String JavaDoc) it.next();
129          org.jboss.aop.metadata.SimpleMetaData methodData = metaData.getMethodMetaData(method);
130          TreeNode[] methodNodes = createMetaDataTree(methodData,
131                  "Metadata for method " + method,
132                  "AOPMethodMetaData.jsp?classname=" + classname + "&method=" + java.net.URLEncoder.encode(method));
133          methods.add(createTreeNode(
134                  method, // name
135
"Metadata for method " + method,
136                  "images/starfolder.gif", // Icon URL
137
null,
138                  null, // menu
139
methodNodes, // sub nodes
140
null // Sub-Resources
141
));
142
143       }
144       return (TreeNode[]) methods.toArray(new TreeNode[methods.size()]);
145    }
146
147    TreeNode[] loadFieldMetaData(ClassAdvisor advisor, String JavaDoc classname) throws Exception JavaDoc
148    {
149       org.jboss.aop.metadata.FieldMetaData metaData = advisor.getFieldMetaData();
150
151       Iterator JavaDoc it = metaData.getFields();
152       if (!it.hasNext()) return null;
153       ArrayList JavaDoc fields = new ArrayList JavaDoc();
154       while (it.hasNext())
155       {
156          String JavaDoc field = (String JavaDoc) it.next();
157          org.jboss.aop.metadata.SimpleMetaData fieldData = metaData.getFieldMetaData(field);
158          TreeNode[] fieldNodes = createMetaDataTree(fieldData,
159                  "Metadata for field " + field,
160                  "AOPFieldMetaData.jsp?classname=" + classname + "&field=" + field);
161          fields.add(createTreeNode(
162                  field, // name
163
"Metadata for field " + field,
164                  "images/starfolder.gif", // Icon URL
165
null,
166                  null, // menu
167
fieldNodes, // sub nodes
168
null // Sub-Resources
169
));
170
171       }
172       return (TreeNode[]) fields.toArray(new TreeNode[fields.size()]);
173    }
174
175    TreeNode[] loadConstructorMetaData(ClassAdvisor advisor, String JavaDoc classname) throws Exception JavaDoc
176    {
177       org.jboss.aop.metadata.ConstructorMetaData metaData = advisor.getConstructorMetaData();
178
179       Iterator JavaDoc it = metaData.getConstructors();
180       if (!it.hasNext()) return null;
181       ArrayList JavaDoc constructors = new ArrayList JavaDoc();
182       while (it.hasNext())
183       {
184          String JavaDoc signature = (String JavaDoc)it.next();
185          org.jboss.aop.metadata.SimpleMetaData constructorData = metaData.getConstructorMetaData(signature);
186          TreeNode[] constructorNodes = createMetaDataTree(constructorData,
187                  "Metadata for constructor",
188                  "AOPConstructorMetaData.jsp?classname=" + classname + "&constructor=" + java.net.URLEncoder.encode(signature));
189          constructors.add(createTreeNode(
190                  signature, // name
191
"Metaata for constructor " + signature,
192                  "images/starfolder.gif", // Icon URL
193
null,
194                  null, // menu
195
constructorNodes, // sub nodes
196
null // Sub-Resources
197
));
198
199       }
200       return (TreeNode[]) constructors.toArray(new TreeNode[constructors.size()]);
201    }
202
203    TreeNode getMetaData(ClassAdvisor advisor) throws Exception JavaDoc
204    {
205       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
206
207       String JavaDoc classname = advisor.getClazz().getName();
208       
209       TreeNode[] defaultMetaData = loadDefaultMetaData(advisor, classname);
210       if (defaultMetaData != null)
211       {
212          nodes.add(createTreeNode(
213                  "Default",
214                  "Default metadata for for " + classname, // description
215
"images/starfolder.gif", // Icon URL
216
null,
217                  null, // menu
218
defaultMetaData, // sub nodes
219
null // Sub-Resources
220
));
221       }
222
223       TreeNode[] classMetaData = loadClassMetaData(advisor, classname);
224       if (classMetaData != null)
225       {
226          nodes.add(createTreeNode(
227                "Class",
228                "Class metadata for for " + classname, // description
229
"images/starfolder.gif", // Icon URL
230
null,
231                null, // menu
232
classMetaData, // sub nodes
233
null // Sub-Resources
234
));
235       }
236
237       TreeNode[] methodMetaData = loadMethodMetaData(advisor, classname);
238       if (methodMetaData != null)
239       {
240          nodes.add(createTreeNode(
241                  "Methods",
242                  "Method metadata for for " + classname, // description
243
"images/starfolder.gif", // Icon URL
244
null,
245                  null, // menu
246
methodMetaData, // sub nodes
247
null // Sub-Resources
248
));
249       }
250
251       TreeNode[] fieldMetaData = loadFieldMetaData(advisor, classname);
252       if (fieldMetaData != null)
253       {
254          nodes.add(createTreeNode(
255                  "Fields",
256                  "Field metadata for for " + classname, // description
257
"images/starfolder.gif", // Icon URL
258
null,
259                  null, // menu
260
fieldMetaData, // sub nodes
261
null // Sub-Resources
262
));
263       }
264
265       TreeNode[] constructorMetaData = loadConstructorMetaData(advisor, classname);
266       if (constructorMetaData != null)
267       {
268          nodes.add(createTreeNode(
269                  "Constructors",
270                  "Constructor metadata for for " + classname, // description
271
"images/starfolder.gif", // Icon URL
272
null,
273                  null, // menu
274
constructorMetaData, // sub nodes
275
null // Sub-Resources
276
));
277       }
278
279
280       if (nodes.size() == 0) return null;
281       TreeNode[] subnodes = (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
282
283       return createTreeNode(
284               "Metadata", // name
285
"Metadata for " + classname, // description
286
"images/starfolder.gif", // Icon URL
287
null,
288               null, // menu
289
subnodes, // sub nodes
290
null // Sub-Resources
291
);
292    }
293
294    TreeNode[] getIntroductions(ClassAdvisor advisor) throws Exception JavaDoc
295    {
296       ArrayList JavaDoc introductions = advisor.getInterfaceIntroductions();
297       if (introductions == null || introductions.size() == 0) return null;
298
299       TreeNode[] nodes = new TreeNode[introductions.size()];
300       for (int i = 0; i < introductions.size(); i++)
301       {
302          InterfaceIntroduction introduction = (InterfaceIntroduction) introductions.get(i);
303          nodes[i] = createTreeNode(
304                  "Introduction " + i, // name
305
"Introduction for " + advisor.getName(), // description
306
"images/service.gif", // Icon URL
307
"AOPIntroductionPointcut.jsp?pointcut=" + java.net.URLEncoder.encode(introduction.getName()), // Default URL
308
null, // menu
309
null, // sub nodes
310
null // Sub-Resources
311
);
312       }
313
314       return nodes;
315    }
316
317    public static String JavaDoc shortenMethod(String JavaDoc classname, Method JavaDoc method)
318    {
319       return method.toString().replaceAll(classname + "." + method.getName(), method.getName());
320    }
321
322    public static String JavaDoc shortenConstructor(String JavaDoc classname, Constructor JavaDoc constructor)
323    {
324       String JavaDoc base = classname.substring(classname.lastIndexOf('.') + 1);
325       return constructor.toString().replaceAll(classname, base);
326    }
327
328    public static String JavaDoc shortenField(String JavaDoc classname, Field JavaDoc field)
329    {
330       return field.toString().replaceAll(classname + "." + field.getName(), field.getName());
331    }
332
333    public TreeNode[] createAdvisorNodes(ClassAdvisor advisor) throws Exception JavaDoc
334    {
335       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
336       populateIntroductions(advisor, nodes);
337
338       populateConstructors(advisor, nodes);
339       populateMethods(advisor, nodes);
340
341       populateFields(advisor, nodes);
342       TreeNode metadata = getMetaData(advisor);
343       if (metadata != null) nodes.add(metadata);
344
345       return (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
346    }
347
348    private void populateFields(ClassAdvisor advisor, ArrayList JavaDoc nodes) throws Exception JavaDoc
349    {
350       if (advisor.getAdvisedFields() == null) return;
351       ArrayList JavaDoc fieldWriteNodes = new ArrayList JavaDoc();
352       ArrayList JavaDoc fieldReadNodes = new ArrayList JavaDoc();
353       for (int i = 0; i < advisor.getAdvisedFields().length; i++)
354       {
355          Field JavaDoc f = advisor.getAdvisedFields()[i];
356          Interceptor[] chain = advisor.getFieldWriteInterceptors()[i];
357          if (chain != null && chain.length > 0)
358          {
359             fieldWriteNodes.add(createTreeNode(
360                     shortenField(advisor.getName(), f),
361                     "Field write interceptor chain",
362                     "images/service.gif", // Icon URL
363
"AOPFieldChain.jsp?classname=" + java.net.URLEncoder.encode(advisor.getName())
364                     + "&field=" + i
365                     + "&mode=write",
366                     null, // menu
367
null, // sub nodes
368
null // Sub-Resources
369
));
370          }
371          chain = advisor.getFieldReadInterceptors()[i];
372          if (chain != null && chain.length > 0)
373          {
374             fieldReadNodes.add(createTreeNode(
375                     shortenField(advisor.getName(), f),
376                     "Field read interceptor chain",
377                     "images/service.gif", // Icon URL
378
"AOPFieldChain.jsp?classname=" + java.net.URLEncoder.encode(advisor.getName())
379                     + "&field=" + i
380                     + "&mode=read",
381                     null, // menu
382
null, // sub nodes
383
null // Sub-Resources
384
));
385          }
386       }
387       
388       if (fieldWriteNodes.size() > 0 && fieldWriteNodes.size() > 0)
389       {
390          ArrayList JavaDoc fieldReadWriteNodes = new ArrayList JavaDoc();
391           if (fieldWriteNodes.size() > 0)
392           {
393              TreeNode[] cnodes = (TreeNode[]) fieldWriteNodes.toArray(new TreeNode[fieldWriteNodes.size()]);
394              fieldReadWriteNodes.add(createTreeNode(
395                      "write interceptors", // name
396
"field write info", // description
397
"images/starfolder.gif", // Icon URL
398
null,
399                      null, // menu
400
cnodes, // sub nodes
401
null // Sub-Resources
402
));
403           }
404     
405           if (fieldReadNodes.size() > 0)
406           {
407              TreeNode[] cnodes = (TreeNode[]) fieldReadNodes.toArray(new TreeNode[fieldReadNodes.size()]);
408              fieldReadWriteNodes.add(createTreeNode(
409                      "read interceptors", // name
410
"field read info", // description
411
"images/starfolder.gif", // Icon URL
412
null,
413                      null, // menu
414
cnodes, // sub nodes
415
null // Sub-Resources
416
));
417           }
418           
419           TreeNode[] fieldRwNodes = (TreeNode[]) fieldReadWriteNodes.toArray(new TreeNode[fieldReadWriteNodes.size()]);
420           nodes.add(createTreeNode(
421                "Fields", //name
422
"field info", //description
423
"images/starfolder.gif", // Icon URL
424
null,
425                null, // menu
426
fieldRwNodes, // sub nodes
427
null // Sub-Resources
428
));
429
430       }
431 }
432
433    private void populateConstructors(ClassAdvisor advisor, ArrayList JavaDoc nodes) throws Exception JavaDoc
434    {
435       if (advisor.getConstructors() == null) return;
436       if (advisor.getConstructorInterceptors() == null) return;
437       if (advisor.getMethodCalledByConInterceptors() == null) return;
438       ArrayList JavaDoc constructorNodes = new ArrayList JavaDoc();
439       for (int i = 0; i < advisor.getConstructors().length; i++)
440       {
441          Constructor JavaDoc con = advisor.getConstructors()[i];
442          Interceptor[] chain = advisor.getConstructorInterceptors()[i];
443          HashMap methodCallers = advisor.getMethodCalledByConInterceptors()[i];
444          HashMap conCallers = advisor.getConCalledByConInterceptors()[i];
445          if ((chain != null && chain.length > 0) || methodCallers != null || conCallers != null)
446          {
447             ArrayList JavaDoc conNodes = new ArrayList JavaDoc();
448             if (chain != null && chain.length > 0)
449             {
450                conNodes.add(createTreeNode(
451                        "Interceptors",
452                        "Execution Interceptors",
453                        "images/service.gif", // Icon URL
454
"AOPConstructorChain.jsp?classname=" + java.net.URLEncoder.encode(con.getDeclaringClass().getName())
455                        + "&constructor=" + i,
456                        null, // menu
457
null, // sub nodes
458
null // Sub-Resources
459
));
460             }
461             if (conCallers != null)
462             {
463                conNodes.add(createTreeNode(
464                        "constructor callers",
465                        "constructor caller interceptions",
466                        "images/starfolder.gif", // Icon URL
467
null,
468                        null, // menu
469
createConstructorConstructorCallers(i, advisor, conCallers), // sub nodes
470
null // Sub-Resources
471
));
472             }
473             if (methodCallers != null)
474             {
475                conNodes.add(createTreeNode(
476                        "method callers",
477                        "method caller interceptions",
478                        "images/starfolder.gif", // Icon URL
479
null,
480                        null, // menu
481
createConstructorMethodCallers(i, advisor, methodCallers), // sub nodes
482
null // Sub-Resources
483
));
484             }
485             TreeNode[] cnodes = (TreeNode[]) conNodes.toArray(new TreeNode[conNodes.size()]);
486             constructorNodes.add(createTreeNode(
487                     shortenConstructor(advisor.getName(), con), // name
488
"constructor info", // description
489
"images/starfolder.gif", // Icon URL
490
null,
491                     null, // menu
492
cnodes, // sub nodes
493
null // Sub-Resources
494
));
495          }
496       }
497       if (constructorNodes.size() > 0)
498       {
499          TreeNode[] cnodes = (TreeNode[]) constructorNodes.toArray(new TreeNode[constructorNodes.size()]);
500          nodes.add(createTreeNode(
501                  "Constructors", // name
502
"constructor info", // description
503
"images/starfolder.gif", // Icon URL
504
null,
505                  null, // menu
506
cnodes, // sub nodes
507
null // Sub-Resources
508
));
509       }
510    }
511
512    private void populateMethods(ClassAdvisor advisor, ArrayList JavaDoc nodes) throws Exception JavaDoc
513    {
514       if (advisor.getMethodInterceptors() == null) return;
515       ArrayList JavaDoc methodNodes = new ArrayList JavaDoc();
516       long[] keys = advisor.getMethodInterceptors().keys();
517       for (int i = 0; i < keys.length; i++)
518       {
519          long key = keys[i];
520          MethodInfo method = (MethodInfo) advisor.getMethodInterceptors().get(key);
521          HashMap methodCallers = (HashMap) advisor.getMethodCalledByMethodInterceptors().get(key);
522          HashMap conCallers = (HashMap) advisor.getConCalledByMethodInterceptors().get(key);
523          if (method == null && methodCallers == null) continue;
524          if (method != null && methodCallers == null && (method.getInterceptors() == null || method.getInterceptors().length < 1)) continue;
525          ArrayList JavaDoc mNodes = new ArrayList JavaDoc();
526          if (method.getInterceptors() != null && method.getInterceptors().length > 0 || methodCallers != null || conCallers != null)
527          {
528             mNodes.add(createTreeNode(
529                     "Interceptors",
530                     "Execution Interceptors",
531                     "images/service.gif", // Icon URL
532
"AOPMethodChain.jsp?classname=" + java.net.URLEncoder.encode(advisor.getName())
533                     + "&method=" + keys[i],
534                     null, // menu
535
null, // sub nodes
536
null // Sub-Resources
537
));
538          }
539          if (conCallers != null)
540          {
541             mNodes.add(createTreeNode(
542                     "constructor callers",
543                     "constructor caller interceptions",
544                     "images/starfolder.gif", // Icon URL
545
null,
546                     null, // menu
547
createMethodConstructorCallers(key, advisor, conCallers), // sub nodes
548
null // Sub-Resources
549
));
550          }
551          if (methodCallers != null)
552          {
553             mNodes.add(createTreeNode(
554                     "method callers",
555                     "method caller interceptions",
556                     "images/starfolder.gif", // Icon URL
557
null,
558                     null, // menu
559
createMethodMethodCallers(key, advisor, methodCallers), // sub nodes
560
null // Sub-Resources
561
));
562          }
563          TreeNode[] mnodes = (TreeNode[]) mNodes.toArray(new TreeNode[mNodes.size()]);
564          methodNodes.add(createTreeNode(
565                  shortenMethod(advisor.getName(), method.getAdvisedMethod()), // name
566
"method info", // description
567
"images/starfolder.gif", // Icon URL
568
null,
569                  null, // menu
570
mnodes, // sub nodes
571
null // Sub-Resources
572
));
573       }
574       if (methodNodes.size() > 0)
575       {
576          TreeNode[] cnodes = (TreeNode[]) methodNodes.toArray(new TreeNode[methodNodes.size()]);
577          nodes.add(createTreeNode(
578                  "Methods", // name
579
"method info", // description
580
"images/starfolder.gif", // Icon URL
581
null,
582                  null, // menu
583
cnodes, // sub nodes
584
null // Sub-Resources
585
));
586       }
587    }
588
589    private void populateIntroductions(ClassAdvisor advisor, ArrayList JavaDoc nodes) throws Exception JavaDoc
590    {
591       ArrayList JavaDoc introductions = advisor.getInterfaceIntroductions();
592       if (introductions != null && introductions.size() > 0)
593       {
594          TreeNode[] introductionNodes = getIntroductions(advisor);
595          TreeNode introductionsNode = createTreeNode(
596                  "Introductions", // name
597
"Introductions for " + advisor.getName(), // description
598
"images/starfolder.gif", // Icon URL
599
null,
600                  null, // menu
601
introductionNodes, // sub nodes
602
null // Sub-Resources
603
);
604          nodes.add(introductionsNode);
605       }
606    }
607
608    public TreeNode[] createConstructorMethodCallers(int index, ClassAdvisor advisor, HashMap called) throws Exception JavaDoc
609    {
610       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
611       Iterator JavaDoc it = called.keySet().iterator();
612       while (it.hasNext())
613       {
614          String JavaDoc calledClass = (String JavaDoc) it.next();
615          TLongObjectHashMap map = (TLongObjectHashMap) called.get(calledClass);
616          Object JavaDoc[] values = map.getValues();
617          long[] keys = map.keys();
618          for (int i = 0; i < values.length; i++)
619          {
620             CallerMethodInfo caller = (CallerMethodInfo) values[i];
621             nodes.add(createTreeNode(
622                     caller.getMethod().toString(),
623                     "caller interceptions",
624                     "images/service.gif", // Icon URL
625
"AOPConstructorMethodCallerChain.jsp?index=" + index + "&hash=" + java.net.URLEncoder.encode(Long.toString(keys[i])) + "&classname=" + java.net.URLEncoder.encode(advisor.getName()) + "&calledclassname=" + java.net.URLEncoder.encode(calledClass),
626                     null, // menu
627
null, // sub nodes
628
null // Sub-Resources
629
));
630          }
631       }
632       return (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
633    }
634
635    public TreeNode[] createConstructorConstructorCallers(int index, ClassAdvisor advisor, HashMap called) throws Exception JavaDoc
636    {
637       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
638       Iterator JavaDoc it = called.keySet().iterator();
639       while (it.hasNext())
640       {
641          String JavaDoc calledClass = (String JavaDoc) it.next();
642          TLongObjectHashMap map = (TLongObjectHashMap) called.get(calledClass);
643          Object JavaDoc[] values = map.getValues();
644          long[] keys = map.keys();
645          for (int i = 0; i < values.length; i++)
646          {
647             CallerConstructorInfo caller = (CallerConstructorInfo) values[i];
648             nodes.add(createTreeNode(
649                     caller.getConstructor().toString(),
650                     "caller interceptions",
651                     "images/service.gif", // Icon URL
652
"AOPConstructorConstructorCallerChain.jsp?index=" + index + "&hash=" + java.net.URLEncoder.encode(Long.toString(keys[i])) + "&classname=" + java.net.URLEncoder.encode(advisor.getName()) + "&calledclassname=" + java.net.URLEncoder.encode(calledClass),
653                     null, // menu
654
null, // sub nodes
655
null // Sub-Resources
656
));
657          }
658       }
659       return (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
660    }
661
662    public TreeNode[] createMethodMethodCallers(long callingHash, ClassAdvisor advisor, HashMap called) throws Exception JavaDoc
663    {
664       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
665       Iterator JavaDoc it = called.keySet().iterator();
666       while (it.hasNext())
667       {
668          String JavaDoc calledClass = (String JavaDoc) it.next();
669          TLongObjectHashMap map = (TLongObjectHashMap) called.get(calledClass);
670          Object JavaDoc[] values = map.getValues();
671          long[] keys = map.keys();
672          for (int i = 0; i < values.length; i++)
673          {
674             CallerMethodInfo caller = (CallerMethodInfo) values[i];
675             nodes.add(createTreeNode(
676                     caller.getMethod().toString(),
677                     "caller interceptions",
678                     "images/service.gif", // Icon URL
679
"AOPMethodMethodCallerChain.jsp?callinghash=" + callingHash + "&hash=" + java.net.URLEncoder.encode(Long.toString(keys[i])) + "&classname=" + java.net.URLEncoder.encode(advisor.getName()) + "&calledclassname=" + java.net.URLEncoder.encode(calledClass),
680                     null, // menu
681
null, // sub nodes
682
null // Sub-Resources
683
));
684          }
685       }
686       return (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
687    }
688
689    public TreeNode[] createMethodConstructorCallers(long callingHash, ClassAdvisor advisor, HashMap called) throws Exception JavaDoc
690    {
691       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
692       Iterator JavaDoc it = called.keySet().iterator();
693       while (it.hasNext())
694       {
695          String JavaDoc calledClass = (String JavaDoc) it.next();
696          TLongObjectHashMap map = (TLongObjectHashMap) called.get(calledClass);
697          Object JavaDoc[] values = map.getValues();
698          long[] keys = map.keys();
699          for (int i = 0; i < values.length; i++)
700          {
701             CallerConstructorInfo caller = (CallerConstructorInfo) values[i];
702             nodes.add(createTreeNode(
703                     caller.getConstructor().toString(),
704                     "caller interceptions",
705                     "images/service.gif", // Icon URL
706
"AOPMethodConstructorCallerChain.jsp?callinghash=" + callingHash + "&hash=" + java.net.URLEncoder.encode(Long.toString(keys[i])) + "&classname=" + java.net.URLEncoder.encode(advisor.getName()) + "&calledclassname=" + java.net.URLEncoder.encode(calledClass),
707                     null, // menu
708
null, // sub nodes
709
null // Sub-Resources
710
));
711          }
712       }
713       return (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
714    }
715
716    public TreeNode[] getUnboundBindings() throws Exception JavaDoc
717    {
718       ArrayList JavaDoc unbounded = new ArrayList JavaDoc();
719       Iterator JavaDoc it = AspectManager.instance().getBindings().values().iterator();
720       while (it.hasNext())
721       {
722          AdviceBinding binding = (AdviceBinding) it.next();
723          if (!binding.hasAdvisors())
724          {
725             unbounded.add(createTreeNode(
726                     binding.getName(),
727                     "Unbounded Binding",
728                     "images/service.gif", // Icon URL
729
"AOPBinding.jsp?binding=" + java.net.URLEncoder.encode(binding.getName()),
730                     null, // menu
731
null, // sub nodes
732
null // Sub-Resources
733
));
734          }
735       }
736       if (unbounded.size() == 0) return null;
737       return (TreeNode[])unbounded.toArray(new TreeNode[unbounded.size()]);
738    }
739
740
741    TreeNode[] createAOPNodes(Package JavaDoc root) throws Exception JavaDoc
742    {
743       ArrayList JavaDoc nodes = new ArrayList JavaDoc();
744       Iterator JavaDoc it = root.packages.entrySet().iterator();
745       while (it.hasNext())
746       {
747          Map.Entry entry = (Map.Entry) it.next();
748          String JavaDoc pkgName = (String JavaDoc) entry.getKey();
749          Package JavaDoc p = (Package JavaDoc) entry.getValue();
750          nodes.add(createTreeNode(
751                  pkgName, // name
752
"Package " + pkgName, // description
753
"images/starfolder.gif", // Icon URL
754
null, // Default URL
755
null, // menu
756
createAOPNodes(p), // sub nodes
757
null // Sub-Resources
758
));
759       }
760       it = root.advisors.entrySet().iterator();
761       while (it.hasNext())
762       {
763          Map.Entry entry = (Map.Entry) it.next();
764          String JavaDoc classname = (String JavaDoc) entry.getKey();
765          ClassAdvisor advisor = (ClassAdvisor) entry.getValue();
766          nodes.add(createTreeNode(
767                  classname, // name
768
"Class " + classname, // description
769
"images/serviceset.gif", // Icon URL
770
null,
771                  null, // menu
772
createAdvisorNodes(advisor), // sub nodes
773
null // Sub-Resources
774
)
775          );
776       }
777       TreeNode[] result;
778       if (nodes.size() == 0)
779       {
780          result = null;
781       }
782       else
783       {
784          result = (TreeNode[]) nodes.toArray(new TreeNode[nodes.size()]);
785       }
786
787       return result;
788    }
789
790    protected TreeNode getTreeForResource(String JavaDoc profile, ManageableResource resource)
791    {
792       try
793       {
794          TreeNode[] unbounded = getUnboundBindings();
795          TreeNode[] children = new TreeNode[2];
796          children[0] = createTreeNode(
797                  "Classes", // name
798
"Display all Classes", // description
799
"images/serviceset.gif", // Icon URL
800
null, // Default URL
801
null,
802                  createAOPNodes(Package.aopClassMap()), // sub nodes
803
null // Sub-Resources
804
);
805          children[1] = createTreeNode(
806                  "Unbound Bindings", // name
807
"Unbound Bindings", // description
808
"images/serviceset.gif", // Icon URL
809
null, // Default URL
810
null,
811                  unbounded, // sub nodes
812
null // Sub-Resources
813
);
814          return createTreeNode (
815                "AOP", // name
816
"AOP Management", // description
817
"images/spirale32.gif", // Icon URL
818
null, // Default URL
819
null,
820                children,
821                null);
822       }
823       catch (Exception JavaDoc e)
824       {
825          e.printStackTrace();
826          return null;
827       }
828    }
829    
830    
831    public static String JavaDoc outputChain(Interceptor[] chain)
832    {
833       String JavaDoc output = "";
834       for (int i = 0; i < chain.length; i++)
835       {
836          output += "<tr>";
837          if (chain[i] instanceof AbstractAdvice)
838          {
839             output +="<td><font size=\"1\">advice</font></td><td><font size=\"1\">" + chain[i].getName() + "</font></td>";
840          }
841          else if (chain[i] instanceof CFlowInterceptor)
842          {
843             output +="<td><font size=\"1\">cflow</font></td><td><font size=\"1\">" + ((CFlowInterceptor) chain[i]).getCFlowString() + "</font></td>";
844          }
845          else
846          {
847             output +="<td><font size=\"1\">interceptor</font></td><td><font size=\"1\">" + chain[i].getClass().getName() + "</font></td>";
848          }
849          output += "</tr>";
850       }
851       return output;
852    }
853
854    // org.jboss.console.plugins.helpers.PluginWrapper overrides -----------------------
855

856    public void init(ServletConfig JavaDoc servletConfig) throws Exception JavaDoc
857    {
858       super.init(servletConfig);
859       refreshPoller = new RefreshPoller();
860       refreshPoller.start();
861    }
862
863    class RefreshPoller extends Thread JavaDoc
864    {
865
866       final static int REFRESH_RATE = 20 * 1000;
867
868       RefreshPoller()
869       {
870          setName("AOPListner");
871          setDaemon(true);
872       }
873      
874       public void run()
875       {
876          try
877          {
878             int advisorCount = 0;
879             while (!isInterrupted())
880             {
881                int count = AspectManager.instance().getAdvisors().size();
882                if (count != advisorCount)
883                {
884                   pm.regenerateAdminTree();
885                }
886                advisorCount = count;
887                
888                Thread.sleep(REFRESH_RATE);
889             }
890          }
891          catch (InterruptedException JavaDoc e)
892          {
893             return;
894          }
895       }
896    }
897    
898    public void destroy()
899    {
900       super.destroy();
901       try
902       {
903          refreshPoller.interrupt();
904          refreshPoller.join();
905       }
906       catch (Exception JavaDoc e)
907       {
908       }
909    }
910
911    /**
912     * For jsp pages to get hold of the advisor given a classname, since
913     * AspectManager.getAdvisor(String classname) has been deprecated.
914     * (I attempted to load the class from the class name and then to
915     * call AspectManager.findAdvisor(), which worked on first deploy,
916     * but returned null on subsequent deploys)
917     * @param classname
918     * @return
919     */

920    public static ClassAdvisor findAdvisor(String JavaDoc classname)
921    {
922       return AdvisorFinder.getAdvisor(classname);
923    }
924    /**
925     * Helper class For jsp pages to get hold of the advisor given a classname, since
926     * AspectManager.getAdvisor(String classname) has been deprecated.
927     */

928    static class AdvisorFinder
929    {
930       public static ClassAdvisor getAdvisor(String JavaDoc classname)
931       {
932          String JavaDoc[] name = classname.split("\\.");
933          Package JavaDoc root = Package.aopClassMap();
934          
935          if (!root.name.equals("classes"))throw new RuntimeException JavaDoc("Did not get expected root 'classes'");
936          
937          for (Iterator JavaDoc it = root.packages.entrySet().iterator() ; it.hasNext() ; )
938          {
939             Map.Entry entry = (Map.Entry) it.next();
940             Package JavaDoc pkg = (Package JavaDoc) entry.getValue();
941             ClassAdvisor advisor = findAdvisor(pkg, classname, name, 0);
942             if (advisor != null)
943             {
944                return advisor;
945             }
946          }
947          return null;
948       }
949
950       private static ClassAdvisor findAdvisor(Package JavaDoc pkg, String JavaDoc classname, String JavaDoc[] name, int depth)
951       {
952          if (depth >= name.length || !pkg.name.equals(name[depth]))
953          {
954             return null;
955          }
956          
957          for (Iterator JavaDoc it = pkg.packages.entrySet().iterator() ; it.hasNext() ; )
958          {
959             Map.Entry entry = (Map.Entry) it.next();
960             Package JavaDoc p = (Package JavaDoc) entry.getValue();
961             ClassAdvisor advisor = findAdvisor(p, classname, name, depth + 1);
962             if (advisor != null)
963             {
964                return advisor;
965             }
966          }
967
968          for (Iterator JavaDoc it = pkg.advisors.entrySet().iterator(); it.hasNext() ; )
969          {
970             Map.Entry entry = (Map.Entry) it.next();
971             ClassAdvisor advisor = (ClassAdvisor) entry.getValue();
972             if (advisor.getClazz().getName().equals(classname))
973             {
974                return advisor;
975             }
976          }
977          
978          return null;
979       }
980    }
981    
982
983       
984 }
985
Popular Tags