1 28 29 package com.caucho.vfs; 30 31 import com.caucho.util.L10N; 32 import com.caucho.util.Log; 33 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 import java.io.IOException ; 38 import java.util.Map ; 39 import java.util.logging.Logger ; 40 41 46 public class JndiPath extends FilesystemPath { 47 protected static final Logger log = Log.open(JndiPath.class); 48 protected static final L10N L = new L10N(JndiPath.class); 49 50 private Context parent; 51 52 55 public JndiPath() 56 { 57 super(null, "/", "/"); 58 59 _root = this; 60 } 61 62 65 protected JndiPath(FilesystemPath root, String userPath, String path) 66 { 67 super(root, userPath, path); 68 } 69 70 78 public Path fsWalk(String userPath, 79 Map <String ,Object > attributes, 80 String path) 81 { 82 return new JndiPath(_root, userPath, path); 83 } 84 85 88 public String getScheme() 89 { 90 return "jndi"; 91 } 92 93 96 public boolean mkdir() 97 throws IOException 98 { 99 try { 100 Context parent = getAllButLast(getPath()); 101 102 parent.createSubcontext(getTail()); 103 104 return true; 105 } catch (Exception e) { 106 throw new IOExceptionWrapper(e); 107 } 108 } 109 110 113 public Object getObject() 114 throws IOException 115 { 116 try { 117 Context parent = getAllButLast(getPath()); 118 119 return parent.lookup(getTail()); 120 } catch (Exception e) { 121 throw new IOExceptionWrapper(e); 122 } 123 } 124 125 130 public void setObject(Object value) 131 throws IOException 132 { 133 try { 134 Context parent = getAllButLast(getPath()); 135 136 parent.rebind(getTail(), value); 137 } catch (Exception e) { 138 e.printStackTrace(); 139 throw new IOExceptionWrapper(e); 140 } 141 } 142 143 150 private Context getAllButLast(String path) 151 throws NamingException 152 { 153 if (parent != null) 154 return parent; 155 156 Context context = new InitialContext (); 157 158 int head = 1; 159 int tail; 160 161 while ((tail = path.indexOf('/', head)) > 0) { 162 String section = path.substring(head, tail); 163 164 if (context == null) 165 throw new NamingException (L.l("null context for `{0}'", path)); 166 167 context = (Context ) context.lookup(section); 168 169 head = tail + 1; 170 } 171 172 parent = context; 173 174 return parent; 175 } 176 } 177 | Popular Tags |