protected Object lookup()

in xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java [107:169]


    protected Object lookup(String stringName, Name parsedName) throws NamingException {
        if (stringName == null && parsedName == null) {
            throw new IllegalArgumentException("Both stringName and parsedName are null");
        }
        if (stringName == null) stringName = parsedName.toString();

        // try to look up the name directly (this is the fastest path)
        Object directLookup = getDeepBinding(stringName);
        if (directLookup != null) {
            return ContextUtil.resolve(directLookup, stringName, parsedName, this);
        }

        // if the parsed name has no parts, they are asking for the current context
        if (parsedName == null) parsedName = getNameParser().parse(stringName);
        if (parsedName.isEmpty()) {
            return this;
        }

        // we didn't find an entry, pop the first element off the parsed name and attempt to
        // get a context from the bindings and delegate to that context
        Object localValue;
        String firstNameElement = parsedName.get(0);
        if (firstNameElement.length() == 0) {
            // the element is null... this is normally caused by looking up with a trailing '/' character
            localValue = this;
        } else {
            localValue = getBinding(firstNameElement);
        }

        if (localValue != null) {

            // if the name only had one part, we've looked up everything
            if (parsedName.size() == 1) {
                localValue = ContextUtil.resolve(localValue, stringName, parsedName, this);
                return localValue;
            }

            // if we have a link ref, follow it
            if (localValue instanceof LinkRef) {
                LinkRef linkRef = (LinkRef) localValue;
                localValue = lookup(linkRef.getLinkName());
            }

            // we have more to lookup so we better have a context object
            if (!(localValue instanceof Context)) {
                throw new NameNotFoundException(stringName);
            }

            // delegate to the sub-context
            return ((Context) localValue).lookup(parsedName.getSuffix(1));
        }

        // if we didn't find an entry, it may be an absolute name
        Object value = faultLookup(stringName, parsedName);
        if (value != null) {
            return value;
        }
        if (parsedName.size() > 1) {
            throw new NotContextException(stringName);
        } else {
            throw new NameNotFoundException(stringName);
        }
    }