KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > client > NamingContextImpl


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, Frederic Maistre
23  */

24 package fr.dyade.aaa.jndi2.client;
25
26 import java.io.*;
27 import java.net.*;
28 import java.util.*;
29 import javax.naming.*;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 import fr.dyade.aaa.jndi2.msg.*;
35
36 public class NamingContextImpl implements Context {
37
38   private NamingConnection connection;
39
40   private CompositeName contextPath;
41
42   public NamingContextImpl(NamingConnection connection,
43                            CompositeName contextPath) throws NamingException {
44     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
45       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.<init>(" +
46                        connection + ',' + contextPath + ')');
47     this.connection = connection;
48     this.contextPath = contextPath;
49   }
50
51   /** Empty constructor, called by the subclass. */
52   protected NamingContextImpl()
53   {
54     contextPath = new CompositeName();
55   }
56
57   public void bind(Name name, Object JavaDoc obj) throws NamingException {
58     bind(name.toString(), obj);
59   }
60
61   public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
62     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
63       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.bind(" +
64                        name + ',' + obj + ')');
65     if (obj instanceof Referenceable) {
66       obj = ((Referenceable)obj).getReference();
67     }
68     JndiReply reply = connection.invoke(
69       new BindRequest(merge(contextPath, name), obj));
70     if (reply instanceof JndiError) {
71       NamingException exc = ((JndiError)reply).getException();
72       exc.fillInStackTrace();
73       throw exc;
74     }
75   }
76
77   public void rebind(Name name, Object JavaDoc obj) throws NamingException {
78     rebind(name.toString(), obj);
79   }
80
81   public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
82     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
83       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.rebind(" +
84                        name + ',' + obj + ')');
85     if (obj instanceof Referenceable) {
86       obj = ((Referenceable)obj).getReference();
87     }
88     JndiReply reply = connection.invoke(
89       new BindRequest(merge(contextPath, name), obj, true));
90     if (reply instanceof JndiError) {
91       NamingException exc = ((JndiError)reply).getException();
92       exc.fillInStackTrace();
93       throw exc;
94     }
95   }
96
97   public Object JavaDoc lookup(Name name) throws NamingException {
98     return lookup(name.toString());
99   }
100
101   public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
102     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
103       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.lookup(" +
104                        name + ')');
105     CompositeName path = merge(contextPath, name);
106     JndiReply reply = connection.invoke(new LookupRequest(path));
107     if (reply instanceof JndiError) {
108       NamingException exc = ((JndiError)reply).getException();
109       exc.fillInStackTrace();
110       throw exc;
111     } else if (reply instanceof LookupReply) {
112       Object JavaDoc obj = ((LookupReply)reply).getObject();
113       return obj;
114     } else {
115       return new NamingContextImpl(
116         connection.cloneConnection(), path);
117     }
118   }
119
120   public void unbind(Name name) throws NamingException {
121     unbind(name.toString());
122   }
123
124   public void unbind(String JavaDoc name) throws NamingException {
125     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
126       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.unbind(" +
127                        name + ')');
128     JndiReply reply = connection.invoke(
129       new UnbindRequest(merge(contextPath, name)));
130     if (reply instanceof JndiError) {
131       NamingException exc = ((JndiError)reply).getException();
132       exc.fillInStackTrace();
133       throw exc;
134     }
135   }
136
137   public void close() throws NamingException {}
138
139   protected void finalize() throws Throwable JavaDoc {
140     close();
141   }
142
143   public Hashtable getEnvironment() throws NamingException {
144     return connection.getEnvironment();
145   }
146
147   public NamingEnumeration list(Name name) throws NamingException {
148     return list(name.toString());
149   }
150   public NamingEnumeration list(String JavaDoc name) throws NamingException {
151     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
152       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.list(" +
153                        name + ')');
154     JndiReply reply = connection.invoke(
155       new ListRequest(merge(contextPath, name)));
156     if (reply instanceof JndiError) {
157       NamingException exc = ((JndiError)reply).getException();
158       exc.fillInStackTrace();
159       throw exc;
160     } else {
161       return ((ListReply)reply).getEnumeration();
162     }
163   }
164
165   public NamingEnumeration listBindings(Name name) throws NamingException {
166     return listBindings(name.toString());
167   }
168
169   public NamingEnumeration listBindings(String JavaDoc name) throws NamingException {
170     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
171       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.listBindings(" +
172                        name + ')');
173     CompositeName queryPath = merge(contextPath, name);
174     JndiReply reply = connection.invoke(
175       new ListBindingsRequest(queryPath));
176     if (reply instanceof JndiError) {
177       NamingException exc = ((JndiError)reply).getException();
178       exc.fillInStackTrace();
179       throw exc;
180     } else {
181       ListBindingsReply lbr = (ListBindingsReply)reply;
182
183       // 1- resolve contexts
184
Binding[] bindings = lbr.getContexts();
185       for (int i = 0; i < bindings.length; i++) {
186         CompositeName subCtxPath = (CompositeName)queryPath.clone();
187         subCtxPath.add(bindings[i].getName());
188         bindings[i].setObject(new NamingContextImpl(
189           connection.cloneConnection(), subCtxPath));
190       }
191
192       // 2- resolve references
193
lbr.resolveReferences();
194       return lbr.getEnumeration();
195     }
196   }
197
198   public Context createSubcontext(Name name) throws NamingException {
199     return createSubcontext(name.toString());
200   }
201
202   public Context createSubcontext(String JavaDoc name) throws NamingException {
203     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
204       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.createSubcontext(" +
205                        name + ')');
206     CompositeName path = merge(contextPath, name);
207     JndiReply reply = connection.invoke(
208       new CreateSubcontextRequest(path));
209     if (reply instanceof JndiError) {
210       NamingException exc = ((JndiError)reply).getException();
211       exc.fillInStackTrace();
212       throw exc;
213     } else {
214       return new NamingContextImpl(
215         connection.cloneConnection(), path);
216     }
217   }
218
219   public void destroySubcontext(Name name) throws NamingException {
220     destroySubcontext(name.toString());
221   }
222
223   public void destroySubcontext(String JavaDoc name) throws NamingException {
224     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
225       Trace.logger.log(BasicLevel.DEBUG, "NamingContextImpl.destroySubcontext(" +
226                        name + ')');
227     JndiReply reply = connection.invoke(
228       new DestroySubcontextRequest(merge(contextPath, name)));
229     if (reply instanceof JndiError) {
230       NamingException exc = ((JndiError)reply).getException();
231       exc.fillInStackTrace();
232       throw exc;
233     }
234   }
235
236   public String JavaDoc getNameInNamespace() throws NamingException {
237     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
238       Trace.logger.log(BasicLevel.DEBUG,
239                        "NamingContextImpl.getNameInNamespace()");
240     return contextPath.toString();
241   }
242
243   public Object JavaDoc addToEnvironment(Name propName, Object JavaDoc propVal) throws NamingException {
244     throw(new NamingException("Not yet available"));
245   }
246   public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException {
247     throw(new NamingException("Not yet available"));
248   }
249   public Name composeName(Name name, Name prefix) throws NamingException {
250     throw(new NamingException("Not yet available"));
251   }
252   public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException {
253     throw(new NamingException("Not yet available"));
254   }
255   public NameParser getNameParser(Name name) throws NamingException {
256     throw(new NamingException("Not yet available"));
257   }
258   public NameParser getNameParser(String JavaDoc name) throws NamingException {
259     throw(new NamingException("Not yet available"));
260   }
261   public Object JavaDoc lookupLink(Name name) throws NamingException {
262     throw(new NamingException("Not yet available"));
263   }
264   public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException {
265     throw(new NamingException("Not yet available"));
266   }
267   public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException {
268     throw(new NamingException("Not yet available"));
269   }
270   public void rename(Name oldName, Name newName) throws NamingException {
271     throw(new NamingException("Not yet available"));
272   }
273   public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException {
274     throw(new NamingException("Not yet available"));
275   }
276
277   private static CompositeName merge(CompositeName path, String JavaDoc name) throws NamingException {
278     if (name == null) throw new InvalidNameException();
279     CompositeName res = new CompositeName(name);
280     trim(res);
281     for (int i = path.size() - 1; i > -1 ; i--) {
282       res.add(0, path.get(i));
283     }
284     return res;
285   }
286
287   private static CompositeName trim(CompositeName name) throws NamingException {
288     int i = 0;
289     while (i < name.size()) {
290       String JavaDoc s = (String JavaDoc)name.remove(i);
291       s = s.trim();
292       if (s.length() > 0) {
293         name.add(i, s);
294         i++;
295       }
296     }
297     return name;
298   }
299
300   public String JavaDoc toString() {
301     return '(' + super.toString() +
302       ",connection=" + connection +
303       ",contextPath=" + contextPath + ')';
304   }
305 }
306
Popular Tags