1 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 ; 36 import java.util.Hashtable ; 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 lookup(String 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 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 lookup(Name name) 94 throws NamingException 95 { 96 return lookup(name.toString()); 97 } 98 99 public void bind(String name, Object 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 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 e) { 123 throw new NamingException(e.toString()); 124 } 125 } 126 127 public void bind(Name name, Object obj) 128 throws NamingException 129 { 130 bind(name.toString(), obj); 131 } 132 133 public void rebind(String name, Object 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 e) { 144 throw new NamingException(e.toString()); 145 } 146 } 147 148 public void rebind(Name name, Object obj) 149 throws NamingException 150 { 151 rebind(name.toString(), obj); 152 } 153 154 public void unbind(String name) 155 throws NamingException 156 { 157 Path subpath = _path.lookup(name); 158 159 try { 160 subpath.remove(); 161 } catch (IOException 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 oldName, String newName) 173 throws NamingException 174 { 175 Object 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 obj = lookup(oldName); 184 bind(newName, obj); 185 unbind(oldName); 186 } 187 188 public NamingEnumeration list(String 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 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 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 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 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 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 lookupLink(String name) 260 throws NamingException 261 { 262 throw new NamingException(L.l("links not supported")); 263 } 264 265 public Object lookupLink(Name name) 266 throws NamingException 267 { 268 return lookupLink(name.toString()); 269 } 270 271 public NameParser getNameParser(String 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 composeName(String suffix, String 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 getNameInNamespace() 296 throws NamingException 297 { 298 return _path.getPath(); 299 } 300 301 public Object addToEnvironment(String prop, Object value) 302 throws NamingException 303 { 304 throw new UnsupportedOperationException (); 305 314 } 315 316 public Object removeFromEnvironment(String prop) 317 throws NamingException 318 { 319 throw new UnsupportedOperationException (); 320 329 } 330 331 public Hashtable 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 name) 344 throws NamingException 345 { 346 return new CompositeName(name); 347 } 348 } 349 } 350 | Popular Tags |