1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import javax.naming.*; 26 import org.openide.filesystems.FileObject; 27 28 33 final class DefaultSettingsContext implements Context, NameParser { 34 35 private final DataObject dobj; 36 private final Hashtable env; 37 38 39 public DefaultSettingsContext(DataObject dobj) { 40 this.dobj = dobj; 41 env = new Hashtable (); 42 } 43 44 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 45 return null; 46 } 47 48 public void bind(Name name, Object obj) throws NamingException { 49 String attrName = getRelativeName(name); 50 FileObject fo = dobj.getPrimaryFile(); 51 Object attrVal = fo.getAttribute(attrName); 52 if (attrVal != null) { 53 throw new NameAlreadyBoundException(attrName + " = " + attrVal); } 55 56 try { 57 fo.setAttribute(attrName, obj); 58 } catch (IOException ex) { 59 NamingException ne = new NamingException(attrName + " = " + obj); ne.setRootCause(ex); 61 } 62 } 63 64 public void bind(String name, Object obj) throws NamingException { 65 bind(parse(name), obj); 66 } 67 68 public void close() throws NamingException { 69 } 70 71 72 public Name composeName(Name name, Name prefix) throws NamingException { 73 throw new OperationNotSupportedException(); 74 } 75 76 77 public String composeName(String name, String prefix) throws NamingException { 78 throw new OperationNotSupportedException(); 79 } 80 81 82 public Context createSubcontext(Name name) throws NamingException { 83 throw new OperationNotSupportedException(); 84 } 85 86 87 public Context createSubcontext(String name) throws NamingException { 88 throw new OperationNotSupportedException(); 89 } 90 91 92 public void destroySubcontext(Name name) throws NamingException { 93 throw new OperationNotSupportedException(); 94 } 95 96 97 public void destroySubcontext(String name) throws NamingException { 98 throw new OperationNotSupportedException(); 99 } 100 101 public Hashtable getEnvironment() throws NamingException { 102 return env; 103 } 104 105 public String getNameInNamespace() throws NamingException { 106 return "."; } 109 110 public NameParser getNameParser(Name name) throws NamingException { 111 return this; 112 } 113 114 public NameParser getNameParser(String name) throws NamingException { 115 return this; 116 } 117 118 public NamingEnumeration list(String name) throws NamingException { 119 return list(parse(name)); 120 } 121 122 public NamingEnumeration list(Name name) throws NamingException { 123 if (name == null) throw new InvalidNameException("name cannot be null"); 125 int size = name.size(); 126 if (size == 0) throw new InvalidNameException("name cannot be empty"); if (size > 1 || !".".equals(name.get(0))) { 128 throw new InvalidNameException("subcontexts unsupported: " + name); } 130 131 return new BindingEnumeration(dobj.getPrimaryFile()); 132 } 133 134 public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { 135 return list(name); 136 } 137 138 public NamingEnumeration<Binding> listBindings(String name) throws NamingException { 139 return list(name); 140 } 141 142 public Object lookup(String name) throws NamingException { 143 return lookup(parse(name)); 144 } 145 146 public Object lookup(Name name) throws NamingException { 147 String attrName = getRelativeName(name); 148 return dobj.getPrimaryFile().getAttribute(attrName); 149 } 150 151 public Object lookupLink(String name) throws NamingException { 152 return lookupLink(parse(name)); 153 } 154 155 public Object lookupLink(Name name) throws NamingException { 156 return lookup(name); 157 } 158 159 public void rebind(Name name, Object obj) throws NamingException { 160 String attrName = getRelativeName(name); 161 FileObject fo = dobj.getPrimaryFile(); 162 163 try { 164 fo.setAttribute(attrName, obj); 165 } catch (IOException ex) { 166 NamingException ne = new NamingException(name + " = " + obj); ne.setRootCause(ex); 168 } 169 } 170 171 public void rebind(String name, Object obj) throws NamingException { 172 rebind(parse(name), obj); 173 } 174 175 public Object removeFromEnvironment(String propName) throws NamingException { 176 return null; 177 } 178 179 public void rename(Name oldName, Name newName) throws NamingException { 180 String oldAttrName = getRelativeName(oldName); 181 String newAttrName = getRelativeName(newName); 182 FileObject fo = dobj.getPrimaryFile(); 183 Object attrVal = fo.getAttribute(newAttrName); 184 185 if (attrVal != null) { 186 throw new NameAlreadyBoundException(newAttrName + " = " + attrVal); } 188 189 try { 190 attrVal = fo.getAttribute(oldAttrName); 191 fo.setAttribute(newAttrName, attrVal); 192 fo.setAttribute(oldAttrName, null); 193 } catch (IOException ex) { 194 NamingException ne = new NamingException(oldName + "->" + newName); ne.setRootCause(ex); 196 } 197 } 198 199 public void rename(String oldName, String newName) throws NamingException { 200 rename(parse(oldName), parse(newName)); 201 } 202 203 public void unbind(String name) throws NamingException { 204 unbind(parse(name)); 205 } 206 207 public void unbind(Name name) throws NamingException { 208 String attrName = getRelativeName(name); 209 FileObject fo = dobj.getPrimaryFile(); 210 211 if (fo.getAttribute(attrName) == null) { 212 NamingException ne = new NameNotFoundException(); 213 ne.setResolvedName(name); 214 throw ne; 215 } 216 217 try { 218 fo.setAttribute(attrName, null); 219 } catch (IOException ex) { 220 NamingException ne = new NamingException(); ne.setResolvedName(name); 222 ne.setRootCause(ex); 223 } 224 } 225 226 228 public Name parse(String name) throws NamingException { 229 if (name == null) throw new InvalidNameException("name cannot be null"); 231 return new CompositeName(name); 232 } 233 234 235 private String getRelativeName(Name name) throws NamingException { 236 if (name == null) throw new InvalidNameException("name cannot be null"); if (name.isEmpty()) throw new InvalidNameException("name cannot be empty"); 239 String rel = null; 240 Enumeration en = name.getAll(); 241 while (en.hasMoreElements()) { 242 if (rel == null) { 243 String item = (String ) en.nextElement(); 244 if (".".equals(item)) continue; if ("..".equals(item)) { throw new InvalidNameException("subcontexts unsupported: " + name); } 248 249 rel = item; 250 } else { 251 throw new InvalidNameException("subcontexts unsupported: " + name); } 253 } 254 return rel; 255 } 256 257 public String toString() { 258 String retValue = super.toString(); 259 return retValue + '[' + dobj + ']'; 260 } 261 262 264 private static final class BindingEnumeration implements NamingEnumeration<Binding> { 265 266 private final Enumeration <String > en; 267 private final FileObject fo; 268 269 public BindingEnumeration(FileObject fo) { 270 this.fo = fo; 271 this.en = fo.getAttributes(); 272 } 273 274 public void close() throws NamingException { 275 } 276 277 public boolean hasMore() throws NamingException { 278 return hasMoreElements(); 279 } 280 281 public boolean hasMoreElements() { 282 return en.hasMoreElements(); 283 } 284 285 public Binding next() throws NamingException { 286 return nextElement(); 287 } 288 289 public Binding nextElement() { 290 String name = en.nextElement(); 291 Object val = fo.getAttribute(name); 292 return new Binding(name, val); 293 } 294 295 } 296 297 } 298 | Popular Tags |