From: Scott M S. <st...@us...> - 2003-03-18 20:21:26
|
User: starksm Date: 03/03/18 12:21:18 Modified: src/main/org/jboss/naming Tag: Branch_3_0 JNDIView.java Log: Apply patch for bug #664400 - JNDIView.listXML output ill-formed Revision Changes Path No revision No revision 1.17.2.2 +266 -153 jboss/src/main/org/jboss/naming/JNDIView.java Index: JNDIView.java =================================================================== RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/naming/JNDIView.java,v retrieving revision 1.17.2.1 retrieving revision 1.17.2.2 diff -u -r1.17.2.1 -r1.17.2.2 --- JNDIView.java 10 May 2002 02:22:58 -0000 1.17.2.1 +++ JNDIView.java 18 Mar 2003 20:21:12 -0000 1.17.2.2 @@ -44,7 +44,7 @@ * @jmx:mbean name="jboss:type=JNDIView" * extends="org.jboss.system.ServiceMBean" * - * @version <tt>$Revision: 1.17.2.1 $</tt> + * @version <tt>$Revision: 1.17.2.2 $</tt> * @author <a href="mailto:Sco...@jb...">Scott Stark</a>. * @author Vladimir Blagojevic <vla...@xi...> * @author <a href="mailto:d_j...@us...">David Jencks</a> @@ -196,76 +196,23 @@ catch(Exception e) { log.error("getDeployedApplications failed", e); - buffer.append( "<jndi>" ); - buffer.append( "<error>" ); - buffer.append( '\n' ); - buffer.append( "<message>" + "Failed to getDeployedApplications " + e.toString() + "</message>" ); - buffer.append( '\n' ); - buffer.append( "</error>" ); - buffer.append( '\n' ); - buffer.append( "</jndi>" ); - buffer.append( '\n' ); + openJndiTag( buffer ); + appendErrorTag( buffer, + "Failed to getDeployedApplications " + e.toString() ); + closeJndiTag( buffer ); return buffer.toString(); } - buffer.append( "<jndi>" ); - buffer.append( '\n' ); + openJndiTag( buffer ); // List each application JNDI namespace for (Iterator i = ejbModules.iterator(); i.hasNext(); ) { ObjectName app = (ObjectName) i.next(); - buffer.append( "<ejbmodule>" ); - buffer.append( '\n' ); - buffer.append( "<file>" + app.getKeyProperty("url") + "</file>" ); - buffer.append( '\n' ); - try - { - Collection containers = (Collection)server.getAttribute(app, "Containers"); - for (Iterator iter = containers.iterator(); iter.hasNext();) - { - Container con = (Container)iter.next(); - /* Set the thread class loader to that of the container as - the class loader is used by the java: context object - factory to partition the container namespaces. - */ - Thread.currentThread().setContextClassLoader(con.getClassLoader()); - String bean = con.getBeanMetaData().getEjbName(); - buffer.append( "<context>" ); - buffer.append( '\n' ); - buffer.append( "<name>java:comp</name>" ); - buffer.append( '\n' ); - buffer.append( "<attribute name=\"bean\">" + bean + "</attribute>" ); - buffer.append( '\n' ); - try - { - context = new InitialContext(); - context = (Context)context.lookup("java:comp"); - } - catch(NamingException e) - { - buffer.append( "<error>" ); - buffer.append( '\n' ); - buffer.append( "<message>" + "Failed on lookup, " + e.toString( true ) + "</message>" ); - buffer.append( '\n' ); - buffer.append( "</error>" ); - buffer.append( '\n' ); - continue; - } - listXML( context, buffer ); - buffer.append( "</context>" ); - buffer.append( '\n' ); - } - } - catch(Throwable e) - { - log.error("getConainers failed", e); - buffer.append("<pre>"); - buffer.append("Failed to get ejbs in module\n"); - formatException(buffer, e); - buffer.append("</pre>"); - } - buffer.append( "</ejbmodule>" ); - buffer.append( '\n' ); + openEjbModuleTag( buffer, app.getKeyProperty("url") ); + + listModuleContainers( buffer, app ); + + closeEjbModuleTag( buffer ); } // List the java: namespace @@ -274,49 +221,65 @@ { context = new InitialContext(); context = (Context) context.lookup("java:"); - buffer.append( "<context>" ); - buffer.append( '\n' ); - buffer.append( "<name>java:</name>" ); - buffer.append( '\n' ); - listXML( context, buffer ); - buffer.append( "</context>" ); - buffer.append( '\n' ); } catch(NamingException e) { - log.error("Failed to get InitialContext", e); - buffer.append( "<error>" ); - buffer.append( '\n' ); - buffer.append( "<message>" + "Failed to get InitialContext, " + e.toString( true ) + "</message>" ); - buffer.append( '\n' ); - buffer.append( "</error>" ); - buffer.append( '\n' ); + log.error("Failed to get InitialContext for (java:)", e); + appendErrorTag( buffer, + "Failed to get InitialContext for (java:), " + + e.toString(true) ); } + if ( context != null ) + { + openContextTag( buffer ); + appendJavaNameTag( buffer ); + try + { + listXML( context, buffer ); + } + catch(Throwable t) + { + log.error("Failed to list contents of (java:)", t); + appendErrorTag( buffer, + "Failed to list contents of (java:), " + + t.toString() ); + } + closeContextTag( buffer ); + + } // if ( context != null ) + // List the global JNDI namespace try { context = new InitialContext(); - buffer.append( "<context>" ); - buffer.append( '\n' ); - buffer.append( "<name>Global</name>" ); - buffer.append( '\n' ); - listXML( context, buffer ); - buffer.append( "</context>" ); - buffer.append( '\n' ); } catch(NamingException e) { log.error("Failed to get InitialContext", e); - buffer.append( "<error>" ); - buffer.append( '\n' ); - buffer.append( "<message>" + "Failed to get InitialContext, " + e.toString( true ) + "</message>" ); - buffer.append( '\n' ); - buffer.append( "</error>" ); - buffer.append( '\n' ); + appendErrorTag( buffer, + "Failed to get InitialContext, " + e.toString(true) ); } - buffer.append( "</jndi>" ); - buffer.append( '\n' ); + + if ( context != null ) + { + openContextTag( buffer ); + appendGlobalNameTag( buffer ); + try + { + listXML( context, buffer ); + } + catch(Throwable t) + { + log.error("Failed to list global contents ", t); + appendErrorTag( buffer, + "Failed to list global contents, " + t.toString() ); + } + closeContextTag( buffer ); + + } // if ( context != null ) + + closeJndiTag( buffer ); return buffer.toString(); } @@ -403,7 +366,7 @@ } catch(Throwable t) { - log.error("Invalid LinkRef", t); + log.error("Invalid LinkRef for: " + name, t); buffer.append("[invalid]"); } } @@ -491,78 +454,57 @@ String name = pair.getName(); if( isLinkRef ) { + LinkRef link = null; + // Get the try { - LinkRef link = (LinkRef) ctx.lookupLink(name); - buffer.append( "<link-ref>" ); - buffer.append( '\n' ); - buffer.append( "<name>" + pair.getName() + "</name>" ); - buffer.append( '\n' ); - buffer.append( "<link>" + link.getLinkName() + "</link>" ); - buffer.append( '\n' ); - buffer.append( "<attribute name=\"class\">" + pair.getClassName() + "</attribute>" ); - buffer.append( '\n' ); - buffer.append( "</link-ref>" ); - buffer.append( '\n' ); + link = (LinkRef) ctx.lookupLink(name); } catch(Throwable t) { - log.error("Invalid LinkRef", t); + log.error("Invalid LinkRef for: " + name, t); - buffer.append( "<link-ref>" ); - buffer.append( '\n' ); - buffer.append( "<name>Invalid</name>" ); - buffer.append( '\n' ); - buffer.append( "</link-ref>" ); - buffer.append( '\n' ); + appendLinkRefErrorTag( buffer ); } + + appendLinkRefTag( buffer, link, pair ); } else { if( recursive ) { + Object value = null; + try { - Object value = ctx.lookup(name); - if( value instanceof Context ) { - Context subctx = (Context) value; - buffer.append( "<context>" ); - buffer.append( '\n' ); - buffer.append( "<name>" + pair.getName() + "</name>" ); - buffer.append( '\n' ); - buffer.append( "<attribute name=\"class\">" + pair.getClassName() + "</attribute>" ); - buffer.append( '\n' ); + value = ctx.lookup(name); + } + catch(Throwable t) { + appendErrorTag( buffer, + "Failed to lookup: " + name + + ", errmsg=" + t.getMessage() ); + } + + if( value instanceof Context ) { + Context subctx = (Context) value; + openContextTag( buffer ); + appendNCPTag( buffer, pair ); + + try { listXML( subctx, buffer ); - buffer.append( "</context>" ); - buffer.append( '\n' ); } - else { - buffer.append( "<non-context>" ); - buffer.append( '\n' ); - buffer.append( "<name>" + pair.getName() + "</name>" ); - buffer.append( '\n' ); - buffer.append( "<attribute name=\"value\">" + value + "</attribute>" ); - buffer.append( '\n' ); - buffer.append( "</non-context>" ); - buffer.append( '\n' ); + catch(Throwable t) { + appendErrorTag( buffer, + "Failed to list contents of: " + name + + ", errmsg=" + t.getMessage() ); } + + closeContextTag( buffer ); } - catch(Throwable t) { - buffer.append( "<error>" ); - buffer.append( '\n' ); - buffer.append( "<message>" + "Failed to lookup: "+name+", errmsg="+t.getMessage() + "</message>" ); - buffer.append( '\n' ); - buffer.append( "</error>" ); - buffer.append( '\n' ); + else { + appendNonContextTag( buffer, pair ); } } else { - buffer.append( "<leaf>" ); - buffer.append( '\n' ); - buffer.append( "<name>" + pair.getName() + "</name>" ); - buffer.append( '\n' ); - buffer.append( "<attribute name=\"class\">" + pair.getClassName() + "</attribute>" ); - buffer.append( '\n' ); - buffer.append( "</leaf>" ); - buffer.append( '\n' ); + appendLeafTag( buffer, pair ); } } } @@ -570,13 +512,183 @@ } catch(NamingException ne) { - buffer.append( "<error>" ); - buffer.append( '\n' ); - buffer.append( "<message>" + "error while listing context "+ctx.toString () + ": " + ne.toString(true) + "</message>" ); - buffer.append( '\n' ); - buffer.append( "</error>" ); - buffer.append( '\n' ); + appendErrorTag( buffer, + "error while listing context " + + ctx.toString () + ": " + ne.toString(true) ); + } + } + + private void listModuleContainers(StringBuffer buffer, ObjectName app) + { + Collection containers = null; + + try + { + containers = (Collection)server.getAttribute(app, "Containers"); + } + catch(Throwable t) + { + log.error("getContainers failed", t); + appendPreExceptionTag( buffer, "Failed to get ejbs in module", t ); + } + + for (Iterator iter = containers.iterator(); iter.hasNext();) + { + listContainerContext( buffer, (Container)iter.next() ); + } + + } // listModuleContainers() + + private void listContainerContext(StringBuffer buffer, Container con) + { + /* Set the thread class loader to that of the container as + the class loader is used by the java: context object + factory to partition the container namespaces. + */ + Thread.currentThread().setContextClassLoader(con.getClassLoader()); + String bean = con.getBeanMetaData().getEjbName(); + openContextTag( buffer ); + appendBeanTag( buffer, bean ); + Context context = null; + try + { + context = new InitialContext(); + context = (Context)context.lookup("java:comp"); + } + catch(NamingException e) + { + appendErrorTag( buffer, + "Failed on lookup " + e.toString( true ) ); + context = null; } + + if ( context != null ) + { + try + { + listXML( context, buffer ); + } + catch(Throwable t) + { + appendErrorTag( buffer, + "Failed on list contents, " + t.toString() ); + } + } // if ( context != null ) + + closeContextTag( buffer ); + + } // listContainerContext() + + private void openJndiTag(StringBuffer buffer) + { + buffer.append( "<jndi>\n" ); + } + + private void closeJndiTag(StringBuffer buffer) + { + buffer.append( "</jndi>\n" ); + } + + private void openEjbModuleTag(StringBuffer buffer, String file) + { + buffer.append( "<ejbmodule>\n" ); + buffer.append( "<file>" + file + "</file>\n" ); + } + + private void closeEjbModuleTag(StringBuffer buffer) + { + buffer.append( "</ejbmodule>\n" ); + } + + private void appendPreExceptionTag(StringBuffer buffer, + String msg, + Throwable t) + { + buffer.append("<pre>\n + msg\n"); + formatException(buffer, t); + buffer.append("</pre>\n"); + } + + private void appendBeanTag(StringBuffer buffer, String bean) + { + buffer.append( "<name>java:comp</name>\n" ); + buffer.append( "<attribute name=\"bean\">" + bean + "</attribute>\n" ); + } + + private void appendJavaNameTag(StringBuffer buffer) + { + buffer.append( "<name>java:</name>\n" ); + } + + private void appendGlobalNameTag(StringBuffer buffer) + { + buffer.append( "<name>Global</name>\n" ); + } + + private void appendLinkRefTag(StringBuffer buffer, + LinkRef link, + NameClassPair ncp) + { + buffer.append( "<link-ref>\n" ); + buffer.append( "<name>" + ncp.getName() + "</name>\n" ); + try + { + String lName = link.getLinkName(); + buffer.append( "<link>" + lName + "</link>\n" ); + } + catch(NamingException e) + { + appendErrorTag( buffer, + "Failed to getLinkName, " + e.toString( true ) ); + } + buffer.append( "<attribute name=\"class\">" + ncp.getClassName() + + "</attribute>\n" ); + buffer.append( "</link-ref>\n" ); + } + + private void appendLinkRefErrorTag(StringBuffer buffer) + { + buffer.append( "<link-ref>\n" ); + buffer.append( "<name>Invalid</name>\n" ); + buffer.append( "</link-ref>\n" ); + } + + private void openContextTag(StringBuffer buffer) + { + buffer.append( "<context>\n" ); + } + + private void closeContextTag(StringBuffer buffer) + { + buffer.append( "</context>\n" ); + } + + private void appendNonContextTag(StringBuffer buffer, NameClassPair ncp) + { + buffer.append( "<non-context>\n" ); + appendNCPTag( buffer, ncp ); + buffer.append( "</non-context>\n" ); + } + + private void appendLeafTag(StringBuffer buffer, NameClassPair ncp) + { + buffer.append( "<leaf>\n" ); + appendNCPTag( buffer, ncp ); + buffer.append( "</leaf>\n" ); + } + + private void appendNCPTag(StringBuffer buffer, NameClassPair ncp) + { + buffer.append( "<name>" + ncp.getName() + "</name>\n" ); + buffer.append( "<attribute name=\"class\">" + ncp.getClassName() + + "</attribute>\n" ); + } + + private void appendErrorTag(StringBuffer buffer, String msg) + { + buffer.append( "<error>\n" ); + buffer.append( "<message>" + msg + "</message>\n" ); + buffer.append( "</error>\n" ); } private void formatException(StringBuffer buffer, Throwable t) @@ -589,3 +701,4 @@ buffer.append("</pre>\n"); } } + |