Coverage Report - org.apache.xbean.command.Lookup
 
Classes in this File Line Coverage Branch Coverage Complexity
Lookup
0%
0/57
0%
0/10
4.6
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
     // execute jndi lookups
 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  
             // TODO:1: Output the different data types differently
 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  
 }