1 package com.ca.directory.jxplorer.broker; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.commons.naming.*; 5 6 import javax.naming.*; 7 import javax.naming.directory.*; 8 9 import java.util.*; 10 import java.util.logging.Logger ; 11 import java.util.logging.Level ; 12 13 19 20 public class Special 21 { 22 private static Logger log = Logger.getLogger(Special.class.getName()); 23 24 private Special() {} 26 41 42 public static DXAttributes getAdditionSet(RDN newRDN, Attributes oldSet, Attributes newSet) throws NamingException 43 { 44 DXAttributes changes = new DXAttributes(); 45 NamingEnumeration testAtts = newSet.getAll(); 46 47 49 while (testAtts.hasMore()) 50 { 51 Attribute testVal = (Attribute)testAtts.next(); 52 Attribute oldVal = oldSet.get(testVal.getID()); 53 if (! emptyAtt(testVal)) { 55 if (oldVal == null) changes.put(testVal); else if (emptyAtt(oldVal)) changes.put(testVal); else if (attributesEqual(oldVal, testVal) == false) 60 { 61 Attribute adds = getDiff(oldVal, testVal); 62 if (adds != null) changes.put(adds); 63 } 64 } 65 } 66 return changes; 67 } 68 69 75 76 public static DXAttribute getDiff(Attribute baseAtt, Attribute changeAtt) 77 { 78 try 79 { 80 Enumeration changeVals = changeAtt.getAll(); 81 DXNamingEnumeration baseVals = new DXNamingEnumeration(baseAtt.getAll()); 82 DXAttribute mods = new DXAttribute(baseAtt.getID()); 83 while (changeVals.hasMoreElements()) 84 { 85 Object o = changeVals.nextElement(); 86 if (baseVals.contains(o) == false) 87 mods.add(o); 88 } 89 return (mods.size() > 0)?mods:null; 90 } 91 catch (Exception e) 92 { 93 log.log(Level.WARNING, "Unexpected Error in Special.java: ", e); 94 return null; 95 } 96 } 97 98 108 109 public static DXAttributes getReplacementSet(RDN newRDN, Attributes oldSet, Attributes newSet) 110 throws NamingException 111 { 112 113 DXAttributes changes = new DXAttributes(); 114 NamingEnumeration testAtts = newSet.getAll(); 115 116 while (testAtts.hasMore()) { 118 Attribute testAtt = (Attribute)testAtts.next(); 119 120 String ID = testAtt.getID(); 121 122 Attribute oldAtt = oldSet.get(ID); 123 124 128 129 if ((newRDN!=null) && (newRDN.contains(ID))) { String namingValue = newRDN.getRawVal(ID); 132 133 if (oldAtt != null) { 135 testAtt = new DXAttribute(testAtt); oldAtt = new DXAttribute(oldAtt); testAtt.remove(namingValue); oldAtt.remove(namingValue); 139 140 System.out.println("and finally we end up with: \n new: " + testAtt.toString() + "\n old: " + oldAtt.toString()); 141 } 142 } 143 144 145 if (attributesEqual(oldAtt, testAtt)) 146 { 147 if (emptyAtt(testAtt) == false) 148 changes.put(testAtt); 149 } 150 151 } 152 153 return changes; 154 } 155 156 157 158 168 169 public static DXAttributes getDeletionSet(RDN newRDN, Attributes oldSet, Attributes newSet) throws NamingException 170 { 171 DXAttributes changes = new DXAttributes(); 172 NamingEnumeration oldAtts = oldSet.getAll(); 173 174 175 177 while (oldAtts.hasMore()) 178 { 179 Attribute oldAtt = (Attribute)oldAtts.next(); 180 Object val = oldAtt.get(); 181 182 if ("".equals(val)) 183 { 184 val = null; } 186 187 if ((val != null)) { 189 String ID = oldAtt.getID(); 190 191 if (newRDN == null || newRDN.contains(ID)==false) { 193 Attribute newAtt = (Attribute)newSet.get(ID); 194 195 if (newAtt != null) { if (emptyAtt(newAtt)) 198 newAtt = null; 199 } 200 201 if (newAtt == null) { 203 changes.put(new DXAttribute(oldAtt.getID(), null)); } 205 else if (attributesEqual(newAtt, oldAtt) == false) 206 { 207 changes.put(getDiff(newAtt, oldAtt)); } 209 } 210 } 211 } 212 213 return changes; 214 } 215 234 235 239 public static boolean emptyAtt(Attribute att) 240 { 241 return DXAttribute.isEmpty(att); 242 } 243 244 245 private static boolean attributesEqual(Attribute a, Attribute b) 246 throws NamingException 247 { 248 if (a == null && b == null) return true; 250 if (a == null || b == null) return false; 251 if (a.size() == 0 && b.size() == 0) return true; 252 if (a.size() == 0 || b.size() == 0) return false; 253 if (a.get() == null && b.get() == null) return true; 254 if (a.get() == null || b.get() == null) return false; 255 if (a.getID().equals(b.getID())==false) return false; 256 257 try 258 { 259 Object [] A = CBArray.enumerationToArray(a.getAll()); 260 Object [] B = CBArray.enumerationToArray(b.getAll()); 261 return CBArray.isUnorderedEqual(A,B); 262 } 263 catch (NamingException e) 264 { 265 log.log(Level.WARNING, "Naming Exception testing attributes " + a.getID() + " & " + b.getID() + " in DXAttributes:attributesEqual()", e); 266 } 267 return false; } 269 270 } | Popular Tags |