KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > sync > ldap > worker > UpdateGroupAssignments


1 package de.webman.sync.ldap.worker;
2
3 import de.webman.sync.Worker;
4 import de.webman.sync.ACLAdaptor;
5 import de.webman.sync.SyncException;
6 import de.webman.sync.ldap.LDAPAdaptor;
7 import de.webman.acl.ProfileFactory;
8 import de.webman.acl.Profile;
9 import de.webman.acl.LoginFactory;
10 import de.webman.acl.Login;
11 import com.teamkonzept.lib.TKVector;
12 import com.teamkonzept.lib.TKException;
13 import java.util.List JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import org.apache.log4j.Category;
18
19 /**
20  * Gets a list of all changed users in the ldap system (the dirty flag is
21  * set). A changed user denotes that one or more of its webman group
22  * assignments has been changed, removed or added. This modifications in
23  * the ldap system are updated in the webman system. If successful, the
24  * dirty flag in the ldap system is reset to "false". (caatoosee ldap
25  * connection use case 3)
26  *
27  * @author <a HREF="mailto:gregor@webman.de">Gregor Klinke</a>
28  * @version $Revision: 1.2 $
29  **/

30 public class UpdateGroupAssignments
31     implements Worker
32 {
33     /* $Id: UpdateGroupAssignments.java,v 1.2 2002/04/12 14:56:02 gregor Exp $ */
34     /**
35      * logging facilitiy
36      **/

37     private static Category cat = Category.getInstance(UpdateGroupAssignments.class);
38
39
40     /**
41      * constants for statistics
42      **/

43     private static final int STAT_CHANGED = 0;
44
45     /**
46      * constants for statistics
47      **/

48     private static final int STAT_FAILED = 1;
49
50     /**
51      * constants for statistics
52      **/

53     private static final int STAT_UPDATED = 2;
54
55     /**
56      * constants for statistics
57      **/

58     private static final int STAT_WARNINGS = 3;
59
60     /**
61      * constants for statistics
62      **/

63     private static final int STAT_IGNORED = 4;
64
65     /**
66      * constants for statistics
67      **/

68     private static final int SLOTS_STAT = 5;
69     
70
71     /**
72      * the main start method for this worker
73      * @param adaptor the access control adaptor to use
74      **/

75     public void run(ACLAdaptor adaptor)
76         throws SyncException
77     {
78         try {
79             cat.debug("synchronize for changed group assignments");
80             HashMap JavaDoc groups = null;
81             
82             try {
83                 /* get a list of all groups in the system */
84                 TKVector pfvect = ProfileFactory.getInstance().getProfiles();
85                 groups = new HashMap JavaDoc(pfvect.size());
86                 
87                 /* copy all profiles into a hash table for faster lookup */
88                 for (Enumeration JavaDoc en = pfvect.elements(); en.hasMoreElements(); ) {
89                     Profile p = (Profile)en.nextElement();
90                     
91                     if (p.isProfile())
92                         groups.put(p.getLogin(), p.getID());
93                 }
94             }
95             catch (Exception JavaDoc e) {
96                 throw new SyncException(e);
97             }
98             
99             List JavaDoc ldap_users = adaptor.getChangedUsers();
100             int[] statistics = new int[SLOTS_STAT];
101             
102             for (Iterator JavaDoc it = ldap_users.iterator(); it.hasNext(); ) {
103                 de.webman.sync.User ldapuser = (de.webman.sync.User)it.next();
104                 
105                 if (ldapuser.isDirty()) {
106                     statistics[STAT_CHANGED] ++;
107                     checkGroupAssignments(adaptor, ldapuser, groups, statistics);
108                 }
109             }
110             
111             cat.info("synchronizing changed group assignments; STATISTICS:\n" +
112                      ldap_users.size() + " changed ldap users found,\n" +
113                      groups.size() + " webman groups found in total,\n" +
114                      statistics[STAT_CHANGED] + " user entries to be changed,\n" +
115                      statistics[STAT_UPDATED] + " user entries updated successfully,\n" +
116                      statistics[STAT_FAILED] + " user entry updates failed,\n" +
117                      statistics[STAT_WARNINGS] + " group assignment warnings,\n" +
118                      statistics[STAT_IGNORED] + " user entries ignored");
119         }
120         catch (NullPointerException JavaDoc npe) {
121             npe.printStackTrace();
122         }
123     }
124
125
126     /**
127      * check the group assignment for a user
128      **/

129     private void checkGroupAssignments(ACLAdaptor adaptor,
130                                        de.webman.sync.User ldapuser, HashMap JavaDoc groups,
131                                        int[] statistics)
132     {
133         /* ignore webman users, if they are listed */
134         if ((adaptor instanceof LDAPAdaptor) &&
135             ((LDAPAdaptor)adaptor).ignoreUsers().contains(ldapuser.getWebmanName())) {
136             statistics[STAT_IGNORED]++;
137             return;
138         }
139         
140         boolean failed = false;
141
142         /* [1] load the webman user matching the ldap user */
143         de.webman.acl.Login wmuser = null;
144         try {
145             wmuser = LoginFactory.getInstance().getLogin(ldapuser.getWebmanName());
146         }
147         catch (Exception JavaDoc e) {
148             /* user unknown */
149             cat.warn("unknown webman-user found in LDAP: '" + ldapuser.getWebmanName() + "' " +
150                      "(user-dn: '" + ldapuser.getID() + "')");
151             statistics[STAT_FAILED] ++;
152             return;
153         }
154
155         /* [2] get all parents (=groups) from the webman user and put them
156            into a hash map for fast lookup */

157         HashMap JavaDoc user_groups = null;
158         TKVector upr = null;
159         try {
160             upr = wmuser.getParents();
161             user_groups = new HashMap JavaDoc(upr.size());
162
163             for (Enumeration JavaDoc en = upr.elements(); en.hasMoreElements(); ) {
164                 Profile p = (Profile)en.nextElement();
165                 user_groups.put(p.getLogin(), p.getID());
166             }
167         }
168         catch (Exception JavaDoc e) {
169             /* user unknown */
170             cat.warn("can't load groups for webman-user: '" + ldapuser.getWebmanName() + "' " +
171                      "(user-dn: '" + ldapuser.getID() + "')");
172             statistics[STAT_FAILED] ++;
173             return;
174         }
175
176         /* [3] create a HashMap which contains all groups from the ldapuser
177            (which are valid) */

178         HashMap JavaDoc ldap_groups = new HashMap JavaDoc();
179         for (Iterator JavaDoc it = ldapuser.getGroups().iterator(); it.hasNext(); ) {
180             String JavaDoc ldapgroup = (String JavaDoc)it.next();
181             
182             if (groups.containsKey(ldapgroup)) {
183                 ldap_groups.put(ldapgroup, groups.get(ldapgroup));
184             }
185             else {
186                 cat.warn("unknown webman-group found in LDAP: '" + ldapgroup + "' " +
187                          "(user-dn: '" + ldapuser.getID() + "')");
188                 failed = true;
189                 statistics[STAT_WARNINGS] ++;
190             }
191         }
192
193
194         /* [4] first pass: assign groups to the webman user, which are
195            missing in the webmanuser, but exist for the ldapuser. Do this
196            by cycling over the ldapuser's groups, create those which does
197            not exist in user_groups, and remove the groups entries from
198            ldap_groups and user_groups if done */

199         for (Iterator JavaDoc lit = ldap_groups.keySet().iterator(); lit.hasNext(); ) {
200             String JavaDoc gkey = (String JavaDoc)lit.next();
201             if (!user_groups.containsKey(gkey)) {
202                 /* ldap-group not assign to wm-user; do this */
203                 if (!assignUserToGroup((Integer JavaDoc)ldap_groups.get(gkey), wmuser.getID())) {
204                     failed = true;
205                     statistics[STAT_WARNINGS]++;
206                 }
207             }
208             
209             lit.remove();
210             user_groups.remove(gkey);
211         }
212         
213         /* [5] second pass: take away all groups from the webman user,
214            which are missing in the ldapuser, but exist for the webmanuser.
215            Do this by cycling over the user_groups and remove all those
216            groups which are missing in ldap_groups */

217         for (Iterator JavaDoc wit = user_groups.keySet().iterator(); wit.hasNext(); ) {
218             String JavaDoc gkey = (String JavaDoc)wit.next();
219             if (!ldap_groups.containsKey(gkey)) {
220                 /* wm-user has an assigned group, which has to be removed; do it */
221                 if (!removeGroupFromUser((Integer JavaDoc)user_groups.get(gkey), wmuser.getID())) {
222                     failed = true;
223                     statistics[STAT_WARNINGS]++;
224                 }
225             }
226         }
227         
228         /* [6] fourth step: reset the dirty flag, if successful */
229         if (!failed) {
230             try {
231                 adaptor.setDirtyFlagForUser(ldapuser, false);
232                 statistics[STAT_UPDATED]++;
233             }
234             catch (SyncException se) {
235                 statistics[STAT_FAILED]++;
236             }
237         }
238         else
239             statistics[STAT_FAILED]++;
240     }
241     
242     private boolean assignUserToGroup(Integer JavaDoc gid, Integer JavaDoc uid)
243     {
244         Profile group = null;
245         de.webman.acl.Login wmuser = null;
246         try {
247             wmuser = LoginFactory.getInstance().getLogin(uid);
248             group = ProfileFactory.getInstance().getProfile(gid);
249             if (group != null && wmuser != null) {
250                 cat.info("assign user '" + wmuser.getLogin() + "' to group '" +
251                          group.getLogin() + "' ");
252                 group.addChild(wmuser);
253                 ProfileFactory.getInstance().modifyProfile(group);
254                 return true;
255             }
256         }
257         catch (Exception JavaDoc e) {
258             ;
259         }
260         cat.warn("can't assign user '" + wmuser.getLogin() + "' to group '" +
261                  group != null ? group.getLogin() : gid.toString() + "'");
262         return false;
263     }
264
265     private boolean removeGroupFromUser(Integer JavaDoc gid, Integer JavaDoc uid)
266     {
267         Profile group = null;
268         de.webman.acl.Login wmuser = null;
269         try {
270             group = ProfileFactory.getInstance().getProfile(gid);
271             if (group != null) {
272                 wmuser = LoginFactory.getInstance().getLogin(uid);
273                 cat.info("remove user '" + wmuser.getLogin() + "' from group '" +
274                          group.getLogin() + "' ");
275                 group.removeChild(wmuser);
276                 ProfileFactory.getInstance().modifyProfile(group);
277                 return true;
278             }
279         }
280         catch (Exception JavaDoc e) {
281             ;
282         }
283         cat.warn("can't assign user '" + wmuser.getLogin() + "' to group '" +
284                  group != null ? group.getLogin() : gid.toString() + "'");
285         return false;
286         
287     }
288 }
289
Popular Tags