1 16 17 package org.apache.commons.configuration; 18 19 import java.util.ArrayList ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Set ; 24 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NameClassPair ; 28 import javax.naming.NamingEnumeration ; 29 import javax.naming.NamingException ; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 44 public class JNDIConfiguration extends AbstractConfiguration 45 { 46 47 private static Log log = LogFactory.getLog(JNDIConfiguration.class); 48 49 50 private String prefix; 51 52 53 private Context context; 54 55 56 private Context baseContext; 57 58 59 private Set clearedProperties = new HashSet (); 60 61 67 public JNDIConfiguration() throws NamingException ![JavaDoc](../../../../../cmn/javadoc.gif) 68 { 69 this((String ) null); 70 } 71 72 80 public JNDIConfiguration(String prefix) throws NamingException ![JavaDoc](../../../../../cmn/javadoc.gif) 81 { 82 this(new InitialContext (), prefix); 83 } 84 85 91 public JNDIConfiguration(Context context) 92 { 93 this(context, null); 94 } 95 96 103 public JNDIConfiguration(Context context, String prefix) 104 { 105 this.context = context; 106 this.prefix = prefix; 107 } 108 109 119 private void recursiveGetKeys(Set keys, Context context, String prefix) throws NamingException ![JavaDoc](../../../../../cmn/javadoc.gif) 120 { 121 NamingEnumeration elements = null; 122 123 try 124 { 125 elements = context.list(""); 126 127 while (elements.hasMore()) 129 { 130 NameClassPair nameClassPair = (NameClassPair ) elements.next(); 131 String name = nameClassPair.getName(); 132 Object object = context.lookup(name); 133 134 StringBuffer key = new StringBuffer (); 136 key.append(prefix); 137 if (key.length() > 0) 138 { 139 key.append("."); 140 } 141 key.append(name); 142 143 if (object instanceof Context ) 144 { 145 Context subcontext = (Context ) object; 147 recursiveGetKeys(keys, subcontext, key.toString()); 148 } 149 else 150 { 151 keys.add(key.toString()); 153 } 154 } 155 } 156 finally 157 { 158 if (elements != null) 160 { 161 elements.close(); 162 } 163 } 164 } 165 166 169 public Iterator getKeys() 170 { 171 return getKeys(""); 172 } 173 174 177 public Iterator getKeys(String prefix) 178 { 179 String [] splitPath = StringUtils.split(prefix, "."); 181 182 List path = new ArrayList (); 183 184 for (int i = 0; i < splitPath.length; i++) 185 { 186 path.add(splitPath[i]); 187 } 188 189 try 190 { 191 Context context = getContext(path, getBaseContext()); 193 194 Set keys = new HashSet (); 196 if (context != null) 197 { 198 recursiveGetKeys(keys, context, prefix); 199 } 200 else if (containsKey(prefix)) 201 { 202 keys.add(prefix); 204 } 205 206 return keys.iterator(); 207 } 208 catch (NamingException e) 209 { 210 log.error(e.getMessage(), e); 211 return new ArrayList ().iterator(); 212 } 213 } 214 215 225 private Context getContext(List path, Context context) throws NamingException ![JavaDoc](../../../../../cmn/javadoc.gif) 226 { 227 if (path == null || path.isEmpty()) 229 { 230 return context; 231 } 232 233 String key = (String ) path.get(0); 234 235 NamingEnumeration elements = null; 237 238 try 239 { 240 elements = context.list(""); 241 while (elements.hasMore()) 242 { 243 NameClassPair nameClassPair = (NameClassPair ) elements.next(); 244 String name = nameClassPair.getName(); 245 Object object = context.lookup(name); 246 247 if (object instanceof Context && name.equals(key)) 248 { 249 Context subcontext = (Context ) object; 250 251 return getContext(path.subList(1, path.size()), subcontext); 253 } 254 } 255 } 256 finally 257 { 258 if (elements != null) 259 { 260 elements.close(); 261 } 262 } 263 264 return null; 265 } 266 267 270 public boolean isEmpty() 271 { 272 try 273 { 274 NamingEnumeration enumeration = null; 275 276 try 277 { 278 enumeration = getBaseContext().list(""); 279 return !enumeration.hasMore(); 280 } 281 finally 282 { 283 if (enumeration != null) 285 { 286 enumeration.close(); 287 } 288 } 289 } 290 catch (NamingException e) 291 { 292 log.error(e.getMessage(), e); 293 return true; 294 } 295 } 296 297 303 public void setProperty(String key, Object value) 304 { 305 throw new UnsupportedOperationException ("This operation is not supported"); 306 } 307 308 311 public void clearProperty(String key) 312 { 313 clearedProperties.add(key); 314 } 315 316 319 public boolean containsKey(String key) 320 { 321 if (clearedProperties.contains(key)) 322 { 323 return false; 324 } 325 key = StringUtils.replace(key, ".", "/"); 326 try 327 { 328 getBaseContext().lookup(key); 330 return true; 331 } 332 catch (NamingException e) 333 { 334 log.error(e.getMessage(), e); 335 return false; 336 } 337 } 338 339 342 public String getPrefix() 343 { 344 return prefix; 345 } 346 347 352 public void setPrefix(String prefix) 353 { 354 this.prefix = prefix; 355 356 baseContext = null; 358 } 359 360 363 public Object getProperty(String key) 364 { 365 if (clearedProperties.contains(key)) 366 { 367 return null; 368 } 369 370 try 371 { 372 key = StringUtils.replace(key, ".", "/"); 373 return getBaseContext().lookup(key); 374 } 375 catch (NamingException e) 376 { 377 log.error(e.getMessage(), e); 378 return null; 379 } 380 } 381 382 388 protected void addPropertyDirect(String key, Object obj) 389 { 390 throw new UnsupportedOperationException ("This operation is not supported"); 391 } 392 393 396 public Context getBaseContext() throws NamingException ![JavaDoc](../../../../../cmn/javadoc.gif) 397 { 398 if (baseContext == null) 399 { 400 baseContext = (Context ) getContext().lookup(prefix == null ? "" : prefix); 401 } 402 403 return baseContext; 404 } 405 406 410 public Context getContext() 411 { 412 return context; 413 } 414 415 418 public void setContext(Context context) 419 { 420 clearedProperties.clear(); 422 423 this.context = context; 425 } 426 } 427 | Popular Tags |