001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.xbean.command;
018
019import javax.naming.Context;
020import javax.naming.InitialContext;
021import javax.naming.NameClassPair;
022import javax.naming.NameNotFoundException;
023import javax.naming.NamingEnumeration;
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.PrintStream;
027
028public class Lookup implements Command {
029
030    private final javax.naming.Context ctx;
031
032    public Lookup() throws Exception {
033        this(new InitialContext());
034    }
035
036    public Lookup(Context ctx) {
037        this.ctx = ctx;
038    }
039
040    public static void register() {
041        try {
042            Lookup cmd = new Lookup();
043            CommandRegistry.register("lookup", cmd);
044        } catch (Exception e) {
045        }
046    }
047
048    private static String PWD = "";
049
050    // execute jndi lookups
051    public int main(String[] args, InputStream in, PrintStream out) {
052        try {
053            String name = "";
054            if (args == null || args.length == 0) {
055                name = PWD;
056            } else {
057                name = args[0];
058            }
059            Object obj = null;
060            try {
061                obj = ctx.lookup(name);
062            } catch (NameNotFoundException e) {
063                out.print("lookup: ");
064                out.print(name);
065                out.println(": No such object or subcontext");
066                return -1;
067            } catch (Throwable e) {
068                out.print("lookup: error: ");
069                e.printStackTrace(new PrintStream(out));
070                return -1;
071            }
072            if (obj instanceof Context) {
073                list(name, in, out);
074                return 0;
075            }
076            // TODO:1: Output the different data types differently
077            out.println("" + obj);
078            return 0;
079            
080        } catch (Exception e) {
081            e.printStackTrace(new PrintStream(out));
082            return -2;
083        }
084    }
085
086    public void list(String name, InputStream in, PrintStream out) throws IOException {
087        try {
088            NamingEnumeration names = null;
089            try {
090                names = ctx.list(name);
091            } catch (NameNotFoundException e) {
092                out.print("lookup: ");
093                out.print(name);
094                out.println(": No such object or subcontext");
095                return;
096            } catch (Throwable e) {
097                out.print("lookup: error: ");
098                e.printStackTrace(new PrintStream(out));
099                return;
100            }
101            if (names == null) {
102                return;
103            }
104            while (names.hasMore()) {
105                NameClassPair entry = (NameClassPair) names.next();
106                out.println(entry.getName());
107            }
108        } catch (Exception e) {
109            e.printStackTrace(new PrintStream(out));
110        }
111    }
112}