KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > broker > Special


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 JavaDoc;
11 import java.util.logging.Level JavaDoc;
12
13 /**
14  * This static class contains special code for Mitchell Rozonkiewiecz
15  * of the OS390 security group, who requires special handling of ldap
16  * modify requests. It is only activated if a file 'specialocs.txt' is
17  * present in the browser home directory.
18  */

19
20 public class Special
21 {
22     private static Logger JavaDoc log = Logger.getLogger(Special.class.getName());
23
24     private Special() {} // class can't be instantiated.
25

26     /**
27      * Returns the set of attributes that are in the newSet, but
28      * not in the old set, excluding the rdnclass.
29      * IN ADDITION: it returns the set of all attribute values that
30      * have been modified or added as a add records (In the case of
31      * modified attributes, this requires a separate delete op to
32      * work properly)
33      * @param newRDN the RDN of the newer entry.
34      * (usually this is the same as the RDN of the original entry).
35      * May be null if it is not to be checked.
36      * <b>Not currently used</b>
37      * @param oldSet the set of already existing attributes
38      * @param newSet the set of new attributes to test
39      * @return attributes that must be added to the object.
40      */

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         // no need to check for rdn as it must exist in the old set already :-)
48

49         while (testAtts.hasMore())
50         {
51             Attribute testVal = (Attribute)testAtts.next();
52             Attribute oldVal = oldSet.get(testVal.getID());
53             if (! emptyAtt(testVal)) // don't add empty atts!
54
{
55                 if (oldVal == null) // if the old attribute was null
56
changes.put(testVal); // ...add it
57
else if (emptyAtt(oldVal)) // or if it had only null values add it. (It was probably created by the browser,
58
changes.put(testVal); // and doesn't exist in the database)
59
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     /**
70      * Returns an attribute containing all values that are
71      * contained in changeAtt, but not in baseAtt.
72      * @param baseAtt the basic attribute set to compare changeAtt against
73      * @param changeAtt an attribute set containing possibly changed values.
74      */

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 JavaDoc 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 JavaDoc e)
92         {
93             log.log(Level.WARNING, "Unexpected Error in Special.java: ", e);
94             return null;
95         }
96     }
97     
98     /**
99      * Returns all attributes that have not changed.
100      *
101      * @param newRDN the RDN of the newer entry.
102      * (usually this is the same as the RDN of the original entry).
103      * May be null if it is not to be checked.
104      * @param oldSet the set of already existing attributes
105      * @param newSet the set of new attributes to test
106      * @return attributes that require updating
107      */

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()) // find changed attributes
117
{
118             Attribute testAtt = (Attribute)testAtts.next();
119         
120             String JavaDoc ID = testAtt.getID();
121             
122             Attribute oldAtt = oldSet.get(ID);
123
124             /*
125              * Check if we're dealing with a naming attribute - if we are, make
126              * sure the naming value doesn't get included in our handling...
127              */

128             
129             if ((newRDN!=null) && (newRDN.contains(ID))) // need to tread carefully, as
130
{ // we're dealing with a naming att...
131
String JavaDoc namingValue = newRDN.getRawVal(ID);
132             
133                 if (oldAtt != null) // Would this be an Error???
134
{
135                     testAtt = new DXAttribute(testAtt); // make local copies,
136
oldAtt = new DXAttribute(oldAtt); // (so we can remove the naming value,
137
testAtt.remove(namingValue); // without affecting the passed in data)
138
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     /**
159      * Returns the set of attributes that are in the oldSet, but
160      * not in the new set, and thus must be deleted. (excluding the rdnclass).
161      *
162      * @param newRDN the RDN of the newer entry.
163      * (usually this is the same as the RDN of the original entry).
164      * May be null if it is not to be checked.
165      * @param oldSet the set of already existing attributes
166      * @param newSet the set of new attributes to test
167      */

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         // look for old attributes that no longer exist...
176

177         while (oldAtts.hasMore())
178         {
179             Attribute oldAtt = (Attribute)oldAtts.next();
180             Object JavaDoc val = oldAtt.get();
181             
182             if ("".equals(val))
183             {
184                 val = null; //XXX zero length string hack - don't automatically delete zero length strings.
185
}
186             
187             if ((val != null)) // if the old value was real (i.e. not null or blank string...)
188
{
189                 String JavaDoc ID = oldAtt.getID();
190
191                 if (newRDN == null || newRDN.contains(ID)==false) // (can't delete the rdn class...)
192
{
193                     Attribute newAtt = (Attribute)newSet.get(ID);
194
195                     if (newAtt != null) // check whether the new att exists, and has
196
{ // a real value - if not, delete it.
197
if (emptyAtt(newAtt))
198                             newAtt = null;
199                     }
200                     
201                     if (newAtt == null) // .. but it no longer exists...
202
{
203                         changes.put(new DXAttribute(oldAtt.getID(), null)); // ... delete it.
204
}
205                     else if (attributesEqual(newAtt, oldAtt) == false)
206                     {
207                         changes.put(getDiff(newAtt, oldAtt)); // delete changed/removed values
208
}
209                 }
210             }
211         }
212         
213         return changes;
214     }
215 /*
216     static private NamingEnumeration getMissingValues(NamingEnumeration A, NamingEnumeration B)
217         throws NamingException
218     {
219         //if (A==null||B==null) return new DXNamingEnumeration();
220         
221         DXNamingEnumeration a = new DXNamingEnumeration(A);
222         DXNamingEnumeration b = new DXNamingEnumeration(B);
223         if (a==null) System.out.println("a = null!"); else System.out.println("a = " + a.toString());
224         if (b==null) System.out.println("b = null!"); else System.out.println("b = " + b.toString());
225
226         DXNamingEnumeration ret = new DXNamingEnumeration(b);
227         while (a.hasMore())
228         {
229             ret.remove(a.next());
230         }
231         return ret;
232     }
233 */

234
235     /**
236      * Utility ftn: checks that an attribute is not null and has at least
237      * one non-null value.
238      */

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         // sanity checks...
249
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 JavaDoc[] A = CBArray.enumerationToArray(a.getAll());
260             Object JavaDoc[] 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; // only here if error occurred.
268
}
269     
270 }
Popular Tags