| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.xbean.command; |
| 18 | |
|
| 19 | |
import javax.naming.Context; |
| 20 | |
import javax.naming.InitialContext; |
| 21 | |
import javax.naming.NameClassPair; |
| 22 | |
import javax.naming.NameNotFoundException; |
| 23 | |
import javax.naming.NamingEnumeration; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.InputStream; |
| 26 | |
import java.io.PrintStream; |
| 27 | |
|
| 28 | |
public class Lookup implements Command { |
| 29 | |
|
| 30 | |
private final javax.naming.Context ctx; |
| 31 | |
|
| 32 | |
public Lookup() throws Exception { |
| 33 | 0 | this(new InitialContext()); |
| 34 | 0 | } |
| 35 | |
|
| 36 | 0 | public Lookup(Context ctx) { |
| 37 | 0 | this.ctx = ctx; |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public static void register() { |
| 41 | |
try { |
| 42 | 0 | Lookup cmd = new Lookup(); |
| 43 | 0 | CommandRegistry.register("lookup", cmd); |
| 44 | 0 | } catch (Exception e) { |
| 45 | 0 | } |
| 46 | 0 | } |
| 47 | |
|
| 48 | 0 | private static String PWD = ""; |
| 49 | |
|
| 50 | |
|
| 51 | |
public int main(String[] args, InputStream in, PrintStream out) { |
| 52 | |
try { |
| 53 | 0 | String name = ""; |
| 54 | 0 | if (args == null || args.length == 0) { |
| 55 | 0 | name = PWD; |
| 56 | |
} else { |
| 57 | 0 | name = args[0]; |
| 58 | |
} |
| 59 | 0 | Object obj = null; |
| 60 | |
try { |
| 61 | 0 | obj = ctx.lookup(name); |
| 62 | 0 | } catch (NameNotFoundException e) { |
| 63 | 0 | out.print("lookup: "); |
| 64 | 0 | out.print(name); |
| 65 | 0 | out.println(": No such object or subcontext"); |
| 66 | 0 | return -1; |
| 67 | 0 | } catch (Throwable e) { |
| 68 | 0 | out.print("lookup: error: "); |
| 69 | 0 | e.printStackTrace(new PrintStream(out)); |
| 70 | 0 | return -1; |
| 71 | 0 | } |
| 72 | 0 | if (obj instanceof Context) { |
| 73 | 0 | list(name, in, out); |
| 74 | 0 | return 0; |
| 75 | |
} |
| 76 | |
|
| 77 | 0 | out.println("" + obj); |
| 78 | 0 | return 0; |
| 79 | |
|
| 80 | 0 | } catch (Exception e) { |
| 81 | 0 | e.printStackTrace(new PrintStream(out)); |
| 82 | 0 | return -2; |
| 83 | |
} |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void list(String name, InputStream in, PrintStream out) throws IOException { |
| 87 | |
try { |
| 88 | 0 | NamingEnumeration names = null; |
| 89 | |
try { |
| 90 | 0 | names = ctx.list(name); |
| 91 | 0 | } catch (NameNotFoundException e) { |
| 92 | 0 | out.print("lookup: "); |
| 93 | 0 | out.print(name); |
| 94 | 0 | out.println(": No such object or subcontext"); |
| 95 | 0 | return; |
| 96 | 0 | } catch (Throwable e) { |
| 97 | 0 | out.print("lookup: error: "); |
| 98 | 0 | e.printStackTrace(new PrintStream(out)); |
| 99 | 0 | return; |
| 100 | 0 | } |
| 101 | 0 | if (names == null) { |
| 102 | 0 | return; |
| 103 | |
} |
| 104 | 0 | while (names.hasMore()) { |
| 105 | 0 | NameClassPair entry = (NameClassPair) names.next(); |
| 106 | 0 | out.println(entry.getName()); |
| 107 | 0 | } |
| 108 | 0 | } catch (Exception e) { |
| 109 | 0 | e.printStackTrace(new PrintStream(out)); |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | |
} |