KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > impl > NamingContext


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Sofiane Chibani
22  * Contributor(s): David Feliot, Nicolas Tachker
23  */

24 package fr.dyade.aaa.jndi2.impl;
25
26 import fr.dyade.aaa.util.*;
27 import java.util.*;
28 import java.io.*;
29 import javax.naming.*;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 public class NamingContext implements Serializable, Cloneable JavaDoc {
35
36   private NamingContextId id;
37
38   private Object JavaDoc ownerId;
39
40   private Vector records;
41
42   public NamingContext(NamingContextId id,
43                        Object JavaDoc ownerId) {
44     this.id = id;
45     this.ownerId = ownerId;
46     records = new Vector();
47   }
48
49   public final NamingContextId getId() {
50     return id;
51   }
52
53   public final Object JavaDoc getOwnerId() {
54     return ownerId;
55   }
56
57   public void setOwnerId(Object JavaDoc ownerId) {
58     this.ownerId = ownerId;
59   }
60
61   public Record getRecord(String JavaDoc name) {
62     for (int i = 0; i < records.size(); i++) {
63       Record r = (Record)records.elementAt(i);
64       if (r.getName().equals(name)) return r;
65     }
66     return null;
67   }
68
69   public void addRecord(Record record) {
70     records.addElement(record);
71   }
72
73   public boolean removeRecord(String JavaDoc name) {
74     for (int i = 0; i < records.size(); i++) {
75       Record r = (Record)records.elementAt(i);
76       if (r.getName().equals(name)) {
77         records.removeElementAt(i);
78         return true;
79       }
80     }
81     return false;
82   }
83
84   public int size() {
85     return records.size();
86   }
87
88   public NameClassPair[] getNameClassPairs() {
89     NameClassPair[] res = new NameClassPair[records.size()];
90     for (int i = 0; i < records.size(); i++) {
91       Record r = (Record)records.elementAt(i);
92       if (r instanceof ObjectRecord) {
93         ObjectRecord or = (ObjectRecord)r;
94         res[i] = new NameClassPair(
95           or.getName(),
96           getClassName(or.getObject()),
97           true);
98       } else if (r instanceof ContextRecord) {
99         res[i] = new NameClassPair(
100           r.getName(),
101           Context.class.getName(),
102           true);
103       }
104     }
105     return res;
106   }
107
108   public Binding[] getBindings() {
109     Binding[] res = new Binding[records.size()];
110     for (int i = 0; i < records.size(); i++) {
111       Record r = (Record)records.elementAt(i);
112       if (r instanceof ObjectRecord) {
113         ObjectRecord or = (ObjectRecord)r;
114         res[i] = new Binding(
115           or.getName(),
116           getClassName(or.getObject()),
117           or.getObject(),
118           true);
119       } else if (r instanceof ContextRecord) {
120         res[i] = new Binding(
121           r.getName(),
122           Context.class.getName(),
123           null,
124           true);
125       }
126     }
127     return res;
128   }
129
130   private static String JavaDoc getClassName(Object JavaDoc obj) {
131     if (obj instanceof Reference) {
132       Reference ref = (Reference)obj;
133       return ref.getClassName();
134     } else {
135       return obj.getClass().getName();
136     }
137   }
138
139   public Object JavaDoc clone() {
140     try {
141       NamingContext clone =
142         (NamingContext)super.clone();
143       clone.records = (Vector)records.clone();
144       // other attributes are cloned
145
return clone;
146     } catch (CloneNotSupportedException JavaDoc exc) {
147       return null;
148     }
149   }
150
151   public String JavaDoc toString() {
152     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
153     buf.append('(' + super.toString());
154     buf.append(",id=" + id);
155     buf.append(",ownerId=" + ownerId);
156     buf.append(",records=");
157     Strings.toString(buf, records);
158     buf.append(')');
159     return buf.toString();
160   }
161 }
162
Popular Tags