1 45 package org.openejb.server.telnet; 46 47 import java.io.DataInputStream ; 48 import java.io.IOException ; 49 import java.io.PrintStream ; 50 51 import javax.ejb.EJBHome ; 52 import javax.naming.Context ; 53 import javax.naming.NameClassPair ; 54 import javax.naming.NameNotFoundException ; 55 import javax.naming.NamingEnumeration ; 56 57 import org.openejb.OpenEJB; 58 import org.openejb.core.ivm.naming.IvmContext; 59 60 63 public class Lookup extends Command 64 { 65 66 javax.naming.Context ctx = OpenEJB.getJNDIContext(); 67 68 public static void register() 69 { 70 Lookup cmd = new Lookup(); 71 Command.register( "lookup", cmd ); 72 } 74 75 static String PWD = ""; 76 77 public void exec( Arguments args, DataInputStream in, PrintStream out ) throws IOException 79 { 80 try 81 { 82 String name = ""; 83 if ( args == null || args.count() == 0 ) 84 { 85 name = PWD; 86 } 87 else 88 { 89 name = args.get(); 90 } 91 92 Object obj = null; 93 try 94 { 95 obj = ctx.lookup( name ); 96 } 97 catch ( NameNotFoundException e ) 98 { 99 out.print( "lookup: " ); 100 out.print( name ); 101 out.println( ": No such object or subcontext" ); 102 return; 103 } 104 catch ( Throwable e ) 105 { 106 out.print( "lookup: error: " ); 107 e.printStackTrace( new PrintStream ( out ) ); 108 return; 109 } 110 111 if ( obj instanceof Context ) 112 { 113 list( name, in, out ); 114 return; 115 } 116 117 out.println( "" + obj ); 119 } 120 catch ( Exception e ) 121 { 122 e.printStackTrace( new PrintStream ( out ) ); 123 } 124 } 125 126 public void list( String name, DataInputStream in, PrintStream out ) throws IOException 127 { 128 try 129 { 130 NamingEnumeration enumeration = null; 131 try 132 { 133 134 enumeration = ctx.list( name ); 135 } 136 catch ( NameNotFoundException e ) 137 { 138 out.print( "lookup: " ); 139 out.print( name ); 140 out.println( ": No such object or subcontext" ); 141 return; 142 } 143 catch ( Throwable e ) 144 { 145 out.print( "lookup: error: " ); 146 e.printStackTrace( new PrintStream ( out ) ); 147 return; 148 } 149 150 if ( enumeration == null ) 151 { 152 return; 153 } 154 155 while ( enumeration.hasMore() ) 156 { 157 158 NameClassPair entry = ( NameClassPair ) enumeration.next(); 159 String eName = entry.getName(); 160 Class eClass = null; 161 162 if ( IvmContext.class.getName().equals( entry.getClassName() ) ) 163 { 164 eClass = IvmContext.class; 165 } 166 else 167 { 168 try 169 { 170 ClassLoader cl = OpenEJB.getContextClassLoader(); 171 eClass = Class.forName( entry.getClassName(), true, cl ); 172 } 173 catch ( Throwable t ) 174 { 175 eClass = java.lang.Object .class; 176 } 177 } 178 179 if ( Context .class.isAssignableFrom( eClass ) ) 180 { 181 out.print( TextConsole.TTY_Bright ); 183 out.print( TextConsole.TTY_FG_Blue ); 184 out.print( entry.getName() ); 185 out.print( TextConsole.TTY_Reset ); 186 } 187 else if ( EJBHome .class.isAssignableFrom( eClass ) ) 188 { 189 out.print( TextConsole.TTY_Bright ); 191 out.print( TextConsole.TTY_FG_Green ); 192 out.print( entry.getName() ); 193 out.print( TextConsole.TTY_Reset ); 194 } 195 else 196 { 197 out.print( entry.getName() ); 199 } 200 out.println(); 201 } 202 } 203 catch ( Exception e ) 204 { 205 e.printStackTrace( new PrintStream ( out ) ); 206 } 207 } 208 } 209 210 | Popular Tags |