KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > jndi > NamingContextImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: NamingContextImpl.java 7:13:54 PM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry.jndi;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import javax.naming.Binding JavaDoc;
28 import javax.naming.CompositeName JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.NameClassPair JavaDoc;
32 import javax.naming.NameParser JavaDoc;
33 import javax.naming.NamingEnumeration JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.objectweb.petals.kernel.registry.msg.request.BindRequest;
37 import org.objectweb.petals.kernel.registry.msg.request.CreateSubcontextRequest;
38 import org.objectweb.petals.kernel.registry.msg.request.DestroySubcontextRequest;
39 import org.objectweb.petals.kernel.registry.msg.request.ListBindingsRequest;
40 import org.objectweb.petals.kernel.registry.msg.request.ListRequest;
41 import org.objectweb.petals.kernel.registry.msg.request.LookupLinkRequest;
42 import org.objectweb.petals.kernel.registry.msg.request.LookupRequest;
43 import org.objectweb.petals.kernel.registry.msg.request.RebindRequest;
44 import org.objectweb.petals.kernel.registry.msg.request.RenameRequest;
45 import org.objectweb.petals.kernel.registry.msg.request.UnbindRequest;
46
47 /**
48  * JNDI context
49  *
50  * @author ddesjardins - eBMWebsourcing
51  */

52 public class NamingContextImpl implements Context JavaDoc, Serializable JavaDoc {
53
54     private static final long serialVersionUID = 1L;
55
56     /**
57      * Name of the context
58      */

59     protected String JavaDoc contextName;
60
61     /**
62      * Context environment
63      */

64     protected Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env;
65
66     /**
67      * Connection to the RegistryServer
68      */

69     protected JNDIConnection jndiConnection;
70
71     public NamingContextImpl(Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env,
72         JNDIConnection jndiConnection, String JavaDoc contextName) {
73         super();
74         this.env = env;
75         this.jndiConnection = jndiConnection;
76         this.contextName = contextName;
77     }
78
79     /**
80      * @see Context#addToEnvironment(String, Object)
81      */

82     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
83         throws NamingException JavaDoc {
84         Object JavaDoc out = env.get(propName);
85         env.put(propName, propVal);
86         return out;
87     }
88
89     /**
90      * @see Context#bind(Name, Object)
91      */

92     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
93         bind(name.toString(), obj);
94     }
95
96     /**
97      * @see Context#bind(String, Object)
98      */

99     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
100         jndiConnection.send(new BindRequest(contextName, name, obj, 0, 0));
101     }
102
103     /**
104      * @see Context#close()
105      */

106     public void close() throws NamingException JavaDoc {
107         env = null;
108         jndiConnection = null;
109         contextName = null;
110     }
111
112     /**
113      * @see Context#composeName(Name, Name)
114      */

115     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
116         if (name == null) {
117             throw new NamingException JavaDoc("Name must be non null");
118         }
119         if (prefix == null) {
120             throw new NamingException JavaDoc("Prefix must be non null");
121         }
122         return new CompositeName JavaDoc(prefix.toString() + name.toString());
123     }
124
125     /**
126      * @see Context#composeName(String, String)
127      */

128     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
129         throws NamingException JavaDoc {
130         if (name == null) {
131             throw new NamingException JavaDoc("Name must be non null");
132         }
133         if (prefix == null) {
134             throw new NamingException JavaDoc("Prefix must be non null");
135         }
136         return prefix + name;
137     }
138
139     /**
140      * @see Context#createSubcontext(Name)
141      */

142     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
143         return createSubcontext(name.toString());
144     }
145
146     /**
147      * @see Context#createSubcontext(String)
148      */

149     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
150         Object JavaDoc newName = jndiConnection.send(new CreateSubcontextRequest(
151             contextName, name, 0, 0));
152         NamingContextImpl contextImpl = new NamingContextImpl(env,
153             jndiConnection, (String JavaDoc) newName);
154         return contextImpl;
155     }
156
157     /**
158      * @see Context#destroySubcontext(Name)
159      */

