KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > PathJndiContext


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.naming;
30
31 import com.caucho.util.L10N;
32 import com.caucho.vfs.Path;
33
34 import javax.naming.*;
35 import java.io.IOException JavaDoc;
36 import java.util.Hashtable JavaDoc;
37
38 public class PathJndiContext implements Context {
39   private static L10N L = new L10N(PathJndiContext.class);
40
41   private PathJndiContext _root;
42   private Path _path;
43
44   public PathJndiContext(Path path)
45   {
46     _root = this;
47     _path = path;
48   }
49
50   PathJndiContext(Path path, PathJndiContext root)
51   {
52     _root = root;
53     _path = path;
54     if (root == null)
55       _root = root;
56   }
57
58   public Path getPath()
59   {
60     return _path;
61   }
62
63   public Object JavaDoc lookup(String JavaDoc name)
64     throws NamingException
65   {
66     if (name == null || name.equals(""))
67       return new PathJndiContext(_path.lookup(null), _root);
68
69     Path subpath = _path.lookup(name);
70
71     if (subpath == null) {
72       throw new NamingException(L.l("bad path {0}", name));
73     }
74       
75     if (subpath.isDirectory())
76       return new PathJndiContext(subpath, _root);
77
78     else if (subpath.isObject()) {
79       try {
80         return subpath.getValue();
81       } catch (Exception JavaDoc e) {
82         throw new NamingException(e.toString());
83       }
84     }
85
86     else if (! subpath.exists())
87       return null;
88     
89     else
90       throw new NamingException(L.l("lookup can't handle files"));
91   }
92   
93   public Object JavaDoc lookup(Name name)
94     throws NamingException
95   {
96     return lookup(name.toString());
97   }
98
99   public void bind(String JavaDoc name, Object JavaDoc obj)
100     throws NamingException
101   {
102     Path subpath = _path.lookup(name);
103
104     Path parent = subpath.getParent();
105     if (! parent.exists()) {
106       try {
107         parent.mkdirs();
108       } catch (IOException JavaDoc e) {
109         throw new NamingException(e.toString());
110       }
111     }
112
113     if (! parent.isDirectory())
114       throw new NamingException(L.l("bind expects directory for `{0}'",
115                                     subpath.getParent()));
116     else if (subpath.exists())
117       throw new NameAlreadyBoundException(L.l("`{0}' already has a binding",
118                                               subpath));
119
120       try {
121       subpath.setValue(obj);
122     } catch (Exception JavaDoc e) {
123       throw new NamingException(e.toString());
124     }
125   }
126   
127   public void bind(Name name, Object JavaDoc obj)
128     throws NamingException
129   {
130     bind(name.toString(), obj);
131   }
132
133   public void rebind(String JavaDoc name, Object JavaDoc obj)
134     throws NamingException
135   {
136     Path subpath = _path.lookup(name);
137
138     try {
139       Path parent = subpath.getParent();
140       if (! parent.exists())
141         parent.mkdirs();
142       subpath.setValue(obj);
143     } catch (Exception JavaDoc e) {
144       throw new NamingException(e.toString());
145     }
146   }
147   
148   public void rebind(Name name, Object JavaDoc obj)
149     throws NamingException
150   {
151     rebind(name.toString(), obj);
152   }
153
154   public void unbind(String JavaDoc name)
155     throws NamingException
156   {
157     Path subpath = _path.lookup(name);
158
159     try {
160       subpath.remove();
161     } catch (IOException JavaDoc e) {
162       throw new NamingException(L.l("can't remove `{0}'"));
163     }
164   }
165   
166   public void unbind(Name name)
167     throws NamingException
168   {
169     unbind(name.toString());
170   }
171
172   public void rename(String JavaDoc oldName, String JavaDoc newName)
173     throws NamingException
174   {
175     Object JavaDoc obj = lookup(oldName);
176     bind(newName, obj);
177     unbind(oldName);
178   }
179
180   public void rename(Name oldName, Name newName)
181     throws NamingException
182   {
183     Object JavaDoc obj = lookup(oldName);
184     bind(newName, obj);
185     unbind(oldName);
186   }
187
188   public NamingEnumeration list(String JavaDoc name)
189     throws NamingException
190   {
191     return null;
192   }
193
194   public NamingEnumeration list(Name name)
195     throws NamingException
196   {
197     return list(name.toString());
198   }
199
200   public NamingEnumeration listBindings(String JavaDoc name)
201     throws NamingException
202   {
203     return null;
204   }
205
206   public NamingEnumeration listBindings(Name name)
207     throws NamingException
208   {
209     return list(name.toString());
210   }
211
212   public void destroySubcontext(String JavaDoc name)
213     throws NamingException
214   {
215     Path subpath = _path.lookup(name);
216
217     if (! subpath.exists())
218       throw new NameNotFoundException(name);
219     if (! subpath.isDirectory())
220       throw new NotContextException(name);
221
222     try {
223       subpath.remove();
224     } catch (IOException JavaDoc e) {
225       throw new ContextNotEmptyException(name);
226     }
227   }
228
229   public void destroySubcontext(Name name)
230     throws NamingException
231   {
232     destroySubcontext(name.toString());
233   }
234
235   public Context createSubcontext(String JavaDoc name)
236     throws NamingException
237   {
238     Path subpath = _path.lookup(name);
239
240     if (! subpath.getParent().isDirectory())
241       throw new NamingException(L.l("parent of `{0}' must be directory",
242                                     name));
243
244     try {
245       subpath.mkdir();
246     } catch (IOException JavaDoc e) {
247       throw new ContextNotEmptyException(name);
248     }
249
250     return new PathJndiContext(subpath, _root);
251   }
252
253   public Context createSubcontext(Name name)
254     throws NamingException
255   {
256     return createSubcontext(name.toString());
257   }
258
259   public Object JavaDoc lookupLink(String JavaDoc name)
260     throws NamingException
261   {
262     throw new NamingException(L.l("links not supported"));
263   }
264
265   public Object JavaDoc lookupLink(Name name)
266     throws NamingException
267   {
268     return lookupLink(name.toString());
269   }
270
271   public NameParser getNameParser(String JavaDoc name)
272     throws NamingException
273   {
274     return new PathNameParser();
275   }
276
277   public NameParser getNameParser(Name name)
278     throws NamingException
279   {
280     return getNameParser(name.toString());
281   }
282
283   public String JavaDoc composeName(String JavaDoc suffix, String JavaDoc prefix)
284     throws NamingException
285   {
286     return prefix + "/" + suffix;
287   }
288
289   public Name composeName(Name suffix, Name prefix)
290     throws NamingException
291   {
292     return null;
293   }
294
295   public String JavaDoc getNameInNamespace()
296     throws NamingException
297   {
298     return _path.getPath();
299   }
300
301   public Object JavaDoc addToEnvironment(String JavaDoc prop, Object JavaDoc value)
302     throws NamingException
303   {
304     throw new UnsupportedOperationException JavaDoc();
305     /*
306     try {
307       Object old = _path.getAttribute(prop);
308       _path.setAttribute(prop, value);
309       return old;
310     } catch (IOException e) {
311       throw new NamingException(e.toString());
312     }
313     */

314   }
315
316   public Object JavaDoc removeFromEnvironment(String JavaDoc prop)
317     throws NamingException
318   {
319     throw new UnsupportedOperationException JavaDoc();
320     /*
321     try {
322       Object old = _path.getAttribute(prop);
323       _path.removeAttribute(prop);
324       return old;
325     } catch (IOException e) {
326       throw new NamingException(e.toString());
327     }
328     */

329   }
330
331   public Hashtable JavaDoc getEnvironment()
332     throws NamingException
333   {
334     return null;
335   }
336
337   public void close()
338     throws NamingException
339   {
340   }
341
342   static class PathNameParser implements NameParser {
343     public Name parse(String JavaDoc name)
344       throws NamingException
345     {
346       return new CompositeName(name);
347     }
348   }
349 }
350
Popular Tags