1 package com.ca.commons.jndi; 2 3 import javax.naming.*; 4 import javax.naming.directory.*; 5 import java.util.ArrayList ; 6 import java.util.ListIterator ; 7 import java.util.logging.Logger ; 8 9 22 public class AdvancedOps extends BasicOps 23 { 24 26 protected NameParser parser; 27 28 private final static Logger log = Logger.getLogger(AdvancedOps.class.getName()); 29 30 37 38 public AdvancedOps(DirContext c) 39 throws NamingException 40 { 41 super(c); 42 parser = getBaseNameParser(); 43 } 44 45 52 53 public AdvancedOps(ConnectionData cData) 54 throws NamingException 55 { 56 super(cData); 57 parser = getBaseNameParser(); 58 } 59 60 61 71 72 public static BasicOps getInstance(ConnectionData cData) 73 throws NamingException 74 { 75 AdvancedOps newObject = new AdvancedOps(openContext(cData)); 76 return newObject; 77 } 78 79 82 83 public void startOperation(String heading, String operationName) 84 { 85 } 86 87 90 91 public void stopOperation() 92 { 93 } 94 95 98 99 public void pop() 100 { 101 } 102 103 109 110 public NamingEnumeration push(NamingEnumeration elements) 111 { 112 return elements; 113 } 114 115 120 public void push(ArrayList elements) 121 { 122 123 } 124 127 128 public void inc() 129 { 130 } 131 132 137 138 public void deleteTree(Name nodeDN) throws NamingException 140 { 141 142 try 143 { 144 if (nodeDN == null) 145 throw new NamingException("null DN passed to deleteTree."); 146 147 log.finer("recursively delete Tree " + nodeDN.toString()); 148 149 startOperation("Deleting " + nodeDN.toString(), "deleted "); 150 recDeleteTree(nodeDN); 151 } 152 finally 153 { 154 stopOperation(); 155 } 156 157 } 158 159 164 165 protected void recDeleteTree(Name dn) 166 throws NamingException 167 { 168 log.info("deleting " + dn); 169 170 ArrayList childArray = getChildren(dn); 171 172 push(childArray); 174 ListIterator children = childArray.listIterator(); 175 176 while (children.hasNext()) 177 { 178 recDeleteTree((Name) children.next()); 179 } 180 pop(); 182 deleteEntry(dn); 183 inc(); } 185 186 191 192 201 202 public void moveTree(Name oldNodeDN, Name newNodeDN) throws NamingException 204 { 205 try 206 { 207 if (oldNodeDN == null) 208 throw new NamingException("the original DN passed to moveTree is null."); 209 210 if (newNodeDN == null) 211 throw new NamingException("the destination DN passed to moveTree is null."); 212 213 log.finer("recursively move tree from " + oldNodeDN.toString() + " to " + newNodeDN.toString()); 214 215 startOperation("Moving " + oldNodeDN.toString(), "moving"); 216 recMoveTree(oldNodeDN, newNodeDN); 217 } 218 finally 219 { 220 stopOperation(); 221 } 222 } 223 224 241 242 protected void recMoveTree(Name from, Name to) throws NamingException 244 { 245 if (from.size() == to.size() && from.startsWith(to.getPrefix(to.size() - 1))) { 247 try 248 { 249 renameEntry(from, to, true); 250 } 251 catch (javax.naming.ContextNotEmptyException e) 253 { 254 if (e.getMessage().indexOf("subtree rename not supported") > -1) 255 { 256 recCopyAndDeleteTree(from, to); 257 } 258 } 259 } 260 else { recCopyAndDeleteTree(from, to); 263 } 264 } 265 266 private void recCopyAndDeleteTree(Name from, Name to) 267 throws NamingException 268 { 269 if (!exists(from)) 274 throw new NamingException("The DN that you are trying to move does not exist."); 275 276 try 277 { 278 recCopyTree(from, to); 279 } 280 catch (NamingException e) 281 { 282 recDeleteTree(to); throw e; } 285 286 recDeleteTree(from); 287 } 288 289 294 295 296 304 305 public void copyTree(Name oldNodeDN, Name newNodeDN) throws NamingException 307 { 308 try 309 { 310 if (oldNodeDN == null) 311 throw new NamingException("the original DN passed to copyTree is null."); 312 313 if (newNodeDN == null) 314 throw new NamingException("the destination DN passed to copyTree is null."); 315 316 log.finer("recursively copy tree from " + oldNodeDN.toString() + " to " + newNodeDN.toString()); 317 318 startOperation("Copying " + oldNodeDN.toString(), "copying"); 319 recCopyTree(oldNodeDN, newNodeDN); 320 } 321 finally 322 { 323 stopOperation(); 324 } 325 } 326 327 328 335 336 protected void recCopyTree(Name from, Name to) 337 throws NamingException 338 { 339 copyEntry(from, to); 341 inc(); 342 343 ArrayList childArray = getChildren(from); 344 345 push(childArray); 346 347 ListIterator children = childArray.listIterator(); 348 349 while (children.hasNext()) 350 { 351 Name childDN = (Name)children.next(); 352 353 Name destinationDN = (Name)to.clone(); 354 destinationDN.add(childDN.get(childDN.size()-1)); 355 356 recCopyTree(childDN, destinationDN); 357 } 358 pop(); 359 } 360 361 367 protected ArrayList getChildren(Name base) 368 throws NamingException 369 { 370 ArrayList children = new ArrayList (); 371 NamingEnumeration rawList = list(base); 372 373 if (rawList.hasMoreElements()) 374 { 375 while (rawList.hasMoreElements()) 376 { 377 NameClassPair child = (NameClassPair)rawList.next(); 378 380 Name childDN = parser.parse(child.getName()); 385 386 if (childDN.size() == 1) 387 { 388 childDN = ((Name)base.clone()).add(child.getName()); 389 } 390 391 children.add(childDN); 392 } 393 } 394 return children; 395 } 396 } | Popular Tags |