001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.core;
018:
019: import javax.naming.Binding;
020: import javax.naming.Context;
021: import javax.naming.NamingException;
022:
023: /** This is used for both NameClass and Binding.
024: * Lazy eval - so the extra methods in Binding don't affect us.
025: * For most contexts we'll deal getting the class name is as expensive
026: * as getting the object. In addition most operations will only use the name.
027: *
028: */
029: public class ServerBinding extends Binding {
030: public ServerBinding(String name, Context ctx, boolean isRelative) {
031: super (name, null);
032: this .ctx = ctx;
033: this .name = name;
034: this .isRel = isRelative;
035: }
036:
037: public void recycle() {
038: }
039:
040: private Context ctx;
041: private Object boundObj;
042: private String name;
043: private boolean isRel = true;
044: private String className;
045:
046: private void lookup() {
047: try {
048: boundObj = ctx.lookup(name);
049: } catch (NamingException ex) {
050: ex.printStackTrace();
051: }
052: }
053:
054: public String getClassName() {
055: if (className != null)
056: return className;
057: if (boundObj == null)
058: lookup();
059:
060: if (boundObj != null)
061: className = boundObj.getClass().getName();
062: return className;
063: }
064:
065: public String getName() {
066: return name;
067: }
068:
069: public void setName(String name) {
070: this .name = name;
071: }
072:
073: public void setClassName(String name) {
074: this .className = name;
075: }
076:
077: public boolean isRelative() {
078: return isRel;
079: }
080:
081: public void setRelative(boolean r) {
082: isRel = r;
083: }
084:
085: /**
086: * Generates the string representation of this name/class pair.
087: * The string representation consists of the name and class name separated
088: * by a colon (':').
089: * The contents of this string is useful
090: * for debugging and is not meant to be interpreted programmatically.
091: *
092: * @return The string representation of this name/class pair.
093: */
094: public String toString() {
095: return (isRelative() ? "" : "(not relative)") + getName()
096: + ": " + getClassName();
097: }
098:
099: public Object getObject() {
100: if (boundObj != null)
101: return boundObj;
102: lookup();
103: return boundObj;
104: }
105:
106: public void setObject(Object obj) {
107: boundObj = obj;
108: }
109: }
|