KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > JNDIView


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.naming;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.management.Attribute JavaDoc;
32 import javax.management.AttributeList JavaDoc;
33 import javax.management.InstanceNotFoundException JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.LinkRef JavaDoc;
39 import javax.naming.NameClassPair JavaDoc;
40 import javax.naming.NamingEnumeration JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42
43 import org.jboss.ejb.Container;
44 import org.jboss.ejb.EjbModule;
45 import org.jboss.system.ServiceMBeanSupport;
46 import org.jboss.web.AbstractWebDeployerMBean;
47 import org.jboss.web.WebApplication;
48
49 /**
50  * A simple utlity mbean that allows one to recursively list the default
51  * JBoss InitialContext.
52  *
53  * @jmx:mbean name="jboss:type=JNDIView"
54  * extends="org.jboss.system.ServiceMBean"
55  *
56  * @version <tt>$Revision: 57455 $</tt>
57  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
58  * @author Vladimir Blagojevic <vladimir@xisnext.2y.net>
59  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
60  */

61 public class JNDIView
62       extends ServiceMBeanSupport
63       implements JNDIViewMBean
64 {
65    /** The HANamingService attributes, order is significant in getHAUrl() */
66    protected static final String JavaDoc[] g_haAttributes = new String JavaDoc[]{"BindAddress", "Port"};
67    
68    /** The HANamingService service name */
69    protected String JavaDoc haNamingService;
70   
71    /**
72     * Provided for JMX compliance.
73     */

74    public JNDIView()
75    {
76    }
77
78    /**
79     * List deployed application java:comp namespaces, the java:
80     * namespace as well as the global InitialContext JNDI namespace.
81     *
82     * @jmx:managed-operation
83     *
84     * @param verbose, if true, list the class of each object in addition to its name
85     */

86    public String JavaDoc list(boolean verbose)
87    {
88       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
89       Context JavaDoc context = null;
90       ClassLoader JavaDoc currentLoader = Thread.currentThread().getContextClassLoader();
91
92       try
93       {
94          // Get all deployed web applications so that we can list their
95
// java: namespaces which are ClassLoader local
96
Iterator JavaDoc it = (Iterator JavaDoc) server.getAttribute(AbstractWebDeployerMBean.OBJECT_NAME, "DeployedApplications");
97
98          if (it.hasNext() == true)
99          {
100             buffer.append("<h1>Web Applications</h1>\n");
101          }
102
103          while (it.hasNext() == true)
104          {
105             WebApplication webApplication = (WebApplication) it.next();
106             
107             Thread.currentThread().setContextClassLoader(webApplication.getMetaData().getENCLoader());
108
109             buffer.append("<h2>java:comp namespace of the " + webApplication.getCanonicalName()
110                   + " application:</h2>\n");
111
112             try
113             {
114                context = new InitialContext JavaDoc();
115                context = (Context JavaDoc) context.lookup("java:comp");
116             }
117             catch (NamingException JavaDoc e)
118             {
119                buffer.append("Failed on lookup, " + e.toString(true));
120                formatException(buffer, e);
121                continue;
122             }
123             buffer.append("<pre>\n");
124             list(context, " ", buffer, verbose);
125             buffer.append("</pre>\n");
126          }
127
128       }
129       catch (Throwable JavaDoc e)
130       {
131          log.debug("Unable to list web applications ENC", e);
132       }
133       
134       // Get all deployed applications so that we can list their
135
// java: namespaces which are ClassLoader local
136
Set JavaDoc ejbModules = null;
137       try
138       {
139          ejbModules = server.queryNames(EjbModule.EJB_MODULE_QUERY_NAME, null);
140       }
141       catch (Throwable JavaDoc e)
142       {
143          log.error("getDeployedApplications failed", e);
144          buffer.append("Failed to getDeployedApplications\n");
145          formatException(buffer, e);
146          buffer.insert(0, "<pre>");
147          buffer.append("</pre>");
148          return buffer.toString();
149       }
150
151       // List each application JNDI namespace
152
for (Iterator JavaDoc i = ejbModules.iterator(); i.hasNext();)
153       {
154          ObjectName JavaDoc app = (ObjectName JavaDoc) i.next();
155          String JavaDoc module = app.getKeyProperty("module");
156          if( module == null )
157             module = app.toString();
158          buffer.append("<h1>Ejb Module: " + module + "</h1>\n");
159          try
160          {
161             Collection JavaDoc containers = (Collection JavaDoc) server.getAttribute(app, "Containers");
162             for (Iterator JavaDoc iter = containers.iterator(); iter.hasNext();)
163             {
164                Container con = (Container) iter.next();
165                /* Set the thread class loader to that of the container as
166                   the class loader is used by the java: context object
167                   factory to partition the container namespaces.
168                */

169                Thread.currentThread().setContextClassLoader(con.getClassLoader());
170                String JavaDoc bean = con.getBeanMetaData().getEjbName();
171                buffer.append("<h2>java:comp namespace of the " + bean + " bean:</h2>\n");
172
173                try
174                {
175                   context = new InitialContext JavaDoc();
176                   context = (Context JavaDoc) context.lookup("java:comp");
177                }
178                catch (NamingException JavaDoc e)
179                {
180                   buffer.append("Failed on lookup, " + e.toString(true));
181                   formatException(buffer, e);
182                   continue;
183                }
184                buffer.append("<pre>\n");
185                list(context, " ", buffer, verbose);
186                buffer.append("</pre>\n");
187             }
188          }
189          catch (Throwable JavaDoc e)
190          {
191             log.error("getConainers failed", e);
192             buffer.append("<pre>");
193             buffer.append("Failed to get ejbs in module\n");
194             formatException(buffer, e);
195             buffer.append("</pre>");
196          }
197       }
198
199       // List the java: namespace
200
Thread.currentThread().setContextClassLoader(currentLoader);
201       try
202       {
203          context = new InitialContext JavaDoc();
204          context = (Context JavaDoc) context.lookup("java:");
205          buffer.append("<h1>java: Namespace</h1>\n");
206          buffer.append("<pre>\n");
207          list(context, " ", buffer, verbose);
208          buffer.append("</pre>\n");
209       }
210       catch (NamingException JavaDoc e)
211       {
212          log.error("lookup for java: failed", e);
213          buffer.append("Failed to get InitialContext, " + e.toString(true));
214          formatException(buffer, e);
215       }
216
217       // List the global JNDI namespace
218
try
219       {
220          context = new InitialContext JavaDoc();
221          buffer.append("<h1>Global JNDI Namespace</h1>\n");
222          buffer.append("<pre>\n");
223          list(context, " ", buffer, verbose);
224          buffer.append("</pre>\n");
225       }
226       catch (NamingException JavaDoc e)
227       {
228          log.error("Failed to get InitialContext", e);
229          buffer.append("Failed to get InitialContext, " + e.toString(true));
230          formatException(buffer, e);
231       }
232       
233       // List the HA-JNDI namespace if the HAJNDI service is available
234
try
235       {
236          String JavaDoc url = getHAUrl();
237          if (url != null)
238          {
239             java.util.Hashtable JavaDoc env = new java.util.Hashtable JavaDoc();
240             env.put(Context.PROVIDER_URL, url);
241             context = new InitialContext JavaDoc(env);
242             buffer.append("<h1>HA-JNDI Namespace</h1>\n");
243             buffer.append("<pre>\n");
244             list(context, " ", buffer, verbose);
245             buffer.append("</pre>\n");
246          }
247       }
248       catch (NamingException JavaDoc ne)
249       {
250          log.error("Failed to get InitialContext", ne);
251          buffer.append("Failed to get InitialContext, " + ne.toString(true));
252          formatException(buffer, ne);
253       }
254       return buffer.toString();
255    }
256
257    /**
258     * List deployed application java:comp namespaces, the java:
259     * namespace as well as the global InitialContext JNDI namespace in a
260     * XML Format.
261     *
262     * @jmx:managed-operation
263     *
264     * @param verbose, if true, list the class of each object in addition to its name
265     **/

266    public String JavaDoc listXML()
267    {
268       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
269       Set JavaDoc ejbModules = null;
270       Context JavaDoc context = null;
271       ClassLoader JavaDoc currentLoader = Thread.currentThread().getContextClassLoader();
272
273       openJndiTag(buffer);
274       try
275       {
276          // Get all deployed web applications so that we can list their
277
// java: namespaces which are ClassLoader local
278
Iterator JavaDoc it = (Iterator JavaDoc) server.getAttribute(AbstractWebDeployerMBean.OBJECT_NAME, "DeployedApplications");
279                   
280          while ( it.hasNext()==true )
281          {
282             WebApplication webApplication = (WebApplication) it.next();
283             openWebModuleTag(buffer, webApplication.getCanonicalName());
284             
285             Thread.currentThread().setContextClassLoader(webApplication.getMetaData().getENCLoader());
286             
287             try
288             {
289                context = new InitialContext JavaDoc();
290                context = (Context JavaDoc) context.lookup("java:comp");
291                
292                listXML(context, buffer);
293             }
294                catch (NamingException JavaDoc e)
295                {
296                   buffer.append("Failed on lookup, " + e.toString(true));
297                   formatException(buffer, e);
298                   continue;
299                }
300              finally
301             {
302                closeWebModuleTag(buffer);
303             }
304          }
305       }
306       catch (Throwable JavaDoc e)
307       {
308          log.debug("Unable to list web applications ENC", e);
309       }
310       
311       /* Get all deployed applications so that we can list their
312          java: namespaces which are ClassLoader local
313       */

314       try
315       {
316          ejbModules = server.queryNames(EjbModule.EJB_MODULE_QUERY_NAME, null);
317       }
318       catch (Exception JavaDoc e)
319       {
320          log.error("getDeployedApplications failed", e);
321          appendErrorTag(buffer,
322                "Failed to getDeployedApplications " + e.toString());
323          closeJndiTag(buffer);
324          return buffer.toString();
325       }
326
327       // List each application JNDI namespace
328
for (Iterator JavaDoc i = ejbModules.iterator(); i.hasNext();)
329       {
330          ObjectName JavaDoc app = (ObjectName JavaDoc) i.next();
331          openEjbModuleTag(buffer, app.getKeyProperty("url"));
332
333          listModuleContainers(buffer, app);
334
335          closeEjbModuleTag(buffer);
336       }
337
338       // List the java: namespace
339
Thread.currentThread().setContextClassLoader(currentLoader);
340       try
341       {
342          context = new InitialContext JavaDoc();
343          context = (Context JavaDoc) context.lookup("java:");
344       }
345       catch (NamingException JavaDoc e)
346       {
347          log.error("Failed to get InitialContext for (java:)", e);
348          appendErrorTag(buffer,
349                "Failed to get InitialContext for (java:), " +
350                e.toString(true));
351       }
352
353       if (context != null)
354       {
355          openContextTag(buffer);
356          appendJavaNameTag(buffer);
357          try
358          {
359             listXML(context, buffer);
360          }
361          catch (Throwable JavaDoc t)
362          {
363             log.error("Failed to list contents of (java:)", t);
364             appendErrorTag(buffer,
365                   "Failed to list contents of (java:), " +
366                   t.toString());
367          }
368          closeContextTag(buffer);
369
370       } // if ( context != null )
371

372       // List the global JNDI namespace
373
try
374       {
375          context = new InitialContext JavaDoc();
376       }
377       catch (NamingException JavaDoc e)
378       {
379          log.error("Failed to get InitialContext", e);
380          appendErrorTag(buffer,
381                "Failed to get InitialContext, " + e.toString(true));
382       }
383
384       if (context != null)
385       {
386          openContextTag(buffer);
387          appendGlobalNameTag(buffer);
388          try
389          {
390             listXML(context, buffer);
391          }
392          catch (Throwable JavaDoc t)
393          {
394             log.error("Failed to list global contents ", t);
395             appendErrorTag(buffer,
396                   "Failed to list global contents, " + t.toString());
397          }
398          closeContextTag(buffer);
399
400       } // if ( context != null )
401

402       // List the HA-JNDI namespace if the HAJNDI service is available
403
String JavaDoc url = null;
404       try
405       {
406          url = getHAUrl();
407          if (url != null)
408          {
409             java.util.Hashtable JavaDoc env = new java.util.Hashtable JavaDoc();
410             env.put(Context.PROVIDER_URL, url);
411             context = new InitialContext JavaDoc(env);
412          }
413       }
414       catch (NamingException JavaDoc e)
415       {
416          log.error("Failed to get InitialContext", e);
417          appendErrorTag(buffer,
418                "Failed to get InitialContext, " + e.toString(true));
419       }
420
421       if (url != null && context != null)
422       {
423          openContextTag(buffer);
424          appendHANameTag(buffer);
425          try
426          {
427             listXML(context, buffer);
428          }
429          catch (Throwable JavaDoc t)
430          {
431             log.error("Failed to list HA-JNDI contents ", t);
432             appendErrorTag(buffer,
433                   "Failed to list HA-JNDI contents, " + t.toString());
434          }
435          closeContextTag(buffer);
436
437       } // if ( url != null && context != null )
438

439       closeJndiTag(buffer);
440       
441       return buffer.toString();
442    }
443    
444    public String JavaDoc getHANamingService()
445    {
446       return haNamingService;
447    }
448    public void setHANamingService(String JavaDoc serviceName)
449    {
450       haNamingService = serviceName;
451    }
452
453    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
454          throws javax.management.MalformedObjectNameException JavaDoc
455    {
456       return name == null ? OBJECT_NAME : name;
457    }
458
459    private void list(Context JavaDoc ctx, String JavaDoc indent, StringBuffer JavaDoc buffer, boolean verbose)
460    {
461       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
462       try
463       {
464          NamingEnumeration JavaDoc ne = ctx.list("");
465          while (ne.hasMore())
466          {
467             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) ne.next();
468             log.trace("pair: " + pair);
469
470             String JavaDoc name = pair.getName();
471             String JavaDoc className = pair.getClassName();
472             boolean recursive = false;
473             boolean isLinkRef = false;
474             boolean isProxy = false;
475             Class JavaDoc c = null;
476             try
477             {
478                c = loader.loadClass(className);
479                log.trace("type: " + c);
480
481                if (Context JavaDoc.class.isAssignableFrom(c))
482                   recursive = true;
483                if (LinkRef JavaDoc.class.isAssignableFrom(c))
484                   isLinkRef = true;
485
486                isProxy = Proxy.isProxyClass(c);
487             }
488             catch (ClassNotFoundException JavaDoc cnfe)
489             {
490                // If this is a $Proxy* class its a proxy
491
if (className.startsWith("$Proxy"))
492                {
493                   isProxy = true;
494                   // We have to get the class from the binding
495
try
496                   {
497                      Object JavaDoc p = ctx.lookup(name);
498                      c = p.getClass();
499                   }
500                   catch (NamingException JavaDoc e)
501                   {
502                      Throwable JavaDoc t = e.getRootCause();
503                      if (t instanceof ClassNotFoundException JavaDoc)
504                      {
505                         // Get the class name from the exception msg
506
String JavaDoc msg = t.getMessage();
507                         if (msg != null)
508                         {
509                            // Reset the class name to the CNFE class
510
className = msg;
511                         }
512                      }
513                   }
514                }
515             }
516
517             buffer.append(indent + " +- " + name);
518
519             // Display reference targets
520
if (isLinkRef)
521             {
522                // Get the
523
try
524                {
525                   log.trace("looking up LinkRef; name=" + name);
526                   Object JavaDoc obj = ctx.lookupLink(name);
527                   log.trace("Object type: " + obj.getClass());
528
529                   LinkRef JavaDoc link = (LinkRef JavaDoc) obj;
530                   buffer.append("[link -> ");
531                   buffer.append(link.getLinkName());
532                   buffer.append(']');
533                }
534                catch (Throwable JavaDoc t)
535                {
536                   log.debug("Invalid LinkRef for: " + name, t);
537                   buffer.append("invalid]");
538                }
539             }
540
541             // Display proxy interfaces
542
if (isProxy)
543             {
544                buffer.append(" (proxy: " + pair.getClassName());
545                if (c != null)
546                {
547                   Class JavaDoc[] ifaces = c.getInterfaces();
548                   buffer.append(" implements ");
549                   for (int i = 0; i < ifaces.length; i++)
550                   {
551                      buffer.append(ifaces[i]);
552                      buffer.append(',');
553                   }
554                   buffer.setCharAt(buffer.length() - 1, ')');
555                }
556                else
557                {
558                   buffer.append(" implements " + className + ")");
559                }
560             }
561             else if (verbose)
562             {
563                buffer.append(" (class: " + pair.getClassName() + ")");
564             }
565
566             buffer.append('\n');
567             if (recursive)
568             {
569                try
570                {
571                   Object JavaDoc value = ctx.lookup(name);
572                   if (value instanceof Context JavaDoc)
573                   {
574                      Context JavaDoc subctx = (Context JavaDoc) value;
575                      list(subctx, indent + " | ", buffer, verbose);
576                   }
577                   else
578                   {
579                      buffer.append(indent + " | NonContext: " + value);
580                      buffer.append('\n');
581                   }
582                }
583                catch (Throwable JavaDoc t)
584                {
585                   buffer.append("Failed to lookup: " + name + ", errmsg=" + t.getMessage());
586                   buffer.append('\n');
587                }
588             }
589          }
590          ne.close();
591       }
592       catch (NamingException JavaDoc ne)
593       {
594          buffer.append("error while listing context " + ctx.toString() + ": " + ne.toString(true));
595          formatException(buffer, ne);
596       }
597    }
598
599    private void listXML(Context JavaDoc ctx, StringBuffer JavaDoc buffer)
600    {
601       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
602       try
603       {
604          NamingEnumeration JavaDoc ne = ctx.list("");
605          while (ne.hasMore())
606          {
607             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) ne.next();
608             boolean recursive = false;
609             boolean isLinkRef = false;
610             try
611             {
612                Class JavaDoc c = loader.loadClass(pair.getClassName());
613                if (Context JavaDoc.class.isAssignableFrom(c))
614                   recursive = true;
615                if (LinkRef JavaDoc.class.isAssignableFrom(c))
616                   isLinkRef = true;
617             }
618             catch (ClassNotFoundException JavaDoc cnfe)
619             {
620             }
621
622             String JavaDoc name = pair.getName();
623             if (isLinkRef)
624             {
625                Object JavaDoc obj = null;
626                LinkRef JavaDoc link = null;
627                try
628                {
629                   obj = ctx.lookupLink(name);
630                   link = (LinkRef JavaDoc) obj;
631                }
632                catch (Throwable JavaDoc t)
633                {
634                   log.error("Invalid LinkRef for: " + name, t);
635
636                   appendLinkRefErrorTag(buffer);
637                }
638
639                appendLinkRefTag(buffer, link, pair);
640             }
641             else
642             {
643                if (recursive)
644                {
645                   Object JavaDoc value = null;
646
647                   try
648                   {
649                      value = ctx.lookup(name);
650                   }
651                   catch (Throwable JavaDoc t)
652                   {
653                      appendErrorTag(buffer,
654                            "Failed to lookup: " + name +
655                            ", errmsg=" + t.getMessage());
656                   }
657
658                   if (value instanceof Context JavaDoc)
659                   {
660                      Context JavaDoc subctx = (Context JavaDoc) value;
661                      openContextTag(buffer);
662                      appendNCPTag(buffer, pair);
663
664                      try
665                      {
666                         listXML(subctx, buffer);
667                      }
668                      catch (Throwable JavaDoc t)
669                      {
670                         appendErrorTag(buffer,
671                               "Failed to list contents of: " + name +
672                               ", errmsg=" + t.getMessage());
673                      }
674
675                      closeContextTag(buffer);
676                   }
677                   else
678                   {
679                      appendNonContextTag(buffer, pair);
680                   }
681                }
682                else
683                {
684                   appendLeafTag(buffer, pair);
685                }
686             }
687          }
688          ne.close();
689       }
690       catch (NamingException JavaDoc ne)
691       {
692          appendErrorTag(buffer,
693                "error while listing context " +
694                ctx.toString() + ": " + ne.toString(true));
695       }
696    }
697
698    private void listModuleContainers(StringBuffer JavaDoc buffer, ObjectName JavaDoc app)
699    {
700       Collection JavaDoc containers = null;
701
702       try
703       {
704          containers = (Collection JavaDoc) server.getAttribute(app, "Containers");
705       }
706       catch (Throwable JavaDoc t)
707       {
708          log.error("getContainers failed", t);
709          appendPreExceptionTag(buffer, "Failed to get ejbs in module", t);
710       }
711
712       for (Iterator JavaDoc iter = containers.iterator(); iter.hasNext();)
713       {
714          listContainerContext(buffer, (Container) iter.next());
715       }
716
717    } // listModuleContainers()
718