160     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
161         destroySubcontext(name.toString());
162     }
163
164     /**
165      * @see Context#destroySubcontext(String)
166      */

167     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
168         jndiConnection.send(new DestroySubcontextRequest(contextName, name, 0,
169             0));
170     }
171
172     /**
173      * @see Context#getEnvironment()
174      */

175     public Hashtable JavaDoc<?, ?> getEnvironment() throws NamingException JavaDoc {
176         return env;
177     }
178
179     /**
180      * @see Context#getNameInNamespace()
181      */

182     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
183         return contextName;
184     }
185
186     /**
187      * @see Context#getNameParser(Name)
188      */

189     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
190         return new JNDINameParser();
191     }
192
193     /**
194      * @see Context#getNameParser(String)
195      */

196     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
197         return new JNDINameParser();
198     }
199
200     /**
201      * @see Context#list(Name)
202      */

203     public NamingEnumeration JavaDoc<NameClassPair JavaDoc> list(Name JavaDoc name)
204         throws NamingException JavaDoc {
205         return list(name.toString());
206     }
207
208     /**
209      * @see Context#list(String)
210      */

211     public NamingEnumeration JavaDoc<NameClassPair JavaDoc> list(String JavaDoc name)
212         throws NamingException JavaDoc {
213         NameClassPair JavaDoc[] names = (NameClassPair JavaDoc[]) jndiConnection
214             .send(new ListRequest(contextName, name, 0, 0));
215         return new NamingEnumerationImpl<NameClassPair JavaDoc>(names);
216     }
217
218     /**
219      * @see Context#listBindings(Name)
220      */

221     public NamingEnumeration JavaDoc<Binding JavaDoc> listBindings(Name JavaDoc name)
222         throws NamingException JavaDoc {
223         return listBindings(name.toString());
224     }
225
226     /**
227      * @see Context#listBindings(String)
228      */

229     public NamingEnumeration JavaDoc<Binding JavaDoc> listBindings(String JavaDoc name)
230         throws NamingException JavaDoc {
231         Binding JavaDoc[] bindings = (Binding JavaDoc[]) jndiConnection
232             .send(new ListBindingsRequest(contextName, name, 0, 0));
233         return new NamingEnumerationImpl<Binding JavaDoc>(bindings);
234     }
235
236     /**
237      * @see Context#lookup(Name)
238      */

239     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
240         return lookup(name.toString());
241     }
242
243     /**
244      * @see Context#lookup(String)
245      */

246     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
247         return jndiConnection.send(new LookupRequest(contextName, name, 0, 0));
248     }
249
250     /**
251      * @see Context#lookupLink(Name)
252      */

253     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
254         return lookupLink(name.toString());
255     }
256
257     /**
258      * @see Context#lookupLink(String)
259      */

260     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
261         return jndiConnection.send(new LookupLinkRequest(contextName, name, 0,
262             0));
263     }
264
265     /**
266      * @see Context#rebind(Name, Object)
267      */

268     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
269         rebind(name.toString(), obj);
270     }
271
272     /**
273      * @see Context#rebind(String, Object)
274      */

275     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
276         jndiConnection.send(new RebindRequest(contextName, name, obj, 0, 0));
277     }
278
279     /**
280      * @see Context#removeFromEnvironment(String)
281      */

282     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
283         Object JavaDoc out = env.get(propName);
284         if (env.containsKey(propName)) {
285             env.remove(propName);
286         }
287         return out;
288     }
289
290     /**
291      * @see Context#rename(Name, Name)
292      */

293     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
294         rename(oldName.toString(), newName.toString());
295     }
296
297     /**
298      * @see Context#rename(String, String)
299      */

300     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
301         jndiConnection.send(new RenameRequest(contextName, oldName, newName, 0,
302             0));
303     }
304
305     /**
306      * @see Context#unbind(Name)
307      */

308     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
309         unbind(name.toString());
310     }
311
312     /**
313      * @see Context#unbind(String)
314      */

315     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
316         jndiConnection.send(new UnbindRequest(contextName, name, 0, 0));
317     }
318
319 }
320
Popular Tags