KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > telnet > Lookup


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Lookup.java 2487 2006-02-22 22:05:03Z dblevins $
44  */

45 package org.openejb.server.telnet;
46
47 import java.io.DataInputStream JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.PrintStream JavaDoc;
50
51 import javax.ejb.EJBHome JavaDoc;
52 import javax.naming.Context JavaDoc;
53 import javax.naming.NameClassPair JavaDoc;
54 import javax.naming.NameNotFoundException JavaDoc;
55 import javax.naming.NamingEnumeration JavaDoc;
56
57 import org.openejb.OpenEJB;
58 import org.openejb.core.ivm.naming.IvmContext;
59
60 /**
61  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
62  */

63 public class Lookup extends Command
64 {
65
66     javax.naming.Context JavaDoc ctx = OpenEJB.getJNDIContext();
67
68     public static void register()
69     {
70         Lookup cmd = new Lookup();
71         Command.register( "lookup", cmd );
72         //Command.register("list", cmd);
73
}
74
75     static String JavaDoc PWD = "";
76
77     // execute jndi lookups
78
public void exec( Arguments args, DataInputStream JavaDoc in, PrintStream JavaDoc out ) throws IOException JavaDoc
79     {
80         try
81         {
82             String JavaDoc name = "";
83             if ( args == null || args.count() == 0 )
84             {
85                 name = PWD;
86             }
87             else
88             {
89                 name = args.get();
90             }
91
92             Object JavaDoc obj = null;
93             try
94             {
95                 obj = ctx.lookup( name );
96             }
97             catch ( NameNotFoundException JavaDoc e )
98             {
99                 out.print( "lookup: " );
100                 out.print( name );
101                 out.println( ": No such object or subcontext" );
102                 return;
103             }
104             catch ( Throwable JavaDoc e )
105             {
106                 out.print( "lookup: error: " );
107                 e.printStackTrace( new PrintStream JavaDoc( out ) );
108                 return;
109             }
110
111             if ( obj instanceof Context JavaDoc )
112             {
113                 list( name, in, out );
114                 return;
115             }
116
117             // TODO:1: Output the different data types differently
118
out.println( "" + obj );
119         }
120         catch ( Exception JavaDoc e )
121         {
122             e.printStackTrace( new PrintStream JavaDoc( out ) );
123         }
124     }
125
126     public void list( String JavaDoc name, DataInputStream JavaDoc in, PrintStream JavaDoc out ) throws IOException JavaDoc
127     {
128         try
129         {
130             NamingEnumeration JavaDoc enumeration = null;
131             try
132             {
133
134                 enumeration = ctx.list( name );
135             }
136             catch ( NameNotFoundException JavaDoc e )
137             {
138                 out.print( "lookup: " );
139                 out.print( name );
140                 out.println( ": No such object or subcontext" );
141                 return;
142             }
143             catch ( Throwable JavaDoc e )
144             {
145                 out.print( "lookup: error: " );
146                 e.printStackTrace( new PrintStream JavaDoc( out ) );
147                 return;
148             }
149
150             if ( enumeration == null )
151             {
152                 return;
153             }
154
155             while ( enumeration.hasMore() )
156             {
157
158                 NameClassPair JavaDoc entry = ( NameClassPair JavaDoc ) enumeration.next();
159                 String JavaDoc eName = entry.getName();
160                 Class JavaDoc 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 JavaDoc cl = OpenEJB.getContextClassLoader();
171                         eClass = Class.forName( entry.getClassName(), true, cl );
172                     }
173                     catch ( Throwable JavaDoc t )
174                     {
175                         eClass = java.lang.Object JavaDoc.class;
176                     }
177                 }
178
179                 if ( Context JavaDoc.class.isAssignableFrom( eClass ) )
180                 {
181                     //out.print("-c- ");
182
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 JavaDoc.class.isAssignableFrom( eClass ) )
188                 {
189                     //out.print("-b- ");
190
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("-o- ");
198
out.print( entry.getName() );
199                 }
200                 out.println();
201             }
202         }
203         catch ( Exception JavaDoc e )
204         {
205             e.printStackTrace( new PrintStream JavaDoc( out ) );
206         }
207     }
208 }
209
210
Popular Tags