719    private void listContainerContext(StringBuffer JavaDoc buffer, Container con)
720    {
721       /* Set the thread class loader to that of the container as
722          the class loader is used by the java: context object
723          factory to partition the container namespaces.
724       */

725       Thread.currentThread().setContextClassLoader(con.getClassLoader());
726       String JavaDoc bean = con.getBeanMetaData().getEjbName();
727       openContextTag(buffer);
728       appendBeanTag(buffer, bean);
729       Context JavaDoc context = null;
730       try
731       {
732          context = new InitialContext JavaDoc();
733          context = (Context JavaDoc) context.lookup("java:comp");
734       }
735       catch (NamingException JavaDoc e)
736       {
737          appendErrorTag(buffer,
738                "Failed on lookup " + e.toString(true));
739          context = null;
740       }
741
742       if (context != null)
743       {
744          try
745          {
746             listXML(context, buffer);
747          }
748          catch (Throwable JavaDoc t)
749          {
750             appendErrorTag(buffer,
751                   "Failed on list contents, " + t.toString());
752          }
753       } // if ( context != null )
754

755       closeContextTag(buffer);
756
757    } // listContainerContext()
758

759    private void openJndiTag(StringBuffer JavaDoc buffer)
760    {
761       buffer.append("<jndi>\n");
762    }
763
764    private void closeJndiTag(StringBuffer JavaDoc buffer)
765    {
766       buffer.append("</jndi>\n");
767    }
768
769    private void openWebModuleTag(StringBuffer JavaDoc buffer, String JavaDoc file)
770    {
771       buffer.append("<webmodule>\n");
772       buffer.append("<file>").append(file).append("</file>\n");
773    }
774
775    private void closeWebModuleTag(StringBuffer JavaDoc buffer)
776    {
777       buffer.append("</webmodule>\n");
778    }
779    
780    private void openEjbModuleTag(StringBuffer JavaDoc buffer, String JavaDoc file)
781    {
782       buffer.append("<ejbmodule>\n");
783       buffer.append("<file>" + file + "</file>\n");
784    }
785
786    private void closeEjbModuleTag(StringBuffer JavaDoc buffer)
787    {
788       buffer.append("</ejbmodule>\n");
789    }
790
791    private void appendPreExceptionTag(StringBuffer JavaDoc buffer,
792                                       String JavaDoc msg,
793                                       Throwable JavaDoc t)
794    {
795       buffer.append("<pre>\n" + msg+"\n");
796       formatException(buffer, t);
797       buffer.append("</pre>\n");
798    }
799
800    private void appendBeanTag(StringBuffer JavaDoc buffer, String JavaDoc bean)
801    {
802       buffer.append("<name>java:comp</name>\n");
803       buffer.append("<attribute name='bean'>" + bean + "</attribute>\n");
804    }
805
806    private void appendJavaNameTag(StringBuffer JavaDoc buffer)
807    {
808       buffer.append("<name>java:</name>\n");
809    }
810
811    private void appendGlobalNameTag(StringBuffer JavaDoc buffer)
812    {
813       buffer.append("<name>Global</name>\n");
814    }
815    
816    private void appendHANameTag(StringBuffer JavaDoc buffer)
817    {
818       buffer.append("<name>HA</name>\n");
819    }
820
821    private void appendLinkRefTag(StringBuffer JavaDoc buffer,
822                                  LinkRef JavaDoc link,
823                                  NameClassPair JavaDoc ncp)
824    {
825       buffer.append("<link-ref>\n");
826       buffer.append("<name>" + ncp.getName() + "</name>\n");
827       try
828       {
829          String JavaDoc lName = link.getLinkName();
830          buffer.append("<link>" + lName + "</link>\n");
831       }
832       catch (NamingException JavaDoc e)
833       {
834          appendErrorTag(buffer,
835                "Failed to getLinkName, " + e.toString(true));
836       }
837       buffer.append("<attribute name='class'>" + ncp.getClassName() +
838             "</attribute>\n");
839       buffer.append("</link-ref>\n");
840    }
841
842    private void appendLinkRefErrorTag(StringBuffer JavaDoc buffer)
843    {
844       buffer.append("<link-ref>\n");
845       buffer.append("<name>Invalid</name>\n");
846       buffer.append("</link-ref>\n");
847    }
848
849    private void openContextTag(StringBuffer JavaDoc buffer)
850    {
851       buffer.append("<context>\n");
852    }
853
854    private void closeContextTag(StringBuffer JavaDoc buffer)
855    {
856       buffer.append("</context>\n");
857    }
858
859    private void appendNonContextTag(StringBuffer JavaDoc buffer, NameClassPair JavaDoc ncp)
860    {
861       buffer.append("<non-context>\n");
862       appendNCPTag(buffer, ncp);
863       buffer.append("</non-context>\n");
864    }
865
866    private void appendLeafTag(StringBuffer JavaDoc buffer, NameClassPair JavaDoc ncp)
867    {
868       buffer.append("<leaf>\n");
869       appendNCPTag(buffer, ncp);
870       buffer.append("</leaf>\n");
871    }
872
873    private void appendNCPTag(StringBuffer JavaDoc buffer, NameClassPair JavaDoc ncp)
874    {
875       buffer.append("<name>" + ncp.getName() + "</name>\n");
876       buffer.append("<attribute name='class'>" + ncp.getClassName() +
877             "</attribute>\n");
878    }
879
880    private void appendErrorTag(StringBuffer JavaDoc buffer, String JavaDoc msg)
881    {
882       buffer.append("<error>\n");
883       buffer.append("<message>" + msg + "</message>\n");
884       buffer.append("</error>\n");
885    }
886
887    private void formatException(StringBuffer JavaDoc buffer, Throwable JavaDoc t)
888    {
889       StringWriter JavaDoc sw = new StringWriter JavaDoc();
890       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
891       buffer.append("<pre>\n");
892       t.printStackTrace(pw);
893       buffer.append(sw.toString());
894       buffer.append("</pre>\n");
895    }
896    
897    private String JavaDoc getHAUrl()
898    {
899       String JavaDoc bindAddress = null;
900       String JavaDoc portNumber = null;
901       
902       AttributeList JavaDoc list = getHAJndiAttributes();
903       // list will be null if HA-JNDI service couldn't be retrieved
904
if (list == null)
905          return null;
906
907       // list[0] is BindAddress
908
Object JavaDoc o = list.get(0);
909       if (o != null)
910          bindAddress = ((Attribute JavaDoc)o).getValue().toString();
911       
912       // list[1] is Port
913
o = list.get(1);
914       if (o != null)
915          portNumber = ((Attribute JavaDoc)o).getValue().toString();
916
917       if (bindAddress != null && portNumber != null)
918          return "jnp://"+bindAddress+":"+portNumber;
919
920       return null;
921    }
922    
923    private AttributeList JavaDoc getHAJndiAttributes()
924    {
925       if (haNamingService == null)
926       {
927          // HA-JNDI is not deployed, so there will be no attributes
928
return null;
929       }
930
931       try
932       {
933          ObjectName JavaDoc name = new ObjectName JavaDoc(haNamingService);
934          return server.getAttributes(name, g_haAttributes);
935       }
936       catch (InstanceNotFoundException JavaDoc e1)
937       {
938          // this is expected if HA-JNDI service isn't deployed - do not report it
939
return null;
940       }
941       catch (Exception JavaDoc e)
942       {
943          // this is not expected - report it
944
log.error("JNDIView.getHAJndiAttributes() failed", e);
945          return null;
946       }
947    }
948 }
949
Popular Tags