Source Code Cross Referenced for JNDIView.java in  » EJB-Server-JBoss-4.2.1 » server » org » jboss » naming » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server JBoss 4.2.1 » server » org.jboss.naming 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source.
003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004:         * as indicated by the @author tags. See the copyright.txt file in the
005:         * distribution for a full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package org.jboss.naming;
023:
024:        import java.io.PrintWriter;
025:        import java.io.StringWriter;
026:        import java.lang.reflect.Proxy;
027:        import java.util.Collection;
028:        import java.util.Iterator;
029:        import java.util.Set;
030:
031:        import javax.management.Attribute;
032:        import javax.management.AttributeList;
033:        import javax.management.InstanceNotFoundException;
034:        import javax.management.MBeanServer;
035:        import javax.management.ObjectName;
036:        import javax.naming.Context;
037:        import javax.naming.InitialContext;
038:        import javax.naming.LinkRef;
039:        import javax.naming.NameClassPair;
040:        import javax.naming.NamingEnumeration;
041:        import javax.naming.NamingException;
042:
043:        import org.jboss.ejb.Container;
044:        import org.jboss.ejb.EjbModule;
045:        import org.jboss.system.ServiceMBeanSupport;
046:        import org.jboss.web.AbstractWebDeployerMBean;
047:        import org.jboss.web.WebApplication;
048:
049:        /**
050:         * A simple utlity mbean that allows one to recursively list the default
051:         * JBoss InitialContext.
052:         *
053:         * @jmx:mbean name="jboss:type=JNDIView"
054:         *            extends="org.jboss.system.ServiceMBean"
055:         *            
056:         * @version <tt>$Revision: 57209 $</tt>
057:         * @author  <a href="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
058:         * @author  Vladimir Blagojevic <vladimir@xisnext.2y.net>
059:         * @author  <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
060:         */
061:        public class JNDIView extends ServiceMBeanSupport implements 
062:                JNDIViewMBean {
063:            /** The HANamingService attributes, order is significant in getHAUrl() */
064:            protected static final String[] g_haAttributes = new String[] {
065:                    "BindAddress", "Port" };
066:
067:            /** The HANamingService service name */
068:            protected String haNamingService;
069:
070:            /**
071:             * Provided for JMX compliance.
072:             */
073:            public JNDIView() {
074:            }
075:
076:            /**
077:             * List deployed application java:comp namespaces, the java:
078:             * namespace as well as the global InitialContext JNDI namespace.
079:             *
080:             * @jmx:managed-operation
081:             * 
082:             * @param verbose, if true, list the class of each object in addition to its name
083:             */
084:            public String list(boolean verbose) {
085:                StringBuffer buffer = new StringBuffer(4096);
086:                Context context = null;
087:                ClassLoader currentLoader = Thread.currentThread()
088:                        .getContextClassLoader();
089:
090:                try {
091:                    // Get all deployed web applications so that we can list their
092:                    // java: namespaces which are ClassLoader local
093:                    Iterator it = (Iterator) server.getAttribute(
094:                            AbstractWebDeployerMBean.OBJECT_NAME,
095:                            "DeployedApplications");
096:
097:                    if (it.hasNext() == true) {
098:                        buffer.append("<h1>Web Applications</h1>\n");
099:                    }
100:
101:                    while (it.hasNext() == true) {
102:                        WebApplication webApplication = (WebApplication) it
103:                                .next();
104:
105:                        Thread.currentThread().setContextClassLoader(
106:                                webApplication.getClassLoader());
107:
108:                        buffer.append("<h2>java:comp namespace of the "
109:                                + webApplication.getDeploymentInfo()
110:                                        .getCanonicalName()
111:                                + " application:</h2>\n");
112:
113:                        try {
114:                            context = new InitialContext();
115:                            context = (Context) context.lookup("java:comp");
116:                        } catch (NamingException e) {
117:                            buffer.append("Failed on lookup, "
118:                                    + e.toString(true));
119:                            formatException(buffer, e);
120:                            continue;
121:                        }
122:                        buffer.append("<pre>\n");
123:                        list(context, " ", buffer, verbose);
124:                        buffer.append("</pre>\n");
125:                    }
126:
127:                } catch (Throwable e) {
128:                    log.debug("Unable to list web applications ENC", e);
129:                }
130:
131:                // Get all deployed applications so that we can list their
132:                // java: namespaces which are ClassLoader local
133:                Set ejbModules = null;
134:                try {
135:                    ejbModules = server.queryNames(
136:                            EjbModule.EJB_MODULE_QUERY_NAME, null);
137:                } catch (Throwable e) {
138:                    log.error("getDeployedApplications failed", e);
139:                    buffer.append("Failed to getDeployedApplications\n");
140:                    formatException(buffer, e);
141:                    buffer.insert(0, "<pre>");
142:                    buffer.append("</pre>");
143:                    return buffer.toString();
144:                }
145:
146:                // List each application JNDI namespace
147:                for (Iterator i = ejbModules.iterator(); i.hasNext();) {
148:                    ObjectName app = (ObjectName) i.next();
149:                    String module = app.getKeyProperty("module");
150:                    if (module == null)
151:                        module = app.toString();
152:                    buffer.append("<h1>Ejb Module: " + module + "</h1>\n");
153:                    try {
154:                        Collection containers = (Collection) server
155:                                .getAttribute(app, "Containers");
156:                        for (Iterator iter = containers.iterator(); iter
157:                                .hasNext();) {
158:                            Container con = (Container) iter.next();
159:                            /* Set the thread class loader to that of the container as
160:                               the class loader is used by the java: context object
161:                               factory to partition the container namespaces.
162:                             */
163:                            Thread.currentThread().setContextClassLoader(
164:                                    con.getClassLoader());
165:                            String bean = con.getBeanMetaData().getEjbName();
166:                            buffer.append("<h2>java:comp namespace of the "
167:                                    + bean + " bean:</h2>\n");
168:
169:                            try {
170:                                context = new InitialContext();
171:                                context = (Context) context.lookup("java:comp");
172:                            } catch (NamingException e) {
173:                                buffer.append("Failed on lookup, "
174:                                        + e.toString(true));
175:                                formatException(buffer, e);
176:                                continue;
177:                            }
178:                            buffer.append("<pre>\n");
179:                            list(context, " ", buffer, verbose);
180:                            buffer.append("</pre>\n");
181:                        }
182:                    } catch (Throwable e) {
183:                        log.error("getConainers failed", e);
184:                        buffer.append("<pre>");
185:                        buffer.append("Failed to get ejbs in module\n");
186:                        formatException(buffer, e);
187:                        buffer.append("</pre>");
188:                    }
189:                }
190:
191:                // List the java: namespace
192:                Thread.currentThread().setContextClassLoader(currentLoader);
193:                try {
194:                    context = new InitialContext();
195:                    context = (Context) context.lookup("java:");
196:                    buffer.append("<h1>java: Namespace</h1>\n");
197:                    buffer.append("<pre>\n");
198:                    list(context, " ", buffer, verbose);
199:                    buffer.append("</pre>\n");
200:                } catch (NamingException e) {
201:                    log.error("lookup for java: failed", e);
202:                    buffer.append("Failed to get InitialContext, "
203:                            + e.toString(true));
204:                    formatException(buffer, e);
205:                }
206:
207:                // List the global JNDI namespace
208:                try {
209:                    context = new InitialContext();
210:                    buffer.append("<h1>Global JNDI Namespace</h1>\n");
211:                    buffer.append("<pre>\n");
212:                    list(context, " ", buffer, verbose);
213:                    buffer.append("</pre>\n");
214:                } catch (NamingException e) {
215:                    log.error("Failed to get InitialContext", e);
216:                    buffer.append("Failed to get InitialContext, "
217:                            + e.toString(true));
218:                    formatException(buffer, e);
219:                }
220:
221:                // List the HA-JNDI namespace if the HAJNDI service is available
222:                try {
223:                    String url = getHAUrl();
224:                    if (url != null) {
225:                        java.util.Hashtable env = new java.util.Hashtable();
226:                        env.put(Context.PROVIDER_URL, url);
227:                        context = new InitialContext(env);
228:                        buffer.append("<h1>HA-JNDI Namespace</h1>\n");
229:                        buffer.append("<pre>\n");
230:                        list(context, " ", buffer, verbose);
231:                        buffer.append("</pre>\n");
232:                    }
233:                } catch (NamingException ne) {
234:                    log.error("Failed to get InitialContext", ne);
235:                    buffer.append("Failed to get InitialContext, "
236:                            + ne.toString(true));
237:                    formatException(buffer, ne);
238:                }
239:                return buffer.toString();
240:            }
241:
242:            /**
243:             * List deployed application java:comp namespaces, the java:
244:             * namespace as well as the global InitialContext JNDI namespace in a
245:             * XML Format.
246:             *
247:             * @jmx:managed-operation
248:             *
249:             * @param verbose, if true, list the class of each object in addition to its name
250:             **/
251:            public String listXML() {
252:                StringBuffer buffer = new StringBuffer(4096);
253:                Set ejbModules = null;
254:                Context context = null;
255:                ClassLoader currentLoader = Thread.currentThread()
256:                        .getContextClassLoader();
257:
258:                openJndiTag(buffer);
259:                try {
260:                    // Get all deployed web applications so that we can list their
261:                    // java: namespaces which are ClassLoader local
262:                    Iterator it = (Iterator) server.getAttribute(
263:                            AbstractWebDeployerMBean.OBJECT_NAME,
264:                            "DeployedApplications");
265:
266:                    while (it.hasNext() == true) {
267:                        WebApplication webApplication = (WebApplication) it
268:                                .next();
269:                        openWebModuleTag(buffer, webApplication
270:                                .getDeploymentInfo().getCanonicalName());
271:
272:                        Thread.currentThread().setContextClassLoader(
273:                                webApplication.getClassLoader());
274:
275:                        try {
276:                            context = new InitialContext();
277:                            context = (Context) context.lookup("java:comp");
278:
279:                            listXML(context, buffer);
280:                        } catch (NamingException e) {
281:                            buffer.append("Failed on lookup, "
282:                                    + e.toString(true));
283:                            formatException(buffer, e);
284:                            continue;
285:                        } finally {
286:                            closeWebModuleTag(buffer);
287:                        }
288:                    }
289:                } catch (Throwable e) {
290:                    log.debug("Unable to list web applications ENC", e);
291:                }
292:
293:                /* Get all deployed applications so that we can list their
294:                   java: namespaces which are ClassLoader local
295:                 */
296:                try {
297:                    ejbModules = server.queryNames(
298:                            EjbModule.EJB_MODULE_QUERY_NAME, null);
299:                } catch (Exception e) {
300:                    log.error("getDeployedApplications failed", e);
301:                    appendErrorTag(buffer, "Failed to getDeployedApplications "
302:                            + e.toString());
303:                    closeJndiTag(buffer);
304:                    return buffer.toString();
305:                }
306:
307:                // List each application JNDI namespace
308:                for (Iterator i = ejbModules.iterator(); i.hasNext();) {
309:                    ObjectName app = (ObjectName) i.next();
310:                    openEjbModuleTag(buffer, app.getKeyProperty("url"));
311:
312:                    listModuleContainers(buffer, app);
313:
314:                    closeEjbModuleTag(buffer);
315:                }
316:
317:                // List the java: namespace
318:                Thread.currentThread().setContextClassLoader(currentLoader);
319:                try {
320:                    context = new InitialContext();
321:                    context = (Context) context.lookup("java:");
322:                } catch (NamingException e) {
323:                    log.error("Failed to get InitialContext for (java:)", e);
324:                    appendErrorTag(buffer,
325:                            "Failed to get InitialContext for (java:), "
326:                                    + e.toString(true));
327:                }
328:
329:                if (context != null) {
330:                    openContextTag(buffer);
331:                    appendJavaNameTag(buffer);
332:                    try {
333:                        listXML(context, buffer);
334:                    } catch (Throwable t) {
335:                        log.error("Failed to list contents of (java:)", t);
336:                        appendErrorTag(buffer,
337:                                "Failed to list contents of (java:), "
338:                                        + t.toString());
339:                    }
340:                    closeContextTag(buffer);
341:
342:                } //   if ( context != null )
343:
344:                // List the global JNDI namespace
345:                try {
346:                    context = new InitialContext();
347:                } catch (NamingException e) {
348:                    log.error("Failed to get InitialContext", e);
349:                    appendErrorTag(buffer, "Failed to get InitialContext, "
350:                            + e.toString(true));
351:                }
352:
353:                if (context != null) {
354:                    openContextTag(buffer);
355:                    appendGlobalNameTag(buffer);
356:                    try {
357:                        listXML(context, buffer);
358:                    } catch (Throwable t) {
359:                        log.error("Failed to list global contents ", t);
360:                        appendErrorTag(buffer,
361:                                "Failed to list global contents, "
362:                                        + t.toString());
363:                    }
364:                    closeContextTag(buffer);
365:
366:                } //   if ( context != null )
367:
368:                // List the HA-JNDI namespace if the HAJNDI service is available
369:                String url = null;
370:                try {
371:                    url = getHAUrl();
372:                    if (url != null) {
373:                        java.util.Hashtable env = new java.util.Hashtable();
374:                        env.put(Context.PROVIDER_URL, url);
375:                        context = new InitialContext(env);
376:                    }
377:                } catch (NamingException e) {
378:                    log.error("Failed to get InitialContext", e);
379:                    appendErrorTag(buffer, "Failed to get InitialContext, "
380:                            + e.toString(true));
381:                }
382:
383:                if (url != null && context != null) {
384:                    openContextTag(buffer);
385:                    appendHANameTag(buffer);
386:                    try {
387:                        listXML(context, buffer);
388:                    } catch (Throwable t) {
389:                        log.error("Failed to list HA-JNDI contents ", t);
390:                        appendErrorTag(buffer,
391:                                "Failed to list HA-JNDI contents, "
392:                                        + t.toString());
393:                    }
394:                    closeContextTag(buffer);
395:
396:                } //   if ( url != null && context != null )
397:
398:                closeJndiTag(buffer);
399:                return buffer.toString();
400:            }
401:
402:            public String getHANamingService() {
403:                return haNamingService;
404:            }
405:
406:            public void setHANamingService(String serviceName) {
407:                haNamingService = serviceName;
408:            }
409:
410:            protected ObjectName getObjectName(MBeanServer server,
411:                    ObjectName name)
412:                    throws javax.management.MalformedObjectNameException {
413:                return name == null ? OBJECT_NAME : name;
414:            }
415:
416:            private void list(Context ctx, String indent, StringBuffer buffer,
417:                    boolean verbose) {
418:                ClassLoader loader = Thread.currentThread()
419:                        .getContextClassLoader();
420:                try {
421:                    NamingEnumeration ne = ctx.list("");
422:                    while (ne.hasMore()) {
423:                        NameClassPair pair = (NameClassPair) ne.next();
424:                        log.trace("pair: " + pair);
425:
426:                        String name = pair.getName();
427:                        String className = pair.getClassName();
428:                        boolean recursive = false;
429:                        boolean isLinkRef = false;
430:                        boolean isProxy = false;
431:                        Class c = null;
432:                        try {
433:                            c = loader.loadClass(className);
434:                            log.trace("type: " + c);
435:
436:                            if (Context.class.isAssignableFrom(c))
437:                                recursive = true;
438:                            if (LinkRef.class.isAssignableFrom(c))
439:                                isLinkRef = true;
440:
441:                            isProxy = Proxy.isProxyClass(c);
442:                        } catch (ClassNotFoundException cnfe) {
443:                            // If this is a $Proxy* class its a proxy
444:                            if (className.startsWith("$Proxy")) {
445:                                isProxy = true;
446:                                // We have to get the class from the binding
447:                                try {
448:                                    Object p = ctx.lookup(name);
449:                                    c = p.getClass();
450:                                } catch (NamingException e) {
451:                                    Throwable t = e.getRootCause();
452:                                    if (t instanceof  ClassNotFoundException) {
453:                                        // Get the class name from the exception msg
454:                                        String msg = t.getMessage();
455:                                        if (msg != null) {
456:                                            // Reset the class name to the CNFE class
457:                                            className = msg;
458:                                        }
459:                                    }
460:                                }
461:                            }
462:                        }
463:
464:                        buffer.append(indent + " +- " + name);
465:
466:                        // Display reference targets
467:                        if (isLinkRef) {
468:                            // Get the 
469:                            try {
470:                                log.trace("looking up LinkRef; name=" + name);
471:                                Object obj = ctx.lookupLink(name);
472:                                log.trace("Object type: " + obj.getClass());
473:
474:                                LinkRef link = (LinkRef) obj;
475:                                buffer.append("[link -> ");
476:                                buffer.append(link.getLinkName());
477:                                buffer.append(']');
478:                            } catch (Throwable t) {
479:                                log.debug("Invalid LinkRef for: " + name, t);
480:                                buffer.append("invalid]");
481:                            }
482:                        }
483:
484:                        // Display proxy interfaces
485:                        if (isProxy) {
486:                            buffer.append(" (proxy: " + pair.getClassName());
487:                            if (c != null) {
488:                                Class[] ifaces = c.getInterfaces();
489:                                buffer.append(" implements ");
490:                                for (int i = 0; i < ifaces.length; i++) {
491:                                    buffer.append(ifaces[i]);
492:                                    buffer.append(',');
493:                                }
494:                                buffer.setCharAt(buffer.length() - 1, ')');
495:                            } else {
496:                                buffer.append(" implements " + className + ")");
497:                            }
498:                        } else if (verbose) {
499:                            buffer.append(" (class: " + pair.getClassName()
500:                                    + ")");
501:                        }
502:
503:                        buffer.append('\n');
504:                        if (recursive) {
505:                            try {
506:                                Object value = ctx.lookup(name);
507:                                if (value instanceof  Context) {
508:                                    Context subctx = (Context) value;
509:                                    list(subctx, indent + " |  ", buffer,
510:                                            verbose);
511:                                } else {
512:                                    buffer.append(indent + " |   NonContext: "
513:                                            + value);
514:                                    buffer.append('\n');
515:                                }
516:                            } catch (Throwable t) {
517:                                buffer.append("Failed to lookup: " + name
518:                                        + ", errmsg=" + t.getMessage());
519:                                buffer.append('\n');
520:                            }
521:                        }
522:                    }
523:                    ne.close();
524:                } catch (NamingException ne) {
525:                    buffer.append("error while listing context "
526:                            + ctx.toString() + ": " + ne.toString(true));
527:                    formatException(buffer, ne);
528:                }
529:            }
530:
531:            private void listXML(Context ctx, StringBuffer buffer) {
532:                ClassLoader loader = Thread.currentThread()
533:                        .getContextClassLoader();
534:                try {
535:                    NamingEnumeration ne = ctx.list("");
536:                    while (ne.hasMore()) {
537:                        NameClassPair pair = (NameClassPair) ne.next();
538:                        boolean recursive = false;
539:                        boolean isLinkRef = false;
540:                        try {
541:                            Class c = loader.loadClass(pair.getClassName());
542:                            if (Context.class.isAssignableFrom(c))
543:                                recursive = true;
544:                            if (LinkRef.class.isAssignableFrom(c))
545:                                isLinkRef = true;
546:                        } catch (ClassNotFoundException cnfe) {
547:                        }
548:
549:                        String name = pair.getName();
550:                        if (isLinkRef) {
551:                            Object obj = null;
552:                            LinkRef link = null;
553:                            try {
554:                                obj = ctx.lookupLink(name);
555:                                link = (LinkRef) obj;
556:                            } catch (Throwable t) {
557:                                log.error("Invalid LinkRef for: " + name, t);
558:
559:                                appendLinkRefErrorTag(buffer);
560:                            }
561:
562:                            appendLinkRefTag(buffer, link, pair);
563:                        } else {
564:                            if (recursive) {
565:                                Object value = null;
566:
567:                                try {
568:                                    value = ctx.lookup(name);
569:                                } catch (Throwable t) {
570:                                    appendErrorTag(buffer, "Failed to lookup: "
571:                                            + name + ", errmsg="
572:                                            + t.getMessage());
573:                                }
574:
575:                                if (value instanceof  Context) {
576:                                    Context subctx = (Context) value;
577:                                    openContextTag(buffer);
578:                                    appendNCPTag(buffer, pair);
579:
580:                                    try {
581:                                        listXML(subctx, buffer);
582:                                    } catch (Throwable t) {
583:                                        appendErrorTag(buffer,
584:                                                "Failed to list contents of: "
585:                                                        + name + ", errmsg="
586:                                                        + t.getMessage());
587:                                    }
588:
589:                                    closeContextTag(buffer);
590:                                } else {
591:                                    appendNonContextTag(buffer, pair);
592:                                }
593:                            } else {
594:                                appendLeafTag(buffer, pair);
595:                            }
596:                        }
597:                    }
598:                    ne.close();
599:                } catch (NamingException ne) {
600:                    appendErrorTag(buffer, "error while listing context "
601:                            + ctx.toString() + ": " + ne.toString(true));
602:                }
603:            }
604:
605:            private void listModuleContainers(StringBuffer buffer,
606:                    ObjectName app) {
607:                Collection containers = null;
608:
609:                try {
610:                    containers = (Collection) server.getAttribute(app,
611:                            "Containers");
612:                } catch (Throwable t) {
613:                    log.error("getContainers failed", t);
614:                    appendPreExceptionTag(buffer,
615:                            "Failed to get ejbs in module", t);
616:                }
617:
618:                for (Iterator iter = containers.iterator(); iter.hasNext();) {
619:                    listContainerContext(buffer, (Container) iter.next());
620:                }
621:
622:            } //   listModuleContainers()
623:
624:            private void listContainerContext(StringBuffer buffer, Container con) {
625:                /* Set the thread class loader to that of the container as
626:                   the class loader is used by the java: context object
627:                   factory to partition the container namespaces.
628:                 */
629:                Thread.currentThread().setContextClassLoader(
630:                        con.getClassLoader());
631:                String bean = con.getBeanMetaData().getEjbName();
632:                openContextTag(buffer);
633:                appendBeanTag(buffer, bean);
634:                Context context = null;
635:                try {
636:                    context = new InitialContext();
637:                    context = (Context) context.lookup("java:comp");
638:                } catch (NamingException e) {
639:                    appendErrorTag(buffer, "Failed on lookup "
640:                            + e.toString(true));
641:                    context = null;
642:                }
643:
644:                if (context != null) {
645:                    try {
646:                        listXML(context, buffer);
647:                    } catch (Throwable t) {
648:                        appendErrorTag(buffer, "Failed on list contents, "
649:                                + t.toString());
650:                    }
651:                } //   if ( context != null )
652:
653:                closeContextTag(buffer);
654:
655:            } //   listContainerContext()
656:
657:            private void openJndiTag(StringBuffer buffer) {
658:                buffer.append("<jndi>\n");
659:            }
660:
661:            private void closeJndiTag(StringBuffer buffer) {
662:                buffer.append("</jndi>\n");
663:            }
664:
665:            private void openWebModuleTag(StringBuffer buffer, String file) {
666:                buffer.append("<webmodule>\n");
667:                buffer.append("<file>").append(file).append("</file>\n");
668:            }
669:
670:            private void closeWebModuleTag(StringBuffer buffer) {
671:                buffer.append("</webmodule>\n");
672:            }
673:
674:            private void openEjbModuleTag(StringBuffer buffer, String file) {
675:                buffer.append("<ejbmodule>\n");
676:                buffer.append("<file>" + file + "</file>\n");
677:            }
678:
679:            private void closeEjbModuleTag(StringBuffer buffer) {
680:                buffer.append("</ejbmodule>\n");
681:            }
682:
683:            private void appendPreExceptionTag(StringBuffer buffer, String msg,
684:                    Throwable t) {
685:                buffer.append("<pre>\n" + msg + "\n");
686:                formatException(buffer, t);
687:                buffer.append("</pre>\n");
688:            }
689:
690:            private void appendBeanTag(StringBuffer buffer, String bean) {
691:                buffer.append("<name>java:comp</name>\n");
692:                buffer.append("<attribute name='bean'>" + bean
693:                        + "</attribute>\n");
694:            }
695:
696:            private void appendJavaNameTag(StringBuffer buffer) {
697:                buffer.append("<name>java:</name>\n");
698:            }
699:
700:            private void appendGlobalNameTag(StringBuffer buffer) {
701:                buffer.append("<name>Global</name>\n");
702:            }
703:
704:            private void appendHANameTag(StringBuffer buffer) {
705:                buffer.append("<name>HA</name>\n");
706:            }
707:
708:            private void appendLinkRefTag(StringBuffer buffer, LinkRef link,
709:                    NameClassPair ncp) {
710:                buffer.append("<link-ref>\n");
711:                buffer.append("<name>" + ncp.getName() + "</name>\n");
712:                try {
713:                    String lName = link.getLinkName();
714:                    buffer.append("<link>" + lName + "</link>\n");
715:                } catch (NamingException e) {
716:                    appendErrorTag(buffer, "Failed to getLinkName, "
717:                            + e.toString(true));
718:                }
719:                buffer.append("<attribute name='class'>" + ncp.getClassName()
720:                        + "</attribute>\n");
721:                buffer.append("</link-ref>\n");
722:            }
723:
724:            private void appendLinkRefErrorTag(StringBuffer buffer) {
725:                buffer.append("<link-ref>\n");
726:                buffer.append("<name>Invalid</name>\n");
727:                buffer.append("</link-ref>\n");
728:            }
729:
730:            private void openContextTag(StringBuffer buffer) {
731:                buffer.append("<context>\n");
732:            }
733:
734:            private void closeContextTag(StringBuffer buffer) {
735:                buffer.append("</context>\n");
736:            }
737:
738:            private void appendNonContextTag(StringBuffer buffer,
739:                    NameClassPair ncp) {
740:                buffer.append("<non-context>\n");
741:                appendNCPTag(buffer, ncp);
742:                buffer.append("</non-context>\n");
743:            }
744:
745:            private void appendLeafTag(StringBuffer buffer, NameClassPair ncp) {
746:                buffer.append("<leaf>\n");
747:                appendNCPTag(buffer, ncp);
748:                buffer.append("</leaf>\n");
749:            }
750:
751:            private void appendNCPTag(StringBuffer buffer, NameClassPair ncp) {
752:                buffer.append("<name>" + ncp.getName() + "</name>\n");
753:                buffer.append("<attribute name='class'>" + ncp.getClassName()
754:                        + "</attribute>\n");
755:            }
756:
757:            private void appendErrorTag(StringBuffer buffer, String msg) {
758:                buffer.append("<error>\n");
759:                buffer.append("<message>" + msg + "</message>\n");
760:                buffer.append("</error>\n");
761:            }
762:
763:            private void formatException(StringBuffer buffer, Throwable t) {
764:                StringWriter sw = new StringWriter();
765:                PrintWriter pw = new PrintWriter(sw);
766:                buffer.append("<pre>\n");
767:                t.printStackTrace(pw);
768:                buffer.append(sw.toString());
769:                buffer.append("</pre>\n");
770:            }
771:
772:            private String getHAUrl() {
773:                String bindAddress = null;
774:                String portNumber = null;
775:
776:                AttributeList list = getHAJndiAttributes();
777:                // list will be null if HA-JNDI service couldn't be retrieved
778:                if (list == null)
779:                    return null;
780:
781:                // list[0] is BindAddress
782:                Object o = list.get(0);
783:                if (o != null)
784:                    bindAddress = ((Attribute) o).getValue().toString();
785:
786:                // list[1] is Port
787:                o = list.get(1);
788:                if (o != null)
789:                    portNumber = ((Attribute) o).getValue().toString();
790:
791:                if (bindAddress != null && portNumber != null)
792:                    return "jnp://" + bindAddress + ":" + portNumber;
793:
794:                return null;
795:            }
796:
797:            private AttributeList getHAJndiAttributes() {
798:                if (haNamingService == null) {
799:                    // HA-JNDI is not deployed, so there will be no attributes
800:                    return null;
801:                }
802:
803:                try {
804:                    ObjectName name = new ObjectName(haNamingService);
805:                    return server.getAttributes(name, g_haAttributes);
806:                } catch (InstanceNotFoundException e1) {
807:                    // this is expected if HA-JNDI service isn't deployed - do not report it
808:                    return null;
809:                } catch (Exception e) {
810:                    // this is not expected - report it
811:                    log.error("JNDIView.getHAJndiAttributes() failed", e);
812:                    return null;
813:                }
814:            }
815:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.