KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > core > ServerBinding


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.core;
18
19 import javax.naming.Binding JavaDoc;
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 /** This is used for both NameClass and Binding.
24  * Lazy eval - so the extra methods in Binding don't affect us.
25  * For most contexts we'll deal getting the class name is as expensive
26  * as getting the object. In addition most operations will only use the name.
27  *
28  */

29 public class ServerBinding extends Binding JavaDoc {
30     public ServerBinding( String JavaDoc name, Context JavaDoc ctx, boolean isRelative ) {
31         super( name, null );
32         this.ctx=ctx;
33         this.name = name;
34         this.isRel=isRelative;
35     }
36
37     public void recycle() {
38     }
39
40     private Context JavaDoc ctx;
41     private Object JavaDoc boundObj;
42     private String JavaDoc name;
43     private boolean isRel = true;
44     private String JavaDoc className;
45
46     private void lookup() {
47         try {
48             boundObj=ctx.lookup(name);
49         } catch( NamingException JavaDoc ex ) {
50             ex.printStackTrace();
51         }
52     }
53
54     public String JavaDoc getClassName() {
55         if( className!=null ) return className;
56         if( boundObj==null ) lookup();
57
58         if( boundObj!=null )
59             className=boundObj.getClass().getName();
60         return className;
61     }
62
63     public String JavaDoc getName() {
64         return name;
65     }
66
67     public void setName(String JavaDoc name) {
68         this.name = name;
69     }
70
71     public void setClassName(String JavaDoc name) {
72         this.className = name;
73     }
74
75     public boolean isRelative() {
76         return isRel;
77     }
78
79     public void setRelative(boolean r) {
80         isRel = r;
81     }
82
83     /**
84      * Generates the string representation of this name/class pair.
85      * The string representation consists of the name and class name separated
86      * by a colon (':').
87      * The contents of this string is useful
88      * for debugging and is not meant to be interpreted programmatically.
89      *
90      * @return The string representation of this name/class pair.
91      */

92     public String JavaDoc toString() {
93         return (isRelative() ? "" : "(not relative)") + getName() + ": " +
94                 getClassName();
95     }
96
97
98     public Object JavaDoc getObject() {
99         if( boundObj!=null )
100             return boundObj;
101         lookup();
102         return boundObj;
103     }
104
105     public void setObject(Object JavaDoc obj) {
106         boundObj = obj;
107     }
108 }
109
Popular Tags