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: */package org.apache.openejb.server.telnet;
017:
018: import java.io.DataInputStream;
019: import java.io.IOException;
020: import java.io.PrintStream;
021:
022: import javax.ejb.EJBHome;
023: import javax.naming.Context;
024: import javax.naming.NameClassPair;
025: import javax.naming.NameNotFoundException;
026: import javax.naming.NamingEnumeration;
027:
028: import org.apache.openejb.ClassLoaderUtil;
029: import org.apache.openejb.loader.SystemInstance;
030: import org.apache.openejb.spi.ContainerSystem;
031: import org.apache.openejb.core.ivm.naming.IvmContext;
032:
033: public class Lookup extends Command {
034:
035: javax.naming.Context ctx;
036:
037: {
038: ContainerSystem containerSystem = SystemInstance.get()
039: .getComponent(ContainerSystem.class);
040: ctx = containerSystem.getJNDIContext();
041: }
042:
043: public static void register() {
044: Lookup cmd = new Lookup();
045: Command.register("lookup", cmd);
046:
047: }
048:
049: static String PWD = "";
050:
051: public void exec(Arguments args, DataInputStream in, PrintStream out)
052: throws IOException {
053: try {
054: String name = "";
055: if (args == null || args.count() == 0) {
056: name = PWD;
057: } else {
058: name = args.get();
059: }
060:
061: Object obj = null;
062: try {
063: obj = ctx.lookup(name);
064: } catch (NameNotFoundException e) {
065: out.print("lookup: ");
066: out.print(name);
067: out.println(": No such object or subcontext");
068: return;
069: } catch (Throwable e) {
070: out.print("lookup: error: ");
071: e.printStackTrace(new PrintStream(out));
072: return;
073: }
074:
075: if (obj instanceof Context) {
076: list(name, in, out);
077: return;
078: }
079:
080: out.println("" + obj);
081: } catch (Exception e) {
082: e.printStackTrace(new PrintStream(out));
083: }
084: }
085:
086: public void list(String name, DataInputStream in, PrintStream out)
087: throws IOException {
088: try {
089: NamingEnumeration enumeration = null;
090: try {
091:
092: enumeration = ctx.list(name);
093: } catch (NameNotFoundException e) {
094: out.print("lookup: ");
095: out.print(name);
096: out.println(": No such object or subcontext");
097: return;
098: } catch (Throwable e) {
099: out.print("lookup: error: ");
100: e.printStackTrace(new PrintStream(out));
101: return;
102: }
103:
104: if (enumeration == null) {
105: return;
106: }
107:
108: while (enumeration.hasMore()) {
109:
110: NameClassPair entry = (NameClassPair) enumeration
111: .next();
112: String eName = entry.getName();
113: Class eClass = null;
114:
115: if (IvmContext.class.getName().equals(
116: entry.getClassName())) {
117: eClass = IvmContext.class;
118: } else {
119: try {
120: ClassLoader cl = ClassLoaderUtil
121: .getContextClassLoader();
122: eClass = Class.forName(entry.getClassName(),
123: true, cl);
124: } catch (Throwable t) {
125: eClass = java.lang.Object.class;
126: }
127: }
128:
129: if (Context.class.isAssignableFrom(eClass)) {
130:
131: out.print(TextConsole.TTY_Bright);
132: out.print(TextConsole.TTY_FG_Blue);
133: out.print(entry.getName());
134: out.print(TextConsole.TTY_Reset);
135: } else if (EJBHome.class.isAssignableFrom(eClass)) {
136:
137: out.print(TextConsole.TTY_Bright);
138: out.print(TextConsole.TTY_FG_Green);
139: out.print(entry.getName());
140: out.print(TextConsole.TTY_Reset);
141: } else {
142:
143: out.print(entry.getName());
144: }
145: out.println();
146: }
147: } catch (Exception e) {
148: e.printStackTrace(new PrintStream(out));
149: }
150: }
151: }
|